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(opentelemetry): add withActiveSpan function to attach Effect to current Span #3705

Merged
merged 6 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
19 changes: 19 additions & 0 deletions .changeset/gorgeous-llamas-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
"@effect/opentelemetry": patch
---

add withActiveSpan function to attach Effect to current Span

This function allows you to connect the Effect spans into a parent span
that was created outside of Effect, using the OpenTelemetry context propagation:

```ts
Effect.gen(function*() {
yield* Effect.sleep("100 millis").pipe(Effect.withSpan("sleep"));
yield* Console.log("done");
}).pipe(
Effect.withSpan("program"),
// This connects child spans to the current OpenTelemetry context
Tracer.withActiveSpan,
)
```
12 changes: 12 additions & 0 deletions packages/opentelemetry/src/Tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,15 @@ export const TraceFlags: Tag<Otel.TraceFlags, Otel.TraceFlags> = internal.traceF
* @category tags
*/
export const TraceState: Tag<Otel.TraceState, Otel.TraceState> = internal.traceStateTag

/**
* Attach the provided Effect to the current Span as reported from OpenTelemetry's
* context propagation.
*
* This is handy when you set up OpenTelemetry outside of Effect and want to
* attach to a parent span.
*
* @since 0.37.5
* @category effects
tim-smart marked this conversation as resolved.
Show resolved Hide resolved
*/
export const withActiveSpan: <A, E, R>(effect: Effect<A, E, R>) => Effect<A, E, R> = internal.withActiveSpan
10 changes: 10 additions & 0 deletions packages/opentelemetry/src/internal/tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,13 @@ const unknownToAttributeValue = (value: unknown): OtelApi.AttributeValue => {
}
return Inspectable.toStringUnknown(value)
}

export const withActiveSpan = <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>
Effect.suspend(() => {
const activeSpan = OtelApi.trace.getActiveSpan()
if (!activeSpan) {
return effect
}
const span = makeExternalSpan(activeSpan.spanContext())
return Effect.withParentSpan(effect, span)
})
33 changes: 32 additions & 1 deletion packages/opentelemetry/test/Tracer.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { currentOtelSpan, OtelSpan } from "@effect/opentelemetry/internal/tracer"
import { currentOtelSpan, OtelSpan, withActiveSpan } from "@effect/opentelemetry/internal/tracer"
import * as NodeSdk from "@effect/opentelemetry/NodeSdk"
import * as it from "@effect/vitest"
import * as OtelApi from "@opentelemetry/api"
import { AsyncHooksContextManager } from "@opentelemetry/context-async-hooks"
import { InMemorySpanExporter, SimpleSpanProcessor } from "@opentelemetry/sdk-trace-base"
import * as Effect from "effect/Effect"
import * as Runtime from "effect/Runtime"
import { assert, describe, expect } from "vitest"

const TracingLive = NodeSdk.layer(Effect.sync(() => ({
Expand Down Expand Up @@ -76,6 +77,36 @@ describe("Tracer", () => {
),
TracingLive
))

it.scoped("withActiveSpan", () =>
Effect.gen(function*() {
const effect = Effect.gen(function*() {
const span = yield* Effect.currentParentSpan
assert(span._tag === "Span")
const parent = yield* span.parent
return parent
}).pipe(Effect.withSpan("child"), withActiveSpan)

const runtime = yield* Effect.runtime()

yield* Effect.promise(async () => {
await OtelApi.trace.getTracer("test").startActiveSpan("otel-span", {
root: true,
attributes: { "root": "yes" }
}, async (span) => {
try {
const parent = await Runtime.runPromise(runtime)(effect)
const { spanId, traceId } = span.spanContext()
expect(parent).toMatchObject({
spanId,
traceId
})
} finally {
span.end()
}
})
})
}).pipe(Effect.provide(TracingLive)))
})

describe("not provided", () => {
Expand Down