-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathopen-telemetry-hook.spec.ts
164 lines (135 loc) · 4.46 KB
/
open-telemetry-hook.spec.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { EvaluationDetails, HookContext } from '@openfeature/nodejs-sdk';
const setAttributes = jest.fn();
const setAttribute = jest.fn();
const recordException = jest.fn();
const end = jest.fn();
const startSpan = jest.fn(() => ({
setAttributes,
setAttribute,
recordException,
end,
}));
const getTracer = jest.fn(() => ({ startSpan }));
jest.mock('@opentelemetry/api', () => ({
trace: {
getTracer,
},
}));
// Import must be after the mocks
import { OpenTelemetryHook } from './open-telemetry-hook';
describe('OpenTelemetry Hooks', () => {
const hookContext: HookContext = {
flagKey: 'testFlagKey',
clientMetadata: {
name: 'testClient',
},
providerMetadata: {
name: 'testProvider',
},
context: {},
defaultValue: true,
flagValueType: 'boolean',
};
let otelHook: OpenTelemetryHook;
beforeEach(() => {
otelHook = new OpenTelemetryHook();
});
afterEach(() => {
jest.clearAllMocks();
});
it('should use the same span with all the hooks', () => {
const evaluationDetails: EvaluationDetails<boolean> = {
flagKey: hookContext.flagKey,
value: true,
};
const setSpanMapSpy = jest.spyOn(otelHook['spanMap'], 'set');
const testError = new Error();
otelHook.before(hookContext);
expect(setSpanMapSpy).toBeCalled();
otelHook.after(hookContext, evaluationDetails);
expect(setAttribute).toBeCalledWith('feature_flag.evaluated_value', 'true');
otelHook.error(hookContext, testError);
expect(recordException).toBeCalledWith(testError);
otelHook.finally(hookContext);
expect(end).toBeCalled();
});
describe('before hook', () => {
it('should start a new span', () => {
expect(otelHook.before(hookContext)).toBeUndefined();
expect(getTracer).toBeCalled();
expect(startSpan).toBeCalledWith('testProvider testFlagKey', {
attributes: {
'feature_flag.flag_key': 'testFlagKey',
'feature_flag.provider_name': 'testProvider',
},
});
expect(otelHook['spanMap'].has(hookContext)).toBeTruthy;
});
});
describe('after hook', () => {
it('should set the variant as a span attribute', () => {
const evaluationDetails: EvaluationDetails<boolean> = {
flagKey: hookContext.flagKey,
value: true,
variant: 'enabled',
}; // The before hook should
otelHook.before(hookContext);
otelHook.after(hookContext, evaluationDetails);
expect(setAttribute).toBeCalledWith(
'feature_flag.evaluated_variant',
'enabled'
);
});
it('should set the value as a span attribute', () => {
const evaluationDetails: EvaluationDetails<boolean> = {
flagKey: hookContext.flagKey,
value: true,
}; // The before hook should
otelHook.before(hookContext);
otelHook.after(hookContext, evaluationDetails);
expect(setAttribute).toBeCalledWith(
'feature_flag.evaluated_value',
'true'
);
});
it('should set the value without extra quotes if value is already string', () => {
const evaluationDetails: EvaluationDetails<string> = {
flagKey: hookContext.flagKey,
value: 'already-string',
};
otelHook.before(hookContext);
otelHook.after(hookContext, evaluationDetails);
expect(setAttribute).toBeCalledWith(
'feature_flag.evaluated_value',
'already-string'
);
});
});
describe('error hook', () => {
const testError = new Error();
it('should not call recordException because the span is undefined', () => {
otelHook.error(hookContext, testError);
expect(otelHook['spanMap'].has(hookContext)).toBeFalsy;
expect(recordException).not.toBeCalledWith(testError);
});
it('should call recordException with a test error', () => {
otelHook.before(hookContext);
otelHook.error(hookContext, testError);
expect(otelHook['spanMap'].has(hookContext)).toBeTruthy;
expect(recordException).toBeCalledWith(testError);
});
});
describe('finally hook', () => {
it('should not call end because the span is undefined', () => {
otelHook.finally(hookContext);
expect(otelHook['spanMap'].has(hookContext)).toBeFalsy;
expect(end).not.toBeCalled();
});
it('should call end to finish the span', () => {
otelHook.before(hookContext);
otelHook.finally(hookContext);
expect(otelHook['spanMap'].has(hookContext)).toBeTruthy;
expect(end).toBeCalled();
});
});
});