Skip to content

Commit 546a5db

Browse files
tim-smarteffect-bot
authored andcommitted
add Tracer Span.addLinks, for dynamically linking spans (#4465)
1 parent fd50be6 commit 546a5db

File tree

8 files changed

+61
-11
lines changed

8 files changed

+61
-11
lines changed

.changeset/eight-hats-turn.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@effect/opentelemetry": minor
3+
"effect": minor
4+
---
5+
6+
add Tracer Span.addLinks, for dynamically linking spans

packages/effect/src/Effect.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12962,6 +12962,17 @@ export const linkSpans: {
1296212962
): Effect<A, E, R>
1296312963
} = effect.linkSpans
1296412964

12965+
/**
12966+
* Add span links to the current span.
12967+
*
12968+
* @since 3.14.0
12969+
* @category Tracing
12970+
*/
12971+
export const linkSpanCurrent: {
12972+
(span: Tracer.AnySpan, attributes?: Readonly<Record<string, unknown>> | undefined): Effect<void>
12973+
(links: ReadonlyArray<Tracer.SpanLink>): Effect<void>
12974+
} = effect.linkSpanCurrent
12975+
1296512976
/**
1296612977
* Create a new span for tracing.
1296712978
*

packages/effect/src/Tracer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ export interface Span {
121121
end(endTime: bigint, exit: Exit.Exit<unknown, unknown>): void
122122
attribute(key: string, value: unknown): void
123123
event(name: string, startTime: bigint, attributes?: Record<string, unknown>): void
124+
addLinks(links: ReadonlyArray<SpanLink>): void
124125
}
125126

126127
/**

packages/effect/src/internal/core-effect.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1950,6 +1950,21 @@ export const annotateCurrentSpan: {
19501950
))
19511951
}
19521952

1953+
/* @internal */
1954+
export const linkSpanCurrent: {
1955+
(span: Tracer.AnySpan, attributes?: Readonly<Record<string, unknown>> | undefined): Effect.Effect<void>
1956+
(links: ReadonlyArray<Tracer.SpanLink>): Effect.Effect<void>
1957+
} = function(): Effect.Effect<void> {
1958+
const args = arguments
1959+
const links: ReadonlyArray<Tracer.SpanLink> = Array.isArray(args[0])
1960+
? args[0]
1961+
: [{ _tag: "SpanLink", span: args[0], attributes: args[1] ?? {} }]
1962+
return ignore(core.flatMap(
1963+
currentSpan,
1964+
(span) => core.sync(() => span.addLinks(links))
1965+
))
1966+
}
1967+
19531968
/* @internal */
19541969
export const annotateSpans = dual<
19551970
{

packages/effect/src/internal/core.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3147,7 +3147,8 @@ const NoopSpanProto: Omit<Tracer.Span, "parent" | "name" | "context"> = {
31473147
kind: "internal",
31483148
attribute() {},
31493149
event() {},
3150-
end() {}
3150+
end() {},
3151+
addLinks() {}
31513152
}
31523153

31533154
/** @internal */

packages/effect/src/internal/tracer.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ export class NativeSpan implements Tracer.Span {
4444
status: Tracer.SpanStatus
4545
attributes: Map<string, unknown>
4646
events: Array<[name: string, startTime: bigint, attributes: Record<string, unknown>]> = []
47+
links: Array<Tracer.SpanLink>
4748

4849
constructor(
4950
readonly name: string,
5051
readonly parent: Option.Option<Tracer.AnySpan>,
5152
readonly context: Context.Context<never>,
52-
readonly links: ReadonlyArray<Tracer.SpanLink>,
53+
links: Iterable<Tracer.SpanLink>,
5354
readonly startTime: bigint,
5455
readonly kind: Tracer.SpanKind
5556
) {
@@ -60,6 +61,7 @@ export class NativeSpan implements Tracer.Span {
6061
this.attributes = new Map()
6162
this.traceId = parent._tag === "Some" ? parent.value.traceId : randomHexString(32)
6263
this.spanId = randomHexString(16)
64+
this.links = Array.from(links)
6365
}
6466

6567
end(endTime: bigint, exit: Exit.Exit<unknown, unknown>): void {
@@ -78,6 +80,11 @@ export class NativeSpan implements Tracer.Span {
7880
event(name: string, startTime: bigint, attributes?: Record<string, unknown>): void {
7981
this.events.push([name, startTime, attributes ?? {}])
8082
}
83+
84+
addLinks(links: ReadonlyArray<Tracer.SpanLink>): void {
85+
// eslint-disable-next-line no-restricted-syntax
86+
this.links.push(...links)
87+
}
8188
}
8289

8390
/** @internal */

packages/opentelemetry/package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@
4747
"coverage": "vitest --coverage"
4848
},
4949
"peerDependencies": {
50-
"@opentelemetry/api": "^1.6",
51-
"@opentelemetry/resources": "^1.22",
52-
"@opentelemetry/sdk-metrics": "^1.22",
53-
"@opentelemetry/sdk-trace-base": "^1.22",
54-
"@opentelemetry/sdk-trace-node": "^1.22",
55-
"@opentelemetry/sdk-trace-web": "^1.22",
56-
"@opentelemetry/semantic-conventions": "^1.24.1",
50+
"@opentelemetry/api": "^1.9",
51+
"@opentelemetry/resources": "^1.25",
52+
"@opentelemetry/sdk-metrics": "^1.25",
53+
"@opentelemetry/sdk-trace-base": "^1.25",
54+
"@opentelemetry/sdk-trace-node": "^1.25",
55+
"@opentelemetry/sdk-trace-web": "^1.25",
56+
"@opentelemetry/semantic-conventions": "^1.25",
5757
"effect": "workspace:^"
5858
},
5959
"peerDependenciesMeta": {

packages/opentelemetry/src/internal/tracer.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class OtelSpan implements EffectTracer.Span {
3939
readonly name: string,
4040
readonly parent: Option.Option<EffectTracer.AnySpan>,
4141
readonly context: Context.Context<never>,
42-
readonly links: ReadonlyArray<EffectTracer.SpanLink>,
42+
readonly links: Array<EffectTracer.SpanLink>,
4343
startTime: bigint,
4444
readonly kind: EffectTracer.SpanKind
4545
) {
@@ -76,6 +76,15 @@ export class OtelSpan implements EffectTracer.Span {
7676
this.attributes.set(key, value)
7777
}
7878

79+
addLinks(links: ReadonlyArray<EffectTracer.SpanLink>): void {
80+
// eslint-disable-next-line no-restricted-syntax
81+
this.links.push(...links)
82+
this.span.addLinks(links.map((link) => ({
83+
context: makeSpanContext(link.span),
84+
attributes: recordToAttributes(link.attributes)
85+
})))
86+
}
87+
7988
end(endTime: bigint, exit: Exit<unknown, unknown>) {
8089
const hrTime = nanosToHrTime(endTime)
8190
this.status = {
@@ -141,7 +150,7 @@ export const make = Effect.map(Tracer, (tracer) =>
141150
name,
142151
parent,
143152
context,
144-
links,
153+
links.slice(),
145154
startTime,
146155
kind
147156
)

0 commit comments

Comments
 (0)