Skip to content

Commit

Permalink
fix: commit to keep current process, update types, create Tracer
Browse files Browse the repository at this point in the history
  • Loading branch information
xnanodax committed Jan 29, 2025
1 parent 07a4c93 commit 6436bcd
Show file tree
Hide file tree
Showing 9 changed files with 352 additions and 280 deletions.
38 changes: 29 additions & 9 deletions src/v3/ActiveTrace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ interface StateHandlersBase<AllPossibleScopesT> {
[handler: string]: (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
payload: any,
) => void | undefined | Transition<AllPossibleScopesT>
) =>
| void
| undefined
| (Transition<AllPossibleScopesT> & { transitionFromState?: never })
}

type StatesBase<AllPossibleScopesT> = Record<
Expand Down Expand Up @@ -244,7 +247,7 @@ export class TraceStateMachine<
#provisionalBuffer: SpanAndAnnotation<AllPossibleScopesT>[] = []

// eslint-disable-next-line consistent-return
#processProvisionalBuffer(): OnEnterStatePayload<AllPossibleScopesT> | void {
#processProvisionalBuffer(): Transition<AllPossibleScopesT> | void {
// process items in the buffer (stick the scope in the entries) (if its empty, well we can skip this!)
let span: SpanAndAnnotation<AllPossibleScopesT> | undefined
// eslint-disable-next-line no-cond-assign
Expand All @@ -264,9 +267,9 @@ export class TraceStateMachine<
]!.timeoutDuration,
)
},

onActive: () => ({
transitionToState: 'active',
transitionFromState: INITIAL_STATE,
}),

onProcessSpan: (
Expand Down Expand Up @@ -863,12 +866,13 @@ export class TraceStateMachine<
>
>
const transitionPayload = currentStateHandlers[event]?.(payload)
console.log('transitionPayload', transitionPayload)

Check warning on line 869 in src/v3/ActiveTrace.ts

View workflow job for this annotation

GitHub Actions / Test on node 22

Unexpected console statement
if (transitionPayload) {
const transitionFromState = this.currentState as NonTerminalTraceStates
this.currentState = transitionPayload.transitionToState
const onEnterStateEvent: OnEnterStatePayload<AllPossibleScopesT> = {
transitionFromState,
...transitionPayload,
transitionFromState,
}
return this.emit('onEnterState', onEnterStateEvent) ?? onEnterStateEvent
}
Expand Down Expand Up @@ -1022,18 +1026,21 @@ export class ActiveTrace<
this.stateMachine.emit('onInterrupt', reason)
}

transitionDraftToActive(
transitionDraftToActive({
inputAndDefinitionModifications,
onEnd,
}: {
inputAndDefinitionModifications: TraceModifications<
TracerScopeKeysT,
AllPossibleScopesT,
OriginatedFromT
>,
>
onEnd: SingleTraceReportFn<
TracerScopeKeysT,
AllPossibleScopesT,
OriginatedFromT
>,
) {
>
}) {
const { attributes } = this.draftInput

this.input = {
Expand Down Expand Up @@ -1198,10 +1205,11 @@ export class ActiveTrace<
endOfOperationSpan.span.duration,
)
: [...this.recordedItems],
input: this.input,
input: this.draftInput,
},
transition,
)
// TODO: move the onEnd being ActiveTrace
this.input.onEnd(traceRecording, this)

// memory clean-up in case something retains the ActiveTrace instance
Expand All @@ -1212,3 +1220,15 @@ export class ActiveTrace<
}
}
}

export type AllPossibleActiveTraces<
ForEachPossibleScopeT,
AllPossibleScopesT = ForEachPossibleScopeT,
> = ForEachPossibleScopeT extends ForEachPossibleScopeT
? ActiveTrace<
KeysOfUnion<ForEachPossibleScopeT> & KeysOfUnion<AllPossibleScopesT>,
AllPossibleScopesT,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
any
>
: never
2 changes: 0 additions & 2 deletions src/v3/convertToRum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ export interface SpanSummaryAttributes {

export interface RumTraceRecording<TracerScopeT>
extends TraceRecordingBase<TracerScopeT> {
scope: TracerScopeT

// spans that don't exist as separate spans in the DB
// useful for things like renders, which can repeat tens of times
// during the same operation
Expand Down
30 changes: 26 additions & 4 deletions src/v3/recordingComputeUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-continue */
import type { FinalState } from './ActiveTrace'
import type { SpanAndAnnotation } from './spanAnnotationTypes'
import type { ActiveTraceInput } from './spanTypes'
import type { ActiveTraceInput, DraftTraceInput } from './spanTypes'
import type { TraceRecording } from './traceRecordingTypes'
import type {
PossibleScopeObject,
Expand Down Expand Up @@ -303,6 +303,27 @@ function getComputedRenderBeaconSpans<
return computedRenderBeaconSpans
}

function isActiveTraceInput<
TracerScopeKeysT extends KeysOfUnion<AllPossibleScopesT>,
AllPossibleScopesT,
const OriginatedFromT extends string,
>(
input:
| DraftTraceInput<
SelectScopeByKey<TracerScopeKeysT, AllPossibleScopesT>,
OriginatedFromT
>
| ActiveTraceInput<
SelectScopeByKey<TracerScopeKeysT, AllPossibleScopesT>,
OriginatedFromT
>,
): input is ActiveTraceInput<
SelectScopeByKey<TracerScopeKeysT, AllPossibleScopesT>,
OriginatedFromT
> {
return Boolean(input.scope)
}

export function createTraceRecording<
TracerScopeKeysT extends KeysOfUnion<AllPossibleScopesT>,
AllPossibleScopesT,
Expand All @@ -329,9 +350,10 @@ export function createTraceRecording<
interruptionReason && transitionFromState !== 'waiting-for-interactive'
const computedSpans = !wasInterrupted ? getComputedSpans(data) : {}
const computedValues = !wasInterrupted ? getComputedValues(data) : {}
const computedRenderBeaconSpans = !wasInterrupted
? getComputedRenderBeaconSpans(recordedItems, input)
: {}
const computedRenderBeaconSpans =
!wasInterrupted && isActiveTraceInput(input)
? getComputedRenderBeaconSpans(recordedItems, input)
: {}

const anyNonSuppressedErrors = recordedItems.some(
(spanAndAnnotation) =>
Expand Down
Loading

0 comments on commit 6436bcd

Please sign in to comment.