Skip to content

Commit

Permalink
fix: add cancelDraftTrace, add instance methods to tracer
Browse files Browse the repository at this point in the history
  • Loading branch information
xnanodax committed Feb 11, 2025
1 parent 01b3326 commit 2a921d4
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/v3/recordingComputeUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {
getComputedSpans,
getComputedValues,
} from './recordingComputeUtils'
import type { CompleteTraceDefinition } from './types'
import {
createMockSpanAndAnnotation,
createTimestamp,
} from './testUtility/createMockFactory'
import type { CompleteTraceDefinition } from './types'

type AnyScope = Record<string, unknown>

Expand Down
1 change: 1 addition & 0 deletions src/v3/traceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class TraceManager<
// warn on miss?
},
getActiveTrace: () => this.activeTrace,
cancelDraftTrace: () => this.activeTrace?.interrupt('draft-cancelled'),
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/v3/traceManagerWithDraft.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ describe('TraceManager', () => {
`
processSpans(spans, traceManager)

// tracer.transitionDraftToActive({ scope: { ticketId: '1' } })

expect(reportFn).toHaveBeenCalled()

const report: Parameters<ReportFn<TicketScope, TicketScope>>[0] =
Expand Down
24 changes: 14 additions & 10 deletions src/v3/tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,22 @@ export class Tracer<
/**
* @returns The ID of the trace.
*/
start(
start = (
input: StartTraceConfig<
SelectScopeByKey<TracerScopeKeysT, AllPossibleScopesT>,
OriginatedFromT
>,
): string | undefined {
): string | undefined => {
const traceId = this.createDraft(input)
if (!traceId) return undefined

this.transitionDraftToActive({ scope: input.scope })
return traceId
}

createDraft(
createDraft = (
input: BaseStartTraceConfig<OriginatedFromT>,
): string | undefined {
): string | undefined => {
const id = input.id ?? this.traceUtilities.generateId()

// Verify that the originatedFrom value is valid and has a corresponding timeout
Expand Down Expand Up @@ -100,17 +100,21 @@ export class Tracer<
return id
}

cancelDraft = () => {
this.traceUtilities.cancelDraftTrace()
}

// can have config changed until we move into active
// from input: scope (required), attributes (optional, merge into)
// from definition, can add items to: requiredSpans (additionalRequiredSpans), debounceOn (additionalDebounceOnSpans)
// documentation: interruption still works and all the other events are buffered
transitionDraftToActive(
transitionDraftToActive = (
inputAndDefinitionModifications: TraceModifications<
TracerScopeKeysT,
AllPossibleScopesT,
OriginatedFromT
>,
): void {
): void => {
const activeTrace = this.traceUtilities.getActiveTrace()
if (!activeTrace) {
this.traceUtilities.reportErrorFn(
Expand Down Expand Up @@ -140,13 +144,13 @@ export class Tracer<
typedActiveTrace.transitionDraftToActive(inputAndDefinitionModifications)
}

defineComputedSpan(
defineComputedSpan = (
definition: ComputedSpanDefinitionInput<
TracerScopeKeysT,
AllPossibleScopesT,
OriginatedFromT
>,
): void {
): void => {
this.definition.computedSpanDefinitions.push({
...definition,
startSpan:
Expand All @@ -168,7 +172,7 @@ export class Tracer<
})
}

defineComputedValue<
defineComputedValue = <
MatchersT extends (
| SpanMatcherFn<TracerScopeKeysT, AllPossibleScopesT, OriginatedFromT>
| SpanMatchDefinition<TracerScopeKeysT, AllPossibleScopesT>
Expand All @@ -180,7 +184,7 @@ export class Tracer<
MatchersT,
OriginatedFromT
>,
): void {
): void => {
this.definition.computedValueDefinitions.push({
...definition,
matches: definition.matches.map((m) =>
Expand Down
2 changes: 2 additions & 0 deletions src/v3/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export type TraceInterruptionReason =
| 'idle-component-no-longer-idle'
| 'matched-on-interrupt'
| 'invalid-state-transition'
| 'draft-cancelled'

export type SingleTraceReportFn<
TracerScopeKeysT extends KeysOfUnion<AllPossibleScopesT>,
Expand Down Expand Up @@ -102,6 +103,7 @@ export interface TraceManagerUtilities<AllPossibleScopesT>
traceToCleanUp: AllPossibleActiveTraces<AllPossibleScopesT>,
) => void
getActiveTrace: () => AllPossibleActiveTraces<AllPossibleScopesT> | undefined
cancelDraftTrace: () => void
}

export interface TraceModifications<
Expand Down

0 comments on commit 2a921d4

Please sign in to comment.