全部產品
Search
文件中心

Simple Log Service:Log轉為Trace

更新時間:Jul 27, 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