Skip to content

Commit

Permalink
refactor(opentelemetry): replace plugin attribute with plugin metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Revolyssup committed Jan 23, 2025
1 parent ff5c6f0 commit 61f20b6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
46 changes: 31 additions & 15 deletions apisix/plugins/opentelemetry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ local lrucache = core.lrucache.new({

local asterisk = string.byte("*", 1)

local attr_schema = {
local metadata_schema = {
type = "object",
properties = {
trace_id_source = {
Expand Down Expand Up @@ -192,18 +192,20 @@ local _M = {
priority = 12009,
name = plugin_name,
schema = schema,
attr_schema = attr_schema,
metadata_schema = metadata_schema,
}


function _M.check_schema(conf)
function _M.check_schema(conf, schema_type)
if schema_type == core.schema.TYPE_METADATA then
return core.schema.check(metadata_schema, conf)
end
return core.schema.check(schema, conf)
end


local hostname
local sampler_factory
local plugin_info

function _M.init()
if process.type() ~= "worker" then
Expand All @@ -218,26 +220,40 @@ function _M.init()
}
hostname = core.utils.gethostname()

plugin_info = plugin.plugin_attr(plugin_name) or {}
local check = {"collector.address"}
core.utils.check_https(check, plugin_info, plugin_name)
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)
-- plugin_info = plugin.plugin_attr(plugin_name) or {}
-- local check = {"collector.address"}
-- core.utils.check_https(check, plugin_info, plugin_name)
-- 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)
-- return
-- end

-- if plugin_info.trace_id_source == "x-request-id" then
-- id_generator.new_ids = function()
-- local trace_id = core.request.headers()["x-request-id"] or ngx_var.request_id
-- return trace_id, id_generator.new_span_id()
-- end
-- end
end


local function create_tracer_obj(conf)
local metadata = plugin.plugin_metadata(plugin_name)
if metadata == nil then
core.log.warn("plugin_metadata is required for opentelemetry plugin to working properly")
return
end
core.log.info("metadata: ", core.json.delay_encode(metadata))
local plugin_info = metadata.value

if plugin_info.trace_id_source == "x-request-id" then
id_generator.new_ids = function()
local trace_id = core.request.headers()["x-request-id"] or ngx_var.request_id
return trace_id, id_generator.new_span_id()
end
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,
Expand Down
2 changes: 1 addition & 1 deletion conf/config.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ plugins: # plugin list (sorted by priority)
- request-id # priority: 12015
- zipkin # priority: 12011
#- skywalking # priority: 12010
#- opentelemetry # priority: 12009
- opentelemetry # priority: 12009
- ext-plugin-pre-req # priority: 12000
- fault-injection # priority: 11000
- mocking # priority: 10900
Expand Down

0 comments on commit 61f20b6

Please sign in to comment.