Skip to content

Commit 56db9b3

Browse files
xnanodaxniieani
andauthored
feat: computedSpans now allow using matchingIndex (#31)
Co-authored-by: Bazyli Brzoska <[email protected]> Co-authored-by: Cynthia Ma <[email protected]>
1 parent 500c409 commit 56db9b3

6 files changed

+475
-53
lines changed

src/v3/ensureMatcherFn.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,27 @@ export function convertLabelMatchersToFns<
7777
}
7878
return matchers
7979
}
80+
81+
/**
82+
* Helper function to ensure that the matcher is a function or a special token
83+
*/
84+
export function ensureMatcherFnOrSpecialToken<
85+
SelectedRelationNameT extends keyof RelationSchemasT,
86+
RelationSchemasT,
87+
VariantsT extends string,
88+
SpecialToken extends string,
89+
>(
90+
spanMatcher:
91+
| SpanMatch<SelectedRelationNameT, RelationSchemasT, VariantsT>
92+
| SpecialToken,
93+
):
94+
| SpanMatcherFn<SelectedRelationNameT, RelationSchemasT, VariantsT>
95+
| SpecialToken
96+
| undefined {
97+
// Handle string types (special matchers)
98+
if (typeof spanMatcher === 'string') {
99+
return spanMatcher
100+
}
101+
102+
return ensureMatcherFn(spanMatcher)
103+
}

src/v3/matchSpan.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface SpanMatcherTags {
77
idleCheck?: boolean
88
continueWithErrorStatus?: boolean
99
requiredSpan?: boolean
10+
matchingIndex?: number
1011
}
1112

1213
/**
@@ -52,6 +53,11 @@ export interface SpanMatchDefinitionCombinator<
5253
isIdle?: boolean
5354
label?: string
5455
fn?: SpanMatcherFn<SelectedRelationNameT, RelationSchemasT, VariantsT>
56+
/**
57+
* This only has an effect on startSpan and endSpan for defining computed spans
58+
* It must be defined on the top level matcher definition
59+
* */
60+
matchingIndex?: number
5561
}
5662

5763
export type SpanMatchDefinition<
@@ -394,5 +400,11 @@ export function fromDefinition<
394400
matchers.push(definition.fn)
395401
}
396402

397-
return withAllConditions(...matchers)
403+
const combined = withAllConditions(...matchers)
404+
405+
if (typeof definition.matchingIndex === 'number') {
406+
combined.matchingIndex = definition.matchingIndex
407+
}
408+
409+
return combined
398410
}

0 commit comments

Comments
 (0)