-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Changes from 12 commits
3a48576
5bee718
69cff93
96a5ac9
559c1c4
0e57181
af98d66
8995793
6506d2a
4283e72
66ba699
5f56ed7
722e910
6da51cc
927a4c4
06a899d
295fabb
43d7833
c36b8d8
ec3db1c
4c1bc7c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "@kbn/apm-utils", | ||
"main": "./target/index.js", | ||
"types": "./target/index.d.ts", | ||
"version": "1.0.0", | ||
"license": "SSPL-1.0 OR Elastic License 2.0", | ||
"private": true, | ||
"scripts": { | ||
"build": "../../node_modules/.bin/tsc", | ||
"kbn:bootstrap": "yarn build", | ||
"kbn:watch": "yarn build --watch" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import agent from 'elastic-apm-node'; | ||
import asyncHooks from 'async_hooks'; | ||
|
||
export interface SpanOptions { | ||
name: string; | ||
type?: string; | ||
subtype?: string; | ||
labels?: Record<string, string>; | ||
} | ||
|
||
export function parseSpanOptions(optionsOrName: SpanOptions | string) { | ||
const options = typeof optionsOrName === 'string' ? { name: optionsOrName } : optionsOrName; | ||
|
||
return options; | ||
} | ||
|
||
export function withSpan<T>(optionsOrName: SpanOptions | string, cb: () => Promise<T>): Promise<T> { | ||
const options = parseSpanOptions(optionsOrName); | ||
|
||
const { name, type, subtype, labels } = options; | ||
|
||
if (!agent.isStarted()) { | ||
return cb() as any; | ||
} | ||
|
||
const span = agent.startSpan(name); | ||
|
||
if (!span) { | ||
return cb() as any; | ||
} | ||
|
||
const resource = new asyncHooks.AsyncResource('fake_async'); | ||
|
||
return resource.runInAsyncScope(() => { | ||
// set the active span for the newly created async context | ||
// @ts-ignore | ||
agent._instrumentation.activeSpan = span; | ||
if (type) { | ||
span.type = type; | ||
} | ||
if (subtype) { | ||
span.subtype = subtype; | ||
} | ||
|
||
if (labels) { | ||
span.addLabels(labels); | ||
} | ||
|
||
const promise = cb(); | ||
|
||
return promise | ||
.then( | ||
(res) => { | ||
span.outcome = 'success'; | ||
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. 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 commentThe reason will be displayed to describe this comment to others. Learn more. To me this looks ok.
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 commentThe 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. |
||
return res; | ||
}, | ||
(err) => { | ||
span.outcome = 'failure'; | ||
throw err; | ||
} | ||
) | ||
.finally(() => { | ||
span.end(); | ||
resource.emitDestroy(); | ||
}); | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"declaration": true, | ||
"outDir": "./target", | ||
"stripInternal": false, | ||
"declarationMap": true, | ||
"types": [ | ||
"node" | ||
] | ||
}, | ||
"include": [ | ||
"./src/**/*.ts" | ||
], | ||
"exclude": [ | ||
"target" | ||
] | ||
} |
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?