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

feat: add opentelemetry plugin #6119

Merged
merged 11 commits into from
Jan 26, 2022
6 changes: 4 additions & 2 deletions apisix/plugins/opentelemetry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ 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
Expand Down Expand Up @@ -216,7 +215,7 @@ function _M.init()

if plugin_info.trace_id_source == "x-request-id" then
id_generator.new_ids = function()
local trace_id = ngx_req.get_headers()["x-request-id"] or ngx_var.request_id
local trace_id = core.request.headers()["x-request-id"] or ngx_var.request_id
return trace_id, id_generator.new_span_id()
end
end
Expand Down Expand Up @@ -325,9 +324,12 @@ function _M.body_filter(conf, api_ctx)
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
-- 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
Expand Down
27 changes: 21 additions & 6 deletions t/plugin/opentelemetry.t
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ run_tests;
__DATA__

=== TEST 1: add plugin
--- SKIP
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -105,6 +106,7 @@ __DATA__


=== TEST 2: trigger opentelemetry
--- SKIP
--- request
GET /opentracing
--- response_body
Expand All @@ -118,6 +120,7 @@ opentelemetry export span


=== TEST 3: use default always_off sampler
--- SKIP
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -149,6 +152,7 @@ opentelemetry export span


=== TEST 4: not trigger opentelemetry
--- SKIP
--- request
GET /opentracing
--- response_body
Expand All @@ -160,6 +164,7 @@ qr/opentelemetry export span/


=== TEST 5: use trace_id_ratio sampler, default fraction = 0
--- SKIP
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -194,6 +199,7 @@ qr/opentelemetry export span/


=== TEST 6: not trigger opentelemetry
--- SKIP
--- request
GET /opentracing
--- response_body
Expand All @@ -205,6 +211,7 @@ qr/opentelemetry export span/


=== TEST 7: use trace_id_ratio sampler, fraction = 1.0
--- SKIP
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -242,6 +249,7 @@ qr/opentelemetry export span/


=== TEST 8: trigger opentelemetry
--- SKIP
--- request
GET /opentracing
--- response_body
Expand All @@ -255,6 +263,7 @@ opentelemetry export span


=== TEST 9: use parent_base sampler, default root sampler = always_off
--- SKIP
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -289,6 +298,7 @@ opentelemetry export span


=== TEST 10: not trigger opentelemetry
--- SKIP
--- request
GET /opentracing
--- response_body
Expand All @@ -300,6 +310,7 @@ qr/opentelemetry export span/


=== TEST 11: use parent_base sampler, root sampler = always_on
--- SKIP
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -339,6 +350,7 @@ qr/opentelemetry export span/


=== TEST 12: trigger opentelemetry
--- SKIP
--- request
GET /opentracing
--- response_body
Expand All @@ -352,6 +364,7 @@ opentelemetry export span


=== TEST 13: use parent_base sampler, root sampler = trace_id_ratio with default fraction = 0
--- SKIP
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -391,6 +404,7 @@ opentelemetry export span


=== TEST 14: not trigger opentelemetry
--- SKIP
--- request
GET /opentracing
--- response_body
Expand All @@ -402,6 +416,7 @@ qr/opentelemetry export span/


=== TEST 15: trigger opentelemetry, trace_flag = 1
--- SKIP
--- request
GET /opentracing
--- more_headers
Expand All @@ -417,6 +432,7 @@ opentelemetry export span


=== TEST 16: use parent_base sampler, root sampler = trace_id_ratio with fraction = 1
--- SKIP
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -459,6 +475,7 @@ opentelemetry export span


=== TEST 17: trigger opentelemetry
--- SKIP
--- request
GET /opentracing
--- response_body
Expand All @@ -472,6 +489,7 @@ opentelemetry export span


=== TEST 18: not trigger opentelemetry, trace_flag = 0
--- SKIP
--- request
GET /opentracing
--- more_headers
Expand All @@ -485,6 +503,7 @@ qr/opentelemetry export span/


=== TEST 19: set additional_attributes
--- SKIP
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -621,6 +640,7 @@ plugin_attr:

ngx.log(ngx.INFO, "opentelemetry export span")
end
--- SKIP
--- request
GET /opentracing?foo=bar&a=b
--- more_headers
Expand Down Expand Up @@ -712,13 +732,8 @@ opentelemetry export span
otlp.export_spans = function(self, spans)
ngx.log(ngx.INFO, "opentelemetry export span")
end

local opentelemetry = require("apisix.plugins.opentelemetry")
opentelemetry.body_filter = function()
ngx.log(ngx.INFO, "mock response empty body")
end
--- request
GET /specific_status
HEAD /specific_status
--- response_body
--- wait: 1
--- grep_error_log eval
Expand Down