Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add labelMatchers #17

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/v3/ActiveTrace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -636,9 +636,17 @@ export class ActiveTrace<TracerScopeT extends object, AllPossibleScopesT> {
.currentState as NonTerminalTraceStates,
}

spanAndAnnotation = {
const labels = this.getSpanLabels({
span,
annotation,
})

spanAndAnnotation = {
yojenkins marked this conversation as resolved.
Show resolved Hide resolved
span,
annotation: {
...annotation,
labels,
},
}

this.deduplicationStrategy?.recordSpan(span, spanAndAnnotation)
Expand Down Expand Up @@ -687,6 +695,22 @@ export class ActiveTrace<TracerScopeT extends object, AllPossibleScopesT> {
return undefined
}

private getSpanLabels(span: SpanAndAnnotation<AllPossibleScopesT>): string[] {
const labels: string[] = []
const context = { definition: this.definition, input: this.input }
if (!this.definition.labelMatching) return labels

Object.entries(this.definition.labelMatching).forEach(
([label, matcher]) => {
if (matcher(span, context)) {
labels.push(label)
}
},
)

return labels
}

private prepareAndEmitRecording({
transition,
lastRelevantSpanAndAnnotation,
Expand Down
21 changes: 21 additions & 0 deletions src/v3/ensureMatcherFn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,24 @@ export function convertMatchersToFns<
}
return undefined
}

export function convertLabelMatchersToFns<
TracerScopeT extends AllPossibleScopesT,
AllPossibleScopesT,
>(
definitionLabelMatchers: Record<
string,
SpanMatch<TracerScopeT, AllPossibleScopesT>
>,
): Record<string, SpanMatcherFn<TracerScopeT, AllPossibleScopesT>> {
return Object.keys(definitionLabelMatchers).reduce<
bbrzoska marked this conversation as resolved.
Show resolved Hide resolved
Record<string, SpanMatcherFn<TracerScopeT, AllPossibleScopesT>>
>((acc, key) => {
if (!definitionLabelMatchers?.[key]) return acc
const matchFn = convertMatchersToFns([definitionLabelMatchers[key]])
if (matchFn) {
acc[key] = matchFn[0]
}
return acc
}, {})
}
12 changes: 12 additions & 0 deletions src/v3/matchSpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface SpanMatchDefinition<TracerScopeT, AllPossibleScopesT> {
// IMPLEMENTATION TODO: take in scope as a second parameter
occurrence?: number | ((occurrence: number) => boolean)
isIdle?: boolean
label?: string
}

export type SpanMatch<TracerScopeT, AllPossibleScopesT> =
Expand All @@ -63,6 +64,12 @@ export function withName<
}
}

export function withLabel<TracerScopeT, AllPossibleScopesT>(
value: string,
): SpanMatcherFn<TracerScopeT, AllPossibleScopesT> {
return ({ annotation }) => annotation.labels?.includes(value) ?? false
}

/**
* The PerformanceEntry.name of the entry to match. Can be a string, RegExp, or function.
*/
Expand Down Expand Up @@ -231,5 +238,10 @@ export function fromDefinition<
if (definition.isIdle) {
matchers.push(whenIdle(definition.isIdle))
}

if (definition.label) {
matchers.push(withLabel(definition.label))
}

return withAllConditions(...matchers)
}
4 changes: 4 additions & 0 deletions src/v3/spanAnnotationTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export interface SpanAnnotation {
* If true, this is the span that marked the operation as interactive.
*/
markedInteractive?: boolean
/**
* Optional labels for the span based on label definitions from the Tracer.
*/
labels?: string[]
yojenkins marked this conversation as resolved.
Show resolved Hide resolved
}

export interface SpanAnnotationRecord {
Expand Down
11 changes: 10 additions & 1 deletion src/v3/traceManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { ActiveTrace } from './ActiveTrace'
import { convertMatchersToFns, ensureMatcherFn } from './ensureMatcherFn'
import {
convertLabelMatchersToFns,
convertMatchersToFns,
ensureMatcherFn,
} from './ensureMatcherFn'
import { ensureTimestamp } from './ensureTimestamp'
import { type SpanMatcherFn } from './matchSpan'
import type { SpanAnnotationRecord } from './spanAnnotationTypes'
Expand Down Expand Up @@ -67,6 +71,10 @@ export class TraceManager<
)
}

const labelMatching = traceDefinition.labelMatching
? convertLabelMatchersToFns(traceDefinition.labelMatching)
: undefined

const debounceOn = convertMatchersToFns<TracerScopeT, AllPossibleScopesT>(
traceDefinition.debounceOn,
)
Expand All @@ -90,6 +98,7 @@ export class TraceManager<
suppressErrorStatusPropagationOn,
computedSpanDefinitions,
computedValueDefinitions,
labelMatching,
}

return {
Expand Down
11 changes: 11 additions & 0 deletions src/v3/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ export interface TraceDefinition<TracerScopeT, AllPossibleScopesT> {

scopes: readonly KeysOfUnion<TracerScopeT>[]

// TODO: typing this so that the labels are inferred?
labelMatching?: Record<
string,
SpanMatch<NoInfer<TracerScopeT>, AllPossibleScopesT>
>

/**
* This may include renders spans of components that have to be rendered with all data
* to consider the operation as visually complete
Expand Down Expand Up @@ -167,6 +173,11 @@ export interface CompleteTraceDefinition<TracerScopeT, AllPossibleScopesT>
SpanMatcherFn<TracerScopeT, AllPossibleScopesT>[]
>[]

labelMatching?: Record<
string,
SpanMatcherFn<TracerScopeT, AllPossibleScopesT>
>

requiredToEnd: ArrayWithAtLeastOneElement<
SpanMatcherFn<TracerScopeT, AllPossibleScopesT>
>
Expand Down
Loading