Before you can view the trace data of your application, you must use a client to report the trace data to Tracing Analysis. This topic describes how to use Jaeger SDK or the Jaeger agent to report Go application data. This topic also provides examples on how to use the two methods to report Go application data.

Prerequisites

  1. Log on to the Application Real-Time Monitoring Service (ARMS) console. In the left-side navigation pane, choose Tracing Analysis > Cluster Configurations.
  2. In the top navigation bar, select a region. Then, click the Access point information tab. On this tab, turn on Show Token for the Cluster Information parameter.
  3. Set the Client parameter to the client that you want to use to collect trace data. In this example, select Jaeger.
  4. In the Related Information column of the table in the lower part, click the copy icon next to the endpoint that you want to use.

Tracing Analysis console - Endpoints

Note: If you deploy your application in an Alibaba Cloud production environment, select a private endpoint. Otherwise, select a public endpoint.

Background information

The following figure shows how to report data without using the Jaeger agent.

Report Tracing Data Directly

The following figure shows how to report data by using the Jaeger agent.

Report Tracing Data By Jaeger Agent

Method 1: Use Jaeger SDK to report Go application data

In this example, a Go module is used to manage dependencies. You can use Jaeger SDK to instrument your application and report your application data to Tracing Analysis . If you use other tools to manage dependencies, perform operations based on your actual requirements.

  1. Import jaeger-client-go.
    go get github.com/uber/jaeger-client-go
  2. Create a Tracer object.
    Note Replace <endpoint> with the endpoint for Jaeger in the corresponding region on the Access point information page of the Tracing Analysis console. For more information about how to obtain the endpoint, see the Prerequisites section in this topic.
    func NewJaegerTracer(service string) (opentracing.Tracer, io.Closer) {
    
        sender := transport.NewHTTPTransport(
           // Specify the endpoint. The endpoint differs in different regions. 
            "<endpoint>",
        )
       tracer, closer:= jaeger.NewTracer(service,
               jaeger.NewConstSampler(true),
               jaeger.NewRemoteReporter(sender))
       return tracer, closer
    }
  3. Create a span and pass data.
    • If no parent span is available, use the following code:

      // Create a span. 
      span := tracer.StartSpan("myspan")
      // Set a tag. 
      clientSpan.SetTag("mytag", "123")
      // Pass the ID of the trace. 
      tracer.Inject(span.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header))
      ...
      defer  span.Finish()
    • If a parent span is available, use the following code:

      // Extract a SpanContext from an HTTP request or an RPC request. 
      spanCtx, _ := tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(r.Header))
      span := tracer.StartSpan("myspan", opentracing.ChildOf(spanCtx))
      ...
      defer  span.Finish()

    For more information, see Jaeger Bindings for Go OpenTracing API.

Method 2: Use the Jaeger agent to report Go application data

  1. Start the Jaeger agent. For more information, see Install the Jaeger agent.
  2. Import jaeger-client-go.
    go get github.com/uber/jaeger-client-go
  3. Create a Tracer object.
    func NewJaegerTracer(serviceName string) (opentracing.Tracer, io.Closer) {
       sender, _ := jaeger.NewUDPTransport("",0)
       tracer, closer:= jaeger.NewTracer(serviceName,
            jaeger.NewConstSampler(true),
            jaeger.NewRemoteReporter(sender))
        return tracer, closer
    }
  4. Create a span and pass data.
    • If no parent span is available, use the following code:

      // Create a span. 
      span := tracer.StartSpan("myspan")
      // Set a tag. 
      clientSpan.SetTag("mytag", "123")
      // Pass the ID of the trace. 
      tracer.Inject(span.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header))
      ...
      defer  span.Finish()
    • If a parent span is available, use the following code:

      // Extract a SpanContext from an HTTP request or an RPC request. 
      spanCtx, _ := tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(r.Header))
      span := tracer.StartSpan("myspan", opentracing.ChildOf(spanCtx))
      ...
      defer  span.Finish()

    For more information, see Jaeger Bindings for Go OpenTracing API.

Examples

Use Jaeger SDK to report Go application data

  1. Obtain the required endpoint. For more information, see the Prerequisites section in this topic.
  2. Run the following command to download the demo:
    wget https://arms-apm-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/demo/tracing-demo.zip && unzip tracing-demo.zip
  3. Decompress the ZIP package, open the examples/settings.go file in the demo, and then set the TracingAnalysisEndpoint parameter to the endpoint obtained in Step 1, as shown in the following figure.
    jaeger_demo
  4. Run the go mod tidy command to clean up the unused dependencies.
  5. Run the go run tracingdemo command to run the demo.

Use the Jaeger agent to report Go application data

  1. Obtain the required endpoint. For more information, see the Prerequisites section in this topic.
  2. Run the following command to download the demo:
    wget https://arms-apm-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/demo/tracing-demo.zip && unzip tracing-demo.zip
  3. Start the Jaeger agent. For more information, see Install the Jaeger agent.
  4. Open the examples/settings.go file in the demo and set the AgentSwitch parameter to true.
    jaeger_demo
  5. Run the go mod tidy command to clean up the unused dependencies.
  6. Run the go run tracingdemo command to run the demo.

FAQ

Q: Why does the following error occur when I use Jaeger to report Go application data?
2021/06/28 21:11:54 ERROR: error when flushing the buffer: error from collector: 403
2021/06/28 21:11:54 ERROR: error when flushing the buffer: error from collector: 403

A: This is because the specified endpoint is invalid. Specify a valid endpoint and try again.