This is an automated email from the ASF dual-hosted git repository.

shreemaan-abhishek pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 26a34723f fix(opentelemetry): recreate tracer object after plugin 
metadata changed (#13618)
26a34723f is described below

commit 26a34723fd15305eba1f5eac0963adb28276b2c2
Author: Shreemaan Abhishek <[email protected]>
AuthorDate: Tue Jun 30 14:26:37 2026 +0800

    fix(opentelemetry): recreate tracer object after plugin metadata changed 
(#13618)
---
 apisix/plugins/opentelemetry.lua |  6 +++--
 t/plugin/opentelemetry.t         | 56 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 2 deletions(-)

diff --git a/apisix/plugins/opentelemetry.lua b/apisix/plugins/opentelemetry.lua
index 333e6816f..f046460cd 100644
--- a/apisix/plugins/opentelemetry.lua
+++ b/apisix/plugins/opentelemetry.lua
@@ -341,7 +341,8 @@ function _M.rewrite(conf, api_ctx)
     local plugin_info = metadata.value
     local vars = api_ctx.var
 
-    local tracer, err = core.lrucache.plugin_ctx(lrucache, api_ctx, nil,
+    -- key the cache on modifiedIndex so the tracer is rebuilt when metadata 
changes
+    local tracer, err = core.lrucache.plugin_ctx(lrucache, api_ctx, 
metadata.modifiedIndex,
                                                 create_tracer_obj, conf, 
plugin_info)
     if not tracer then
         core.log.error("failed to fetch tracer object: ", err)
@@ -449,7 +450,8 @@ local function inject_core_spans(root_span_ctx, api_ctx, 
conf)
         additional_attributes = conf.additional_attributes,
         additional_header_prefix_attributes = 
conf.additional_header_prefix_attributes
     }
-    local tracer, err = core.lrucache.plugin_ctx(lrucache, api_ctx, nil,
+    -- key the cache on modifiedIndex so the tracer is rebuilt when metadata 
changes
+    local tracer, err = core.lrucache.plugin_ctx(lrucache, api_ctx, 
metadata.modifiedIndex,
                                                 create_tracer_obj, 
inject_conf, plugin_info)
     if not tracer then
         core.log.error("failed to fetch tracer object: ", err)
diff --git a/t/plugin/opentelemetry.t b/t/plugin/opentelemetry.t
index a260a62a2..b1ba88516 100644
--- a/t/plugin/opentelemetry.t
+++ b/t/plugin/opentelemetry.t
@@ -644,3 +644,59 @@ opentracing
 tail -n 1 ci/pod/otelcol-contrib/data-otlp.json
 --- response_body eval
 qr/.*x-injected-by-plugin.*test-value.*/
+
+
+
+=== TEST 29: updating plugin_metadata rebuilds the cached tracer on a warm 
worker
+--- config
+    location /setup_first {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local plugin = require("apisix.plugin")
+            t('/apisix/admin/plugin_metadata/opentelemetry', ngx.HTTP_PUT,
+                
[[{"batch_span_processor":{"max_export_batch_size":1,"inactive_timeout":0.5},"collector":{"address":"127.0.0.1:4318"},"resource":{"service.name":"otel-meta-change-first"}}]])
+            t('/apisix/admin/routes/1', ngx.HTTP_PUT,
+                
[[{"plugins":{"opentelemetry":{"sampler":{"name":"always_on"}}},"upstream":{"nodes":{"127.0.0.1:1980":1},"type":"roundrobin"},"uri":"/opentracing"}]])
+            -- wait until this worker sees the metadata so the warm-up span 
uses it
+            for _ = 1, 50 do
+                local m = plugin.plugin_metadata("opentelemetry")
+                if m and m.value and m.value.resource
+                    and m.value.resource["service.name"] == 
"otel-meta-change-first" then
+                    break
+                end
+                ngx.sleep(0.1)
+            end
+            ngx.say("ok")
+        }
+    }
+    location /setup_second {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local plugin = require("apisix.plugin")
+            t('/apisix/admin/plugin_metadata/opentelemetry', ngx.HTTP_PUT,
+                
[[{"batch_span_processor":{"max_export_batch_size":1,"inactive_timeout":0.5},"collector":{"address":"127.0.0.1:4318"},"resource":{"service.name":"otel-meta-change-second"}}]])
+            -- wait until this worker sees the updated metadata before the 
next span
+            for _ = 1, 50 do
+                local m = plugin.plugin_metadata("opentelemetry")
+                if m and m.value and m.value.resource
+                    and m.value.resource["service.name"] == 
"otel-meta-change-second" then
+                    break
+                end
+                ngx.sleep(0.1)
+            end
+            ngx.say("ok")
+        }
+    }
+--- pipelined_requests eval
+["GET /setup_first", "GET /opentracing", "GET /setup_second", "GET 
/opentracing"]
+--- response_body eval
+["ok\n", "opentracing\n", "ok\n", "opentracing\n"]
+--- wait: 3
+
+
+
+=== TEST 30: last exported span must carry the updated service.name, not the 
stale one
+--- exec
+tail -n 1 ci/pod/otelcol-contrib/data-otlp.json
+--- response_body eval
+qr/otel-meta-change-second/

Reply via email to