Skip to content

Traefik Metrics 概览

Metrics

Traefik 以 OpenTelemetry 格式以及以下特定供应商后端提供指标:

Traefik Proxy 为 on-premisesKubernetes 部署提供了官方 Grafana 仪表板。

OpenTelemetry

默认协议

OpenTelemetry 导出器默认使用 HTTP 将指标导出到 collector,目标地址为 https://localhost:4318/v1/metrics。

配置示例

要启用 OpenTelemetry 指标:

yaml
metrics:
  otlp: {}
toml
[metrics]
  [metrics.otlp]
bash
--metrics.otlp=true

Helm Chart Values

yaml
# values.yaml
metrics:
  # 禁用 Prometheus(默认启用)
  prometheus: null
  # 启用提供 OTel 指标
  otlp:
    enabled: true
    http:
      enabled: true

主要配置选项

字段描述默认值必填
metrics.addInternals启用内部资源的指标。false
metrics.otlp.serviceNameservice name resource 属性。"traefik"
metrics.otlp.resourceAttributes其它 resource 属性。[]
metrics.otlp.addEntryPointsLabels在 entry points 上启用指标。true
metrics.otlp.addRoutersLabels在 routers 上启用指标。false
metrics.otlp.addServicesLabels在 services 上启用指标。true
metrics.otlp.explicitBoundariesHistogram 显式边界。".005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10"
metrics.otlp.pushInterval发送指标到 OpenTelemetry Collector 的间隔。10s
metrics.otlp.http.endpointHTTP 端点。"https://localhost:4318/v1/metrics"
metrics.otlp.http.headers其它请求头。-
metrics.otlp.http.tls.*TLS 配置(ca、cert、key、insecureSkipVerify)。
metrics.otlp.grpc.endpointgRPC 端点。"localhost:4317"
metrics.otlp.grpc.headers其它请求头。-
metrics.otlp.grpc.insecure允许使用不安全协议。false
metrics.otlp.grpc.tls.*gRPC TLS 配置。

resourceAttributes

resourceAttributes 选项允许设置与 traces 一起发送的 resource 属性。Traefik 也支持 OTEL_RESOURCE_ATTRIBUTES 环境变量。

Kubernetes Resource Attributes Detection

此外,在 Kubernetes 集群中运行时,Traefik 会自动发现以下 Kubernetes resource attributes

  • k8s.namespace.name
  • k8s.pod.uid
  • k8s.pod.name

供应商

Datadog

yaml
metrics:
  datadog: {}
toml
[metrics]
  [metrics.datadog]
bash
--metrics.datadog=true
字段描述默认值必填
datadog.address发送指标到 datadog-agent 的地址。127.0.0.1:8125
datadog.addEntryPointsLabels在 entry points 上启用指标。true
datadog.addRoutersLabels在 routers 上启用指标。false
datadog.addServicesLabels在 services 上启用指标。true
datadog.pushInterval推送间隔。10s
datadog.prefix指标前缀。"traefik"

address

指示导出器将指标发送到此地址的 datadog-agent。此地址可以是 Unix Domain Socket (UDS),格式为 unix:///path/to/datadog.socket。当 prefix 设置为 unix 时,socket 类型将自动确定。要显式定义 socket 类型,可以分别使用前缀 unixgram(SOCK_DGRAM)和 unixstream(SOCK_STREAM)。

InfluxDB v2

yaml
metrics:
  influxDB2:
    address: http://localhost:8086
toml
[metrics]
  [metrics.influxDB2]
    address = "http://localhost:8086"
bash
--metrics.influxdb2=true
字段描述默认值必填
metrics.influxDB2.addEntryPointsLabels在 entry points 上启用指标。true
metrics.influxDB2.addRoutersLabels在 routers 上启用指标。false
metrics.influxDB2.addServicesLabels在 services 上启用指标。true
metrics.influxDB2.additionalLabels其它 InfluxDB 标签。-
metrics.influxDB2.pushInterval推送间隔。10s
metrics.influxDB2.addressInfluxDB v2 实例地址。"http://localhost:8086"
metrics.influxDB2.token认证 token。-
metrics.influxDB2.org组织名称。-
metrics.influxDB2.bucket存储桶。-

Prometheus

yaml
metrics:
  prometheus:
    buckets:
      - 0.1
      - 0.3
      - 1.2
      - 5.0
toml
[metrics]
  [metrics.prometheus]
    [metrics.prometheus.buckets]
      - 0.1
      - 0.3
      - 1.2
      - 5.0
