Skip to content

Commit 5a085bc

Browse files
authored
refactor(opentelemetry): replace plugin attribute with plugin metadata (#11940)
1 parent c2324a5 commit 5a085bc

File tree

9 files changed

+343
-170
lines changed

9 files changed

+343
-170
lines changed

apisix/plugins/opentelemetry.lua

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ local lrucache = core.lrucache.new({
5555

5656
local asterisk = string.byte("*", 1)
5757

58-
local attr_schema = {
58+
local metadata_schema = {
5959
type = "object",
6060
properties = {
6161
trace_id_source = {
@@ -192,18 +192,26 @@ local _M = {
192192
priority = 12009,
193193
name = plugin_name,
194194
schema = schema,
195-
attr_schema = attr_schema,
195+
metadata_schema = metadata_schema,
196196
}
197197

198198

199-
function _M.check_schema(conf)
199+
function _M.check_schema(conf, schema_type)
200+
if schema_type == core.schema.TYPE_METADATA then
201+
local ok, err = core.schema.check(metadata_schema, conf)
202+
if not ok then
203+
return ok, err
204+
end
205+
local check = {"collector.address"}
206+
core.utils.check_https(check, conf, plugin_name)
207+
return true
208+
end
200209
return core.schema.check(schema, conf)
201210
end
202211

203212

204213
local hostname
205214
local sampler_factory
206-
local plugin_info
207215

208216
function _M.init()
209217
if process.type() ~= "worker" then
@@ -217,27 +225,16 @@ function _M.init()
217225
trace_id_ratio = trace_id_ratio_sampler_new,
218226
}
219227
hostname = core.utils.gethostname()
228+
end
220229

221-
plugin_info = plugin.plugin_attr(plugin_name) or {}
222-
local check = {"collector.address"}
223-
core.utils.check_https(check, plugin_info, plugin_name)
224-
local ok, err = core.schema.check(attr_schema, plugin_info)
225-
if not ok then
226-
core.log.error("failed to check the plugin_attr[", plugin_name, "]",
227-
": ", err)
228-
return
229-
end
230230

231+
local function create_tracer_obj(conf, plugin_info)
231232
if plugin_info.trace_id_source == "x-request-id" then
232233
id_generator.new_ids = function()
233234
local trace_id = core.request.headers()["x-request-id"] or ngx_var.request_id
234235
return trace_id, id_generator.new_span_id()
235236
end
236237
end
237-
end
238-
239-
240-
local function create_tracer_obj(conf)
241238
-- create exporter
242239
local exporter = otlp_exporter_new(exporter_client_new(plugin_info.collector.address,
243240
plugin_info.collector.request_timeout,
@@ -310,9 +307,17 @@ end
310307

311308

312309
function _M.rewrite(conf, api_ctx)
310+
local metadata = plugin.plugin_metadata(plugin_name)
311+
if metadata == nil then
312+
core.log.warn("plugin_metadata is required for opentelemetry plugin to working properly")
313+
return
314+
end
315+
core.log.info("metadata: ", core.json.delay_encode(metadata))
316+
local plugin_info = metadata.value
313317
local vars = api_ctx.var
314318

315-
local tracer, err = core.lrucache.plugin_ctx(lrucache, api_ctx, nil, create_tracer_obj, conf)
319+
local tracer, err = core.lrucache.plugin_ctx(lrucache, api_ctx, nil,
320+
create_tracer_obj, conf, plugin_info)
316321
if not tracer then
317322
core.log.error("failed to fetch tracer object: ", err)
318323
return

docs/en/latest/plugins/opentelemetry.md

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,36 +36,45 @@ The `opentelemetry` Plugin can be used to report tracing data according to the [
3636

3737
The Plugin only supports binary-encoded [OLTP over HTTP](https://opentelemetry.io/docs/reference/specification/protocol/otlp/#otlphttp).
3838

39-
## Static Configurations
39+
## Configurations
4040

4141
By default, configurations of the Service name, tenant ID, collector, and batch span processor are pre-configured in [default configuration](https://github.com/apache/apisix/blob/master/apisix/cli/config.lua).
4242

43-
To customize these values, add the corresponding configurations to `config.yaml`. For example:
43+
You can change this configuration of the Plugin through the endpoint `apisix/admin/plugin_metadata/opentelemetry` For example:
4444

45-
```yaml
46-
plugin_attr:
47-
opentelemetry:
48-
trace_id_source: x-request-id # Specify the source of the trace ID, `x-request-id` or `random`. When set to `x-request-id`,
49-
# the value of the `x-request-id` header will be used as the trace ID.
50-
resource: # Additional resource to append to the trace.
51-
service.name: APISIX # Set the Service name for OpenTelemetry traces.
52-
collector:
53-
address: 127.0.0.1:4318 # Set the address of the OpenTelemetry collector to send traces to.
54-
request_timeout: 3 # Set the timeout for requests to the OpenTelemetry collector in seconds.
55-
request_headers: # Set the headers to include in requests to the OpenTelemetry collector.
56-
Authorization: token # Set the authorization header to include an access token.
57-
batch_span_processor: # Trace span processor.
58-
drop_on_queue_full: false # Drop spans when the export queue is full.
59-
max_queue_size: 1024 # Set the maximum size of the span export queue.
60-
batch_timeout: 2 # Set the timeout for span batches to wait in the export queue before
61-
# being sent.
62-
inactive_timeout: 1 # Set the timeout for spans to wait in the export queue before being sent,
63-
# if the queue is not full.
64-
max_export_batch_size: 16 # Set the maximum number of spans to include in each batch sent to the OpenTelemetry collector.
65-
set_ngx_var: false # Export opentelemetry variables to nginx variables.
45+
:::note
46+
You can fetch the `admin_key` from `config.yaml` and save to an environment variable with the following command:
47+
48+
```bash
49+
admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g')
6650
```
6751

68-
Reload APISIX for changes to take effect.
52+
:::
53+
54+
```shell
55+
curl http://127.0.0.1:9180/apisix/admin/plugin_metadata/opentelemetry -H "X-API-KEY: $admin_key" -X PUT -d '
56+
{
57+
"trace_id_source": "x-request-id",
58+
"resource": {
59+
"service.name": "APISIX"
60+
},
61+
"collector": {
62+
"address": "127.0.0.1:4318",
63+
"request_timeout": 3,
64+
"request_headers": {
65+
"Authorization": "token"
66+
},
67+
"batch_span_processor": {
68+
"drop_on_queue_full": false,
69+
"max_queue_size": 1024,
70+
"batch_timeout": 2,
71+
"inactive_timeout": 1,
72+
"max_export_batch_size": 16
73+
},
74+
"set_ngx_var": false
75+
}
76+
}'
77+
```
6978

7079
## Attributes
7180

docs/zh/latest/plugins/opentelemetry.md

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,35 +35,45 @@ description: opentelemetry 插件可用于根据 OpenTelemetry 协议规范上
3535

3636
`opentelemetry` 插件可用于根据 [OpenTelemetry Specification](https://opentelemetry.io/docs/reference/specification/) 协议规范上报 Traces 数据。该插件仅支持二进制编码的 OLTP over HTTP,即请求类型为 `application/x-protobuf` 的数据上报。
3737

38-
## 静态配置
38+
## 配置
3939

4040
默认情况下,服务名称、租户 ID、collector 和 batch span processor 的配置已预配置在[默认配置](https://github.com/apache/apisix/blob/master/apisix/cli/config.lua)中。
4141

42-
要自定义这些值,请将相应的配置添加到 `config.yaml` 中。例如:
42+
您可以通过端点 `apisix/admin/plugin_metadata/opentelemetry` 更改插件的配置,例如:
4343

44-
```yaml
45-
plugin_attr:
46-
opentelemetry:
47-
trace_id_source: x-request-id # 指定追踪 ID 的来源,`x-request-id` 或 `random`。当设置为 `x-request-id` 时,
48-
# `x-request-id` 头的值将用作追踪 ID。
49-
resource: # 追加到追踪的额外资源。
50-
service.name: APISIX # 为 OpenTelemetry 追踪设置服务名称。
51-
collector:
52-
address: 127.0.0.1:4318 # 设置要发送追踪的 OpenTelemetry 收集器的地址。
53-
request_timeout: 3 # 设置请求 OpenTelemetry 收集器的超时时间(秒)。
54-
request_headers: # 设置请求 OpenTelemetry 收集器时要包含的头信息。
55-
Authorization: token # 设置授权头以包含访问令牌。
56-
batch_span_processor: # 追踪跨度处理器。
57-
drop_on_queue_full: false # 当导出队列满时丢弃跨度。
58-
max_queue_size: 1024 # 设置跨度导出队列的最大大小。
59-
batch_timeout: 2 # 设置跨度批次在导出队列中等待的超时时间,
60-
# 然后发送。
61-
inactive_timeout: 1 # 设置跨度在导出队列中等待的超时时间,如果队列不满,则发送。
62-
max_export_batch_size: 16 # 设置每个批次发送到 OpenTelemetry 收集器的跨度的最大数量。
63-
set_ngx_var: false # 将 opentelemetry 变量导出到 nginx 变量。
44+
:::note
45+
您可以从“config.yaml”获取“admin_key”,并使用以下命令保存到环境变量中:
46+
47+
```bash
48+
admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g')
6449
```
6550

66-
重新加载 APISIX 以使更改生效。
51+
:::
52+
53+
```shell
54+
curl http://127.0.0.1:9180/apisix/admin/plugin_metadata/opentelemetry -H "X-API-KEY: $admin_key" -X PUT -d '
55+
{
56+
"trace_id_source": "x-request-id",
57+
"resource": {
58+
"service.name": "APISIX"
59+
},
60+
"collector": {
61+
"address": "127.0.0.1:4318",
62+
"request_timeout": 3,
63+
"request_headers": {
64+
"Authorization": "token"
65+
},
66+
"batch_span_processor": {
67+
"drop_on_queue_full": false,
68+
"max_queue_size": 1024,
69+
"batch_timeout": 2,
70+
"inactive_timeout": 1,
71+
"max_export_batch_size": 16
72+
},
73+
"set_ngx_var": false
74+
}
75+
}'
76+
```
6777

6878
## 属性
6979

0 commit comments

Comments
 (0)