All Products
Search
Document Center

Integrate your application with Tracing Analysis - Java

Last Updated: Sep 25, 2018

This topic explains how to integrate your Java application with Tracing Analysis.

How it works

For more information about how the data reporting works, see How data is reported.

Procedure

  1. Add the dependency on Jaeger Client in your Java project.

    1. <dependency>
    2. <groupId>io.jaegertracing</groupId>
    3. <artifactId>jaeger-client</artifactId>
    4. <version>0.31.0</version>
    5. </dependency>
  2. Initialize.

    Note: Please replace the <domain>, <username>, and <password> with the region-specific domain, username, and password found on the Overview page of your console.

    1. // Set application name
    2. Configuration config = new Configuration("Demo-Application");
    3. Configuration.SenderConfiguration sender = new Configuration.SenderConfiguration();
    4. // Set endpoint
    5. sender.withEndpoint("<domain>/api/traces");
    6. // Set username
    7. sender.withAuthUsername("<username>");
    8. // Set password
    9. sender.withAuthPassword("<password>");
    10. // Set sampling rate
    11. config.withSampler(new Configuration.SamplerConfiguration().withType("const").withParam(1));
  3. Create Tracer instance object.

    1. io.opentracing.Tracer tracer = config.getTracer();
  4. Create Span instance object.

    1. Tracer.SpanBuilder parentSpanBuilder = tracer.buildSpan("/api/xtrace/test01");
    2. Span parentSpan = parentSpanBuilder.start();
    3. // Set tag (optional, multiple tags allowed)
    4. parentSpan.setTag("tagKey-01", "tagValue-01");
    5. // Set event (optional, multiple events allowed)
    6. parentSpan.log(System.currentTimeMillis(), "event-01");
    7. // Add child span instance object (optional)
    8. Tracer.SpanBuilder childSpanBuilder = tracer.buildSpan("/api/xtrace/test02").asChildOf(parentSpan);
    9. Span childSpan = childSpanBuilder.start();
    10. // Mark the end of child span
    11. childSpan.finish();
    12. // Mark the end of parent span
    13. parentSpan.finish();
  5. Log on to the Tracing Analysis console and view the invocation traces.

    Note: The complete Demo project can be downloaded here.

How to pass-through the trace information in a distributed system?

For example, if Application A calls Application B through HTTP protocal:

  1. The sender adds trace header in HTTP Header:

    1. private void attachTraceInfo(Tracer tracer, Span span, final Request request) {
    2. tracer.inject(span.context(), Format.Builtin.TEXT_MAP, new TextMap() {
    3. @Override
    4. public void put(String key, String value) {
    5. request.setHeader(key, value);
    6. }
    7. @Override
    8. public Iterator<Map.Entry<String, String>> iterator() {
    9. throw new UnsupportedOperationException("TextMapInjectAdapter should only be used with Tracer.inject()");
    10. }
    11. });
    12. }
  2. The receiver parses trace header from HTTP Header:

    1. protected Span extractTraceInfo(Request request, Tracer tracer) {
    2. Tracer.SpanBuilder spanBuilder = tracer.buildSpan("/api/xtrace/test03");
    3. try {
    4. SpanContext spanContext = tracer.extract(Format.Builtin.TEXT_MAP, new TextMapExtractAdapter(request.getAttachments()));
    5. if (spanContext ! = null) {
    6. spanBuilder.asChildOf(spanContext);
    7. }
    8. } catch (Exception e) {
    9. spanBuilder.withTag("Error", "extract from request fail, error msg:" + e.getMessage());
    10. }
    11. return spanBuilder.start();
    12. }