Skip to content

Commit

Permalink
add SpanKind to Tracer spans (#2597)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim <hello@timsmart.co>
  • Loading branch information
patroza and tim-smart committed Apr 30, 2024
1 parent 1c9454d commit a023f28
Show file tree
Hide file tree
Showing 25 changed files with 150 additions and 229 deletions.
8 changes: 8 additions & 0 deletions .changeset/healthy-lamps-stare.md
@@ -0,0 +1,8 @@
---
"@effect/opentelemetry": minor
"effect": minor
---

add `kind` property to `Tracer.Span`

This can be used to specify what kind of service created the span.
8 changes: 8 additions & 0 deletions .changeset/seven-llamas-chew.md
@@ -0,0 +1,8 @@
---
"@effect/experimental": patch
"@effect/platform": patch
"@effect/rpc": patch
"@effect/sql": patch
---

set span `kind` where applicable
16 changes: 2 additions & 14 deletions packages/effect/src/Channel.ts
Expand Up @@ -2107,26 +2107,14 @@ export const updateService: {
export const withSpan: {
(
name: string,
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
} | undefined
options?: Tracer.SpanOptions | undefined
): <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
self: Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
) => Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Exclude<Env, Tracer.ParentSpan>>
<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
self: Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
name: string,
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
} | undefined
options?: Tracer.SpanOptions | undefined
): Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Exclude<Env, Tracer.ParentSpan>>
} = channel.withSpan

Expand Down
56 changes: 7 additions & 49 deletions packages/effect/src/Effect.ts
Expand Up @@ -5197,13 +5197,7 @@ export const linkSpans: {
*/
export const makeSpan: (
name: string,
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
}
options?: Tracer.SpanOptions
) => Effect<Tracer.Span> = effect.makeSpan

/**
Expand All @@ -5218,13 +5212,7 @@ export const makeSpan: (
*/
export const makeSpanScoped: (
name: string,
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
} | undefined
options?: Tracer.SpanOptions | undefined
) => Effect<Tracer.Span, never, Scope.Scope> = fiberRuntime.makeSpanScoped

