簡單理解OpenTracing技術中的span概念

xing393939發表於2021-12-10

簡單理解

spanType總共有三種:

  1. Entry:表示整個程式的span
  2. Local:表示程式內部的處理,是EntrySpan的一部分
  3. Exit:表示發起一個遠端呼叫,如請求db、redis,如發起http請求、rpc呼叫

spanLayer總共有六種:

  1. Unknown:未知
  2. Database:請求db
  3. RPCFramework:rpc呼叫
  4. Http:發起http請求(或者本身是http服務)
  5. MQ:訊息服務
  6. Cache:快取服務

場景一:一個http請求,處理上傳和請求db,則建立三個span
spanId=0 parentSpanId=-1 spanType=entry spanLayer=http
spanId=1 parentSpanId= 0 spanType=local spanLayer=Unknown
spanId=2 parentSpanId= 0 spanType=exit spanLayer=database

場景二:一個http請求,有http請求,則建立兩個個span
spanId=0 parentSpanId=-1 spanType=entry spanLayer=http
spanId=1 parentSpanId= 0 spanType=exit spanLayer=http

場景三:一個http請求,有訪問redis。場景二的第2個span正是訪問的它。建立兩個span(其中traceId和場景二的traceId相等)
spanId=0 parentSpanId=-1 spanType=entry spanLayer=http refs=包含場景二中第2個span的資訊
spanId=1 parentSpanId= 0 spanType=exit spanLayer=cache

原始日誌

這裡記錄一下真實的trace日誌,來證實上面說的場景二和場景三

場景二

{
    "traceId": "6763beaf2ad8a9e4c05114b978a97072.17.756430259",
    "traceSegmentId": "6763beaf2ad8a9e4c05114b978a97072.17.756430259",
    "spans": [
        {
            "spanId": 0,
            "parentSpanId": -1,
            "startTime": "1639096358706",
            "endTime": "1639096359252",
            "refs": [],
            "operationName": "/answer4/activityStyle",
            "peer": "192.168.2.119:72",
            "spanType": "Entry",
            "spanLayer": "Http",
            "componentId": 8001,
            "isError": false,
            "tags": [
                {
                    "key": "url",
                    "value": "/answer4/activityStyle"
                },
                {
                    "key": "status_code",
                    "value": "200"
                }
            ],
            "logs": [],
            "skipAnalysis": false
        },
        {
            "spanId": 1,
            "parentSpanId": 0,
            "startTime": "1639096358775",
            "endTime": "1639096359250",
            "refs": [],
            "operationName": "/op/answer4/activityStyle",
            "peer": "container-ng9272:62",
            "spanType": "Exit",
            "spanLayer": "Http",
            "componentId": 8002,
            "isError": false,
            "tags": [
                {
                    "key": "url",
                    "value": "http://container-ng9272:62/op/answer4/activityStyle"
                },
                {
                    "key": "status_code",
                    "value": "200"
                }
            ],
            "logs": [],
            "skipAnalysis": false
        }
    ],
    "service": "op",
    "serviceInstance": "ca6185b0-752c-9aab-d9f9-6796da2c0646@172.18.0.5",
    "isSizeLimited": false
}

場景三

{
    "traceId": "6763beaf2ad8a9e4c05114b978a97072.17.756430259",
    "traceSegmentId": "6763beaf2ad8a9e4c05114b978a97072.18.4224945771",
    "spans": [
        {
            "spanId": 0,
            "parentSpanId": -1,
            "startTime": "1639096358782",
            "endTime": "1639096359250",
            "refs": [
                {
                    "refType": "CrossProcess",
                    "traceId": "6763beaf2ad8a9e4c05114b978a97072.17.756430259",
                    "parentTraceSegmentId": "6763beaf2ad8a9e4c05114b978a97072.17.756430259",
                    "parentSpanId": 1,
                    "parentService": "op",
                    "parentServiceInstance": "ca6185b0-752c-9aab-d9f9-6796da2c0646@172.18.0.5",
                    "parentEndpoint": "/answer4/activityStyle",
                    "networkAddressUsedAtPeer": "container-ng9272:62"
                }
            ],
            "operationName": "/op/answer4/activityStyle",
            "peer": "container-ng9272:62",
            "spanType": "Entry",
            "spanLayer": "Http",
            "componentId": 8001,
            "isError": false,
            "tags": [
                {
                    "key": "url",
                    "value": "/op/answer4/activityStyle"
                },
                {
                    "key": "status_code",
                    "value": "200"
                }
            ],
            "logs": [],
            "skipAnalysis": false
        },
        {
            "spanId": 1,
            "parentSpanId": 0,
            "startTime": "1639096358942",
            "endTime": "1639096358966",
            "refs": [],
            "operationName": "Redis->get",
            "peer": "localhost:6379",
            "spanType": "Exit",
            "spanLayer": "Cache",
            "componentId": 7,
            "isError": false,
            "tags": [
                {
                    "key": "db.type",
                    "value": "redis"
                },
                {
                    "key": "redis.command",
                    "value": "GET answer4:opdb"
                }
            ],
            "logs": [],
            "skipAnalysis": false
        }
    ],
    "service": "op",
    "serviceInstance": "ca6185b0-752c-9aab-d9f9-6796da2c0646@172.18.0.5",
    "isSizeLimited": false
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章