OpenTelemetry Go-Zero monitoring [otelzero]

Vladimir Mihailenco
August 15, 2024
3 min read

Go-Zero has built-in OpenTelemetry support, making it easy to add distributed tracing to your microservices without additional instrumentation libraries. By enabling telemetry in your configuration, you can gain insights into application performance, troubleshoot issues, and analyze behavior across your entire service mesh.

What is Go-Zero?

Go-Zero is an open-source microservices framework for the Go programming language. It is designed to simplify the development of high-performance, scalable, and reliable microservices with built-in support for:

  • API gateway and RPC services
  • Service discovery and load balancing
  • Rate limiting and circuit breaking
  • Distributed tracing via OpenTelemetry

Go-Zero's integrated approach to observability means you don't need external instrumentation libraries—telemetry is configured directly in your service configuration.

What is OpenTelemetry?

OpenTelemetry is an open-source observability framework that aims to standardize and simplify the collection, processing, and export of telemetry data from applications and systems.

OpenTelemetry supports multiple programming languages and platforms, making it suitable for a wide range of applications and environments.

OpenTelemetry enables developers to instrument their code and collect telemetry data, which can then be exported to various OpenTelemetry backends or observability platforms for analysis and visualization. Configuration management is simplified through OpenTelemetry environment variables, allowing teams to adjust telemetry settings across development, staging, and production environments without modifying application code.

Go-Zero Telemetry Configuration

Go-Zero uses a Telemetry configuration block that maps to its internal trace.Config struct:

go
type Config struct {
    Name         string            `json:",optional"`
    Endpoint     string            `json:",optional"`
    Sampler      float64           `json:",default=1.0"`
    Batcher      string            `json:",default=otlpgrpc,options=zipkin|otlpgrpc|otlphttp|file"`
    OtlpHeaders  map[string]string `json:",optional"`
    OtlpHttpPath string            `json:",optional"`
    OtlpHttpSecure bool            `json:",optional"`
    Disabled     bool              `json:",optional"`
}
FieldDescription
NameService name that appears in traces
EndpointURL where trace data is sent
SamplerSampling rate from 0.0 to 1.0 (1.0 = 100%)
BatcherExport format: zipkin, otlpgrpc, otlphttp, or file
OtlpHeadersCustom headers for OTLP exporters (e.g., authentication)
OtlpHttpPathCustom HTTP path for OTLP HTTP exporter
OtlpHttpSecureEnable TLS for OTLP HTTP connections
DisabledDisable tracing entirely

Usage

You can instrument your Go-Zero application by enabling OpenTelemetry in your YAML configuration. No code changes are required—Go-Zero automatically instruments HTTP handlers and RPC calls.

API Service Configuration

To start monitoring your Go-Zero API service, add the Telemetry block to your config. If you don't have an Uptrace DSN, follow the Get started guide.

yaml
Name: user-api
Host: 0.0.0.0
Port: 8080
Mode: dev

Telemetry:
  Name: user-api
  Endpoint: localhost:14317
  Sampler: 1.0
  Batcher: otlpgrpc
  OtlpHeaders:
    uptrace-dsn: '<FIXME>'

RPC Service Configuration

For RPC services, the configuration is similar:

yaml
Name: user-rpc
ListenOn: 0.0.0.0:9000
Mode: dev

Telemetry:
  Name: user-rpc
  Endpoint: localhost:14317
  Sampler: 1.0
  Batcher: otlpgrpc
  OtlpHeaders:
    uptrace-dsn: '<FIXME>'

Loading Configuration

Go-Zero automatically initializes telemetry when loading your configuration:

go
package main

import (
    "github.com/zeromicro/go-zero/core/conf"
    "github.com/zeromicro/go-zero/rest"
)

func main() {
    var c rest.RestConf
    conf.MustLoad("etc/config.yaml", &c)

    // Telemetry is automatically initialized
    server := rest.MustNewServer(c)
    defer server.Stop()

    // Register your handlers
    server.Start()
}

You can also find a complete Docker example on GitHub.

What is Uptrace?

Uptrace is an open source APM for OpenTelemetry that supports distributed tracing, metrics, and logs. You can use it to monitor applications and troubleshoot issues.

Uptrace Overview

Uptrace comes with an intuitive query builder, rich dashboards, alerting rules, notifications, and integrations for most languages and frameworks.

Uptrace can process billions of spans and metrics on a single server and allows you to monitor your applications at 10x lower cost.

In just a few minutes, you can try Uptrace by visiting the cloud demo (no login required) or running it locally with Docker. The source code is available on GitHub.

What's next?

Now that you've instrumented your go-zero application, you can visualize traces, analyze performance metrics, and identify bottlenecks in your microservices architecture. Consider exploring Gin or gRPC instrumentation for additional Go observability patterns.