返回

Grafana Tempo:打造专属于您的分布式追踪平台

后端

Grafana Tempo简介

Grafana Tempo是一款开源的分布式追踪系统,由Grafana Labs开发。它旨在为开发人员和运维人员提供一种简单易用的方式来收集、存储和分析应用程序的跟踪数据。Tempo可以与各种流行的编程语言和框架集成,包括Java、Python、Node.js、Go和Ruby。它还与Kubernetes和OpenTelemetry等流行的云原生工具集成。

Tempo具有以下特点:

  • 可扩展性: Tempo可以轻松扩展到处理大量跟踪数据,非常适合大型分布式系统。
  • 高性能: Tempo具有非常高的吞吐量和低延迟,非常适合需要实时跟踪数据的应用程序。
  • 易用性: Tempo易于安装和配置,即使对于没有分布式追踪经验的人员来说也是如此。
  • 开源: Tempo是一款开源软件,这意味着您可以自由地使用、修改和分发它。

在Kubernetes中安装Grafana Tempo

要使用Tempo收集和分析应用程序跟踪数据,我们首先需要在Kubernetes中安装Tempo。

先决条件

在安装Tempo之前,您需要满足以下先决条件:

  • Kubernetes集群(版本1.16或更高)
  • Helm(版本3或更高)

安装步骤

  1. 添加Grafana Labs Helm仓库。
helm repo add grafana https://grafana.github.io/helm-charts
  1. 更新Helm仓库索引。
helm repo update
  1. 安装Tempo。
helm install tempo grafana/tempo

这将在您的Kubernetes集群中安装Tempo。

验证安装

要验证Tempo是否已成功安装,请使用以下命令检查Tempo的Pod是否正在运行:

kubectl get pods -n tempo

您应该看到以下输出:

NAME                                      READY   STATUS    RESTARTS   AGE
tempo-tempo-0                            1/1     Running   0          5m25s
tempo-tempo-istio-sidecar-0              1/1     Running   0          5m25s
tempo-tempo-query-0                       1/1     Running   0          5m25s
tempo-tempo-store-0                       1/1     Running   0          5m25s
tempo-tempo-collector-0                   1/1     Running   0          5m25s

这表明Tempo已成功安装并且正在运行。

使用Tempo收集和分析跟踪数据

要使用Tempo收集和分析跟踪数据,您需要在您的应用程序中集成Tempo客户端库。Tempo客户端库有多种语言可供选择,包括Java、Python、Node.js、Go和Ruby。

集成Tempo客户端库

以下是如何在您的应用程序中集成Tempo客户端库:

Java

import io.opentracing.Tracer;
import io.tempo.sdk.trace.TempoSpanBuilder;
import io.tempo.sdk.trace.TempoTracer;

public class MyApplication {

    private static final Tracer tracer = new TempoTracer.Builder()
            .withCollectorEndpoint("http://tempo-collector:4317")
            .build();

    public static void main(String[] args) {
        // Create a span for the current operation.
        TempoSpanBuilder span = tracer.buildSpan("my-operation");

        // Add tags to the span.
        span.addTag("component", "my-application");

        // Start the span.
        span.start();

        // Do some work.
        try {
            // ...
        } catch (Exception e) {
            // Add an error tag to the span.
            span.addErrorTag(e);
        }

        // Finish the span.
        span.finish();
    }
}

Python

from tempo import Tempo

# Create a Tempo client.
client = Tempo(
    endpoint="http://tempo-collector:4317",
    project="my-project",
)

# Create a span for the current operation.
with client.span("my-operation") as span:
    # Add tags to the span.
    span.add_tag("component", "my-application")

    # Do some work.
    try:
        # ...
    except Exception as e:
        # Add an error tag to the span.
        span.add_error(e)

    # Finish the span.
    span.finish()

Node.js

const tempo = require("@tempo-oss/tempo");

// Create a Tempo client.
const client = new tempo.Tempo({
    endpoint: "http://tempo-collector:4317",
    project: "my-project",
});

// Create a span for the current operation.
const span = client.startSpan("my-operation");

// Add tags to the span.
span.addTag("component", "my-application");

// Do some work.
try {
    // ...
} catch (e) {
    // Add an error tag to the span.
    span.addError(e);
}

// Finish the span.
span.finish();

Go

import (
    "context"
    "log"

    tempo "github.com/tempo-oss/tempo-go/tempo"
)

// Create a Tempo client.
client, err := tempo.NewClient(
    tempo.WithEndpoint("http://tempo-collector:4317"),
    tempo.WithProject("my-project"),
)
if err != nil {
    log.Fatal(err)
}

// Create a span for the current operation.
span := client.StartSpan(context.Background(), "my-operation")

// Add tags to the span.
span.SetTag("component", "my-application")

// Do some work.
try {
    // ...
} catch (e) {
    // Add an error tag to the span.
    span.SetError(e)
}

// Finish the span.
span.End()

Ruby

require "tempo"

# Create a Tempo client.
client = Tempo::Client.new(
    endpoint: "http://tempo-collector:4317",
    project: "my-project",
)

# Create a span for the current operation.
span = client.span("my-operation")

# Add tags to the span.
span.add_tag("component", "my-application")

# Do some work.
try {
    # ...
} catch (e) {
    # Add an error tag to the span.
    span.add_error(e)
}

# Finish the span.
span.finish

发送跟踪数据到Tempo

一旦您在应用程序中集成了Tempo客户端库,您就可以开始发送跟踪数据到Tempo了。

要发送跟踪数据到Tempo,您需要使用Tempo客户端库中的API来创建Span对象并将其发送到Tempo收集器。

以下是如何使用Tempo客户端库发送跟踪数据:

Java

// Create a span for the current operation.
TempoSpanBuilder span = tracer.buildSpan("my-operation");

// Add tags to the span.
span.addTag("component", "my-application");

// Start the span.
span.start();

// Do some work.
try {
    // ...
} catch (Exception e) {
    // Add an error tag to the span.
    span.addErrorTag(e);
}

// Finish the span.
span.finish();

Python

# Create a span for the current operation.
with client.span("my-operation") as span:
    # Add tags to the span.
    span.add_tag("component", "my-application")

    # Do some work.
    try:
        # ...
    except Exception as e:
        # Add an error tag to the span.
        span.add_error(e)

    # Finish the span.
    span.finish()

Node.js

// Create a span for the current operation.
const span = client.startSpan("my-operation");

// Add tags to the span.
span.addTag("component", "my-application");

// Do some work.
try {
    // ...
} catch (e) {
    // Add an error tag to the span.
    span.addError(e);
}

// Finish the span.
span.finish();

Go