全部产品
Search
文档中心

日志服务:Log转为Trace

更新时间:Jul 26, 2024

您在使用Logtail采集日志时,可以使用processor_otel_trace插件将采集到的日志转成SLS Trace。本文介绍processor_otel_trace插件的参数说明和配置示例。

功能入口

当您需要使用Logtail插件处理日志时,您可以在创建或修改Logtail采集配置时,添加插件配置。具体操作,请参见处理插件概述

配置说明

重要

Logtail 1.7.1及以上版本支持processor_otel_trace插件。

表单配置方式

配置处理器类型Log转Trace,相关参数说明如下表所示。

  • 参数说明

    参数

    说明

    SourceKey

    原始字段名。

    Format

    转换后的格式,可选值:

    • protobuf

    • json

    • protojson

    NoKeyError

    当日志中没有对应的原始字段时,是否报错。默认值为false。

    TraceIDNeedDecode

    是否对TraceID进行Base64解码,默认值为false。

    设置Format为protojson时,如果TraceID已进行过Base64编码,则需要设置TraceIDNeedDecode为true,否则将导致转换失败。

    SpanIDNeedDecode

    是否对SpanID进行Base64解码。默认值为false。

    设置Format为protojson时,如果SpanID已进行过Base64编码,则需要设置SpanIDNeedDecode为true,否则将导致转换失败。

    ParentSpanIDNeedDecode

    是否对ParentSpanID进行Base64解码。默认值为false。

    设置Format为protojson时,如果ParentSpanID已进行过Base64编码,则需要设置ParentSpanIDNeedDecode为true,否则将导致转换失败。

  • 配置示例

    • 原始日志

      INFO OtlpJsonLoggingSpanExporter - {"resource":{"attributes":[]},"scopeSpans":[{"scope":{"name":"io.opentelemetry.spring-webmvc-3.1","version":"1.26.0-alpha","attributes":[]},"spans":[{"traceId":"a7adbcb0921fa4a5b90615d0ad38ef95","spanId":"eecaa7e1b63c9f61","parentSpanId":"fa85fcee7fe0e0e8","name":"HelloWorldController.sayHello","kind":1,"startTimeUnixNano":"1696659118615748056","endTimeUnixNano":"1696659128701715998","attributes":[{"key":"thread.id","value":{"intValue":"48"}},{"key":"thread.name","value":{"stringValue":"http-nio-18080-exec-1"}}],"events":[],"links":[],"status":{}}]},{"scope":{"name":"io.opentelemetry.tomcat-7.0","version":"1.26.0-alpha","attributes":[]},"spans":[{"traceId":"a7adbcb0921fa4a5b90615d0ad38ef95","spanId":"fa85fcee7fe0e0e8","name":"GET /hello","kind":2,"startTimeUnixNano":"1696659118479360000","endTimeUnixNano":"1696659128703706165","attributes":[{"key":"http.status_code","value":{"intValue":"200"}},{"key":"user_agent.original","value":{"stringValue":"curl/7.84.0"}},{"key":"net.host.name","value":{"stringValue":"localhost"}},{"key":"http.response_content_length","value":{"intValue":"12"}},{"key":"http.target","value":{"stringValue":"/hello"}},{"key":"net.sock.peer.addr","value":{"stringValue":"127.0.0.1"}},{"key":"net.host.port","value":{"intValue":"18080"}},{"key":"net.sock.peer.port","value":{"intValue":"53649"}},{"key":"thread.id","value":{"intValue":"48"}},{"key":"thread.name","value":{"stringValue":"http-nio-18080-exec-1"}},{"key":"http.route","value":{"stringValue":"/hello"}},{"key":"net.sock.host.addr","value":{"stringValue":"127.0.0.1"}},{"key":"net.protocol.name","value":{"stringValue":"http"}},{"key":"net.protocol.version","value":{"stringValue":"1.1"}},{"key":"http.scheme","value":{"stringValue":"http"}},{"key":"http.method","value":{"stringValue":"GET"}}],"events":[],"links":[],"status":{}}]}],"schemaUrl":"https://opentelemetry.io/schemas/1.19.0"}
    • Logtail插件处理配置

      image

    • 处理结果

      image.png

JSON配置方式

