From c82db59e50179c32f560ad1917d1938eb3e4baa1 Mon Sep 17 00:00:00 2001 From: Jayson Reis Date: Mon, 5 Sep 2022 08:05:31 +0000 Subject: [PATCH] chore: Rename additional_header_attributes to additional_header_prefix_attributes --- apisix/plugins/opentelemetry.lua | 57 +++++++++++-------------- docs/en/latest/plugins/opentelemetry.md | 4 +- t/plugin/opentelemetry2.t | 2 +- 3 files changed, 28 insertions(+), 35 deletions(-) diff --git a/apisix/plugins/opentelemetry.lua b/apisix/plugins/opentelemetry.lua index 6c8d687e6bd94..4bc58bf4275c4 100644 --- a/apisix/plugins/opentelemetry.lua +++ b/apisix/plugins/opentelemetry.lua @@ -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 @@ -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", @@ -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", @@ -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" }, @@ -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" }, @@ -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", @@ -171,7 +171,7 @@ local schema = { minLength = 1, } }, - additional_header_attributes = { + additional_header_prefix_attributes = { type = "array", items = { type = "string", @@ -195,7 +195,6 @@ function _M.check_schema(conf) return core.schema.check(schema, conf) end - local hostname local sampler_factory local plugin_info @@ -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 @@ -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 @@ -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")) @@ -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 @@ -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 @@ -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 ) @@ -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() @@ -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) @@ -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 diff --git a/docs/en/latest/plugins/opentelemetry.md b/docs/en/latest/plugins/opentelemetry.md index 658f65a17ad92..10d6826b2dcf7 100644 --- a/docs/en/latest/plugins/opentelemetry.md +++ b/docs/en/latest/plugins/opentelemetry.md @@ -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 diff --git a/t/plugin/opentelemetry2.t b/t/plugin/opentelemetry2.t index d0ceeecbbc80c..2495d8ef2adf5 100644 --- a/t/plugin/opentelemetry2.t +++ b/t/plugin/opentelemetry2.t @@ -159,7 +159,7 @@ opentelemetry export span "sampler": { "name": "always_on" }, - "additional_header_attributes": [ + "additional_header_prefix_attributes": [ "x-my-header-*" ] }