-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
[APM] Add custom spans around async operations #90403
Conversation
@elasticmachine merge upstream |
Pinging @elastic/apm-ui (Team:apm) |
💛 Build succeeded, but was flaky
Test FailuresKibana Pipeline / general / Chrome X-Pack UI Functional Tests.x-pack/test/functional/apps/ingest_pipelines/feature_controls/ingest_pipelines_security·ts.Ingest pipelines app feature controls security global dashboard all with ingest_pipelines_user should render the "Ingest" section with ingest pipelinesStandard Out
Stack Trace
Metrics [docs]
History
To update your PR or re-run it, just comment with: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice one @dgieselaar 🎉
packages/kbn-apm-utils/src/index.ts
Outdated
return promise | ||
.then( | ||
(res) => { | ||
span.outcome = 'success'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmmm not sure about this one. As outcome changes based on status code and the code here is based on the promise being resolved or rejected. Would double check with the current behaviour.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me this looks ok.
As outcome changes based on status code and the code here is based on the promise being resolved or rejected.
Isn't it rather: "transaction outcome changes based on status code."
So a transaction outcome doesn't depend on span outcome and vice versa. If a span fails it will only also fail the transaction if the error is not caught. Conversely, if a span succeeds, any other span could fail, causing the transaction to fail too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
spans can also result in http requests, for instance when calling an external service (ie, spans can have outcomes based on status code as well). But this function wraps async operations, and it is the responsibility of that operation to reject or resolve based on its interpretation of the results. Interpreting requests is out of scope for this function.
packages/kbn-apm-utils/src/index.ts
Outdated
|
||
const resource = new asyncHooks.AsyncResource('fake_async'); | ||
|
||
return resource.runInAsyncScope(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty sure this would work for promise based cb, Did you happen to investigate if this wrapper works for callback based functions as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean?
@elasticmachine merge upstream |
💚 Build SucceededMetrics [docs]
History
To update your PR or re-run it, just comment with: |
This reverts commit a28318e.
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
Closes #86460.
Provides a
withSpan
utility that wraps an async operation in a custom span.As almost every API call consists of multiple consecutive or parallel ES requests to various ES endpoints, it's hard to see from a trace waterfall what's triggering an ES request and what the intent is. There are also some issues in the Node.js agent where the semantics around span relationships (parent-child or sibling) are not always clear and might not work as expected.
To workaround these issues the
withSpan
helper starts a span, and creates a new async context for the async operation we want to track, essentially isolating it from other async operations, in order to have sane parent-child relationships.See also:
elastic/apm-agent-nodejs#1964
elastic/apm-agent-nodejs#1963
elastic/apm-agent-nodejs#1889
Here's how a /api/apm/services transaction looks without the custom instrumentation:
And here's with custom instrumentation (collapsing the app spans):