配置typeprocessor_otel_tracedetail说明如下表所示。

  • 参数说明

    参数

    类型

    是否必选

    说明

    SourceKey

    String

    原始字段名。

    Format

    String

    转换后的格式,可选值:protobuf、json、protojson。

    NoKeyError

    Boolean

    当日志中没有对应的原始字段时,是否报错。默认值为false。

    TraceIDNeedDecode

    Boolean

    是否对TraceID进行Base64解码,默认值为false。

    设置Format为protojson时,如果TraceID已进行过Base64编码,则需要设置TraceIDNeedDecode为true,否则将导致转换失败。

    SpanIDNeedDecode

    Boolean

    是否对SpanID进行Base64解码。默认值为false。

    设置Format为protojson时,如果SpanID已进行过Base64编码,则需要设置SpanIDNeedDecode为true,否则将导致转换失败。

    ParentSpanIDNeedDecode

    Boolean

    是否对ParentSpanID进行Base64解码。默认值为false。

    设置Format为protojson时,如果ParentSpanID已进行过Base64编码,则需要设置ParentSpanIDNeedDecode为true,否则将导致转换失败。

  • 配置示例

    • 原始日志

      INFO OtlpJsonLoggingSpanExporter - {"resource":{"attributes":[]},"scopeSpans":[{"scope":{"name":"io.opentelemetry.spring-webmvc-3.1","version":"1.26.0-alpha","attributes":[]},"spans":[{"traceId":"a7adbcb0921fa4a5b90615d0ad38ef95","spanId":"eecaa7e1b63c9f61","parentSpanId":"fa85fcee7fe0e0e8","name":"HelloWorldController.sayHello","kind":1,"startTimeUnixNano":"1696659118615748056","endTimeUnixNano":"1696659128701715998","attributes":[{"key":"thread.id","value":{"intValue":"48"}},{"key":"thread.name","value":{"stringValue":"http-nio-18080-exec-1"}}],"events":[],"links":[],"status":{}}]},{"scope":{"name":"io.opentelemetry.tomcat-7.0","version":"1.26.0-alpha","attributes":[]},"spans":[{"traceId":"a7adbcb0921fa4a5b90615d0ad38ef95","spanId":"fa85fcee7fe0e0e8","name":"GET /hello","kind":2,"startTimeUnixNano":"1696659118479360000","endTimeUnixNano":"1696659128703706165","attributes":[{"key":"http.status_code","value":{"intValue":"200"}},{"key":"user_agent.original","value":{"stringValue":"curl/7.84.0"}},{"key":"net.host.name","value":{"stringValue":"localhost"}},{"key":"http.response_content_length","value":{"intValue":"12"}},{"key":"http.target","value":{"stringValue":"/hello"}},{"key":"net.sock.peer.addr","value":{"stringValue":"127.0.0.1"}},{"key":"net.host.port","value":{"intValue":"18080"}},{"key":"net.sock.peer.port","value":{"intValue":"53649"}},{"key":"thread.id","value":{"intValue":"48"}},{"key":"thread.name","value":{"stringValue":"http-nio-18080-exec-1"}},{"key":"http.route","value":{"stringValue":"/hello"}},{"key":"net.sock.host.addr","value":{"stringValue":"127.0.0.1"}},{"key":"net.protocol.name","value":{"stringValue":"http"}},{"key":"net.protocol.version","value":{"stringValue":"1.1"}},{"key":"http.scheme","value":{"stringValue":"http"}},{"key":"http.method","value":{"stringValue":"GET"}}],"events":[],"links":[],"status":{}}]}],"schemaUrl":"https://opentelemetry.io/schemas/1.19.0"}
    • Logtail插件处理配置

      {
        "processors": [
          {
            "type": "processor_split_log_regex",
            "detail": {
              "PreserveOthers": true,
              "SplitKey": "content",
              "SplitRegex": "INFO\\sOtlpJsonLoggingSpanExporter\\s.+"
            }
          },
          {
            "type": "processor_regex",
            "detail": {
              "SourceKey": "content",
              "Regex": "INFO\\sOtlpJsonLoggingSpanExporter\\s-(\\s.+)",
              "Keys": [
                "trace_data"
              ]
            }
          },
          {
            "detail": {
              "SourceKey": "trace_data",
              "Format": "protojson",
              "NoKeyError": true
            },
            "type": "processor_otel_trace"
          }
        ]
      }
    • 处理结果

      image.png