-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathclient.ts
63 lines (53 loc) · 2.13 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import type { Tracer } from '@opentelemetry/api';
import { trace } from '@opentelemetry/api';
import type { BasicTracerProvider } from '@opentelemetry/sdk-trace-base';
import type { Client } from '@sentry/core';
import { SDK_VERSION } from '@sentry/core';
import type { OpenTelemetryClient as OpenTelemetryClientInterface } from '../types';
// Typescript complains if we do not use `...args: any[]` for the mixin, with:
// A mixin class must have a constructor with a single rest parameter of type 'any[]'.ts(2545)
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Wrap a Client class with things we need for OpenTelemetry support.
* Make sure that the Client class passed in is non-abstract!
*
* Usage:
* const OpenTelemetryClient = getWrappedClientClass(NodeClient);
* const client = new OpenTelemetryClient(options);
*/
export function wrapClientClass<
ClassConstructor extends new (...args: any[]) => Client,
WrappedClassConstructor extends new (...args: any[]) => Client & OpenTelemetryClientInterface,
>(ClientClass: ClassConstructor): WrappedClassConstructor {
// @ts-expect-error We just assume that this is non-abstract, if you pass in an abstract class this would make it non-abstract
class OpenTelemetryClient extends ClientClass implements OpenTelemetryClientInterface {
public traceProvider: BasicTracerProvider | undefined;
private _tracer: Tracer | undefined;
public constructor(...args: any[]) {
super(...args);
}
/** Get the OTEL tracer. */
public get tracer(): Tracer {
if (this._tracer) {
return this._tracer;
}
const name = '@sentry/opentelemetry';
const version = SDK_VERSION;
const tracer = trace.getTracer(name, version);
this._tracer = tracer;
return tracer;
}
/**
* @inheritDoc
*/
public async flush(timeout?: number): Promise<boolean> {
const provider = this.traceProvider;
if (provider) {
await provider.forceFlush();
}
return super.flush(timeout);
}
}
return OpenTelemetryClient as unknown as WrappedClassConstructor;
}
/* eslint-enable @typescript-eslint/no-explicit-any */