mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
* refact(NA): apply root_input_dir=src to each already created pkg * refact(NA): update package generator * fix(NA): correctly use rootDir * fix(NA): use root input dir on latest introduced pkgs for jsts_transpiler macro * chore(NA): merge with main * chore(NA): first attempt to complete removal of src folder on a small group of pkgs * Revert "chore(NA): first attempt to complete removal of src folder on a small group of pkgs" This reverts commitb6f34b7530
. * chore(NA): remove src folder requirement from xpack pkgs * chore(NA): remove src folder from analytics pkgs * chore(NA): remove src folder from home pkgs * chore(NA): remove src folder from shared_ux pkgs * fix(NA): remove missing src folder inputs on sharedux pkg * chore(NA): remove src folder from kbn-a* pkgs * chore(NA): remove src folder from kbn-b* pkgs * chore(NA): remove src folder from kbn-c* pkgs * chore(NA): correct exclude pattern for each changed pkg * chore(NA): remove src folder from kbn-y* pkgs * chore(NA): remove src folder from kbn-e* pkgs * chore(NA): remove src folder from kbn-f* and kbn-g* pkgs * chore(NA): remove src folder from kbn-f* and kbn-g* pkgs * chore(NA): remove src folder from kbn-h** pkgs * [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix' * Revert "chore(NA): remove src folder from kbn-h** pkgs" This reverts commitdcdf72bcc1
. * fix(NA): grammar location * test(NA): fix tests for kbn/config-schema * test(NA): fix tests for kbn/config-schema * chore(NA): multiple errors fixed * chore(NA): remove kuery grammar fix * fix(NA): @kbn/ace imports * fix(NA): grammar location * fix(NA): add missing files to tsconfigs * [CI] Auto-commit changed files from 'node scripts/build_plugin_list_docs' * chore(NA): complete tsconfigs * Revert "chore(NA): complete tsconfigs" This reverts commitf48c616864
. * chore(NA): remove src folder from kbn-core* pkgs * chore(NA): remove src folder from kbn-u* pkgs * chore(NA): remove src folder from kbn-ui-shared-deps* pkgs * chore(NA): fix problems on core pkgs * chore(NA): fix problems on core pkgs * [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix' * fix(NA): shared_built_assets correct location' * test(NA): update @kbn/optimizer integration snapshots * chore(NA): remove src folder from kbn-t* p1 pkgs * chore(NA): remove src folder from kbn-t* p2 pkgs * chore(NA): update rootDir on utility types pkg * chore(NA): include missing files on @kbn/test * chore(NA): include missing files on @kbn/test * fix(NA): new jest preset paths on @kbn/test * chore(NA): fix eslint * fix(NA): new jest preset paths on @kbn/test * chore(NA): remove exclusion for mocks folde on @kbn/test * fix(NA): several imports from target_node * fix(NA): @kbn/test mocha and jest types clash * chore(NA): remove src folder from kbn-storybook pkg * chore(NA): remove src folder from kbn-stdio-dev-helpers pkg * chore(NA): remove src folder from kbn-std pkg * chore(NA): remove src folder from kbn-sort-pkg-json pkg * chore(NA): remove src folder from kbn-some-dev-log and kbn-shared-ux-utility pkgs * chore(NA): remove src folder from kbn-ux-storybook pkg * chore(NA): remove src folder from kbn-shared-ux-services pkg * chore(NA): remove src folder from kbn-shared-ux-components pkg * chore(NA): remove src folder from kbn-shared-svg pkg * chore(NA): remove src folder from kbn-server-http-tools pkg * chore(NA): remove src folder from kbn-securitysolution-* pkgs * chore(NA): remove src folder from kbn-r-* pkgs * chore(NA): remove src folder from kbn-p* pkgs * chore(NA): remove src folder from kbn-o* pkgs * chore(NA): remove src folder from kbn-m* pkgs * chore(NA): remove src folder from kbn-j,k,l* pkgs * chore(NA): remove src folder from kbn-j,k,l* pkgs * chore(NA): remove src folder from kbn-io-ts-utils* pkgs * chore(NA): remove src folder from kbn-* pkgs except a few * chore(NA): update @kbn/generate * fix(NA): wrong exclusion on kbn-storybook * chore(NA): remove src folder from kbn-monaco pkg * chore(NA): remove src folder from kbn-interpreter pkg * fix(NA): wrong exclusion on kbn-storybook * chore(NA): update every require for target_*/src * chore(NA): remover src folder from @kbn/handlebars * fix(NA): license for @kbn/handlebars * chore(NA): copy templates as part of the jsts_transpiler macro for @kbn/storybook * chore(NA): update handlebars * fix(NA): @kbn/plugin-generator import paths * fix(NA): bundle sizes * fix(NA): web bundle for @kbn/i18n-react * Revert "fix(NA): bundle sizes" This reverts commit8aefe84fbc
. * Revert "Revert "fix(NA): bundle sizes"" This reverts commite9d87d72a4
. * fix(NA): @kbn/docs-utils index.ts path expectation * chore(NA): merge and solve conflicts with main * fix(NA): relative import to index * chore(NA): merge and solve conflicts with main * [CI] Auto-commit changed files from 'node scripts/precommit_hook.js --ref HEAD~1..HEAD --fix' * chore(NA): apply eslint fix * [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix' Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
116 lines
3 KiB
TypeScript
116 lines
3 KiB
TypeScript
/*
|
|
* 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>;
|
|
intercept?: boolean;
|
|
}
|
|
|
|
type Span = Exclude<typeof agent.currentSpan, undefined | null>;
|
|
|
|
export function parseSpanOptions(optionsOrName: SpanOptions | string) {
|
|
const options = typeof optionsOrName === 'string' ? { name: optionsOrName } : optionsOrName;
|
|
|
|
return options;
|
|
}
|
|
|
|
const runInNewContext = <T extends (...args: any[]) => any>(cb: T): ReturnType<T> => {
|
|
const resource = new asyncHooks.AsyncResource('fake_async');
|
|
|
|
return resource.runInAsyncScope(cb);
|
|
};
|
|
|
|
export async function withSpan<T>(
|
|
optionsOrName: SpanOptions | string,
|
|
cb: (span?: Span) => Promise<T>
|
|
): Promise<T> {
|
|
const options = parseSpanOptions(optionsOrName);
|
|
|
|
const { name, type, subtype, labels, intercept } = options;
|
|
|
|
if (!agent.isStarted()) {
|
|
return cb();
|
|
}
|
|
|
|
let createdSpan: Span | undefined;
|
|
|
|
// When a span starts, it's marked as the active span in its context.
|
|
// When it ends, it's not untracked, which means that if a span
|
|
// starts directly after this one ends, the newly started span is a
|
|
// child of this span, even though it should be a sibling.
|
|
// To mitigate this, we queue a microtask by awaiting a promise.
|
|
if (!intercept) {
|
|
await Promise.resolve();
|
|
|
|
createdSpan = agent.startSpan(name) ?? undefined;
|
|
|
|
if (!createdSpan) {
|
|
return cb();
|
|
}
|
|
}
|
|
|
|
// If a span is created in the same context as the span that we just
|
|
// started, it will be a sibling, not a child. E.g., the Elasticsearch span
|
|
// that is created when calling search() happens in the same context. To
|
|
// mitigate this we create a new context.
|
|
|
|
return runInNewContext(() => {
|
|
const promise = cb(createdSpan);
|
|
|
|
let span: Span | undefined = createdSpan;
|
|
|
|
if (intercept) {
|
|
span = agent.currentSpan ?? undefined;
|
|
}
|
|
|
|
if (!span) {
|
|
return promise;
|
|
}
|
|
|
|
const targetedSpan = span;
|
|
|
|
if (name) {
|
|
targetedSpan.name = name;
|
|
}
|
|
|
|
// @ts-ignore
|
|
if (type) {
|
|
targetedSpan.type = type;
|
|
}
|
|
if (subtype) {
|
|
targetedSpan.subtype = subtype;
|
|
}
|
|
|
|
if (labels) {
|
|
targetedSpan.addLabels(labels);
|
|
}
|
|
|
|
return promise
|
|
.then((res) => {
|
|
if (!targetedSpan.outcome || targetedSpan.outcome === 'unknown') {
|
|
targetedSpan.outcome = 'success';
|
|
}
|
|
return res;
|
|
})
|
|
.catch((err) => {
|
|
if (!targetedSpan.outcome || targetedSpan.outcome === 'unknown') {
|
|
targetedSpan.outcome = 'failure';
|
|
}
|
|
throw err;
|
|
})
|
|
.finally(() => {
|
|
targetedSpan.end();
|
|
});
|
|
});
|
|
}
|