Grafana Tempo 分布式追踪

FreeGuideOnline 13阅读 2026-07-12

yaml server: http_listen_port: 3200

distributor: receivers: otlp: protocols: grpc:

ingester: max_block_duration: 5m

compactor: compaction: block_retention: 48h

storage: trace: backend: local local: path: /tmp/tempo/blocks


### 编写 Docker Compose 文件
创建 `docker-compose.yml`:

```yaml
version: "3"
services:
  tempo:
    image: grafana/tempo:latest
    command: [ "-config.file=/etc/tempo.yaml" ]
    volumes:
      - ./tempo.yaml:/etc/tempo.yaml
      - tempo_data:/tmp/tempo
    ports:
      - "3200:3200"   # Tempo HTTP API
      - "4317:4317"   # OTLP gRPC receiver

  grafana:
    image: grafana/grafana:latest
    environment:
      - GF_AUTH_ANONYMOUS_ENABLED=true
      - GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
    volumes:
      - ./datasources.yaml:/etc/grafana/provisioning/datasources/datasources.yaml
    ports:
      - "3000:3000"

  agent:
    image: grafana/agent:latest
    volumes:
      - ./agent.yaml:/etc/agent/agent.yaml
    depends_on:
      - tempo

volumes:
  tempo_data:

配置 Grafana 数据源

创建 datasources.yaml

apiVersion: 1

datasources:
- name: Tempo
  type: tempo
  access: proxy
  url: http://tempo:3200
  basicAuth: false
  editable: true

配置 Grafana Agent

创建 agent.yaml,让 Agent 发送自身产生的追踪数据到 Tempo:

traces:
  configs:
  - name: default
    receivers:
      otlp:
        protocols:
          grpc:
    remote_write:
      - endpoint: tempo:4317
        insecure: true
    batch:
      timeout: 5s

启动服务

docker-compose up -d

访问 http://localhost:3000,在 Grafana 中即可以看见 Tempo 数据源。

发送追踪数据

你无需改造应用即可发送追踪数据,使用 OpenTelemetry SDK 或现有 Jaeger/Zipkin 客户端均可。

使用 OpenTelemetry 示例

以 Python 为例:

from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor

# 配置导出器,指向 Tempo OTLP 接收端
exporter = OTLPSpanExporter(endpoint="http://localhost:4317", insecure=True)

provider = TracerProvider()
processor = BatchSpanProcessor(exporter)
provider.add_span_processor(processor)
trace.set_tracer_provider(provider)

tracer = trace.get_tracer(__name__)

# 创建 Span
with tracer.start_as_current_span("my-operation") as span:
    span.set_attribute("http.method", "GET")
    # 执行业务逻辑

当应用运行时,Span 会被自动发送到 Tempo。

发送 Jaeger 或 Zipkin 数据

如果已有 Jaeger 客户端,可以将数据直接发送到 Tempo 的相应端口(Jaeger Thrift HTTP 端口 14268,Zipkin HTTP 端口 9411)。在 tempo.yaml 中添加:

distributor:
  receivers:
    jaeger:
      protocols:
        thrift_http:
    zipkin: