All Products
Search
Document Center

Application Real-Time Monitoring Service:Use Jaeger to report Go application data

Last Updated:Dec 20, 2023

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

To obtain an endpoint of Jaeger or Zipkin, perform the following steps:

  1. Log on to the Managed Service for OpenTelemetry console.

  2. In the left-side navigation pane, click Cluster Configurations. Then, click the Access point information tab.

  3. In the top navigation bar, select a region. In the Cluster Information section, turn on Show Token.

  4. In the Client section, click Jaeger or Zipkin.

    Obtain an endpoint of Jaeger or Zipkin in the Related Information column of the table in the lower part.

    Jaeger/Zipkin接入点信息

    Note

    If your application is deployed in an Alibaba Cloud production environment, use a VPC endpoint. Otherwise, use a public endpoint. Generally, use the endpoint of v2 for Zipkin. Use the endpoint of v1 only if you know Zipkin well.

Background information

How is data reported?

  • The following figure shows how to report data without using the Jaeger agent. 不通过Jaeger Agent而直接上报

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

    通过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

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