initial commit

This commit is contained in:
2025-09-01 22:12:29 +02:00
parent b1873f9c1d
commit 02a54f61c0
5598 changed files with 903558 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
import { Span, SpanOptions } from '../../';
/**
* Options needed for span creation
*/
export interface SugaredSpanOptions extends SpanOptions {
/**
* function to overwrite default exception behavior to record the exception. No exceptions should be thrown in the function.
* @param e Error which triggered this exception
* @param span current span from context
*/
onException?: (e: Error, span: Span) => void;
}
//# sourceMappingURL=SugaredOptions.d.ts.map

View File

@@ -0,0 +1,17 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export {};
//# sourceMappingURL=SugaredOptions.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"SugaredOptions.js","sourceRoot":"","sources":["../../../../src/experimental/trace/SugaredOptions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG","sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Span, SpanOptions } from '../../';\n\n/**\n * Options needed for span creation\n */\nexport interface SugaredSpanOptions extends SpanOptions {\n /**\n * function to overwrite default exception behavior to record the exception. No exceptions should be thrown in the function.\n * @param e Error which triggered this exception\n * @param span current span from context\n */\n onException?: (e: Error, span: Span) => void;\n}\n"]}

View File

@@ -0,0 +1,64 @@
import { SugaredSpanOptions } from './SugaredOptions';
import { Context, Span, Tracer } from '../../';
/**
* return a new SugaredTracer created from the supplied one
* @param tracer
*/
export declare function wrapTracer(tracer: Tracer): SugaredTracer;
export declare class SugaredTracer implements Tracer {
private readonly _tracer;
constructor(tracer: Tracer);
startActiveSpan: Tracer['startActiveSpan'];
startSpan: Tracer['startSpan'];
/**
* Starts a new {@link Span} and calls the given function passing it the
* created span as first argument.
* Additionally, the new span gets set in context and this context is activated
* for the duration of the function call.
* The span will be closed after the function has executed.
* If an exception occurs, it is recorded, the status is set to ERROR and the exception is rethrown.
*
* @param name The name of the span
* @param [options] SugaredSpanOptions used for span creation
* @param [context] Context to use to extract parent
* @param fn function called in the context of the span and receives the newly created span as an argument
* @returns return value of fn
* @example
* const something = tracer.withActiveSpan('op', span => {
* // do some work
* });
* @example
* const something = await tracer.withActiveSpan('op', span => {
* // do some async work
* });
*/
withActiveSpan<F extends (span: Span) => ReturnType<F>>(name: string, fn: F): ReturnType<F>;
withActiveSpan<F extends (span: Span) => ReturnType<F>>(name: string, options: SugaredSpanOptions, fn: F): ReturnType<F>;
withActiveSpan<F extends (span: Span) => ReturnType<F>>(name: string, options: SugaredSpanOptions, context: Context, fn: F): ReturnType<F>;
/**
* Starts a new {@link Span} and ends it after execution of fn without setting it on context.
* The span will be closed after the function has executed.
* If an exception occurs, it is recorded, the status is et to ERROR and rethrown.
*
* This method does NOT modify the current Context.
*
* @param name The name of the span
* @param [options] SugaredSpanOptions used for span creation
* @param [context] Context to use to extract parent
* @param fn function called in the context of the span and receives the newly created span as an argument
* @returns Span The newly created span
* @example
* const something = tracer.withSpan('op', span => {
* // do some work
* });
* @example
* const something = await tracer.withSpan('op', span => {
* // do some async work
* });
*/
withSpan<F extends (span: Span) => ReturnType<F>>(name: string, fn: F): ReturnType<F>;
withSpan<F extends (span: Span) => ReturnType<F>>(name: string, options: SugaredSpanOptions, fn: F): ReturnType<F>;
withSpan<F extends (span: Span) => ReturnType<F>>(name: string, options: SugaredSpanOptions, context: Context, fn: F): ReturnType<F>;
withSpan<F extends (span: Span) => ReturnType<F>>(name: string, options: SugaredSpanOptions, context: Context, fn: F): ReturnType<F>;
}
//# sourceMappingURL=SugaredTracer.d.ts.map

View File

@@ -0,0 +1,88 @@
import { context, SpanStatusCode } from '../../';
const defaultOnException = (e, span) => {
span.recordException(e);
span.setStatus({
code: SpanStatusCode.ERROR,
});
};
/**
* return a new SugaredTracer created from the supplied one
* @param tracer
*/
export function wrapTracer(tracer) {
return new SugaredTracer(tracer);
}
export class SugaredTracer {
constructor(tracer) {
this._tracer = tracer;
this.startSpan = tracer.startSpan.bind(this._tracer);
this.startActiveSpan = tracer.startActiveSpan.bind(this._tracer);
}
withActiveSpan(name, arg2, arg3, arg4) {
const { opts, ctx, fn } = massageParams(arg2, arg3, arg4);
return this._tracer.startActiveSpan(name, opts, ctx, (span) => handleFn(span, opts, fn));
}
withSpan(name, arg2, arg3, arg4) {
const { opts, ctx, fn } = massageParams(arg2, arg3, arg4);
const span = this._tracer.startSpan(name, opts, ctx);
return handleFn(span, opts, fn);
}
}
/**
* Massages parameters of withSpan and withActiveSpan to allow signature overwrites
* @param arg
* @param arg2
* @param arg3
*/
function massageParams(arg, arg2, arg3) {
let opts;
let ctx;
let fn;
if (!arg2 && !arg3) {
fn = arg;
}
else if (!arg3) {
opts = arg;
fn = arg2;
}
else {
opts = arg;
ctx = arg2;
fn = arg3;
}
opts = opts !== null && opts !== void 0 ? opts : {};
ctx = ctx !== null && ctx !== void 0 ? ctx : context.active();
return { opts, ctx, fn };
}
/**
* Executes fn, returns results and runs onException in the case of exception to allow overwriting of error handling
* @param span
* @param opts
* @param fn
*/
function handleFn(span, opts, fn) {
var _a;
const onException = (_a = opts.onException) !== null && _a !== void 0 ? _a : defaultOnException;
const errorHandler = (e) => {
onException(e, span);
span.end();
throw e;
};
try {
const ret = fn(span);
// if fn is an async function, attach a recordException and spanEnd callback to the promise
if (typeof (ret === null || ret === void 0 ? void 0 : ret.then) === 'function') {
return ret.then(val => {
span.end();
return val;
}, errorHandler);
}
span.end();
return ret;
}
catch (e) {
// add throw to signal the compiler that this will throw in the inner scope
throw errorHandler(e);
}
}
//# sourceMappingURL=SugaredTracer.js.map

File diff suppressed because one or more lines are too long