-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
also tracer to main.ts
- Loading branch information
Showing
5 changed files
with
195 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { convertTraceToRUM } from './convertToRum' | ||
import { createTraceRecording } from './recordingComputeUtils' | ||
import { ActiveTraceInput } from './spanTypes' | ||
import { | ||
AnyScope, | ||
createMockSpanAndAnnotation, | ||
createTimestamp, | ||
} from './testUtility/createMockFactory' | ||
import type { CompleteTraceDefinition } from './types' | ||
|
||
describe('convertTraceToRUM', () => { | ||
it('should round all numeric values in the trace recording', () => { | ||
const definition: CompleteTraceDefinition<never, AnyScope, 'origin'> = { | ||
name: 'test-trace', | ||
scopes: [], | ||
requiredSpans: [() => true], | ||
computedSpanDefinitions: [], | ||
computedValueDefinitions: [], | ||
variantsByOriginatedFrom: { | ||
origin: { timeoutDuration: 45_000 }, | ||
}, | ||
} | ||
|
||
const input: ActiveTraceInput<{}, 'origin'> = { | ||
id: 'test', | ||
startTime: createTimestamp(0), | ||
scope: {}, | ||
originatedFrom: 'origin', | ||
} | ||
|
||
const traceRecording = createTraceRecording( | ||
{ | ||
definition, | ||
recordedItems: [ | ||
createMockSpanAndAnnotation(100.501, { | ||
name: 'test-component', | ||
type: 'component-render', | ||
scope: {}, | ||
duration: 50.499, | ||
isIdle: false, | ||
renderCount: 1, | ||
renderedOutput: 'loading', | ||
}), | ||
createMockSpanAndAnnotation( | ||
200.001, | ||
{ | ||
name: 'test-component', | ||
type: 'component-render', | ||
scope: {}, | ||
duration: 50.999, | ||
isIdle: true, | ||
renderCount: 2, | ||
renderedOutput: 'content', | ||
}, | ||
{ occurrence: 2 }, | ||
), | ||
], | ||
input: { | ||
id: 'test', | ||
startTime: createTimestamp(0), | ||
scope: { ticketId: '74' }, | ||
originatedFrom: 'origin', | ||
}, | ||
}, | ||
{ transitionFromState: 'active' }, | ||
) | ||
|
||
const context = { | ||
definition, | ||
input, | ||
} | ||
|
||
const result = convertTraceToRUM(traceRecording, context) | ||
|
||
// Check rounded values in embeddedSpans | ||
const embeddedSpan = result.embeddedSpans['component-render|test-component'] | ||
if (embeddedSpan) { | ||
expect(Number.isInteger(embeddedSpan.totalDuration)).toBe(true) | ||
expect(Number.isInteger(embeddedSpan.spans[0]!.startOffset)).toBe(true) | ||
expect(Number.isInteger(embeddedSpan.spans[0]!.duration)).toBe(true) | ||
expect(Number.isInteger(embeddedSpan.spans[1]!.startOffset)).toBe(true) | ||
expect(Number.isInteger(embeddedSpan.spans[1]!.duration)).toBe(true) | ||
|
||
// Check specific rounded values | ||
expect(embeddedSpan.spans[0]!.startOffset).toBe(101) // 100.501 rounded | ||
expect(embeddedSpan.spans[0]!.duration).toBe(50) // 50.499 rounded | ||
expect(embeddedSpan.spans[1]!.startOffset).toBe(200) // 200.001 rounded | ||
expect(embeddedSpan.spans[1]!.duration).toBe(51) // 50.999 rounded | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { SpanAndAnnotation, SpanAnnotation } from '../spanAnnotationTypes' | ||
import { Span } from '../spanTypes' | ||
import type { Timestamp } from '../types' | ||
|
||
const EPOCH_START = 1_000 | ||
|
||
export const createTimestamp = (now: number): Timestamp => ({ | ||
epoch: EPOCH_START + now, | ||
now, | ||
}) | ||
|
||
export type AnyScope = Record<string, unknown> | ||
|
||
export const createAnnotation = ( | ||
span: Span<AnyScope>, | ||
traceStartTime: Timestamp, | ||
partial: Partial<SpanAnnotation> = {}, | ||
): SpanAnnotation => ({ | ||
id: 'test-id', | ||
operationRelativeStartTime: span.startTime.now - traceStartTime.now, | ||
operationRelativeEndTime: | ||
span.startTime.now + span.duration - traceStartTime.now, | ||
occurrence: 1, | ||
recordedInState: 'active', | ||
labels: [], | ||
...partial, | ||
}) | ||
|
||
export const createMockSpan = <TSpan extends Span<AnyScope>>( | ||
startTimeNow: number, | ||
partial: Partial<TSpan>, | ||
): TSpan => | ||
(partial.type === 'component-render' | ||
? { | ||
name: 'test-component', | ||
type: 'component-render', | ||
scope: {}, | ||
startTime: createTimestamp(startTimeNow), | ||
duration: 100, | ||
attributes: {}, | ||
isIdle: true, | ||
renderCount: 1, | ||
renderedOutput: 'content', | ||
...partial, | ||
} | ||
: { | ||
name: 'test-span', | ||
type: 'mark', | ||
startTime: createTimestamp(startTimeNow), | ||
duration: 100, | ||
attributes: {}, | ||
...partial, | ||
}) as TSpan | ||
|
||
export const createMockSpanAndAnnotation = <TSpan extends Span<AnyScope>>( | ||
startTimeNow: number, | ||
spanPartial: Partial<TSpan> = {}, | ||
annotationPartial: Partial<SpanAnnotation> = {}, | ||
): SpanAndAnnotation<AnyScope> => { | ||
const span = createMockSpan<TSpan>(startTimeNow, spanPartial) | ||
return { | ||
span, | ||
annotation: createAnnotation(span, createTimestamp(0), annotationPartial), | ||
} | ||
} |