All Products
Search
Document Center

Integrate your application with Tracing Analysis - Go

Last Updated: Sep 25, 2018

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

How it works

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

Quick Start

  1. Run the following command to download the Demo file in the GOPATH directory.

    1. wget http://arms-apm.oss-cn-hangzhou.aliyuncs.com/tools/tracingtest.zip && unzip tracingtest.zip
  2. Modify the configuration.

    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. sender : = transport.NewHTTPTransport(
    2. // Set the gateway. The gateway varies with regions.
    3. " <domain>/api/traces?format=jaeger.thrift",
    4. // Set username and password
    5. transport.HTTPBasicAuth("<username>", "<password>"),
  3. Run the following command to upload the data.

    1. go run main.go
    2. http done
    3. grpc done

    Note:

    If the following error occurs, which means the username or password is incorrect, please correct them and try again.

    1. go run main.go
    2. http done
    3. 2018/09/17 21:11:54 ERROR: error when flushing the buffer: error from collector: 403
    4. 2018/09/17 21:11:54 ERROR: error when flushing the buffer: error from collector: 403
  4. Log on to the Tracing Analysis console. Wait for about 30 seconds after the previous step is done, and you can then view the reported data.

Direct reporting

  1. Import jaeger-client-go.

    • Package path: github.com/uber/jaeger-client-go
    • Version: >=2.11.0In the case of glide, you need to add the following configurations to glide.yaml:
    1. package: github.com/uber/jaeger-client-go
    2. version: ^2.11.0
    3. subpackages:
    4. transport
  2. Create the Trace object.

    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. func NewJaegerTracer(service string) (opentracing.Tracer, io.Closer) {
    2. sender : = transport.NewHTTPTransport(
    3. // Set the gateway. The gateway varies with regions.
    4. " <domain>/api/traces?format=jaeger.thrift",
    5. // Set username and password
    6. transport.HTTPBasicAuth("<username>", "<password>"),
    7. )
    8. tracer, closer: = jaeger.NewTracer(service,
    9. jaeger.NewConstSampler(true),
    10. jaeger.NewRemoteReporter(sender))
    11. return tracer, closer
    12. }
  3. Create Span instance object and data pass-through.

    • If there is no parentSpan:

      1. // Create Span
      2. span : = tracer.StartSpan("myspan")
      3. // Set Tag
      4. clientSpan.SetTag("mytag", "123")
      5. // Pass-through traceId
      6. tracer.Inject(span.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header))
      7. ...
      8. defer span.Finish()
    • If there is parentSpan:

      1. // Parse spanCtx from HTTP/RPC object
      2. spanCtx, _ : = tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(r.Header))
      3. span : = tracer.StartSpan("myspan", opentracing.ChildOf(spanCtx))
      4. ...
      5. defer span.Finish()

Reporting by Agent

  1. Import jaeger-client-go.

    • Package path: github.com/uber/jaeger-client-go
    • Version: >=2.11.0In the case of glide, you need to add the following configurations to glide.yaml:
    1. package: github.com/uber/jaeger-client-go
    2. version: ^2.11.0
    3. subpackages:
    4. transport
  2. Create the Trace object.

    1. func NewJaegerTracer(serviceName string) (opentracing.Tracer, io.Closer) {
    2. sender, _ : = jaeger.NewUDPTransport("",0)
    3. tracer, closer: = jaeger.NewTracer(serviceName,
    4. jaeger.NewConstSampler(true),
    5. jaeger.NewRemoteReporter(sender))
    6. return tracer, closer
    7. }
  3. Run the following command to start the Agent. The local Agent receives the data from Client, and reports to Collector.

    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. // Use collector.host-port to set the gateway. The gateway varies with regions.
    2. ./xtrace_agent --collector.host-port=<domain> --licenseKey=<username> --pid=<password>