Report Go application data using Jaeger

更新时间:
复制 MD 格式

You can instrument your Go application with Jaeger and report traces to Managed Service for OpenTelemetry. After the traces are reported, Managed Service for OpenTelemetry starts to monitor your application. You can view various monitoring data, such as application topology, call stacks, abnormal transactions, slow transactions, and SQL analysis. This topic describes how to use the Jaeger SDK and Jaeger Agent for instrumentation and to report Go application data.

Important
  • For richer features, advanced tracing capabilities, and a better experience, connect your application to Managed Service for OpenTelemetry by using the OpenTelemetry protocol. For more information, see Preparations.

  • ARMS provides a commercially supported, self-developed agent for Go applications. This agent enables non-intrusive instrumentation and provides a richer feature set and greater stability. For more information, see Monitor Go applications.

Prerequisites

Obtain an endpoint

New console

  1. Log on to the Managed Service for OpenTelemetry console. In the left-side navigation pane, click Integration Center.

  2. On the Integration Center page, click the Jaeger card in the Open Source Frameworks section.

  3. In the Jaeger panel, click the Start Integration tab, and then select a region in which you want to report data.

    Note

    When you access a region for the first time, resources are automatically initialized there.

  4. Configure the Connection Type and Export Protocol parameters and copy an endpoint.

    • Connection Type: If your service is deployed on Alibaba Cloud and resides in the region that you selected, we recommend that you set this parameter to Alibaba Cloud VPC Network. Otherwise, set this parameter to Public Network.

    • Export Protocol: Set this parameter to HTTP (recommended) or gRPC based on the protocol that is supported by the client.

    image

Old console

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

  2. In the left-side navigation pane, click Cluster Configurations. On the page that appears, click the Access point information tab.

  3. In the top navigation bar, select a region in which you want to report data. In the Cluster Information section, turn on Show Token.

  4. Set the Client parameter to Jaeger.

    In the Related Information column of the table, copy an endpoint.jager中国.jpg

    Note

    If your application is deployed in an Alibaba Cloud production environment, use a VPC endpoint. Otherwise, use a public endpoint.

Background information

How is the data reported?

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

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

    image

Method 1: Report data using the Jaeger SDK

This section uses the Go Modules dependency management tool as an example. You can use the Jaeger SDK for instrumentation to directly report data to the Managed Service for OpenTelemetry server side. If you use other dependency management tools, perform the required operations.

  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 your client and region. You can obtain the endpoint from the Endpoint Information page in the Managed Service for OpenTelemetry console. For more information about how to obtain the endpoint, see the Prerequisites section of this topic.

    func NewJaegerTracer(service string) (opentracing.Tracer, io.Closer) {
    
        sender := transport.NewHTTPTransport(
           // Set the gateway. The gateway varies based on the region.
            "<endpoint>",
        )
       tracer, closer:= jaeger.NewTracer(service,
               jaeger.NewConstSampler(true),
               jaeger.NewRemoteReporter(sender))
       return tracer, closer
    }
  3. Create a span instance object and enable data pass-through.

    • If no parentSpan exists:

      // Create a span.
      span := tracer.StartSpan("myspan")
      // Set a tag.
      clientSpan.SetTag("mytag", "123")
      // Pass through the trace ID.
      tracer.Inject(span.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header))
      ...
      defer  span.Finish()
    • If a parentSpan exists:

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

    For more information about how to use the SDK, see Go Doc.

Method 2: Report data using the Jaeger Agent

  1. Start the Jaeger Agent. For more information, see Install 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 instance object and enable data pass-through.

    • If no parentSpan exists:

      // Create a span.
      span := tracer.StartSpan("myspan")
      // Set a tag.
      clientSpan.SetTag("mytag", "123")
      // Pass through the trace ID.
      tracer.Inject(span.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header))
      ...
      defer  span.Finish()
    • If a parentSpan exists:

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

    For more information about how to use the SDK, see Go Doc.

Examples

Report data directly using the Jaeger SDK

  1. Obtain the endpoint. For more information, see the Prerequisites section of this topic.

  2. Run the following command to download the sample file.

    wget https://arms-apm-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/demo/tracing-demo.zip && unzip tracing-demo.zip
  3. Open the examples/settings.go file in the sample folder and replace the placeholder value of TracingAnalysisEndpoint with the endpoint that you obtained in Step 1.

    jaeger_demo

  4. Run the go mod tidy command to manage dependencies.

  5. Run the go run tracingdemo command to run the sample.

Report data using the Jaeger Agent

  1. Obtain the endpoint. For more information, see the Prerequisites section of this topic.

  2. Run the following command to download the sample file.

    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 Jaeger Agent.

  4. Open the examples/settings.go file in the sample folder and change the value of AgentSwitch to true.

    jaeger_demo

  5. Run the go mod tidy command to manage dependencies.

  6. Run the go run tracingdemo command to run the sample.

FAQ

Q: Why does the following error occur during runtime?

2021/06/28 21:11:54 ERROR: error when flushing the buffer: error from collector: 403

A: This error indicates that the endpoint is incorrect. Correct the endpoint and try again.