bash
--metrics.prometheus=true
字段描述默认值必填
metrics.prometheus.addEntryPointsLabels在 entry points 上启用指标。true
metrics.prometheus.addRoutersLabels在 routers 上启用指标。false
metrics.prometheus.addServicesLabels在 services 上启用指标。true
metrics.prometheus.buckets延迟指标的桶。"0.100000, 0.300000, 1.200000, 5.000000"
metrics.prometheus.manualRouting禁用默认内部路由器,以便为 prometheus@internal service 创建自定义路由器。false
metrics.prometheus.entryPoint用于暴露指标的 Traefik Entrypoint 名称。"traefik"
metrics.prometheus.headerLabels从请求头提取的额外标签。

headerLabels

定义 requests_total 指标的额外标签。如果请求中不存在该 header,则会添加具有空值的标签。该标签必须是 Prometheus 指标的有效标签名称。

由于 Go 的处理,Host header 永远不会出现在请求的 Header map 中。要获取请求的 Host 作为标签,请改用 X-Forwarded-Host header。

StatsD

yaml
metrics:
  statsD:
    address: localhost:8125
toml
[metrics]
  [metrics.statsD]
    address = "localhost:8125"
bash
--metrics.statsd=true
字段描述默认值必填
metrics.statsD.addEntryPointsLabels在 entry points 上启用指标。true
metrics.statsD.addRoutersLabels在 routers 上启用指标。false
metrics.statsD.addServicesLabels在 services 上启用指标。true
metrics.statsD.pushInterval推送间隔。10s
metrics.statsD.address发送指标到 statsd 的地址。"127.0.0.1:8125"
metrics.statsD.prefix指标前缀。"traefik"

提供的指标

全局指标

每个后端都提供以下全局指标(名称因后端而异,例如 Prometheus 的是 traefik_config_reloads_total,Datadog 的是 config.reload.total):

指标(Prometheus 名称)类型描述
traefik_config_reloads_totalCount配置重新加载的总次数。
traefik_config_last_reload_successGauge上次成功重新加载配置的时间戳。
traefik_open_connectionsGauge(标签:entrypoint、protocol)按 entrypoint 和 protocol 当前的开放连接数。
traefik_tls_certs_not_afterGauge证书的过期日期。

EntryPoint 指标

指标类型标签
traefik_entrypoint_requests_totalCountcode、method、protocol、entrypoint
traefik_entrypoint_requests_tls_totalCounttls_version、tls_cipher、entrypoint
traefik_entrypoint_request_duration_secondsHistogramcode、method、protocol、entrypoint
traefik_entrypoint_requests_bytes_totalCountcode、method、protocol、entrypoint
traefik_entrypoint_responses_bytes_totalCountcode、method、protocol、entrypoint

Router 指标

指标类型标签
traefik_router_requests_totalCountcode、method、protocol、router、service
traefik_router_requests_tls_totalCounttls_version、tls_cipher、router、service
traefik_router_request_duration_secondsHistogramcode、method、protocol、router、service
traefik_router_requests_bytes_totalCountcode、method、protocol、router、service
traefik_router_responses_bytes_totalCountcode、method、protocol、router、service

Service 指标

指标类型标签
traefik_service_requests_totalCountcode、method、protocol、service
traefik_service_requests_tls_totalCounttls_version、tls_cipher、service
traefik_service_request_duration_secondsHistogramcode、method、protocol、service
traefik_service_retries_totalCountservice
traefik_service_server_upGaugeservice、url
traefik_service_requests_bytes_totalCountcode、method、protocol、service
traefik_service_responses_bytes_totalCountcode、method、protocol、service

标签

可用的标签:entrypoint(处理连接的 entrypoint)、protocol(连接协议)、cn(证书通用名称)、code(请求状态码)、method(请求方法)、router(处理请求的 router)、sans(证书 SANs)、serial(证书序列号)、service(处理请求的 service)、tls_ciphertls_versionurl(service server url)。

method 标签值

如果请求上的 HTTP 方法动词不是 HTTP/1.1 中定义的一组常见方法,也不是 PRI 动词(用于 HTTP/2),则 method 标签的值将变为 EXTENSION_METHOD

OpenTelemetry 语义约定

Traefik Proxy 遵循 官方 OpenTelemetry 语义约定 v1.23.1

HTTP Server

  • http.server.request.duration - Histogram(标签:error.type、http.request.method、http.response.status_code、network.protocol.name、server.address、server.port、url.scheme)- HTTP 服务器请求的持续时间。

HTTP Client

  • http.client.request.duration - Histogram(标签:error.type、http.request.method、http.response.status_code、network.protocol.name、server.address、server.port、url.scheme)- HTTP 客户端请求的持续时间。

在生产环境使用 Traefik OSS?

如果你在工作中使用 Traefik,可以考虑为其添加企业级 API 网关能力或获取 Traefik OSS 的商业支持。

基于 MIT 协议发布