Skip to content

Commit

Permalink
feat: upgrade opentelemetry-lua 0.2-3, plugin config collector.addres…
Browse files Browse the repository at this point in the history
…s support specify https scheme (yangxikun/opentelemetry-lua#60)
  • Loading branch information
roketyyang committed Feb 10, 2023
1 parent 5984321 commit c9590e4
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 23 deletions.
33 changes: 14 additions & 19 deletions apisix/plugins/opentelemetry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ local span_status = require("opentelemetry.trace.span_status")
local resource_new = require("opentelemetry.resource").new
local attr = require("opentelemetry.attribute")

local context_storage = require("opentelemetry.context_storage")
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 context = require("opentelemetry.context").new()
local trace_context_propagator =
require("opentelemetry.trace.propagation.text_map.trace_context_propagator").new()

local ngx = ngx
local ngx_var = ngx.var
Expand Down Expand Up @@ -310,7 +309,7 @@ function _M.rewrite(conf, api_ctx)
end

-- extract trace context from the headers of downstream HTTP request
local upstream_context = trace_context.extract(context, carrier_new())
local upstream_context = trace_context_propagator:extract(context, ngx.req)
local attributes = {
attr.string("service", api_ctx.service_name),
attr.string("route", api_ctx.route_name),
Expand All @@ -333,27 +332,24 @@ function _M.rewrite(conf, api_ctx)
kind = span_kind.server,
attributes = attributes,
})
ctx:attach()
api_ctx.otel_context_token = ctx:attach()

-- inject trace context into the headers of upstream HTTP request
trace_context.inject(ctx, carrier_new())
trace_context_propagator:inject(ctx, ngx.req)
end


function _M.delayed_body_filter(conf, api_ctx)
if ngx.arg[2] then
if api_ctx.otel_context_token and ngx.arg[2] then
local ctx = context:current()
if not ctx then
return
end

local upstream_status = core.response.get_upstream_status(api_ctx)
ctx:detach()
ctx:detach(api_ctx.otel_context_token)
api_ctx.otel_context_token = nil

-- get span from current context
local span = ctx:span()
local upstream_status = core.response.get_upstream_status(api_ctx)
if upstream_status and upstream_status >= 500 then
span:set_status(span_status.error,
span:set_status(span_status.ERROR,
"upstream response status: " .. upstream_status)
end

Expand All @@ -365,15 +361,14 @@ end
-- body_filter maybe not called because of empty http body response
-- so we need to check if the span has finished in log phase
function _M.log(conf, api_ctx)
local ctx = context:current()
if ctx then
if api_ctx.otel_context_token then
-- ctx:detach() is not necessary, because of ctx is stored in ngx.ctx
local upstream_status = core.response.get_upstream_status(api_ctx)

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

Expand Down
2 changes: 1 addition & 1 deletion docs/en/latest/plugins/opentelemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ You can set up the collector by configuring it in you configuration file (`conf/
| trace_id_source | enum | random | Source of the trace ID. Valid values are `random` or `x-request-id`. When set to `x-request-id`, the value of the `x-request-id` header will be used as trace ID. Make sure that is matches the regex pattern `[0-9a-f]{32}`. |
| resource | object | | Additional [resource](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/sdk.md) appended to the trace. |
| collector | object | {address = "127.0.0.1:4318", request_timeout = 3} | OpenTelemetry Collector configuration. |
| collector.address | string | 127.0.0.1:4318 | Collector address. |
| collector.address | string | 127.0.0.1:4318 | Collector address. If the collector serve on https, use https://127.0.0.1:4318 as address. |
| collector.request_timeout | integer | 3 | Report request timeout in seconds. |
| collector.request_headers | object | | Report request HTTP headers. |
| batch_span_processor | object | | Trace span processor. |
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/latest/plugins/opentelemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ title: opentelemetry
| trace_id_source | enum | random | trace ID 的来源。有效值为:`random``x-request-id`。当设置为 `x-request-id` 时,`x-request-id` 头的值将用作跟踪 ID。请确保当前请求 ID 是符合 TraceID 规范的:`[0-9a-f]{32}`|
| resource | object | | 追加到 trace 的额外 [resource](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/sdk.md)|
| collector | object | {address = "127.0.0.1:4318", request_timeout = 3} | OpenTelemetry Collector 配置。 |
| collector.address | string | 127.0.0.1:4318 | 数据采集服务的地址。 |
| collector.address | string | 127.0.0.1:4318 | 数据采集服务的地址。如果数据采集服务使用的是 HTTPS 协议,可以将 address 设置为 https://127.0.0.1:4318。 |
| collector.request_timeout | integer | 3 | 数据采集服务上报请求超时时长,单位为秒。 |
| collector.request_headers | object | | 数据采集服务上报请求附加的 HTTP 请求头。 |
| batch_span_processor | object | | trace span 处理器参数配置。 |
Expand Down
2 changes: 1 addition & 1 deletion rockspec/apisix-master-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ dependencies = {
"inspect == 3.1.1",
"lualdap = 1.2.6-1",
"lua-resty-rocketmq = 0.3.0-0",
"opentelemetry-lua = 0.1-3",
"opentelemetry-lua = 0.2-3",
"net-url = 0.9-1",
"xml2lua = 1.5-2",
"nanoid = 0.1-1",
Expand Down
1 change: 1 addition & 0 deletions t/plugin/opentelemetry.t
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ _EOC_
local client = require("opentelemetry.trace.exporter.http_client")
client.do_request = function()
ngx.log(ngx.INFO, "opentelemetry export span")
return "ok"
end
_EOC_

Expand Down
2 changes: 1 addition & 1 deletion t/plugin/opentelemetry2.t
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ _EOC_
local client = require("opentelemetry.trace.exporter.http_client")
client.do_request = function()
ngx.log(ngx.INFO, "opentelemetry export span")
return "ok"
end
local ctx_new = require("opentelemetry.context").new
require("opentelemetry.context").new = function (...)
Expand Down Expand Up @@ -136,7 +137,6 @@ qr/(opentelemetry export span|opentelemetry context current|plugin body_filter p
plugin body_filter phase
plugin body_filter phase
opentelemetry context current
opentelemetry context current
opentelemetry export span
Expand Down

0 comments on commit c9590e4

Please sign in to comment.