/**
Expand All @@ -5241,13 +5229,7 @@ export const useSpan: {
<A, E, R>(name: string, evaluate: (span: Tracer.Span) => Effect<A, E, R>): Effect<A, E, R>
<A, E, R>(
name: string,
options: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
},
options: Tracer.SpanOptions,
evaluate: (span: Tracer.Span) => Effect<A, E, R>
): Effect<A, E, R>
} = effect.useSpan
Expand All @@ -5261,24 +5243,12 @@ export const useSpan: {
export const withSpan: {
(
name: string,
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
} | undefined
options?: Tracer.SpanOptions | undefined
): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, Exclude<R, Tracer.ParentSpan>>
<A, E, R>(
self: Effect<A, E, R>,
name: string,
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
} | undefined
options?: Tracer.SpanOptions | undefined
): Effect<A, E, Exclude<R, Tracer.ParentSpan>>
} = effect.withSpan

Expand All @@ -5293,24 +5263,12 @@ export const withSpan: {
export const withSpanScoped: {
(
name: string,
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
}
options?: Tracer.SpanOptions
): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, Exclude<R, Tracer.ParentSpan> | Scope.Scope>
<A, E, R>(
self: Effect<A, E, R>,
name: string,
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
}
options?: Tracer.SpanOptions
): Effect<A, E, Exclude<R, Tracer.ParentSpan> | Scope.Scope>
} = fiberRuntime.withSpanScoped

Expand Down
21 changes: 3 additions & 18 deletions packages/effect/src/Layer.ts
Expand Up @@ -959,12 +959,7 @@ export const setScheduler: (scheduler: Scheduler.Scheduler) => Layer<never> = (
*/
export const span: (
name: string,
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
options?: Tracer.SpanOptions & {
readonly onEnd?:
| ((span: Tracer.Span, exit: Exit.Exit<unknown, unknown>) => Effect.Effect<void>)
| undefined
Expand Down Expand Up @@ -1015,12 +1010,7 @@ export const setUnhandledErrorLogLevel: (level: Option.Option<LogLevel>) => Laye
export const withSpan: {
(
name: string,
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
options?: Tracer.SpanOptions & {
readonly onEnd?:
| ((span: Tracer.Span, exit: Exit.Exit<unknown, unknown>) => Effect.Effect<void>)
| undefined
Expand All @@ -1029,12 +1019,7 @@ export const withSpan: {
<A, E, R>(
self: Layer<A, E, R>,
name: string,
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
options?: Tracer.SpanOptions & {
readonly onEnd?:
| ((span: Tracer.Span, exit: Exit.Exit<unknown, unknown>) => Effect.Effect<void>)
| undefined
Expand Down
16 changes: 2 additions & 14 deletions packages/effect/src/Stream.ts
Expand Up @@ -4025,24 +4025,12 @@ export const whenEffect: {
export const withSpan: {
(
name: string,
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
} | undefined
options?: Tracer.SpanOptions | undefined
): <A, E, R>(self: Stream<A, E, R>) => Stream<A, E, Exclude<R, Tracer.ParentSpan>>
<A, E, R>(
self: Stream<A, E, R>,
name: string,
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
} | undefined
options?: Tracer.SpanOptions | undefined
): Stream<A, E, Exclude<R, Tracer.ParentSpan>>
} = internal.withSpan

Expand Down
23 changes: 22 additions & 1 deletion packages/effect/src/Tracer.ts
Expand Up @@ -29,7 +29,8 @@ export interface Tracer {
parent: Option.Option<AnySpan>,
context: Context.Context<never>,
links: ReadonlyArray<SpanLink>,
startTime: bigint
startTime: bigint,
kind: SpanKind
): Span
context<X>(f: () => X, fiber: Fiber.RuntimeFiber<any, any>): X
}
Expand Down Expand Up @@ -80,6 +81,25 @@ export interface ExternalSpan {
readonly context: Context.Context<never>
}

/**
* @since 3.1.0
* @category models
*/
export interface SpanOptions {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<SpanLink> | undefined
readonly parent?: AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
readonly kind?: SpanKind | undefined
}

/**
* @since 3.1.0
* @category models
*/
export type SpanKind = "internal" | "server" | "client" | "producer" | "consumer"

/**
* @since 2.0.0
* @category models
Expand All @@ -95,6 +115,7 @@ export interface Span {
readonly attributes: ReadonlyMap<string, unknown>
readonly links: ReadonlyArray<SpanLink>
readonly sampled: boolean
readonly kind: SpanKind
end(endTime: bigint, exit: Exit.Exit<unknown, unknown>): void
attribute(key: string, value: unknown): void
event(name: string, startTime: bigint, attributes?: Record<string, unknown>): void
Expand Down
16 changes: 2 additions & 14 deletions packages/effect/src/internal/channel.ts
Expand Up @@ -2313,26 +2313,14 @@ export const updateService = dual<
export const withSpan = dual<
(
name: string,
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
}
options?: Tracer.SpanOptions
) => <OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>
) => Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Exclude<Env, Tracer.ParentSpan>>,
<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>(
self: Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Env>,
name: string,
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
}
options?: Tracer.SpanOptions
) => Channel.Channel<OutElem, InElem, OutErr, InErr, OutDone, InDone, Exclude<Env, Tracer.ParentSpan>>
>(3, (self, name, options) =>
unwrapScoped(
Expand Down
62 changes: 19 additions & 43 deletions packages/effect/src/internal/core-effect.ts
Expand Up @@ -2027,13 +2027,7 @@ const bigint0 = BigInt(0)
export const unsafeMakeSpan = <XA, XE>(
fiber: FiberRuntime<XA, XE>,
name: string,
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
}
options?: Tracer.SpanOptions
) => {
const enabled = fiber.getFiberRef(core.currentTracerEnabled)
if (enabled === false) {
Expand Down Expand Up @@ -2071,7 +2065,8 @@ export const unsafeMakeSpan = <XA, XE>(
parent,
options?.context ?? Context.empty(),
links,
timingEnabled ? clock.unsafeCurrentTimeNanos() : bigint0
timingEnabled ? clock.unsafeCurrentTimeNanos() : bigint0,
options?.kind ?? "internal"
)

if (annotationsFromEnv._tag === "Some") {
Expand All @@ -2087,13 +2082,7 @@ export const unsafeMakeSpan = <XA, XE>(
/** @internal */
export const makeSpan = (
name: string,
options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
}
options?: Tracer.SpanOptions
): Effect.Effect<Tracer.Span> => core.withFiberRuntime((fiber) => core.succeed(unsafeMakeSpan(fiber, name, options)))

/* @internal */
Expand All @@ -2107,27 +2096,19 @@ export const spanLinks: Effect.Effect<Chunk.Chunk<Tracer.SpanLink>> = core
/** @internal */
export const useSpan: {
<A, E, R>(name: string, evaluate: (span: Tracer.Span) => Effect.Effect<A, E, R>): Effect.Effect<A, E, R>
<A, E, R>(name: string, options: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | 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>
<A, E, R>(
name: string,
options: Tracer.SpanOptions,
evaluate: (span: Tracer.Span) => Effect.Effect<A, E, R>
): Effect.Effect<A, E, R>
} = <A, E, R>(
name: string,
...args: [evaluate: (span: Tracer.Span) => Effect.Effect<A, E, R>] | [
options: any,
evaluate: (span: Tracer.Span) => Effect.Effect<A, E, R>
]
) => {
const options: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | undefined
readonly parent?: Tracer.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
} | undefined = args.length === 1 ? undefined : args[0]
const options: Tracer.SpanOptions | undefined = args.length === 1 ? undefined : args[0]
const evaluate: (span: Tracer.Span) => Effect.Effect<A, E, R> = args[args.length - 1]

return core.withFiberRuntime<A, E, R>((fiber) => {
Expand All @@ -2154,20 +2135,15 @@ export const withParentSpan = dual<

/** @internal */
export const withSpan = dual<
(name: string, options?: {
readonly attributes?: Record<string, unknown> | undefined
readonly links?: ReadonlyArray<Tracer.SpanLink> | 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.AnySpan | undefined
readonly root?: boolean | undefined
readonly context?: Context.Context<never> | undefined
}) => Effect.Effect<A, E, Exclude<R, Tracer.ParentSpan>>
(
name: string,
options?: Tracer.SpanOptions
) => <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?: Tracer.SpanOptions
) => Effect.Effect<A, E, Exclude<R, Tracer.ParentSpan>>
>(
(args) => typeof args[0] !== "string",
(self, name, options) =>
Expand Down
1 change: 1 addition & 0 deletions packages/effect/src/internal/core.ts
Expand Up @@ -2982,6 +2982,7 @@ const NoopSpanProto: Tracer.Span = {
},
attributes: new Map(),
links: [],
kind: "internal",
attribute() {},
event() {},
end() {}
Expand Down

0 comments on commit a023f28

Please sign in to comment.