Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(opentelemetry): replace plugin attribute with plugin metadata #11940

Merged
merged 16 commits into from
Jan 28, 2025
Merged
41 changes: 23 additions & 18 deletions apisix/plugins/opentelemetry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ local lrucache = core.lrucache.new({

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

local attr_schema = {
local metadata_schema = {
type = "object",
properties = {
trace_id_source = {
Expand Down Expand Up @@ -192,18 +192,26 @@ local _M = {
priority = 12009,
name = plugin_name,
schema = schema,
attr_schema = attr_schema,
metadata_schema = metadata_schema,
}


function _M.check_schema(conf)
function _M.check_schema(conf, schema_type)
if schema_type == core.schema.TYPE_METADATA then
local ok, err = core.schema.check(metadata_schema, conf)
if not ok then
return ok, err
end
local check = {"collector.address"}
core.utils.check_https(check, conf, plugin_name)
return true
end
return core.schema.check(schema, conf)
end


local hostname
local sampler_factory
local plugin_info

function _M.init()
if process.type() ~= "worker" then
Expand All @@ -217,27 +225,16 @@ function _M.init()
trace_id_ratio = trace_id_ratio_sampler_new,
}
hostname = core.utils.gethostname()
end

plugin_info = plugin.plugin_attr(plugin_name) or {}
local check = {"collector.address"}
core.utils.check_https(check, plugin_info, plugin_name)
local ok, err = core.schema.check(attr_schema, plugin_info)
if not ok then
core.log.error("failed to check the plugin_attr[", plugin_name, "]",
": ", err)
return
end

local function create_tracer_obj(conf, plugin_info)
if plugin_info.trace_id_source == "x-request-id" then
id_generator.new_ids = function()
local trace_id = core.request.headers()["x-request-id"] or ngx_var.request_id
return trace_id, id_generator.new_span_id()
end
end
end


local function create_tracer_obj(conf)
-- create exporter
local exporter = otlp_exporter_new(exporter_client_new(plugin_info.collector.address,
plugin_info.collector.request_timeout,
Expand Down Expand Up @@ -310,9 +307,17 @@ end


function _M.rewrite(conf, api_ctx)
local metadata = plugin.plugin_metadata(plugin_name)
if metadata == nil then
core.log.warn("plugin_metadata is required for opentelemetry plugin to working properly")
return
end
core.log.info("metadata: ", core.json.delay_encode(metadata))
local plugin_info = metadata.value
local vars = api_ctx.var

local tracer, err = core.lrucache.plugin_ctx(lrucache, api_ctx, nil, create_tracer_obj, conf)
local tracer, err = core.lrucache.plugin_ctx(lrucache, api_ctx, nil,
create_tracer_obj, conf, plugin_info)
if not tracer then
core.log.error("failed to fetch tracer object: ", err)
return
Expand Down
57 changes: 33 additions & 24 deletions docs/en/latest/plugins/opentelemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,45 @@ The `opentelemetry` Plugin can be used to report tracing data according to the [

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

## Static Configurations
## Configurations

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).

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

```yaml
plugin_attr:
opentelemetry:
trace_id_source: x-request-id # Specify the source of the trace ID, `x-request-id` or `random`. When set to `x-request-id`,
# the value of the `x-request-id` header will be used as the trace ID.
resource: # Additional resource to append to the trace.
service.name: APISIX # Set the Service name for OpenTelemetry traces.
collector:
address: 127.0.0.1:4318 # Set the address of the OpenTelemetry collector to send traces to.
request_timeout: 3 # Set the timeout for requests to the OpenTelemetry collector in seconds.
request_headers: # Set the headers to include in requests to the OpenTelemetry collector.
Authorization: token # Set the authorization header to include an access token.
batch_span_processor: # Trace span processor.
drop_on_queue_full: false # Drop spans when the export queue is full.
max_queue_size: 1024 # Set the maximum size of the span export queue.
batch_timeout: 2 # Set the timeout for span batches to wait in the export queue before
# being sent.
inactive_timeout: 1 # Set the timeout for spans to wait in the export queue before being sent,
# if the queue is not full.
max_export_batch_size: 16 # Set the maximum number of spans to include in each batch sent to the OpenTelemetry collector.
set_ngx_var: false # Export opentelemetry variables to nginx variables.
:::note
You can fetch the `admin_key` from `config.yaml` and save to an environment variable with the following command:

```bash
admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g')
```

Reload APISIX for changes to take effect.
:::

```shell
curl http://127.0.0.1:9180/apisix/admin/plugin_metadata/opentelemetry -H "X-API-KEY: $admin_key" -X PUT -d '
{
"trace_id_source": "x-request-id",
"resource": {
"service.name": "APISIX"
},
"collector": {
"address": "127.0.0.1:4318",
"request_timeout": 3,
"request_headers": {
"Authorization": "token"
},
"batch_span_processor": {
"drop_on_queue_full": false,
"max_queue_size": 1024,
"batch_timeout": 2,
"inactive_timeout": 1,
"max_export_batch_size": 16
},
"set_ngx_var": false
}
}'
```

## Attributes

Expand Down
56 changes: 33 additions & 23 deletions docs/zh/latest/plugins/opentelemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,45 @@ description: opentelemetry 插件可用于根据 OpenTelemetry 协议规范上

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

## 静态配置
## 配置

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

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

```yaml
plugin_attr:
opentelemetry:
trace_id_source: x-request-id # 指定追踪 ID 的来源,`x-request-id` 或 `random`。当设置为 `x-request-id` 时,
# `x-request-id` 头的值将用作追踪 ID。
resource: # 追加到追踪的额外资源。
service.name: APISIX # 为 OpenTelemetry 追踪设置服务名称。
collector:
address: 127.0.0.1:4318 # 设置要发送追踪的 OpenTelemetry 收集器的地址。
request_timeout: 3 # 设置请求 OpenTelemetry 收集器的超时时间(秒)。
request_headers: # 设置请求 OpenTelemetry 收集器时要包含的头信息。
Authorization: token # 设置授权头以包含访问令牌。
batch_span_processor: # 追踪跨度处理器。
drop_on_queue_full: false # 当导出队列满时丢弃跨度。
max_queue_size: 1024 # 设置跨度导出队列的最大大小。
batch_timeout: 2 # 设置跨度批次在导出队列中等待的超时时间,
# 然后发送。
inactive_timeout: 1 # 设置跨度在导出队列中等待的超时时间,如果队列不满,则发送。
max_export_batch_size: 16 # 设置每个批次发送到 OpenTelemetry 收集器的跨度的最大数量。
set_ngx_var: false # 将 opentelemetry 变量导出到 nginx 变量。
:::note
您可以从“config.yaml”获取“admin_key”,并使用以下命令保存到环境变量中:

```bash
admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g')
```

重新加载 APISIX 以使更改生效。
:::

```shell
curl http://127.0.0.1:9180/apisix/admin/plugin_metadata/opentelemetry -H "X-API-KEY: $admin_key" -X PUT -d '
{
"trace_id_source": "x-request-id",
"resource": {
"service.name": "APISIX"
},
"collector": {
"address": "127.0.0.1:4318",
"request_timeout": 3,
"request_headers": {
"Authorization": "token"
},
"batch_span_processor": {
"drop_on_queue_full": false,
"max_queue_size": 1024,
"batch_timeout": 2,
"inactive_timeout": 1,
"max_export_batch_size": 16
},
"set_ngx_var": false
}
}'
```

## 属性

Expand Down
Loading
Loading