Prometheus client_golang vs OpenTelemetry: Go 服务指标埋点

给 Go 服务加指标(比如「查一次词花了多少毫秒」),代码里要选一个埋点库。常见的两个选择是 Prometheus 官方的 client_golang 和 OpenTelemetry 的 Go SDK。两者都能产出同样的 Prometheus 指标,差别在覆盖范围、配置量和对后端的绑定程度。 采集、存储、展示这一层(Alloy、Prometheus、Loki、Grafana)的对比见 可观测性工具对比。本文只讲应用代码里的埋点这一层。 埋点库在哪一层 graph LR A["应用代码埋点库:client_golang 或 OTel SDK"] --> B["采集Prometheus 抓取 / Alloy / OTel Collector"] B --> C["存储Prometheus / Mimir / Grafana Cloud"] C --> D["展示Grafana"] 埋点库决定两件事:代码里怎么记录一个数字,以及这个数字以什么格式交出去(Prometheus 的 /metrics 文本格式,或者 OTLP 协议)。它不决定数据最后存在哪。 Prometheus client_golang github.com/prometheus/client_golang 是 Prometheus 项目的官方 Go 客户端,只做指标。 工作方式是拉(pull):应用在内存里维护计数器和直方图,通过 HTTP 暴露一个 /metrics 端点,Prometheus(或 Alloy)定期来抓。应用不知道谁来读,也不主动往外发。 四种指标类型: 类型 用途 例子 Counter 只增不减的计数 请求总数、错误总数 Gauge 可增可减的当前值 正在处理的请求数、队列长度 Histogram 按桶统计分布,服务端可算分位数 请求耗时,用来算 p95/p99 Summary 客户端直接算分位数 很少用;分位数无法跨实例聚合,一般用 Histogram 最小示例: ...

Created 29296-09-29 · Updated 29296-09-29 · 3 min · 443 words · -

可观测性工具对比:Metrics、Logs、Traces 采集与存储

