Back to Home

OpenTelemetry Go stack: Tempo Prometheus Loki

The article describes the deployment of the OpenTelemetry stack for Go applications with Tempo, Prometheus and Loki. It provides docker-compose, configurations and Go code for initializing providers. Integration into Grafana enables transitions between metrics, traces and logs.

OTel stack for Go: full deployment guide
Advertisement 728x90

Deploying OpenTelemetry Stack for Go: Tracing, Metrics, and Logs

OpenTelemetry stack for Go applications collects traces in Tempo, metrics in Prometheus, and logs in Loki. Client app-1 sends requests to server app-2 via otel-collector. Grafana visualizes all the data. The full stack runs with docker-compose using minimal configuration.

Docker Compose Configuration

Key services: two Go applications, otel-collector as telemetry receiver, backends Tempo/Prometheus/Loki, and Grafana. The observability network isolates traffic.

Key ports:

Google AdInline article slot
  • app-2: 8080
  • otel-collector: 4317 (gRPC), 4318 (HTTP), 8889 (metrics)
  • Prometheus: 9090
  • Tempo: 3200
  • Loki: 3100
  • Grafana: 3000
services:
  app-1:
    build: ./app_1
    depends_on:
      otel-collector:
        condition: service_started
    networks:
      - observability
  app-2:
    build: ./app_2
    ports:
      - "8080:8080"
    networks:
      - observability
  otel-collector:
    image: otel/opentelemetry-collector-contrib:latest
    ports:
      - "4318:4318"
      - "4317:4317"
      - "8889:8889"
    volumes:
      - ./configs/otel-collector-config.yaml:/etc/otelcol/config.yaml:ro
    networks:
      - observability
  prometheus:
    image: prom/prometheus:latest
    ports:
      - "9090:9090"
    volumes:
      - ./configs/prometheus.yml:/etc/prometheus/prometheus.yml:ro
    networks:
      - observability
  tempo:
    image: grafana/tempo:2.7.0
    ports:
      - "3200:3200"
    volumes:
      - ./configs/tempo-config.yaml:/etc/tempo/tempo.yaml
    networks:
      - observability
  loki:
    image: grafana/loki:3.3.2
    ports:
      - "3100:3100"
    volumes:
      - ./configs/loki-config.yaml:/etc/loki/local-config.yaml:ro
    networks:
      - observability
  grafana:
    image: grafana/grafana:latest
    ports:
      - "3000:3000"
    volumes:
      - ./configs/grafana/datasources.yaml:/etc/grafana/provisioning/datasources/datasources.yaml:ro
    networks:
      - observability
networks:
  observability:
    driver: bridge

Tempo Configuration for Tracing

Tempo receives OTLP via gRPC (4317) and HTTP (4318). Trace blocks are compacted with 30m retention. trace_idle_period of 5s ensures quick visibility in the UI.

server:
  http_listen_port: 3200
  grpc_listen_port: 9095
distributor:
  receivers:
    otlp:
      protocols:
        grpc:
          endpoint: "0.0.0.0:4317"
ingester:
  trace_idle_period: 5s
  max_block_duration: 30m
storage:
  trace:
    backend: local
    local:
      path: /var/tempo/traces
compactor:
  compaction:
    block_retention: 30m

Prometheus and Alerting Rules

Metrics are scraped from otel-collector:8889 every 60s. Alert rules track P95 latency > 2000ms and error rate > 5%.

global:
  scrape_interval: 60s
scrape_configs:
  - job_name: "otel-collector"
    static_configs:
      - targets: ["otel-collector:8889"]

Example rules:

Google AdInline article slot
groups:
  - name: app1-service-alerts
    rules:
      - alert: App1LatencyHigh
        expr: histogram_quantile(0.95, sum by (le) (rate(app_1_requests_latency_ms_bucket[5m]))) > 2000
        for: 3m
        labels:
          severity: warning
  - name: app2-service-alerts
    rules:
      - alert: App2HighErrorRate
        expr: rate(app_2_requests_total{status="failed"}[5m]) / rate(app_2_requests_total[5m]) > 0.05
        for: 2m
        labels:
          severity: critical

Loki Configuration for Logs

Logs are stored in filesystem with TSDB schema v13. Indexing by 24h periods. Structured metadata is enabled.

auth_enabled: false
server:
  http_listen_port: 3100
common:
  path_prefix: /tmp/loki
  storage:
    filesystem:
      chunks_directory: /tmp/loki/chunks
schema_config:
  configs:
    - from: 2020-10-24
      store: tsdb
      schema: v13
      index:
        period: 24h
limits_config:
  allow_structured_metadata: true

Integration in Grafana

Datasources link metrics→traces→logs:

  • Prometheus exemplarTraceIdDestinations
  • Loki derivedFields by traceID
  • Tempo serviceMap and tracesToLogsV2

Initializing OTel in the Go Client

The InitOTel function creates providers for all signals with resource attributes. 10% sampling for traces, batching for export.

Google AdInline article slot
func InitOTel(ctx context.Context, serviceName string) (*log.LoggerProvider, func(context.Context) error) {
    ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
    defer cancel()

    res := resource.NewWithAttributes(
       semconv.SchemaURL,
       semconv.ServiceName(serviceName),
       semconv.ServiceVersion("1.0.0"),
    )

    // Tracing
    traceExp, err := otlptracegrpc.New(ctx,
       otlptracegrpc.WithEndpoint("otel-collector:4317"),
       otlptracegrpc.WithInsecure(),
    )
    tracerProvider := sdktrace.NewTracerProvider(
       sdktrace.WithSampler(sdktrace.ParentBased(sdktrace.TraceIDRatioBased(0.1))),
       sdktrace.WithBatcher(traceExp),
       sdktrace.WithResource(res),
    )
    otel.SetTracerProvider(tracerProvider)

    // Metrics
    metricExp, err := otlpmetricgrpc.New(ctx,
       otlpmetricgrpc.WithEndpoint("otel-collector:4317"),
       otlpmetricgrpc.WithInsecure(),
    )
    meterProvider := sdkmetric.NewMeterProvider(
       sdkmetric.WithReader(sdkmetric.NewPeriodicReader(metricExp, sdkmetric.WithInterval(10*time.Second))),
       sdkmetric.WithResource(res),
    )
    otel.SetMeterProvider(meterProvider)

    // Logs
    logExp, _ := otlploggrpc.New(ctx,
       otlploggrpc.WithEndpoint("otel-collector:4317"),
       otlploggrpc.WithInsecure(),
    )
    logProvider := log.NewLoggerProvider(
       log.WithProcessor(log.NewBatchProcessor(logExp)),
       log.WithResource(res),
    )

    return logProvider, func(ctx context.Context) error {
       // shutdown logic
       return nil
    }
}

Key Points

  • Full OTel Stack: tracing (Tempo), metrics (Prometheus), logs (Loki) integrated via otel-collector
  • Go Instrumentation: otelhttp.Transport, otelslog.Handler, counter/histogram metrics
  • Native Grafana Integration: transitions from metrics→traces→logs by traceID
  • Performance: trace_idle_period 5s, 10% sampling, 10s periodic reader
  • Alerting: P95 latency > 2000ms, error rate > 5%

— Editorial Team

Advertisement 728x90

Read Next