From fc7e9f916413a9a6a86a97047dd250c87afcc1c8 Mon Sep 17 00:00:00 2001 From: roketyyang Date: Sun, 16 Jan 2022 23:33:04 +0800 Subject: [PATCH] feature: add opentelemetry plugin. (#3891) --- apisix/plugins/opentelemetry.lua | 8 +++- docs/en/latest/plugins/opentelemetry.md | 6 +-- docs/zh/latest/plugins/opentelemetry.md | 6 +-- t/plugin/opentelemetry.t | 60 +++++-------------------- 4 files changed, 25 insertions(+), 55 deletions(-) diff --git a/apisix/plugins/opentelemetry.lua b/apisix/plugins/opentelemetry.lua index 688ab7abe01c..9d3f7971b81c 100644 --- a/apisix/plugins/opentelemetry.lua +++ b/apisix/plugins/opentelemetry.lua @@ -41,8 +41,14 @@ local context = require("opentelemetry.context").new(context_storage) local carrier_new = require("opentelemetry.trace.propagation.carrier").new local trace_context = require("opentelemetry.trace.propagation.trace_context") +local ngx = ngx local ngx_var = ngx.var local ngx_req = ngx.req +local table = table +local type = type +local pairs = pairs +local ipairs = ipairs +local unpack = unpack local hostname @@ -310,7 +316,7 @@ function _M.body_filter(conf, ctx) local span = context:current():span() if upstream_status and upstream_status >= 500 then span:set_status(span_status.error, - "upstream response status: " .. tostring(upstream_status)) + "upstream response status: " .. upstream_status) end span:finish() diff --git a/docs/en/latest/plugins/opentelemetry.md b/docs/en/latest/plugins/opentelemetry.md index d92970766c10..721c5d4852bd 100644 --- a/docs/en/latest/plugins/opentelemetry.md +++ b/docs/en/latest/plugins/opentelemetry.md @@ -31,9 +31,9 @@ title: opentelemetry ## Name -opentelemetry report Tracing data according to [opentelemetry specification](https://github.com/open-telemetry/opentelemetry-specification). +[OpenTelemetry](https://opentelemetry.io/) report Tracing data according to [opentelemetry specification](https://github.com/open-telemetry/opentelemetry-specification). -Just support reporting in HTTP with Content-Type=application/x-protobuf, the specification: [OTLP/HTTP Request](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/otlp.md#otlphttp-request)。 +Just support reporting in `HTTP` with `Content-Type=application/x-protobuf`, the specification: [OTLP/HTTP Request](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/otlp.md#otlphttp-request)。 ## Attributes @@ -55,7 +55,7 @@ Just support reporting in HTTP with Content-Type=application/x-protobuf, the spe First of all, enable the opentelemetry plugin in the `config.yaml`: -``` +```yaml # Add this in config.yaml plugins: - ... # plugin you need diff --git a/docs/zh/latest/plugins/opentelemetry.md b/docs/zh/latest/plugins/opentelemetry.md index 29feabb988b2..93a3753973f2 100644 --- a/docs/zh/latest/plugins/opentelemetry.md +++ b/docs/zh/latest/plugins/opentelemetry.md @@ -31,9 +31,9 @@ title: opentelemetry ## 名字 -opentelemetry 提供符合 [opentelemetry specification](https://github.com/open-telemetry/opentelemetry-specification) 协议规范的 Tracing 数据上报。 +[OpenTelemetry](https://opentelemetry.io/) 提供符合 [opentelemetry specification](https://github.com/open-telemetry/opentelemetry-specification) 协议规范的 Tracing 数据上报。 -只支持 HTTP 协议 application/x-protobuf 类型的数据上报,相关协议标准:[OTLP/HTTP Request](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/otlp.md#otlphttp-request)。 +只支持 `HTTP` 协议,且请求类型为 `application/x-protobuf` 的数据上报,相关协议标准:[OTLP/HTTP Request](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/otlp.md#otlphttp-request)。 ## 属性 @@ -55,7 +55,7 @@ opentelemetry 提供符合 [opentelemetry specification](https://github.com/open 首先,你需要在 `config.yaml` 里面启用 opentelemetry 插件: -``` +```yaml # 加到 config.yaml plugins: - ... # plugin you need diff --git a/t/plugin/opentelemetry.t b/t/plugin/opentelemetry.t index 27c88c149fdf..99d078e85a76 100644 --- a/t/plugin/opentelemetry.t +++ b/t/plugin/opentelemetry.t @@ -42,6 +42,18 @@ _EOC_ $block->set_value("extra_init_by_lua", $extra_init_by_lua); + if (!$block->request) { + $block->set_value("request", "GET /t"); + } + + if (!$block->response_body) { + $block->set_value("response_body", "passed\n"); + } + + if (!$block->no_error_log && !$block->error_log) { + $block->set_value("no_error_log", "[error]"); + } + $block; }); @@ -107,12 +119,6 @@ __DATA__ ngx.say(body) } } ---- request -GET /t ---- response_body -passed ---- no_error_log -[error] @@ -178,12 +184,6 @@ opentelemetry export span ngx.say(body) } } ---- request -GET /t ---- response_body -passed ---- no_error_log -[error] @@ -253,12 +253,6 @@ qr/opentelemetry export span/ ngx.say(body) } } ---- request -GET /t ---- response_body -passed ---- no_error_log -[error] @@ -334,12 +328,6 @@ qr/opentelemetry export span/ ngx.say(body) } } ---- request -GET /t ---- response_body -passed ---- no_error_log -[error] @@ -411,12 +399,6 @@ opentelemetry export span ngx.say(body) } } ---- request -GET /t ---- response_body -passed ---- no_error_log -[error] @@ -496,12 +478,6 @@ qr/opentelemetry export span/ ngx.say(body) } } ---- request -GET /t ---- response_body -passed ---- no_error_log -[error] @@ -583,12 +559,6 @@ opentelemetry export span ngx.say(body) } } ---- request -GET /t ---- response_body -passed ---- no_error_log -[error] @@ -691,12 +661,6 @@ opentelemetry export span ngx.say(body) } } ---- request -GET /t ---- response_body -passed ---- no_error_log -[error]