Skip to content

Commit

Permalink
feature: add opentelemetry plugin. (apache#3891)
Browse files Browse the repository at this point in the history
  • Loading branch information
roketyyang committed Jan 18, 2022
1 parent 9069345 commit 84084d3
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 214 deletions.
26 changes: 22 additions & 4 deletions apisix/plugins/opentelemetry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ local attr_schema = {
trace_id_source = {
type = "string",
enum = {"x-request-id", "random"},
description = "alternate use x-request-id as trace id",
description = "the source of trace id",
default = "random",
},
resource = {
Expand Down Expand Up @@ -307,11 +307,14 @@ function _M.access(conf, api_ctx)
end


function _M.body_filter(conf, ctx)
function _M.body_filter(conf, api_ctx)
if ngx.arg[2] then
local upstream_status = core.response.get_upstream_status(ctx)
local upstream_status = core.response.get_upstream_status(api_ctx)
local ctx = context:current();
ctx:detach()

-- get span from current context
local span = context:current():span()
local span = ctx:span()
if upstream_status and upstream_status >= 500 then
span:set_status(span_status.error,
"upstream response status: " .. upstream_status)
Expand All @@ -322,4 +325,19 @@ function _M.body_filter(conf, ctx)
end


function _M.log(conf, api_ctx)
local ctx = context:current();
if ctx then
-- get span from current context
local span = ctx:span()
if upstream_status and upstream_status >= 500 then
span:set_status(span_status.error,
"upstream response status: " .. upstream_status)
end

span:finish()
end
end


return _M
6 changes: 3 additions & 3 deletions docs/en/latest/plugins/opentelemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ Just support reporting in `HTTP` with `Content-Type=application/x-protobuf`, the
| sampler.options.root.name | string | optional | always_off | ["always_on", "always_off", "trace_id_ratio"] | sampling strategy
| sampler.options.root.options | object | optional | {fraction = 0} | | sampling strategy parameters
| sampler.options.root.options.fraction | number | optional | 0 | [0, 1] | trace_id_ratio fraction
| additional_attributes | array[string] | optional | | | append to trace span attributes
| additional_attributes[0] | string | required | | | key of ctx.var
| additional_attributes | array[string] | optional | | | attributes (variable and its value) which will be appended to the trace span
| additional_attributes[0] | string | required | | | APISIX or Nginx variable, like `http_header` or `route_id`

## How To Enable

Expand Down Expand Up @@ -94,7 +94,7 @@ We can set the collecting by specifying the configuration in `conf/config.yaml`.

| Name | Type | Default | Description |
| ------------ | ------ | -------- | ----------------------------------------------------- |
| trace_id_source | enum | random | alternate use x-request-id as trace id, valid value is `random` or `x-request-id`, if use `x-request-id`, please make sure it match regex pattern `[0-9a-f]{32}` |
| trace_id_source | enum | random | the source of trace id, the valid value is `random` or `x-request-id`. If `x-request-id` is set, the value of `x-request-id` request header will be used as trace id. Please make sure it match regex pattern `[0-9a-f]{32}` |
| resource | object | | additional [resource](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/sdk.md) append to trace |
| collector | object | {address = "127.0.0.1:4317", request_timeout = 3} | otlp collector |
| collector.address | string | 127.0.0.1:4317 | collector address |
Expand Down
5 changes: 2 additions & 3 deletions docs/zh/latest/plugins/opentelemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ title: opentelemetry
| sampler.options.root.name | string | 可选 | always_off | ["always_on", "always_off", "trace_id_ratio"] | 采样算法
| sampler.options.root.options | object | 可选 | {fraction = 0} | | 采样算法参数
| sampler.options.root.options.fraction | number | 可选 | 0 | [0, 1] | trace_id_ratio 采样算法的百分比
| tags | array[object] | 可选 | | | 追加到 trace span 的属性
| tags.position | string | 必须 | | ["http", "arg", "cookie"] | 变量的位置
| tags.name | string | 必须 | | | 变量的名称
| additional_attributes | array[string] | optional | | | 追加到 trace span 的额外属性(变量名为 key,变量值为 value)
| additional_attributes[0] | string | required | | | APISIX or Nginx 变量,例如 `http_header` or `route_id`

## 如何启用

Expand Down
Loading

0 comments on commit 84084d3

Please sign in to comment.