iT邦幫忙

2022 iThome 鐵人賽

DAY 21
1
DevOps

從零開始的Kubernetes轉換系列 第 21

# Day 21 OpenTelemetry(一)

  • 分享至 

  • xImage
  •  

最近火紅的炸子雞 OpenTelemetry的問世,甚至連AWS都公開支持。今天我們就來看一下OpenTelemetry的部分吧。

首先呢,先上一張圖。如果到opentelemetry的官網看就會看到

目前OpenTelemetry想要涵蓋三大項 Trace(GA), Metrics(RC), Log(Beta)

但Log和metrics的目標還有段距離,但是trace已經support W3C的trace context。

以下我們就用python為例子看一下 opentelemetry的原理吧。

首先安裝

pip install opentelemetry-api
pip install opentelemetry-sdk

接下來我們拿官方的例子來看

# tracing.py
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
    ConsoleSpanExporter,
    SimpleExportSpanProcessor,
)

trace.set_tracer_provider(TracerProvider())
trace.get_tracer_provider().add_span_processor(
    SimpleExportSpanProcessor(ConsoleSpanExporter())
)

tracer = trace.get_tracer(__name__)

with tracer.start_as_current_span("foo"):
    with tracer.start_as_current_span("bar"):
        with tracer.start_as_current_span("baz"):
            print("Hello world from OpenTelemetry Python!")

會得出以下結果

$ python tracing_example.py
{
    "name": "baz",
    "context": {
        "trace_id": "0xb51058883c02f880111c959f3aa786a2",
        "span_id": "0xb2fa4c39f5f35e13",
        "trace_state": "{}"
    },
    "kind": "SpanKind.INTERNAL",
    "parent_id": "0x77e577e6a8813bf4",
    "start_time": "2020-05-07T14:39:52.906272Z",
    "end_time": "2020-05-07T14:39:52.906343Z",
    "status": {
        "status_code": "OK"
    },
    "attributes": {},
    "events": [],
    "links": []
}
{
    "name": "bar",
    "context": {
        "trace_id": "0xb51058883c02f880111c959f3aa786a2",
        "span_id": "0x77e577e6a8813bf4",
        "trace_state": "{}"
    },
    "kind": "SpanKind.INTERNAL",
    "parent_id": "0x3791d950cc5140c5",
    "start_time": "2020-05-07T14:39:52.906230Z",
    "end_time": "2020-05-07T14:39:52.906601Z",
    "status": {
        "status_code": "OK"
    },
    "attributes": {},
    "events": [],
    "links": []
}
{
    "name": "foo",
    "context": {
        "trace_id": "0xb51058883c02f880111c959f3aa786a2",
        "span_id": "0x3791d950cc5140c5",
        "trace_state": "{}"
    },
    "kind": "SpanKind.INTERNAL",
    "parent_id": null,
    "start_time": "2020-05-07T14:39:52.906157Z",
    "end_time": "2020-05-07T14:39:52.906743Z",
    "status": {
        "status_code": "OK"
    },
    "attributes": {},
    "events": [],
    "links": []
}

由上面我們可以看出同一個trace id有不同的span,並且會記錄時間和狀態
像是

    "start_time": "2020-05-07T14:39:52.906157Z",
    "end_time": "2020-05-07T14:39:52.906743Z",
    "status": {
        "status_code": "OK"
    },

並且有紀錄parent id 每一個parent id可以對應到span ID
這樣就可以追蹤在一個trace流裡面總共經過了哪些span。


上一篇
Day 20 Cilium service mesh(三)
下一篇
Day 22 OpenTelemetry(二)
系列文
從零開始的Kubernetes轉換30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言