-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat: add more spans to opentelemetry plugin #12686
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nic-6443
wants to merge
48
commits into
apache:master
Choose a base branch
from
nic-6443:nic/opentelemetry
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+886
−61
Open
Changes from all commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
b5197a1
feat: add more spans to opentelemetry plugin
nic-6443 f6414fd
add todo
nic-6443 0317cf5
f
nic-6443 7944e9f
return span on newspan
Revolyssup ec7adef
fix lint
Revolyssup bb28639
fix CI
Revolyssup 1b29310
fix opentelemetry3
Revolyssup 57cac44
add test
Revolyssup f8d5974
add test
Revolyssup 0c38fc7
revert
Revolyssup ce4277c
revert
Revolyssup 1d6c4e2
revert
Revolyssup 1f0cedb
f
Revolyssup e6c31c0
add test
Revolyssup 234f9f7
add plugin phase test
Revolyssup c3f9ae9
fix test
Revolyssup 03f3906
add test
Revolyssup 12d2513
f
Revolyssup f6e92b8
f
Revolyssup 0577ab2
update docs
Revolyssup b1aaba2
fix lint
Revolyssup c3f37eb
fix otel3
Revolyssup cab6620
fix tests
Revolyssup 9d195e5
remove todo
Revolyssup b505630
rename
Revolyssup 9fc46b0
Update opentelemetry6.t
Revolyssup c5be5f9
apply suggestions
Revolyssup 05eda81
fix lint
Revolyssup 956935a
fix
Revolyssup d2bd719
f
Revolyssup 119f9d3
apply suggestions
Revolyssup 91e37be
fix test
Revolyssup fe16d9e
fix tests
Revolyssup 14b4101
f
Revolyssup 079484d
apply suggestions
Revolyssup c310ade
fix lint
Revolyssup 9cee4ea
Merge branch 'master' into nic/opentelemetry
AlinsRan 8ed32c5
perf spans
AlinsRan 457e886
Merge branch 'master' into nic/opentelemetry
AlinsRan 2500281
f
AlinsRan baee580
remove
AlinsRan 49a55bc
f
AlinsRan 4edbbbb
fix test
AlinsRan 1c3f90a
update md
AlinsRan b2cc72a
update config.yaml.example
AlinsRan 7a26e41
update config.yaml.example and doc
AlinsRan 359ddc4
fix
AlinsRan b9f3215
refactor v3
AlinsRan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,6 +48,7 @@ local pairs = pairs | |
| local ipairs = ipairs | ||
| local unpack = unpack | ||
| local string_format = string.format | ||
| local update_time = ngx.update_time | ||
|
|
||
| local lrucache = core.lrucache.new({ | ||
| type = 'plugin', count = 128, ttl = 24 * 60 * 60, | ||
|
|
@@ -376,49 +377,101 @@ function _M.rewrite(conf, api_ctx) | |
| ngx_var.opentelemetry_span_id = span_context.span_id | ||
| end | ||
|
|
||
| if not ctx:span():is_recording() and ngx.ctx.tracing then | ||
| ngx.ctx.tracing.skip = true | ||
| end | ||
|
|
||
| api_ctx.otel_context_token = ctx:attach() | ||
|
|
||
| -- inject trace context into the headers of upstream HTTP request | ||
| trace_context_propagator:inject(ctx, ngx.req) | ||
| end | ||
|
|
||
|
|
||
| function _M.delayed_body_filter(conf, api_ctx) | ||
| if api_ctx.otel_context_token and ngx.arg[2] then | ||
| local ctx = context:current() | ||
| ctx:detach(api_ctx.otel_context_token) | ||
| api_ctx.otel_context_token = nil | ||
| local function create_child_span(tracer, parent_span_ctx, spans, span_idx) | ||
| local span = spans[span_idx] | ||
| if not span or span.finished then | ||
| return | ||
| end | ||
| span.finished = true | ||
| local new_span_ctx, new_span = tracer:start(parent_span_ctx, span.name, | ||
| { | ||
| kind = span.kind, | ||
| attributes = span.attributes, | ||
| }) | ||
| new_span.start_time = span.start_time | ||
|
|
||
| for _, idx in ipairs(span.child_ids or {}) do | ||
| create_child_span(tracer, new_span_ctx, spans, idx) | ||
| end | ||
| if span.status then | ||
| new_span:set_status(span.status.code, span.status.message) | ||
| end | ||
| new_span:finish(span.end_time) | ||
| end | ||
|
|
||
| -- get span from current context | ||
| local span = ctx:span() | ||
| local upstream_status = core.response.get_upstream_status(api_ctx) | ||
| if upstream_status and upstream_status >= 500 then | ||
| span:set_status(span_status.ERROR, | ||
| "upstream response status: " .. upstream_status) | ||
| end | ||
|
|
||
| span:set_attributes(attr.int("http.status_code", upstream_status)) | ||
| local function inject_core_spans(root_span_ctx, api_ctx, conf) | ||
| local tracing = api_ctx.ngx_ctx.tracing | ||
| if not tracing then | ||
| return | ||
| end | ||
|
|
||
| span:finish() | ||
| local span = root_span_ctx:span() | ||
|
|
||
| local metadata = plugin.plugin_metadata(plugin_name) | ||
| local plugin_info = metadata.value | ||
| if span and not span:is_recording() then | ||
| return | ||
| end | ||
| local inject_conf = { | ||
| sampler = { | ||
| name = "always_on", | ||
| options = conf.sampler.options | ||
| }, | ||
| 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, | ||
| create_tracer_obj, inject_conf, plugin_info) | ||
| if not tracer then | ||
| core.log.error("failed to fetch tracer object: ", err) | ||
| return | ||
| end | ||
|
|
||
| if #tracing.spans == 0 then | ||
| return | ||
| end | ||
| span.start_time = tracing.spans[1].start_time | ||
| for i, _ in ipairs(tracing.spans or {}) do | ||
| create_child_span(tracer, root_span_ctx, tracing.spans, i) | ||
| end | ||
| end | ||
|
|
||
|
|
||
| -- body_filter maybe not called because of empty http body response | ||
| -- so we need to check if the span has finished in log phase | ||
| function _M.log(conf, api_ctx) | ||
| if api_ctx.otel_context_token then | ||
| -- ctx:detach() is not necessary, because of ctx is stored in ngx.ctx | ||
| local upstream_status = core.response.get_upstream_status(api_ctx) | ||
|
|
||
| -- get span from current context | ||
| local span = context:current():span() | ||
| local ctx = context:current() | ||
| local span = ctx:span() | ||
| if upstream_status and upstream_status >= 500 then | ||
| span:set_status(span_status.ERROR, | ||
| "upstream response status: " .. upstream_status) | ||
| end | ||
|
|
||
| inject_core_spans(ctx, api_ctx, conf) | ||
| span:set_attributes(attr.int("http.status_code", upstream_status)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use https://opentelemetry.io/docs/specs/semconv/registry/attributes/http/ |
||
| update_time() | ||
| span:finish() | ||
| if ngx.ctx._apisix_spans then | ||
| for _, sp in ipairs(ngx.ctx._apisix_spans) do | ||
| sp:release() | ||
| end | ||
| ngx.ctx._apisix_spans = nil | ||
| end | ||
| end | ||
| end | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.