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

Baoyuantop 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 6577cfa96 fix(opentelemetry): inject additional_attributes in log 
phase (#13265)
6577cfa96 is described below

commit 6577cfa967dbaaa4d0569a1881df15b1d05307a9
Author: Mohammad Izzraff Janius 
<[email protected]>
AuthorDate: Thu Apr 30 10:14:06 2026 +0800

    fix(opentelemetry): inject additional_attributes in log phase (#13265)
---
 apisix/plugins/opentelemetry.lua |  33 +++++----
 t/plugin/opentelemetry.t         | 151 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 171 insertions(+), 13 deletions(-)

diff --git a/apisix/plugins/opentelemetry.lua b/apisix/plugins/opentelemetry.lua
index 9185d7b20..84bf6202e 100644
--- a/apisix/plugins/opentelemetry.lua
+++ b/apisix/plugins/opentelemetry.lua
@@ -354,19 +354,6 @@ function _M.rewrite(conf, api_ctx)
         table.insert(attributes, attr.string("apisix.service_name", 
api_ctx.service_name))
     end
 
-    if conf.additional_attributes then
-        inject_attributes(attributes, conf.additional_attributes, api_ctx.var, 
false)
-    end
-
-    if conf.additional_header_prefix_attributes then
-        inject_attributes(
-            attributes,
-            conf.additional_header_prefix_attributes,
-            core.request.headers(api_ctx),
-            true
-        )
-    end
-
     -- extract trace context from the headers of downstream HTTP request
     local upstream_context = trace_context_propagator:extract(context, ngx.req)
 
@@ -478,6 +465,26 @@ function _M.log(conf, api_ctx)
         inject_core_spans(ctx, api_ctx, conf)
         span:set_attributes(attr.int("http.status_code", status_code),
                             attr.int("http.response.status_code", status_code))
+
+
+        local attributes = {}
+        if conf.additional_attributes then
+            inject_attributes(attributes, conf.additional_attributes, 
api_ctx.var, false)
+        end
+
+        if conf.additional_header_prefix_attributes then
+            inject_attributes(
+                attributes,
+                conf.additional_header_prefix_attributes,
+                core.request.headers(api_ctx),
+                true
+            )
+        end
+
+        for i = 1, #attributes do
+            span:set_attributes(attributes[i])
+        end
+
         update_time()
         span:finish()
     end
diff --git a/t/plugin/opentelemetry.t b/t/plugin/opentelemetry.t
index 597a450f8..0063cda64 100644
--- a/t/plugin/opentelemetry.t
+++ b/t/plugin/opentelemetry.t
@@ -488,3 +488,154 @@ opentracing
 tail -n 1 ci/pod/otelcol-contrib/data-otlp.json
 --- response_body eval
 qr/.*opentelemetry-lua.*/
+
+
+
+=== TEST 23: setup consumer_name in additional_attributes
+--- extra_yaml_config
+plugins:
+    - opentelemetry
+    - key-auth
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/consumers',
+                ngx.HTTP_PUT,
+                [[{
+                    "username": "john",
+                    "plugins": {
+                        "key-auth": {
+                            "key": "john-key"
+                        }
+                    }
+                }]]
+            )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "plugins": {
+                        "key-auth": {},
+                        "opentelemetry": {
+                            "sampler": {
+                                "name": "always_on"
+                            },
+                            "additional_attributes": [
+                                "consumer_name"
+                            ]
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    },
+                    "uri": "/opentracing"
+                }]]
+            )
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+
+
+
+=== TEST 24: trigger opentelemetry with consumer
+--- extra_yaml_config
+plugins:
+    - opentelemetry
+    - key-auth
+--- request
+GET /opentracing
+--- more_headers
+X-Request-Id: 01010101010101010101010101010102
+apikey: john-key
+--- wait: 2
+--- response_body
+opentracing
+
+
+
+=== TEST 25: check consumer_name in span attributes
+--- exec
+tail -n 1 ci/pod/otelcol-contrib/data-otlp.json
+--- response_body eval
+qr/.*consumer_name.*john.*/
+
+
+
+=== TEST 26: set additional_header_prefix_attributes with header added by 
lower-priority plugin
+--- extra_yaml_config
+plugins:
+    - opentelemetry
+    - serverless-pre-function
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "plugins": {
+                        "serverless-pre-function": {
+                            "phase": "rewrite",
+                            "functions": ["return function(conf, ctx) 
ngx.req.set_header('x-injected-by-plugin', 'test-value') end"]
+                        },
+                        "opentelemetry": {
+                            "sampler": {
+                                "name": "always_on"
+                            },
+                            "additional_header_prefix_attributes": [
+                                "x-injected-*"
+                            ]
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    },
+                    "uri": "/opentracing"
+                }]]
+            )
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+
+
+
+=== TEST 27: trigger opentelemetry with header injected by lower-priority 
plugin
+--- extra_yaml_config
+plugins:
+    - opentelemetry
+    - serverless-pre-function
+--- request
+GET /opentracing
+--- more_headers
+X-Request-Id: 01010101010101010101010101010103
+--- wait: 2
+--- response_body
+opentracing
+
+
+
+=== TEST 28: check header from lower-priority plugin appears in span attributes
+--- exec
+tail -n 1 ci/pod/otelcol-contrib/data-otlp.json
+--- response_body eval
+qr/.*x-injected-by-plugin.*test-value.*/

Reply via email to