Skip to content

Commit bc0b386

Browse files
committed
add transition to draft to active reporting options
1 parent 9516ed0 commit bc0b386

File tree

3 files changed

+173
-69
lines changed

3 files changed

+173
-69
lines changed

src/v3/Trace.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -1223,15 +1223,33 @@ export class Trace<
12231223
RelationSchemasT,
12241224
VariantsT
12251225
>,
1226-
) {
1226+
opts?: {
1227+
noDraftPresentBehavior: 'error' | 'warning' | 'noop'
1228+
},
1229+
): void {
12271230
const { attributes } = this.input
12281231

12291232
const { relatedTo, errors } = validateAndCoerceRelatedToAgainstSchema(
12301233
inputAndDefinitionModifications.relatedTo,
12311234
this.definition.relationSchema,
12321235
)
1233-
if (errors.length > 0) {
1234-
this.traceUtilities.reportWarningFn(
1236+
1237+
let reportingFunction
1238+
const { noDraftPresentBehavior } = opts ?? {}
1239+
1240+
switch (noDraftPresentBehavior) {
1241+
case 'error':
1242+
reportingFunction = this.traceUtilities.reportErrorFn
1243+
break
1244+
case 'noop':
1245+
reportingFunction = undefined
1246+
break
1247+
default:
1248+
reportingFunction = this.traceUtilities.reportWarningFn
1249+
}
1250+
1251+
if (errors.length > 0 && reportingFunction) {
1252+
reportingFunction(
12351253
new Error(
12361254
`Invalid relatedTo value: ${JSON.stringify(
12371255
inputAndDefinitionModifications.relatedTo,

src/v3/TraceManagerWithDraft.test.ts

+131-62
Original file line numberDiff line numberDiff line change
@@ -187,75 +187,144 @@ describe('TraceManager', () => {
187187
expect(report.duration).toBeNull()
188188
})
189189

190-
it('reports error when calling `transitionDraftToActive` when a draft trace has not yet been created', () => {
191-
const traceManager = new TraceManager({
192-
relationSchemas: { ticket: { ticketId: String } },
193-
reportFn,
194-
generateId,
195-
reportErrorFn,
196-
reportWarningFn,
197-
})
198-
199-
const tracer = traceManager.createTracer({
200-
name: 'ticket.basic-operation',
201-
type: 'operation',
202-
relationSchemaName: 'ticket',
203-
requiredSpans: [{ name: 'end' }],
204-
variants: {
205-
cold_boot: { timeout: DEFAULT_COLDBOOT_TIMEOUT_DURATION },
206-
},
190+
describe('transitionDraftToActive: report errors', () => {
191+
it('reports warning when calling `transitionDraftToActive` with no reporting opts when a draft trace has not yet been created', () => {
192+
const traceManager = new TraceManager({
193+
relationSchemas: { ticket: { ticketId: String } },
194+
reportFn,
195+
generateId,
196+
reportErrorFn,
197+
reportWarningFn,
198+
})
199+
200+
const tracer = traceManager.createTracer({
201+
name: 'ticket.basic-operation',
202+
type: 'operation',
203+
relationSchemaName: 'ticket',
204+
requiredSpans: [{ name: 'end' }],
205+
variants: {
206+
cold_boot: { timeout: DEFAULT_COLDBOOT_TIMEOUT_DURATION },
207+
},
208+
})
209+
210+
tracer.transitionDraftToActive({ relatedTo: { ticketId: '1' } })
211+
212+
expect(reportWarningFn).toHaveBeenCalledWith(
213+
expect.objectContaining({
214+
message: expect.stringContaining(
215+
'No currently active trace when initializing',
216+
),
217+
}),
218+
expect.objectContaining({
219+
definition: expect.any(Object),
220+
}),
221+
)
207222
})
208223

209-
tracer.transitionDraftToActive({ relatedTo: { ticketId: '1' } })
210-
211-
expect(reportErrorFn).toHaveBeenCalledWith(
212-
expect.objectContaining({
213-
message: expect.stringContaining(
214-
'No currently active trace when initializing',
215-
),
216-
}),
217-
expect.objectContaining({
218-
definition: expect.any(Object),
219-
}),
220-
)
221-
})
222-
223-
it('reports warning when calling `transitionDraftToActive` again after a trace is active', () => {
224-
const traceManager = new TraceManager({
225-
relationSchemas: { ticket: { ticketId: String } },
226-
reportFn,
227-
generateId,
228-
reportErrorFn,
229-
reportWarningFn,
224+
it('reports error when calling `transitionDraftToActive` when reporting with error when a draft trace has not yet been created', () => {
225+
const traceManager = new TraceManager({
226+
relationSchemas: { ticket: { ticketId: String } },
227+
reportFn,
228+
generateId,
229+
reportErrorFn,
230+
reportWarningFn,
231+
})
232+
233+
const tracer = traceManager.createTracer({
234+
name: 'ticket.basic-operation',
235+
type: 'operation',
236+
relationSchemaName: 'ticket',
237+
requiredSpans: [{ name: 'end' }],
238+
variants: {
239+
cold_boot: { timeout: DEFAULT_COLDBOOT_TIMEOUT_DURATION },
240+
},
241+
})
242+
243+
tracer.transitionDraftToActive(
244+
{ relatedTo: { ticketId: '1' } },
245+
{ noDraftPresentBehavior: 'error' },
246+
)
247+
248+
expect(reportErrorFn).toHaveBeenCalledWith(
249+
expect.objectContaining({
250+
message: expect.stringContaining(
251+
'No currently active trace when initializing',
252+
),
253+
}),
254+
expect.objectContaining({
255+
definition: expect.any(Object),
256+
}),
257+
)
258+
259+
expect(reportWarningFn).not.toHaveBeenCalled()
230260
})
231261

232-
const tracer = traceManager.createTracer({
233-
name: 'ticket.basic-operation',
234-
type: 'operation',
235-
relationSchemaName: 'ticket',
236-
requiredSpans: [{ name: 'end' }],
237-
variants: {
238-
cold_boot: { timeout: DEFAULT_COLDBOOT_TIMEOUT_DURATION },
239-
},
262+
it('does NOT report anything when calling `transitionDraftToActive` when not reporting when a draft trace has not yet been created', () => {
263+
const traceManager = new TraceManager({
264+
relationSchemas: { ticket: { ticketId: String } },
265+
reportFn,
266+
generateId,
267+
reportErrorFn,
268+
reportWarningFn,
269+
})
270+
271+
const tracer = traceManager.createTracer({
272+
name: 'ticket.basic-operation',
273+
type: 'operation',
274+
relationSchemaName: 'ticket',
275+
requiredSpans: [{ name: 'end' }],
276+
variants: {
277+
cold_boot: { timeout: DEFAULT_COLDBOOT_TIMEOUT_DURATION },
278+
},
279+
})
280+
281+
tracer.transitionDraftToActive(
282+
{ relatedTo: { ticketId: '1' } },
283+
{ noDraftPresentBehavior: 'noop' },
284+
)
285+
286+
expect(reportErrorFn).not.toHaveBeenCalled()
287+
288+
expect(reportWarningFn).not.toHaveBeenCalled()
240289
})
241290

242-
tracer.createDraft({
243-
variant: 'cold_boot',
291+
it('reports warning when calling `transitionDraftToActive` with no report opts again after a trace is active', () => {
292+
const traceManager = new TraceManager({
293+
relationSchemas: { ticket: { ticketId: String } },
294+
reportFn,
295+
generateId,
296+
reportErrorFn,
297+
reportWarningFn,
298+
})
299+
300+
const tracer = traceManager.createTracer({
301+
name: 'ticket.basic-operation',
302+
type: 'operation',
303+
relationSchemaName: 'ticket',
304+
requiredSpans: [{ name: 'end' }],
305+
variants: {
306+
cold_boot: { timeout: DEFAULT_COLDBOOT_TIMEOUT_DURATION },
307+
},
308+
})
309+
310+
tracer.createDraft({
311+
variant: 'cold_boot',
312+
})
313+
314+
tracer.transitionDraftToActive({ relatedTo: { ticketId: '1' } })
315+
tracer.transitionDraftToActive({ relatedTo: { ticketId: '2' } })
316+
317+
expect(reportWarningFn).toHaveBeenCalledWith(
318+
expect.objectContaining({
319+
message: expect.stringContaining(
320+
'trace that has already been initialized',
321+
),
322+
}),
323+
expect.objectContaining({
324+
definition: expect.any(Object),
325+
}),
326+
)
244327
})
245-
246-
tracer.transitionDraftToActive({ relatedTo: { ticketId: '1' } })
247-
tracer.transitionDraftToActive({ relatedTo: { ticketId: '2' } })
248-
249-
expect(reportWarningFn).toHaveBeenCalledWith(
250-
expect.objectContaining({
251-
message: expect.stringContaining(
252-
'trace that has already been initialized',
253-
),
254-
}),
255-
expect.objectContaining({
256-
definition: expect.any(Object),
257-
}),
258-
)
259328
})
260329

261330
it('interrupts a draft trace when interrupt() is called with error', () => {

src/v3/Tracer.ts

+21-4
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,30 @@ export class Tracer<
208208
RelationSchemasT,
209209
VariantsT
210210
>,
211+
opts?: {
212+
noDraftPresentBehavior: 'error' | 'warning' | 'noop'
213+
},
211214
): void => {
212215
const trace:
213216
| Trace<SelectedRelationNameT, RelationSchemasT, VariantsT>
214217
| undefined = this.traceUtilities.getCurrentTrace()
215218

219+
let reportingFunction
220+
const { noDraftPresentBehavior } = opts ?? {}
221+
222+
switch (noDraftPresentBehavior) {
223+
case 'error':
224+
reportingFunction = this.traceUtilities.reportErrorFn
225+
break
226+
case 'noop':
227+
reportingFunction = undefined
228+
break
229+
default:
230+
reportingFunction = this.traceUtilities.reportWarningFn
231+
}
232+
216233
if (!trace) {
217-
this.traceUtilities.reportErrorFn(
234+
reportingFunction?.(
218235
new Error(
219236
`No currently active trace when initializing a trace. Call tracer.start(...) or tracer.createDraft(...) beforehand.`,
220237
),
@@ -226,7 +243,7 @@ export class Tracer<
226243

227244
// this is an already initialized active trace, do nothing:
228245
if (!trace.isDraft) {
229-
this.traceUtilities.reportWarningFn(
246+
reportingFunction?.(
230247
new Error(
231248
`You are trying to initialize a trace that has already been initialized before (${trace.definition.name}).`,
232249
),
@@ -238,15 +255,15 @@ export class Tracer<
238255

239256
// verify that trace is the same definition as the Tracer's definition
240257
if (trace.sourceDefinition !== this.definition) {
241-
this.traceUtilities.reportWarningFn(
258+
reportingFunction?.(
242259
new Error(
243260
`You are trying to initialize a trace that is not the same definition as the Tracer's definition is different.`,
244261
),
245262
)
246263
return
247264
}
248265

249-
trace.transitionDraftToActive(inputAndDefinitionModifications)
266+
trace.transitionDraftToActive(inputAndDefinitionModifications, opts)
250267
}
251268

252269
getCurrentTrace = ():

0 commit comments

Comments
 (0)