为了在客户端代码中启用跟踪,您必须初始化每个客户端进程一次向接收器发送跨度的模块。

private SpanReceiverHost spanReceiverHost;

...

  Configuration conf = HBaseConfiguration.create();
  SpanReceiverHost spanReceiverHost = SpanReceiverHost.getInstance(conf); 

然后,您只需在您认为有趣的请求之前开始跟踪跨度,并在请求完成时关闭它。例如,如果要跟踪所有获取操作,请更改此设置:

Configuration config = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(config);
Table table = connection.getTable(TableName.valueOf("t1"));
Get get = new Get(Bytes.toBytes("r1"));
Result res = table.get(get); 

成:

TraceScope ts = Trace.startSpan("Gets", Sampler.ALWAYS);
try {
  Table table = connection.getTable(TableName.valueOf("t1"));
  Get get = new Get(Bytes.toBytes("r1"));
  Result res = table.get(get);
} finally {
  ts.close();
} 

如果你想跟踪一半的“获取”操作,你会传入:

new ProbabilitySampler(0.5) 

代替Sampler.ALWAYSTrace.startSpan()。有关采样器的更多信息,请参见 HTrace README