AlinsRan commented on code in PR #12990:
URL: https://github.com/apache/apisix/pull/12990#discussion_r3548188924


##########
apisix/plugins/opentelemetry.lua:
##########
@@ -230,12 +250,23 @@ end
 
 
 local function create_tracer_obj(conf, plugin_info)
-    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()
+   if plugin_info.trace_id_source == "x-request-id" 
+   and not id_generator_overridden then
+    id_generator.new_ids = function()
+     
+        local header_trace_id = core.request.headers()["x-request-id"]
+                                or ngx_var.request_id
+
+        if is_valid_trace_id(header_trace_id) then
+            return header_trace_id, id_generator.new_span_id()
         end
+
+        -- fallback to default generator for invalid values (e.g. UUID)
+        return original_new_ids()
     end
+    id_generator_overridden = true
+end

Review Comment:
   Fixed. `trace_id_source` lives in plugin metadata (global), so there is no 
per-route mode to preserve — `create_tracer_obj` now resets 
`id_generator.new_ids` to the captured default first and only wraps it for the 
`x-request-id` mode. The lrucache is keyed on `metadata.modifiedIndex`, so 
switching back to `random` rebuilds the tracer and takes effect without a 
worker restart. Added a regression test that a valid `X-Request-Id` is ignored 
once `trace_id_source` is `random`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to