Instrumenting custom plugins #14039
-
Hello, I am using the OpenTelemetry plugin to capture traces from Kong. I have a custom plugin that makes an HTTP request to an external service (Keycloak) for authz. The external service (Keycloak) is also instrumented with OTel and sends the traces to the same OTel collector. However, the trace is not propagated to the external service from Kong through the As a workaround, I am manually propagating the trace from Kong by setting the local active_span = kong.tracing.active_span()
local trace_flags = "01"
if not active_span.should_sample then
trace_flags = "00"
end
local traceparent = "00-" .. to_hex(active_span.trace_id) .. "-" .. to_hex(active_span.span_id) .. "-" .. trace_flags
kong.log.debug("traceparent " .. traceparent)
local res, err = client:request_uri(url, {
method = "GET",
headers = {
["traceparent"] = traceparent,
},
}) However, How to get the span of the plugin in the plugin? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
In which phase did you place that code? |
Beta Was this translation helpful? Give feedback.
-
I am doing it in the access phase. As an alternative, I tried creating a new span in the access phase but that was also created as a child span of the root span and not the plugin span. local active_span = kong.tracing.active_span()
local new_span = kong.tracing.start_span("resty.http.request")
local trace_flags = "01"
if not active_span.should_sample then
trace_flags = "00"
end
local traceparent = "00-" .. to_hex(new_span.trace_id) .. "-" .. to_hex(new_span.span_id) .. "-" .. trace_flags
kong.log.debug("traceparent " .. traceparent)
local res, err = client:request_uri(url, {
method = "GET",
headers = {
["traceparent"] = traceparent,
},
})
if not res then
new_span:record_error(err)
new_span:finish()
end
new_span:finish() |
Beta Was this translation helpful? Give feedback.
-
Hi @avinashupadhya99, what version of Kong did you test this on? I believe the issue was fixed here and after that change, |
Beta Was this translation helpful? Give feedback.
Hi @avinashupadhya99, what version of Kong did you test this on? I believe the issue was fixed here and after that change,
active_span
should be the plugin span, let me know if that isn't the case. Thank you.