-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlaunchdarkly.ts
74 lines (63 loc) · 2.1 KB
/
launchdarkly.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
64
65
66
67
68
69
70
71
72
73
74
import { BasePlugin } from '@opentelemetry/core';
import { SpanKind, diag } from '@opentelemetry/api';
import type * as ldTypes from 'launchdarkly-node-server-sdk';
import * as shimmer from 'shimmer';
import { VERSION } from './version';
export class LaunchDarklyNodeServerPlugin extends BasePlugin<typeof ldTypes> {
static readonly COMPONENT = 'launchdarkly-node-server-sdk';
static readonly COMPONENT_SHORT = 'launchdarkly-node';
readonly supportedVersions = ['5.14.1', '5.14.1'];
constructor(readonly moduleName: string) {
super('@opentelemetry/plugin-launchdarkly', VERSION);
}
protected patch() {
if (this._moduleExports) {
diag.debug('patching launchdarkly');
shimmer.wrap(this._moduleExports, 'init', this._getInitPatch.bind(this));
}
return this._moduleExports;
}
protected unpatch(): void {
if (this._moduleExports) {
shimmer.unwrap(this._moduleExports, 'init');
}
}
private _getVariationPatch(original: Function) {
const instrumentation = this;
diag.debug('Patching variation function');
return async function variation(this: any) {
const span = instrumentation._tracer.startSpan(
'launchdarkly - variation',
{
kind: SpanKind.CLIENT,
attributes: {
component: LaunchDarklyNodeServerPlugin.COMPONENT_SHORT,
},
}
);
const value = await original.apply(this, arguments);
span.setAttributes({
'launchdarkly.com.flag': arguments[0],
'launchdarkly.com.value': value,
'launchdarkly.com.user': JSON.stringify(arguments[1]),
});
span.end();
return value;
};
}
private _getInitPatch(original: (options?: any) => any) {
const instrumentation = this;
return function init(this: any, opts?: any) {
const newClient = original.apply(this, [opts]);
shimmer.wrap(
newClient,
'variation',
instrumentation._getVariationPatch.bind(instrumentation)
);
return newClient;
};
}
}
export const plugin = new LaunchDarklyNodeServerPlugin(
LaunchDarklyNodeServerPlugin.COMPONENT
);