可观测性(Observability)通常分三个信号:Metrics(指标)、Logs(日志)、Traces(链路追踪)。每个信号都有对应的采集工具和存储后端,本文整理常见工具并说明相互关系。 技术栈全景 graph TD A["采集 / AgentAlloy · Promtail · node_exporterTelegraf · Filebeat · OTel Collector"] A -->|push / pull| B["Prometheus(Metrics)"] A -->|push / pull| C["Loki(Logs)"] A -->|push / pull| D["Tempo(Traces)"] B --> E["Grafana(展示 / 查询)"] C --> E D --> E 采集工具对比 Metrics 采集 工具 归属 定位 日志能力 Traces 能力 node_exporter Prometheus / CNCF Linux 主机 metrics 暴露 ✗ ✗ Telegraf InfluxData 通用 metrics 采集(300+ 插件) 弱(有 tail/syslog 插件,非主要定位) ✗ Grafana Alloy Grafana Labs 统一采集(metrics + logs + traces) ✅ ✅ node_exporter:轻量,只暴露 /metrics 端点供 Prometheus scrape,不主动推送。几乎每个 Linux 节点标配。 Telegraf:InfluxDB 生态的核心 agent,插件丰富,适合往 InfluxDB / Prometheus 写数据。如果需要完整日志采集需另加工具(如 Filebeat)。 Alloy:Grafana 2024 年推出的统一 agent,一个进程替代 node_exporter + Promtail + OTel Collector,是 Grafana Agent 的继任者。 Logs 采集 工具 归属 定位 写入目标 维护状态 Promtail Grafana Labs 专用日志 agent(Loki 配套) Loki only 维护模式(停止新功能) Filebeat Elastic 日志采集(ELK 配套) Elasticsearch / Logstash 主动开发 Fluentd / Fluent Bit CNCF 通用日志管道 Elasticsearch、Loki、Kafka 等 主动开发 Grafana Alloy Grafana Labs 统一采集 Loki / Prometheus / Tempo 主动开发 Promtail:已进入维护模式,官方建议迁移到 Alloy。详见 Promtail 介绍。 Filebeat:Elastic 全家桶(ELK)的配套工具,不属于 Grafana 技术栈。 Fluent Bit:比 Fluentd 更轻量,K8s 中常用于节点级日志采集,输出目标灵活(可写 Loki 或 Elasticsearch)。 Alloy:同时支持 logs,是 Grafana 技术栈里的推荐选项。 Traces 采集 工具 归属 定位 OpenTelemetry Collector CNCF 标准链路追踪采集,支持 OTLP / Jaeger / Zipkin Grafana Alloy Grafana Labs 内置 OTel receiver,可替代 OTel Collector Jaeger Agent CNCF Jaeger 专用采集,逐步被 OTel 替代 存储后端对比 工具 信号类型 归属 查询语言 典型搭配 Prometheus Metrics CNCF PromQL Grafana + Alloy/node_exporter Loki Logs Grafana Labs LogQL Grafana + Alloy/Promtail Tempo Traces Grafana Labs TraceQL Grafana + Alloy/OTel Collector InfluxDB Metrics(时序) InfluxData Flux / InfluxQL Grafana + Telegraf Elasticsearch Logs(也可做 metrics) Elastic Lucene / EQL Kibana + Filebeat VictoriaMetrics Metrics VictoriaMetrics PromQL 兼容 可替换 Prometheus,更省内存 展示工具 工具 特点 Grafana 支持所有主流数据源(Prometheus、Loki、Tempo、InfluxDB、Elasticsearch 等),最通用 Kibana Elastic 专用,深度集成 Elasticsearch,日志搜索体验强 InfluxDB UI InfluxDB 内置,适合 InfluxDB 用户 两大技术栈总结 Grafana 技术栈(推荐用于 self-hosted / homelab) graph TD A["Grafana Alloy(一个 agent 全搞定)"] A --> B[Prometheus] A --> C[Loki] A --> D[Tempo] B --> E[Grafana] C --> E D --> E Elastic 技术栈(ELK) graph TD A["Filebeat / Logstash"] --> B[Elasticsearch] B --> C[Kibana] 选型建议 homelab / 个人项目:Grafana 全家桶(Prometheus + Loki + Grafana + Alloy),资源占用合理,开箱即用。 已有 InfluxDB:继续用 Telegraf,不必迁移;如果想要日志需额外引入 Fluent Bit → Loki。 企业 K8s 日志:Fluent Bit(DaemonSet)→ Loki 或 Elasticsearch,视现有技术栈决定。 新项目要全套可观测性:OpenTelemetry(采集 SDK)+ OTel Collector / Alloy → Prometheus + Loki + Tempo + Grafana。

Created 12126-06-12 · Updated 12126-06-12 · 2 min · 357 words · -

Prometheus Pushgateway

Prometheus 的拉取模型(pull model)要求被监控目标必须持续运行并暴露 HTTP /metrics 端点。但有些场景不适合这种模式: 批处理作业(Batch Job):运行几秒或几分钟就退出,Prometheus 来不及抓取 CronJob:定时执行,完成后进程消失 短暂任务:数据管道、一次性脚本 Pushgateway 就是为这类场景设计的中间缓冲层。 工作原理 Batch Job → POST metrics → Pushgateway ← scrape ← Prometheus → Grafana 批处理作业完成后,主动将指标 推送(push) 到 Pushgateway Pushgateway 将指标持久保存在内存中 Prometheus 按照正常 pull 机制定期抓取 Pushgateway 的 /metrics 端点 Grafana 查询 Prometheus 展示数据 安装(Kubernetes) apiVersion: apps/v1 kind: Deployment metadata: name: pushgateway spec: replicas: 1 template: spec: containers: - name: pushgateway image: prom/pushgateway:v1.11.0 ports: - containerPort: 9091 默认端口 9091,Web UI 可以查看当前存储的所有指标。 ...

Created 3036-04-03 · Updated 3036-04-03 · 2 min · 226 words · -