Skip to content

Commit

Permalink
chore: Rename additional_header_attributes to additional_header_prefi…
Browse files Browse the repository at this point in the history
…x_attributes
  • Loading branch information
jaysonsantos committed Sep 5, 2022
1 parent 8266b2a commit c82db59
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 35 deletions.
57 changes: 25 additions & 32 deletions apisix/plugins/opentelemetry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ local always_off_sampler_new = require("opentelemetry.trace.sampling.always_off_
local always_on_sampler_new = require("opentelemetry.trace.sampling.always_on_sampler").new
local parent_base_sampler_new = require("opentelemetry.trace.sampling.parent_base_sampler").new
local trace_id_ratio_sampler_new =
require("opentelemetry.trace.sampling.trace_id_ratio_sampler").new
require("opentelemetry.trace.sampling.trace_id_ratio_sampler").new

local exporter_client_new = require("opentelemetry.trace.exporter.http_client").new
local otlp_exporter_new = require("opentelemetry.trace.exporter.otlp").new
Expand Down Expand Up @@ -60,30 +60,30 @@ local attr_schema = {
properties = {
trace_id_source = {
type = "string",
enum = {"x-request-id", "random"},
enum = { "x-request-id", "random" },
description = "the source of trace id",
default = "random",
},
resource = {
type = "object",
description = "additional resource",
additionalProperties = {{type = "boolean"}, {type = "number"}, {type = "string"}},
additionalProperties = { { type = "boolean" }, { type = "number" }, { type = "string" } },
},
collector = {
type = "object",
description = "opentelemetry collector",
properties = {
address = {type = "string", description = "host:port", default = "127.0.0.1:4318"},
request_timeout = {type = "integer", description = "second uint", default = 3},
address = { type = "string", description = "host:port", default = "127.0.0.1:4318" },
request_timeout = { type = "integer", description = "second uint", default = 3 },
request_headers = {
type = "object",
description = "http headers",
additionalProperties = {
one_of = {{type = "boolean"},{type = "number"}, {type = "string"}},
},
one_of = { { type = "boolean" }, { type = "number" }, { type = "string" } },
},
}
},
default = {address = "127.0.0.1:4318", request_timeout = 3}
default = { address = "127.0.0.1:4318", request_timeout = 3 }
},
batch_span_processor = {
type = "object",
Expand All @@ -92,7 +92,7 @@ local attr_schema = {
drop_on_queue_full = {
type = "boolean",
description = "if true, drop span when queue is full,"
.. " otherwise force process batches",
.. " otherwise force process batches",
},
max_queue_size = {
type = "integer",
Expand Down Expand Up @@ -124,7 +124,7 @@ local schema = {
properties = {
name = {
type = "string",
enum = {"always_on", "always_off", "trace_id_ratio", "parent_base"},
enum = { "always_on", "always_off", "trace_id_ratio", "parent_base" },
title = "sampling strategy",
default = "always_off"
},
Expand All @@ -140,7 +140,7 @@ local schema = {
properties = {
name = {
type = "string",
enum = {"always_on", "always_off", "trace_id_ratio"},
enum = { "always_on", "always_off", "trace_id_ratio" },
title = "sampling strategy",
default = "always_off"
},
Expand All @@ -153,16 +153,16 @@ local schema = {
default = 0,
},
},
default = {fraction = 0}
default = { fraction = 0 }
}
},
default = {name = "always_off", options = {fraction = 0}}
default = { name = "always_off", options = { fraction = 0 } }
},
},
default = {fraction = 0, root = {name = "always_off"}}
default = { fraction = 0, root = { name = "always_off" } }
}
},
default = {name = "always_off", options = {fraction = 0, root = {name = "always_off"}}}
default = { name = "always_off", options = { fraction = 0, root = { name = "always_off" } } }
},
additional_attributes = {
type = "array",
Expand All @@ -171,7 +171,7 @@ local schema = {
minLength = 1,
}
},
additional_header_attributes = {
additional_header_prefix_attributes = {
type = "array",
items = {
type = "string",
Expand All @@ -195,7 +195,6 @@ function _M.check_schema(conf)
return core.schema.check(schema, conf)
end


local hostname
local sampler_factory
local plugin_info
Expand All @@ -217,7 +216,7 @@ function _M.init()
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)
": ", err)
return
end

Expand All @@ -229,15 +228,14 @@ function _M.init()
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,
plugin_info.collector.request_headers))
plugin_info.collector.request_timeout,
plugin_info.collector.request_headers))
-- create span processor
local batch_span_processor = batch_span_processor_new(exporter,
plugin_info.batch_span_processor)
plugin_info.batch_span_processor)
-- create sampler
local sampler
local sampler_name = conf.sampler.name
Expand All @@ -254,7 +252,7 @@ local function create_tracer_obj(conf)
else
sampler = sampler_factory[sampler_name](sampler_options.fraction)
end
local resource_attrs = {attr.string("hostname", hostname)}
local resource_attrs = { attr.string("hostname", hostname) }
if plugin_info.resource then
if not plugin_info.resource["service.name"] then
table.insert(resource_attrs, attr.string("service.name", "APISIX"))
Expand All @@ -280,7 +278,6 @@ local function create_tracer_obj(conf)
return tp:tracer("opentelemetry-lua")
end


