Grafana 系列文章(三):Tempo-使用 HTTP 推送 Spans

grafana,系列,文章,tempo,使用,http,推送,spans · 浏览次数 : 297

小编点评

**使用 Zipkin 接收器将 Spans 推送到 Tempo 中** **步骤:** 1. 创建一个配置文件 `config.yaml`,指定 Zipkin 接收器的地址和端口。 2. 启动 Tempo,并创建一个 `server` 配置。 3. 在 Bash 脚本中使用 `curl` 向 Zipkin 接收器的 HTTP 接口推送 Spans。 **示例配置文件 `config.yaml`:** ```yaml server: http_listen_port: 3200distributor: receivers: zipkin:storage: trace: backend: local local: path: /tmp/tempo/blocks ``` **使用 curl 推送 Spans:** ```bash curl -X POST http://localhost:9411 -H 'Content-Type: application/json' -d '[{ \"id\": \"1234\", \"traceId\": \"0123456789abcdef\", \"timestamp\": 1608239395286533, \"duration\": 100000, \"name\": \"span from bash!\", \"tags\": { \"http.method\": \"GET\", \"http.path\": \"/api\" }, \"localEndpoint\": { \"serviceName\": \"shell script\" }}]' ``` **注意:** * `timestamp` 字段以微秒为单位。 * `duration` 和 `parentId` 字段用于建立追踪中的子任务。 * `localEndpoint` 字段指定 spans 的来源。

正文

👉️URL: https://grafana.com/docs/tempo/latest/api_docs/pushing-spans-with-http/

📝Description:

有时,使用追踪系统是令人生畏的,因为它似乎需要复杂的应用程序仪器或 span 摄取管道,以便 ...

有时,使用追踪系统是令人生畏的,因为你似乎需要复杂的应用程序仪器或 span 摄取管道才能推送 span。本指南旨在展示一种极其基本的技术,即使用 Zipkin 接收器,从 Bash 脚本中用 http/json 推送 span。

启动 Tempo

首先,让我们在配置好 Zipkin 接收器的情况下启动 Tempo。为了做到这一点,要创建一个配置文件,像这样:

server:
  http_listen_port: 3200

distributor:
  receivers:
    zipkin:

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

并且运行 Tempo:

docker run -p 9411:9411 -p 3200:3200 -v $(pwd)/config.yaml:/config.yaml grafana/tempo:latest -config.file /config.yaml

推送 Spans

现在 Tempo 正在运行,并且在 9411 端口监听 Zipkin spans,让我们用curl推送一个 span 到它。

curl -X POST http://localhost:9411 -H 'Content-Type: application/json' -d '[{
 "id": "1234",
 "traceId": "0123456789abcdef",
 "timestamp": 1608239395286533,
 "duration": 100000,
 "name": "span from bash!",
 "tags": {
    "http.method": "GET",
    "http.path": "/api"
  },
  "localEndpoint": {
    "serviceName": "shell script"
  }
}]'

请注意,timestamp字段是以微秒为单位的,是通过运行date +%s%6N得到的。duration字段也是以微秒为单位,所以 100000 是 100 毫秒。

接收 Traces

获得追踪的最简单方法是对 Tempo 执行一个简单的 curl 命令。返回的格式是 OTLP

curl http://localhost:3200/api/traces/0123456789abcdef | jq

