菜单
文档breadcrumb arrow Grafana Alloybreadcrumb arrow 参考breadcrumb arrow 组件breadcrumb arrow otelcolbreadcrumb arrow otelcol.connector.spanlogs
开源

otelcol.connector.spanlogs

otelcol.connector.spanlogs 接收来自其他 otelcol 组件的链路追踪遥测数据,并为每个 span、root 或 process 输出日志遥测数据。这使您能够自动构建链路追踪发现机制。

注意

otelcol.connector.spanlogs 是一个自定义组件,与 OpenTelemetry Collector 中的任何组件无关。它基于 Grafana Agent Static 的 traces 子系统中的 automatic_logging 组件。

您可以通过为 otelcol.connector.spanlogs 组件指定不同的标签来定义多个此类组件。

用法

alloy
otelcol.connector.spanlogs "LABEL" {
  output {
    logs    = [...]
  }
}

参数

otelcol.connector.spanlogs 支持以下参数

名称类型描述默认值必需
spansbool为每个 span 记录一行日志。false
rootsbool为链路追踪中的每个根 span 记录一行日志。false
processesbool为每个 process 记录一行日志。false
eventsbool为每个 span 事件记录一行日志。false
span_attributeslist(string)需要记录的额外 span 属性。[]
process_attributeslist(string)需要记录的额外 process 属性。[]
event_attributeslist(string)需要记录的额外事件属性。[]
labelslist(string)将被记录为标签的键列表。[]

labels 中列出的值应为 span、process 或事件属性的值。

警告

spansevents 设置为 true 可能会导致日志量过大。

otelcol.connector.spanlogs 定义中支持以下块

层级描述必需
overridesoverrides日志主体中键的覆盖配置。
outputoutput配置将接收到的遥测数据发送到何处。

overrides 块

overrides 块配置将记录在日志行主体中的键的覆盖。

支持以下属性

名称类型描述默认值必需
logs_instance_tagstring指示日志行是针对 span、root 还是 process。traces
service_keystring资源的 service name 的日志键。svc
span_name_keystringspan 名称的日志键。span
status_keystringspan 状态的日志键。status
duration_keystringspan 持续时间的日志键。dur
trace_id_keystringSpan 的 Trace ID 的日志键。tid

输出块

output 块配置一组组件,用于转发生成的遥测数据。

支持以下参数

名称类型描述默认值必需
logslist(otelcol.Consumer)要将日志发送到的消费者列表。[]

您必须指定 output 块,但其所有参数都是可选的。默认情况下,遥测数据会被丢弃。相应地配置 logs 参数以将遥测数据发送到其他组件。

导出的字段

以下字段已导出,可供其他组件引用

名称类型描述
inputotelcol.Consumer其他组件可用于发送遥测数据的值。

input 接受任何遥测信号(指标、日志或 traces)的 otelcol.Consumer 数据。

组件健康状况

仅当配置无效时,otelcol.connector.spanlogs 才会报告为不健康。

调试信息

otelcol.connector.spanlogs 不暴露任何组件特定的调试信息。

示例

以下配置将从 span 生成的日志发送到 Loki。

此外,otelcol.processor.attributes 配置了一个“提示”,以便 otelcol.exporter.loki 将 span 的“attribute1”属性提升为 Loki 标签。

alloy
otelcol.receiver.otlp "default" {
  grpc {}

  output {
    traces = [otelcol.connector.spanlogs.default.input]
  }
}

otelcol.connector.spanlogs "default" {
  spans              = true
  roots              = true
  processes          = true
  events             = true
  labels             = ["attribute1", "res_attribute1"]
  span_attributes    = ["attribute1"]
  process_attributes = ["res_attribute1"]
  event_attributes   = ["log.severity", "log.message"]

  output {
    logs = [otelcol.processor.attributes.default.input]
  }
}

otelcol.processor.attributes "default" {
  action {
    key = "loki.attribute.labels"
    action = "insert"
    value = "attribute1"
  }

  output {
    logs = [otelcol.exporter.loki.default.input]
  }
}