local function inject_attributes(attributes, wanted_attributes, source, with_prefix)
for _, key in ipairs(wanted_attributes) do
local is_key_a_match = #key >= 2 and key:byte(-1) == asterisk and with_prefix
Expand All @@ -301,7 +298,6 @@ local function inject_attributes(attributes, wanted_attributes, source, with_pre
end
end


function _M.rewrite(conf, api_ctx)
local tracer, err = core.lrucache.plugin_ctx(lrucache, api_ctx, nil, create_tracer_obj, conf)
if not tracer then
Expand All @@ -320,10 +316,10 @@ function _M.rewrite(conf, api_ctx)
inject_attributes(attributes, conf.additional_attributes, api_ctx.var, false)
end

if conf.additional_header_attributes then
if conf.additional_header_prefix_attributes then
inject_attributes(
attributes,
conf.additional_header_attributes,
conf.additional_header_prefix_attributes,
core.request.headers(api_ctx),
true
)
Expand All @@ -339,7 +335,6 @@ function _M.rewrite(conf, api_ctx)
trace_context.inject(ctx, carrier_new())
end


function _M.delayed_body_filter(conf, api_ctx)
if ngx.arg[2] then
local ctx = context:current()
Expand All @@ -354,14 +349,13 @@ function _M.delayed_body_filter(conf, api_ctx)
local span = ctx:span()
if upstream_status and upstream_status >= 500 then
span:set_status(span_status.error,
"upstream response status: " .. upstream_status)
"upstream response status: " .. upstream_status)
end

span:finish()
end
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)
Expand All @@ -374,12 +368,11 @@ function _M.log(conf, api_ctx)
local span = ctx:span()
if upstream_status and upstream_status >= 500 then
span:set_status(span_status.error,
"upstream response status: " .. upstream_status)
"upstream response status: " .. upstream_status)
end

span:finish()
end
end


return _M
4 changes: 2 additions & 2 deletions docs/en/latest/plugins/opentelemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ The Plugin only supports binary-encoded [OLTP over HTTP](https://opentelemetry.i
| sampler.options.root.options.fraction | number | False | 0 | [0, 1] | Root sampling probability for `trace_id_ratio`. |
| additional_attributes | array[string] | False | | | Variables and its values which will be appended to the trace span. |
| additional_attributes[0] | string | True | | | APISIX or Nginx variables. For example, `http_header` or `route_id`. |
| additional_header_attributes | array[string] | False | | | Variables and its values which will be appended to the trace span. |
| additional_header_attributes[0] | string | True | | | Request headers. For example, `x-my-header"` or `x-my-headers-*` to include all headers with the prefix `x-my-headers-`. |
| additional_header_prefix_attributes | array[string] | False | | | Headers or headers prefixes to be appended to the trace span's attributes. |
| additional_header_prefix_attributes[0]| string | True | | | Request headers. For example, `x-my-header"` or `x-my-headers-*` to include all headers with the prefix `x-my-headers-`. |

### Configuring the collector

Expand Down
2 changes: 1 addition & 1 deletion t/plugin/opentelemetry2.t
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ opentelemetry export span
"sampler": {
"name": "always_on"
},
"additional_header_attributes": [
"additional_header_prefix_attributes": [
"x-my-header-*"
]
}
Expand Down

0 comments on commit c82db59

Please sign in to comment.