{
  "batches": [
    {
      "resource": {
        "attributes": [
          {
            "key": "service.name",
            "value": {
              "stringValue": "shell script"
            }
          }
        ]
      },
      "instrumentationLibrarySpans": [
        {
          "spans": [
            {
              "traceId": "AAAAAAAAAAABI0VniavN7w==",
              "spanId": "AAAAAAAAEjQ=",
              "name": "span from bash!",
              "startTimeUnixNano": "1608239395286533000",
              "endTimeUnixNano": "1608239395386533000",
              "attributes": [
                {
                  "key": "http.path",
                  "value": {
                    "stringValue": "/api"
                  }
                },
                {
                  "key": "http.method",
                  "value": {
                    "stringValue": "GET"
                  }
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

然而,在 bash 中盯着一个 json blob 不是很有趣。让我们启动 Tempo query ,这样我们就可以直观地看到我们的追踪。Tempo query 是 Jaeger Query 的一个 GRPC Plugin,它可以用来查询 Tempo。

docker run --env BACKEND=localhost:3200 --net host grafana/tempo-query:latest

并在你选择的浏览器中打开http://localhost:16686/trace/0123456789abcdef,以查看:

single span

更多 Spans

现在我们已经有了基本的东西,很容易继续建立我们的追踪。通过指定相同的 trace ID 和一个 parent span ID,我们可以开始建立一个追踪。

curl -X POST http://localhost:9411 -H 'Content-Type: application/json' -d '[{
 "id": "5678",
 "traceId": "0123456789abcdef",
 "parentId": "1234",
 "timestamp": 1608239395316533,
 "duration": 100000,
 "name": "child span from bash!",
  "localEndpoint": {
    "serviceName": "shell script"
  }
}]'

而现在,用户界面显示:

parent and child spans

Spans from everything

追踪并不限于具有复杂框架的企业语言。正如你所看到的,从你的 js、python 或 bash 脚本中存储和追踪事件很容易。今天你可以使用 Tempo/分布式追踪来追踪 CI 管道,长期运行的 bash 进程,python 数据处理流程或任何你能想到的其他东西。

祝你追踪 (tracing) 成功!

Grafana 系列文章

Grafana 系列文章

三人行, 必有我师; 知识共享, 天下为公. 本文由东风微鸣技术博客 EWhisper.cn 编写.

与Grafana 系列文章(三):Tempo-使用 HTTP 推送 Spans相似的内容:

Grafana 系列文章(三):Tempo-使用 HTTP 推送 Spans

👉️URL: https://grafana.com/docs/tempo/latest/api_docs/pushing-spans-with-http/ 📝Description: 有时,使用追踪系统是令人生畏的,因为它似乎需要复杂的应用程序仪器或 span 摄取管道,以便 ... 有时,使

Grafana 系列文章(一):基于 Grafana 的全栈可观察性 Demo

📚️Reference: https://github.com/grafana/intro-to-mlt 这是关于 Grafana 中可观察性的三个支柱的一系列演讲的配套资源库。 它以一个自我封闭的 Docker 沙盒的形式出现,包括在本地机器上运行和实验所提供的服务所需的所有组件。 Grafan

Grafana 系列文章(二):使用 Grafana Agent 和 Grafana Tempo 进行 Tracing

👉️URL: https://grafana.com/blog/2020/11/17/tracing-with-the-grafana-cloud-agent-and-grafana-tempo/ ✍Author: Robert Fratto • 17 Nov 2020 📝Description

Grafana 系列文章(四):Grafana Explore

👉️URL: https://grafana.com/docs/grafana/latest/explore/ 📝Description: Explore Grafana 的仪表盘 UI 是关于构建可视化的仪表盘。Explore 剥离了仪表盘和面板选项,这样你就可以。.. Grafana 的仪表

Grafana 系列文章(五):Grafana Explore 查询管理

👉️URL: https://grafana.com/docs/grafana/latest/explore/query-management/ 📝Description: Explore 中的查询管理 为了帮助调试查询,Explore 允许你调查查询请求和响应,以及查询统计数据,... Exp

Grafana 系列文章(六):Grafana Explore 中的日志

👉️URL: https://grafana.com/docs/grafana/latest/explore/logs-integration/#labels-and-detected-fields 📝Description: Explore 中的日志 除了指标之外,Explore 还允许你在以

Grafana 系列文章(七):Grafana Explore 中的 Tracing

👉️URL: https://grafana.com/docs/grafana/latest/explore/trace-integration/ 📝Description: Tracing in Explore Explore 允许你将 tracing 数据源的痕迹可视化。这在 Grafana

Grafana 系列文章(八):Grafana Explore 中的 Inspector

👉️URL: https://grafana.com/docs/grafana/latest/explore/explore-inspector/ 📝Description: Explore 中的检查器 (Inspector). 检查器可以帮助你理解你的查询并排除故障。你可以检查原始数据,把这些

Grafana 系列文章(九):开源云原生日志解决方案 Loki 简介

简介 Grafana Labs 简介 Grafana 是用于时序数据的事实上的仪表盘解决方案。它支持近百个数据源。 Grafana Labs 想从一个仪表盘解决方案转变成一个可观察性 (observability) 平台,成为你需要对系统进行调试时的首选之地。 完整的可观察性 可观察性。关于这意味着

Grafana 系列文章(十):为什么应该使用 Loki

👉️URL: https://grafana.com/blog/2020/09/09/all-the-non-technical-advantages-of-loki-reduce-costs-streamline-operations-build-better-teams/ 📝Descript