Skip to content

Commit

Permalink
add unique identifier to Tracer.ParentSpan tag (#2459)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored and mikearnaldi committed Apr 16, 2024
1 parent bf9f71f commit be9d025
Show file tree
Hide file tree
Showing 17 changed files with 76 additions and 63 deletions.
5 changes: 5 additions & 0 deletions .changeset/many-frogs-impress.md
@@ -0,0 +1,5 @@
---
"effect": minor
---

add unique identifier to Tracer.ParentSpan tag
4 changes: 2 additions & 2 deletions packages/effect/src/Channel.ts
Expand Up @@ -2107,7 +2107,7 @@ export const withSpan: {
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.ParentSpan | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
} | undefined
Expand All @@ -2120,7 +2120,7 @@ export const withSpan: {
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.ParentSpan | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
} | undefined
Expand Down
24 changes: 12 additions & 12 deletions packages/effect/src/Effect.ts
Expand Up @@ -5126,7 +5126,7 @@ export const currentSpan: Effect<Tracer.Span, Cause.NoSuchElementException> = ef
* @since 2.0.0
* @category tracing
*/
export const currentParentSpan: Effect<Tracer.ParentSpan, Cause.NoSuchElementException> = effect.currentParentSpan
export const currentParentSpan: Effect<Tracer.AnySpan, Cause.NoSuchElementException> = effect.currentParentSpan

/**
* @since 2.0.0
Expand All @@ -5148,12 +5148,12 @@ export const spanLinks: Effect<Chunk.Chunk<Tracer.SpanLink>> = effect.spanLinks
*/
export const linkSpans: {
(
span: Tracer.ParentSpan,
span: Tracer.AnySpan,
attributes?: Record<string, unknown>
): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, R>
<A, E, R>(
self: Effect<A, E, R>,
span: Tracer.ParentSpan,
span: Tracer.AnySpan,
attributes?: Record<string, unknown>
): Effect<A, E, R>
} = effect.linkSpans
Expand All @@ -5169,7 +5169,7 @@ export const makeSpan: (
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.ParentSpan | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
}
Expand All @@ -5190,7 +5190,7 @@ export const makeSpanScoped: (
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.ParentSpan | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
}
Expand All @@ -5213,7 +5213,7 @@ export const useSpan: {
options: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.ParentSpan | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
},
Expand All @@ -5233,7 +5233,7 @@ export const withSpan: {
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.ParentSpan | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
} | undefined
Expand All @@ -5244,7 +5244,7 @@ export const withSpan: {
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.ParentSpan | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
} | undefined
Expand All @@ -5265,7 +5265,7 @@ export const withSpanScoped: {
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.ParentSpan | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
}
Expand All @@ -5276,7 +5276,7 @@ export const withSpanScoped: {
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.ParentSpan | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
}
Expand All @@ -5290,8 +5290,8 @@ export const withSpanScoped: {
* @category tracing
*/
export const withParentSpan: {
(span: Tracer.ParentSpan): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, Exclude<R, Tracer.ParentSpan>>
<A, E, R>(self: Effect<A, E, R>, span: Tracer.ParentSpan): Effect<A, E, Exclude<R, Tracer.ParentSpan>>
(span: Tracer.AnySpan): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, Exclude<R, Tracer.ParentSpan>>
<A, E, R>(self: Effect<A, E, R>, span: Tracer.AnySpan): Effect<A, E, Exclude<R, Tracer.ParentSpan>>
} = effect.withParentSpan

// -------------------------------------------------------------------------------------
Expand Down
12 changes: 6 additions & 6 deletions packages/effect/src/Layer.ts
Expand Up @@ -896,7 +896,7 @@ export const setConfigProvider: (configProvider: ConfigProvider) => Layer<never>
* @since 2.0.0
* @category tracing
*/
export const parentSpan: (span: Tracer.ParentSpan) => Layer<Tracer.ParentSpan> = circularLayer.parentSpan
export const parentSpan: (span: Tracer.AnySpan) => Layer<Tracer.ParentSpan> = circularLayer.parentSpan

/**
* @since 2.0.0
Expand Down Expand Up @@ -962,7 +962,7 @@ export const span: (
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.ParentSpan | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
readonly onEnd?:
Expand Down Expand Up @@ -1018,7 +1018,7 @@ export const withSpan: {
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.ParentSpan | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
readonly onEnd?:
Expand All @@ -1032,7 +1032,7 @@ export const withSpan: {
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.ParentSpan | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
readonly onEnd?:
Expand All @@ -1047,8 +1047,8 @@ export const withSpan: {
* @category tracing
*/
export const withParentSpan: {
(span: Tracer.ParentSpan): <A, E, R>(self: Layer<A, E, R>) => Layer<A, E, Exclude<R, Tracer.ParentSpan>>
<A, E, R>(self: Layer<A, E, R>, span: Tracer.ParentSpan): Layer<A, E, Exclude<R, Tracer.ParentSpan>>
(span: Tracer.AnySpan): <A, E, R>(self: Layer<A, E, R>) => Layer<A, E, Exclude<R, Tracer.ParentSpan>>
<A, E, R>(self: Layer<A, E, R>, span: Tracer.AnySpan): Layer<A, E, Exclude<R, Tracer.ParentSpan>>
} = internal.withParentSpan

// -----------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions packages/effect/src/Stream.ts
Expand Up @@ -4025,7 +4025,7 @@ export const withSpan: {
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.ParentSpan | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
} | undefined
Expand All @@ -4036,7 +4036,7 @@ export const withSpan: {
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.ParentSpan | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
} | undefined
Expand Down
18 changes: 13 additions & 5 deletions packages/effect/src/Tracer.ts
Expand Up @@ -26,7 +26,7 @@ export interface Tracer {
readonly [TracerTypeId]: TracerTypeId
span(
name: string,
parent: Option.Option<ParentSpan>,
parent: Option.Option<AnySpan>,
context: Context.Context<never>,
links: ReadonlyArray<SpanLink>,
startTime: bigint
Expand All @@ -52,13 +52,21 @@ export type SpanStatus = {
* @since 2.0.0
* @category models
*/
export type ParentSpan = Span | ExternalSpan
export type AnySpan = Span | ExternalSpan

/**
* @since 2.0.0
* @category tags
*/
export const ParentSpan: Context.Tag<ParentSpan, ParentSpan> = internal.spanTag
export interface ParentSpan {
readonly _: unique symbol
}

/**
* @since 2.0.0
* @category tags
*/
export const ParentSpan: Context.Tag<ParentSpan, AnySpan> = internal.spanTag

/**
* @since 2.0.0
Expand All @@ -81,7 +89,7 @@ export interface Span {
readonly name: string
readonly spanId: string
readonly traceId: string
readonly parent: Option.Option<ParentSpan>
readonly parent: Option.Option<AnySpan>
readonly context: Context.Context<never>
readonly status: SpanStatus
readonly attributes: ReadonlyMap<string, unknown>
Expand All @@ -98,7 +106,7 @@ export interface Span {
*/
export interface SpanLink {
readonly _tag: "SpanLink"
readonly span: ParentSpan
readonly span: AnySpan
readonly attributes: Readonly<Record<string, unknown>>
}

Expand Down
4 changes: 2 additions & 2 deletions packages/effect/src/internal/cause.ts
Expand Up @@ -12,7 +12,7 @@ import { pipeArguments } from "../Pipeable.js"
import { hasProperty, isFunction } from "../Predicate.js"
import type { Predicate, Refinement } from "../Predicate.js"
import * as ReadonlyArray from "../ReadonlyArray.js"
import type { ParentSpan, Span } from "../Tracer.js"
import type { AnySpan, Span } from "../Tracer.js"
import { getBugErrorMessage } from "./errors.js"
import * as OpCodes from "./opCodes/cause.js"

Expand Down Expand Up @@ -990,7 +990,7 @@ export const pretty = <E>(cause: Cause.Cause<E>): string => {
message += `\r\n${filterStack(e.stack)}`
}
if (e.span) {
let current: Span | ParentSpan | undefined = e.span
let current: Span | AnySpan | undefined = e.span
let i = 0
while (current && current._tag === "Span" && i < 10) {
message += `\r\n at ${current.name}`
Expand Down
4 changes: 2 additions & 2 deletions packages/effect/src/internal/channel.ts
Expand Up @@ -2316,7 +2316,7 @@ export const withSpan = dual<
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.ParentSpan | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
}
Expand All @@ -2329,7 +2329,7 @@ export const withSpan = dual<
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.ParentSpan | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
}
Expand Down
22 changes: 11 additions & 11 deletions packages/effect/src/internal/core-effect.ts
Expand Up @@ -1997,15 +1997,15 @@ export const annotateSpans = dual<
)

/** @internal */
export const currentParentSpan: Effect.Effect<Tracer.ParentSpan, Cause.NoSuchElementException> = serviceOptional(
export const currentParentSpan: Effect.Effect<Tracer.AnySpan, Cause.NoSuchElementException> = serviceOptional(
internalTracer.spanTag
)

/** @internal */
export const currentSpan: Effect.Effect<Tracer.Span, Cause.NoSuchElementException> = core.flatMap(
core.context<never>(),
(context) => {
const span = context.unsafeMap.get(internalTracer.spanTag.key) as Tracer.ParentSpan | undefined
const span = context.unsafeMap.get(internalTracer.spanTag.key) as Tracer.AnySpan | undefined
return span !== undefined && span._tag === "Span"
? core.succeed(span)
: core.fail(new core.NoSuchElementException())
Expand All @@ -2015,12 +2015,12 @@ export const currentSpan: Effect.Effect<Tracer.Span, Cause.NoSuchElementExceptio
/* @internal */
export const linkSpans = dual<
(
span: Tracer.ParentSpan,
span: Tracer.AnySpan,
attributes?: Record<string, unknown>
) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>,
<A, E, R>(
self: Effect.Effect<A, E, R>,
span: Tracer.ParentSpan,
span: Tracer.AnySpan,
attributes?: Record<string, unknown>
) => Effect.Effect<A, E, R>
>(
Expand Down Expand Up @@ -2108,7 +2108,7 @@ export const makeSpan = (
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.ParentSpan | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
}
Expand All @@ -2128,7 +2128,7 @@ export const useSpan: {
<A, E, R>(name: string, options: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.ParentSpan | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
}, evaluate: (span: Tracer.Span) => Effect.Effect<A, E, R>): Effect.Effect<A, E, R>
Expand All @@ -2142,7 +2142,7 @@ export const useSpan: {
const options: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.ParentSpan | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
} | undefined = args.length === 1 ? undefined : args[0]
Expand All @@ -2165,24 +2165,24 @@ export const useSpan: {
/** @internal */
export const withParentSpan = dual<
(
span: Tracer.ParentSpan
span: Tracer.AnySpan
) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Tracer.ParentSpan>>,
<A, E, R>(self: Effect.Effect<A, E, R>, span: Tracer.ParentSpan) => Effect.Effect<A, E, Exclude<R, Tracer.ParentSpan>>
<A, E, R>(self: Effect.Effect<A, E, R>, span: Tracer.AnySpan) => Effect.Effect<A, E, Exclude<R, Tracer.ParentSpan>>
>(2, (self, span) => provideService(self, internalTracer.spanTag, span))

/** @internal */
export const withSpan = dual<
(name: string, options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.ParentSpan | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
}) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Tracer.ParentSpan>>,
<A, E, R>(self: Effect.Effect<A, E, R>, name: string, options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.ParentSpan | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
}) => Effect.Effect<A, E, Exclude<R, Tracer.ParentSpan>>
Expand Down
2 changes: 1 addition & 1 deletion packages/effect/src/internal/core.ts
Expand Up @@ -2947,7 +2947,7 @@ export const mapInputContext = dual<
/** @internal */
export const currentSpanFromFiber = <A, E>(fiber: Fiber.RuntimeFiber<A, E>): Option.Option<Tracer.Span> => {
const span = fiber.getFiberRef(currentContext).unsafeMap.get(internalTracer.spanTag.key) as
| Tracer.ParentSpan
| Tracer.AnySpan
| undefined
return span !== undefined && span._tag === "Span" ? Option.some(span) : Option.none()
}
Expand Down
6 changes: 3 additions & 3 deletions packages/effect/src/internal/fiberRuntime.ts
Expand Up @@ -3573,7 +3573,7 @@ export const makeSpanScoped = (
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.ParentSpan | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
}
Expand Down Expand Up @@ -3606,14 +3606,14 @@ export const withSpanScoped = dual<
(name: string, options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.ParentSpan | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
}) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Tracer.ParentSpan> | Scope.Scope>,
<A, E, R>(self: Effect.Effect<A, E, R>, name: string, options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.ParentSpan | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
}) => Effect.Effect<A, E, Exclude<R, Tracer.ParentSpan> | Scope.Scope>
Expand Down

0 comments on commit be9d025

Please sign in to comment.