Skip to content

Commit f93cfd2

Browse files
authored
Merge branch 'main' into feat/console-exporter-resource
2 parents d796d89 + e01f493 commit f93cfd2

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ feat(sdk-trace-base): log resource attributes in ConsoleSpanExporter [#4605](htt
1515

1616
### :bug: (Bug Fix)
1717

18+
* fix(sdk-trace-web): fix invalid timings in span events [#4486](https://github.com/open-telemetry/opentelemetry-js/pull/4486) @Abinet18
19+
1820
### :books: (Refine Doc)
1921

2022
### :house: (Internal)

packages/opentelemetry-sdk-trace-web/src/utils.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,30 @@ export function hasKey<O extends object>(
5656
* @param span
5757
* @param performanceName name of performance entry for time start
5858
* @param entries
59+
* @param refPerfName name of performance entry to use for reference
5960
*/
6061
export function addSpanNetworkEvent(
6162
span: api.Span,
6263
performanceName: string,
63-
entries: PerformanceEntries
64+
entries: PerformanceEntries,
65+
refPerfName?: string
6466
): api.Span | undefined {
67+
let perfTime = undefined;
68+
let refTime = undefined;
6569
if (
6670
hasKey(entries, performanceName) &&
6771
typeof entries[performanceName] === 'number'
6872
) {
69-
span.addEvent(performanceName, entries[performanceName]);
73+
perfTime = entries[performanceName];
74+
}
75+
const refName = refPerfName || PTN.FETCH_START;
76+
// Use a reference time which is the earliest possible value so that the performance timings that are earlier should not be added
77+
// using FETCH START time in case no reference is provided
78+
if (hasKey(entries, refName) && typeof entries[refName] === 'number') {
79+
refTime = entries[refName];
80+
}
81+
if (perfTime !== undefined && refTime !== undefined && perfTime >= refTime) {
82+
span.addEvent(performanceName, perfTime);
7083
return span;
7184
}
7285
return undefined;

packages/opentelemetry-sdk-trace-web/test/utils.test.ts

+77
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,84 @@ describe('utils', () => {
213213
);
214214
});
215215
});
216+
describe('when entries contain invalid performance timing', () => {
217+
it('should only add events with time greater that or equal to reference value to span', () => {
218+
const addEventSpy = sinon.spy();
219+
const span = {
220+
addEvent: addEventSpy,
221+
} as unknown as tracing.Span;
222+
const entries = {
223+
[PTN.FETCH_START]: 123, // default reference time
224+
[PTN.CONNECT_START]: 0,
225+
[PTN.REQUEST_START]: 140,
226+
} as PerformanceEntries;
227+
228+
assert.strictEqual(addEventSpy.callCount, 0);
229+
230+
addSpanNetworkEvent(span, PTN.CONNECT_START, entries);
231+
232+
assert.strictEqual(
233+
addEventSpy.callCount,
234+
0,
235+
'should not call addEvent'
236+
);
237+
238+
addSpanNetworkEvent(span, PTN.REQUEST_START, entries);
239+
240+
assert.strictEqual(
241+
addEventSpy.callCount,
242+
1,
243+
'should call addEvent for valid value'
244+
);
245+
});
246+
});
247+
248+
describe('when entries contain invalid performance timing and a reference event', () => {
249+
it('should only add events with time greater that or equal to reference value to span', () => {
250+
const addEventSpy = sinon.spy();
251+
const span = {
252+
addEvent: addEventSpy,
253+
} as unknown as tracing.Span;
254+
const entries = {
255+
[PTN.FETCH_START]: 120,
256+
[PTN.CONNECT_START]: 120, // this is used as reference time here
257+
[PTN.REQUEST_START]: 10,
258+
} as PerformanceEntries;
259+
260+
assert.strictEqual(addEventSpy.callCount, 0);
261+
262+
addSpanNetworkEvent(
263+
span,
264+
PTN.REQUEST_START,
265+
entries,
266+
PTN.CONNECT_START
267+
);
268+
269+
assert.strictEqual(
270+
addEventSpy.callCount,
271+
0,
272+
'should not call addEvent'
273+
);
274+
275+
addSpanNetworkEvent(span, PTN.FETCH_START, entries, PTN.CONNECT_START);
276+
277+
assert.strictEqual(
278+
addEventSpy.callCount,
279+
1,
280+
'should call addEvent for valid value'
281+
);
282+
283+
addEventSpy.resetHistory();
284+
addSpanNetworkEvent(span, PTN.CONNECT_START, entries, 'foo'); // invalid reference , not adding event to span
285+
assert.strictEqual(
286+
addEventSpy.callCount,
287+
0,
288+
'should not call addEvent for invalid reference(non-existent)'
289+
);
290+
});
291+
});
216292
});
293+
217294
describe('getResource', () => {
218295
const startTime = [0, 123123123] as HrTime;
219296
beforeEach(() => {

0 commit comments

Comments
 (0)