otelcol.exporter.loki "default" {
  forward_to = [loki.write.local.receiver]
}

loki.write "local" {
  endpoint {
    url = "loki:3100"
  }
}

对于这样的输入跟踪…

json
{
  "resourceSpans": [
    {
      "resource": {
        "attributes": [
          {
            "key": "service.name",
            "value": { "stringValue": "TestSvcName" }
          },
          {
            "key": "res_attribute1",
            "value": { "intValue": "78" }
          },
          {
            "key": "unused_res_attribute1",
            "value": { "stringValue": "str" }
          },
          {
            "key": "res_account_id",
            "value": { "intValue": "2245" }
          }
        ]
      },
      "scopeSpans": [
        {
          "spans": [
            {
              "trace_id": "7bba9f33312b3dbb8b2c2c62bb7abe2d",
              "span_id": "086e83747d0e381e",
              "name": "TestSpan",
              "attributes": [
                {
                  "key": "attribute1",
                  "value": { "intValue": "78" }
                },
                {
                  "key": "unused_attribute1",
                  "value": { "intValue": "78" }
                },
                {
                  "key": "account_id",
                  "value": { "intValue": "2245" }
                }
              ],
              "events": [
                {
                  "name": "log",
                  "attributes": [
                    {
                      "key": "log.severity",
                      "value": { "stringValue": "INFO" }
                    },
                    {
                      "key": "log.message",
                      "value": { "stringValue": "TestLogMessage" }
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

… 从 otelcol.connector.spanlogs 输出的日志将如下所示

json
{
  "resourceLogs": [
    {
      "scopeLogs": [
        {
          "log_records": [
            {
              "body": {
                "stringValue": "span=TestSpan dur=0ns attribute1=78 svc=TestSvcName res_attribute1=78 tid=7bba9f33312b3dbb8b2c2c62bb7abe2d"
              },
              "attributes": [
                {
                  "key": "traces",
                  "value": { "stringValue": "span" }
                },
                {
                  "key": "attribute1",
                  "value": { "intValue": "78" }
                },
                {
                  "key": "res_attribute1",
                  "value": { "intValue": "78" }
                }
              ]
            },
            {
              "body": {
                "stringValue": "span=TestSpan dur=0ns attribute1=78 svc=TestSvcName res_attribute1=78 tid=7bba9f33312b3dbb8b2c2c62bb7abe2d"
              },
              "attributes": [
                {
                  "key": "traces",
                  "value": { "stringValue": "root" }
                },
                {
                  "key": "attribute1",
                  "value": { "intValue": "78" }
                },
                {
                  "key": "res_attribute1",
                  "value": { "intValue": "78" }
                }
              ]
            },
            {
              "body": {
                "stringValue": "svc=TestSvcName res_attribute1=78 tid=7bba9f33312b3dbb8b2c2c62bb7abe2d"
              },
              "attributes": [
                {
                  "key": "traces",
                  "value": { "stringValue": "process" }
                },
                {
                  "key": "res_attribute1",
                  "value": { "intValue": "78" }
                }
              ]
            },
            {
              "body": { "stringValue": "span=TestSpan dur=0ns attribute1=78 svc=TestSvcName res_attribute1=78 tid=7bba9f33312b3dbb8b2c2c62bb7abe2d log.severity=INFO log.message=TestLogMessage" },
              "attributes": [
                {
                  "key": "traces",
                  "value": { "stringValue": "event" }
                },
                {
                  "key": "attribute1",
                  "value": { "intValue": "78" }
                },
                {
                  "key": "res_attribute1",
                  "value": { "intValue": "78" }
                },
                {
                  "key": "log.severity",
                  "value": { "stringValue": "INFO" }
                },
                {
                  "key": "log.message",
                  "value": { "stringValue": "TestLogMessage" }
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

兼容组件

otelcol.connector.spanlogs 可以接受来自以下组件的参数

otelcol.connector.spanlogs 有可供以下组件消费的导出

注意

连接某些组件可能不合理,或者组件可能需要进一步配置才能使连接正常工作。有关更多详细信息,请参阅链接的文档。