@effect/opentelemetry 4.0.0-beta.9 → 4.0.0-beta.90
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Logger.d.ts +62 -11
- package/dist/Logger.d.ts.map +1 -1
- package/dist/Logger.js +88 -25
- package/dist/Logger.js.map +1 -1
- package/dist/Metrics.d.ts +38 -14
- package/dist/Metrics.d.ts.map +1 -1
- package/dist/Metrics.js +23 -8
- package/dist/Metrics.js.map +1 -1
- package/dist/NodeSdk.d.ts +84 -12
- package/dist/NodeSdk.d.ts.map +1 -1
- package/dist/NodeSdk.js +28 -5
- package/dist/NodeSdk.js.map +1 -1
- package/dist/Resource.d.ts +57 -13
- package/dist/Resource.d.ts.map +1 -1
- package/dist/Resource.js +48 -13
- package/dist/Resource.js.map +1 -1
- package/dist/Tracer.d.ts +108 -51
- package/dist/Tracer.d.ts.map +1 -1
- package/dist/Tracer.js +122 -69
- package/dist/Tracer.js.map +1 -1
- package/dist/WebSdk.d.ts +80 -11
- package/dist/WebSdk.d.ts.map +1 -1
- package/dist/WebSdk.js +25 -4
- package/dist/WebSdk.js.map +1 -1
- package/dist/index.d.ts +7 -7
- package/dist/index.js +7 -7
- package/dist/internal/attributes.js +5 -0
- package/dist/internal/attributes.js.map +1 -1
- package/dist/internal/metrics.js +5 -5
- package/dist/internal/metrics.js.map +1 -1
- package/package.json +20 -15
- package/src/Logger.ts +94 -25
- package/src/Metrics.ts +39 -15
- package/src/NodeSdk.ts +84 -12
- package/src/Resource.ts +58 -14
- package/src/Tracer.ts +155 -99
- package/src/WebSdk.ts +80 -11
- package/src/index.ts +7 -7
- package/src/internal/attributes.ts +7 -0
- package/src/internal/metrics.ts +6 -6
package/src/Resource.ts
CHANGED
|
@@ -1,27 +1,45 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* OpenTelemetry resource service and layers.
|
|
3
|
+
*
|
|
4
|
+
* An OpenTelemetry resource identifies the process or service that emits spans,
|
|
5
|
+
* metrics, and logs. This module stores that resource in Effect context and
|
|
6
|
+
* provides layers for creating it from explicit service metadata, from
|
|
7
|
+
* `OTEL_SERVICE_NAME` and `OTEL_RESOURCE_ATTRIBUTES`, or as an empty resource.
|
|
8
|
+
* It also includes `configToAttributes` for turning service metadata into raw
|
|
9
|
+
* OpenTelemetry attributes.
|
|
10
|
+
*
|
|
11
|
+
* @since 4.0.0
|
|
3
12
|
*/
|
|
4
13
|
import type * as OtelApi from "@opentelemetry/api"
|
|
5
14
|
import * as Resources from "@opentelemetry/resources"
|
|
6
15
|
import * as OtelSemConv from "@opentelemetry/semantic-conventions"
|
|
7
16
|
import * as Arr from "effect/Array"
|
|
8
17
|
import * as Config from "effect/Config"
|
|
18
|
+
import * as Context from "effect/Context"
|
|
9
19
|
import * as Effect from "effect/Effect"
|
|
10
20
|
import * as Layer from "effect/Layer"
|
|
11
|
-
import * as ServiceMap from "effect/ServiceMap"
|
|
12
21
|
|
|
13
22
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
23
|
+
* Service tag for OpenTelemetry metadata attached to emitted telemetry.
|
|
24
|
+
*
|
|
25
|
+
* **When to use**
|
|
26
|
+
*
|
|
27
|
+
* Use to provide process, service, and deployment metadata that should be
|
|
28
|
+
* attached to spans, metrics, and logs.
|
|
29
|
+
*
|
|
30
|
+
* @category services
|
|
31
|
+
* @since 4.0.0
|
|
16
32
|
*/
|
|
17
|
-
export class Resource extends
|
|
33
|
+
export class Resource extends Context.Service<
|
|
18
34
|
Resource,
|
|
19
35
|
Resources.Resource
|
|
20
36
|
>()("@effect/opentelemetry/Resource") {}
|
|
21
37
|
|
|
22
38
|
/**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
39
|
+
* Creates a `Resource` layer from service metadata and additional OpenTelemetry attributes.
|
|
40
|
+
*
|
|
41
|
+
* @category layers
|
|
42
|
+
* @since 4.0.0
|
|
25
43
|
*/
|
|
26
44
|
export const layer = (config: {
|
|
27
45
|
readonly serviceName: string
|
|
@@ -34,8 +52,30 @@ export const layer = (config: {
|
|
|
34
52
|
)
|
|
35
53
|
|
|
36
54
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
55
|
+
* Converts resource configuration into OpenTelemetry attributes, adding service name, optional service version, and telemetry SDK metadata.
|
|
56
|
+
*
|
|
57
|
+
* **When to use**
|
|
58
|
+
*
|
|
59
|
+
* Use to turn explicit service metadata into a raw OpenTelemetry attribute map
|
|
60
|
+
* for lower-level resource construction or merging with environment-derived
|
|
61
|
+
* attributes via `layerFromEnv`.
|
|
62
|
+
*
|
|
63
|
+
* **Details**
|
|
64
|
+
*
|
|
65
|
+
* The returned record copies `attributes` first, then sets `service.name`,
|
|
66
|
+
* `telemetry.sdk.name`, and `telemetry.sdk.language`. `service.version` is
|
|
67
|
+
* included only when `serviceVersion` is provided.
|
|
68
|
+
*
|
|
69
|
+
* **Gotchas**
|
|
70
|
+
*
|
|
71
|
+
* Custom values for `service.name` and `telemetry.sdk.*` are overwritten by this
|
|
72
|
+
* helper. An empty `serviceVersion` is treated as absent.
|
|
73
|
+
*
|
|
74
|
+
* @see {@link layer} for creating a `Resource` layer from explicit metadata
|
|
75
|
+
* @see {@link layerFromEnv} for merging attributes with OpenTelemetry environment variables
|
|
76
|
+
*
|
|
77
|
+
* @category configuration
|
|
78
|
+
* @since 4.0.0
|
|
39
79
|
*/
|
|
40
80
|
export const configToAttributes = (options: {
|
|
41
81
|
readonly serviceName: string
|
|
@@ -57,8 +97,10 @@ export const configToAttributes = (options: {
|
|
|
57
97
|
}
|
|
58
98
|
|
|
59
99
|
/**
|
|
60
|
-
*
|
|
61
|
-
*
|
|
100
|
+
* Creates a `Resource` layer from OpenTelemetry environment variables, optionally merging additional attributes.
|
|
101
|
+
*
|
|
102
|
+
* @category layers
|
|
103
|
+
* @since 4.0.0
|
|
62
104
|
*/
|
|
63
105
|
export const layerFromEnv = (
|
|
64
106
|
additionalAttributes?:
|
|
@@ -70,7 +112,7 @@ export const layerFromEnv = (
|
|
|
70
112
|
Effect.gen(function*() {
|
|
71
113
|
const serviceName = yield* Config.option(Config.string("OTEL_SERVICE_NAME"))
|
|
72
114
|
const attributes = yield* Config.string("OTEL_RESOURCE_ATTRIBUTES").pipe(
|
|
73
|
-
Config.withDefault(
|
|
115
|
+
Config.withDefault(""),
|
|
74
116
|
Config.map((s) => {
|
|
75
117
|
const attrs = s.split(",")
|
|
76
118
|
return Arr.reduce(attrs, {} as OtelApi.Attributes, (acc, attr) => {
|
|
@@ -94,8 +136,10 @@ export const layerFromEnv = (
|
|
|
94
136
|
)
|
|
95
137
|
|
|
96
138
|
/**
|
|
97
|
-
*
|
|
98
|
-
*
|
|
139
|
+
* Layer that provides an empty OpenTelemetry resource.
|
|
140
|
+
*
|
|
141
|
+
* @category layers
|
|
142
|
+
* @since 4.0.0
|
|
99
143
|
*/
|
|
100
144
|
export const layerEmpty = Layer.succeed(
|
|
101
145
|
Resource,
|
package/src/Tracer.ts
CHANGED
|
@@ -1,18 +1,36 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Bridges Effect tracing into OpenTelemetry by installing an Effect `Tracer`
|
|
3
|
+
* that creates OpenTelemetry spans, records attributes, events, links, errors,
|
|
4
|
+
* and status, and keeps OpenTelemetry context active while traced effects run.
|
|
5
|
+
* Use this module when an application already has an OpenTelemetry
|
|
6
|
+
* `TracerProvider`, or when the Node and Web SDK layers should expose Effect
|
|
7
|
+
* spans to OTLP, console, or other OpenTelemetry-compatible exporters.
|
|
8
|
+
*
|
|
9
|
+
* The layer constructors wire Effect's tracer service to either the global
|
|
10
|
+
* OpenTelemetry tracer provider or an explicitly provided `OtelTracer`. This
|
|
11
|
+
* module does not create exporters or span processors by itself, so spans are
|
|
12
|
+
* exported only when the provider has been configured by the application or by
|
|
13
|
+
* the Node/Web SDK layers. Parentage is taken from Effect spans first and can
|
|
14
|
+
* also attach to the active OpenTelemetry context, while `makeExternalSpan` and
|
|
15
|
+
* `withSpanContext` are the entry points for continuing an incoming remote
|
|
16
|
+
* trace. Preserve `traceFlags` and `traceState` when building external spans;
|
|
17
|
+
* otherwise sampling defaults to sampled and trace state cannot be propagated.
|
|
18
|
+
*
|
|
19
|
+
* @since 4.0.0
|
|
3
20
|
*/
|
|
4
21
|
import * as Otel from "@opentelemetry/api"
|
|
5
22
|
import * as OtelSemConv from "@opentelemetry/semantic-conventions"
|
|
6
23
|
import * as Cause from "effect/Cause"
|
|
7
24
|
import type * as Clock from "effect/Clock"
|
|
25
|
+
import * as Context from "effect/Context"
|
|
8
26
|
import * as Effect from "effect/Effect"
|
|
9
27
|
import * as Exit from "effect/Exit"
|
|
10
28
|
import { constTrue, dual } from "effect/Function"
|
|
11
29
|
import * as Layer from "effect/Layer"
|
|
30
|
+
import * as Option from "effect/Option"
|
|
12
31
|
import * as Predicate from "effect/Predicate"
|
|
13
|
-
import * as ServiceMap from "effect/ServiceMap"
|
|
14
32
|
import * as Tracer from "effect/Tracer"
|
|
15
|
-
import { recordToAttributes, unknownToAttributeValue } from "./internal/attributes.ts"
|
|
33
|
+
import { nanosToHrTime, recordToAttributes, unknownToAttributeValue } from "./internal/attributes.ts"
|
|
16
34
|
import { Resource } from "./Resource.ts"
|
|
17
35
|
|
|
18
36
|
// =============================================================================
|
|
@@ -20,37 +38,45 @@ import { Resource } from "./Resource.ts"
|
|
|
20
38
|
// =============================================================================
|
|
21
39
|
|
|
22
40
|
/**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
41
|
+
* Context service containing the OpenTelemetry `Tracer` used to create spans for Effect tracing.
|
|
42
|
+
*
|
|
43
|
+
* @category services
|
|
44
|
+
* @since 4.0.0
|
|
25
45
|
*/
|
|
26
|
-
export class OtelTracer extends
|
|
46
|
+
export class OtelTracer extends Context.Service<
|
|
27
47
|
OtelTracer,
|
|
28
48
|
Otel.Tracer
|
|
29
49
|
>()("@effect/opentelemetry/Tracer") {}
|
|
30
50
|
|
|
31
51
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
52
|
+
* Context service containing the OpenTelemetry `TracerProvider` used to obtain tracers.
|
|
53
|
+
*
|
|
54
|
+
* @category services
|
|
55
|
+
* @since 4.0.0
|
|
34
56
|
*/
|
|
35
|
-
export class OtelTracerProvider extends
|
|
57
|
+
export class OtelTracerProvider extends Context.Service<
|
|
36
58
|
OtelTracerProvider,
|
|
37
59
|
Otel.TracerProvider
|
|
38
60
|
>()("@effect/opentelemetry/Tracer/OtelTracerProvider") {}
|
|
39
61
|
|
|
40
62
|
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
63
|
+
* Context service containing OpenTelemetry trace flags used when constructing external span contexts.
|
|
64
|
+
*
|
|
65
|
+
* @category services
|
|
66
|
+
* @since 4.0.0
|
|
43
67
|
*/
|
|
44
|
-
export class OtelTraceFlags extends
|
|
68
|
+
export class OtelTraceFlags extends Context.Service<
|
|
45
69
|
OtelTraceFlags,
|
|
46
70
|
Otel.TraceFlags
|
|
47
71
|
>()("@effect/opentelemetry/Tracer/OtelTraceFlags") {}
|
|
48
72
|
|
|
49
73
|
/**
|
|
50
|
-
*
|
|
51
|
-
*
|
|
74
|
+
* Context service containing OpenTelemetry trace state used when constructing external span contexts.
|
|
75
|
+
*
|
|
76
|
+
* @category services
|
|
77
|
+
* @since 4.0.0
|
|
52
78
|
*/
|
|
53
|
-
export class OtelTraceState extends
|
|
79
|
+
export class OtelTraceState extends Context.Service<
|
|
54
80
|
OtelTraceState,
|
|
55
81
|
Otel.TraceState
|
|
56
82
|
>()("@effect/opentelemetry/Tracer/OtelTraceState") {}
|
|
@@ -60,8 +86,10 @@ export class OtelTraceState extends ServiceMap.Service<
|
|
|
60
86
|
// =============================================================================
|
|
61
87
|
|
|
62
88
|
/**
|
|
63
|
-
*
|
|
64
|
-
*
|
|
89
|
+
* Creates an Effect `Tracer` implementation backed by the configured OpenTelemetry tracer.
|
|
90
|
+
*
|
|
91
|
+
* @category constructors
|
|
92
|
+
* @since 4.0.0
|
|
65
93
|
*/
|
|
66
94
|
export const make: Effect.Effect<Tracer.Tracer, never, OtelTracer> = Effect.map(
|
|
67
95
|
Effect.service(OtelTracer),
|
|
@@ -91,8 +119,10 @@ export const make: Effect.Effect<Tracer.Tracer, never, OtelTracer> = Effect.map(
|
|
|
91
119
|
)
|
|
92
120
|
|
|
93
121
|
/**
|
|
94
|
-
*
|
|
95
|
-
*
|
|
122
|
+
* Creates an Effect external span from an OpenTelemetry span context, preserving trace flags and trace state when provided.
|
|
123
|
+
*
|
|
124
|
+
* @category constructors
|
|
125
|
+
* @since 4.0.0
|
|
96
126
|
*/
|
|
97
127
|
export const makeExternalSpan = (options: {
|
|
98
128
|
readonly traceId: string
|
|
@@ -100,22 +130,24 @@ export const makeExternalSpan = (options: {
|
|
|
100
130
|
readonly traceFlags?: number | undefined
|
|
101
131
|
readonly traceState?: string | Otel.TraceState | undefined
|
|
102
132
|
}): Tracer.ExternalSpan => {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
133
|
+
const annotations = Context.mutate(Context.empty(), (annotations) => {
|
|
134
|
+
let next = annotations
|
|
135
|
+
if (options.traceFlags !== undefined) {
|
|
136
|
+
next = Context.add(next, OtelTraceFlags, options.traceFlags)
|
|
137
|
+
}
|
|
108
138
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
139
|
+
if (typeof options.traceState === "string") {
|
|
140
|
+
try {
|
|
141
|
+
next = Context.add(next, OtelTraceState, Otel.createTraceState(options.traceState))
|
|
142
|
+
} catch {
|
|
143
|
+
//
|
|
144
|
+
}
|
|
145
|
+
} else if (options.traceState) {
|
|
146
|
+
next = Context.add(next, OtelTraceState, options.traceState)
|
|
115
147
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
148
|
+
|
|
149
|
+
return next
|
|
150
|
+
})
|
|
119
151
|
|
|
120
152
|
return {
|
|
121
153
|
_tag: "ExternalSpan",
|
|
@@ -131,8 +163,10 @@ export const makeExternalSpan = (options: {
|
|
|
131
163
|
// =============================================================================
|
|
132
164
|
|
|
133
165
|
/**
|
|
134
|
-
*
|
|
135
|
-
*
|
|
166
|
+
* Layer that provides the current global OpenTelemetry tracer provider.
|
|
167
|
+
*
|
|
168
|
+
* @category layers
|
|
169
|
+
* @since 4.0.0
|
|
136
170
|
*/
|
|
137
171
|
export const layerGlobalProvider: Layer.Layer<OtelTracerProvider> = Layer.sync(
|
|
138
172
|
OtelTracerProvider,
|
|
@@ -140,8 +174,10 @@ export const layerGlobalProvider: Layer.Layer<OtelTracerProvider> = Layer.sync(
|
|
|
140
174
|
)
|
|
141
175
|
|
|
142
176
|
/**
|
|
143
|
-
*
|
|
144
|
-
*
|
|
177
|
+
* Layer that creates an OpenTelemetry tracer from the provided tracer provider and resource metadata.
|
|
178
|
+
*
|
|
179
|
+
* @category layers
|
|
180
|
+
* @since 4.0.0
|
|
145
181
|
*/
|
|
146
182
|
export const layerTracer: Layer.Layer<OtelTracer, never, OtelTracerProvider | Resource> = Layer.effect(
|
|
147
183
|
OtelTracer,
|
|
@@ -156,30 +192,47 @@ export const layerTracer: Layer.Layer<OtelTracer, never, OtelTracerProvider | Re
|
|
|
156
192
|
)
|
|
157
193
|
|
|
158
194
|
/**
|
|
159
|
-
*
|
|
160
|
-
*
|
|
195
|
+
* Layer that creates an OpenTelemetry tracer from the global tracer provider and the current resource.
|
|
196
|
+
*
|
|
197
|
+
* @category layers
|
|
198
|
+
* @since 4.0.0
|
|
161
199
|
*/
|
|
162
200
|
export const layerGlobalTracer: Layer.Layer<OtelTracer, never, Resource> = layerTracer.pipe(
|
|
163
201
|
Layer.provide(layerGlobalProvider)
|
|
164
202
|
)
|
|
165
203
|
|
|
166
204
|
/**
|
|
167
|
-
*
|
|
168
|
-
*
|
|
205
|
+
* Layer that installs an Effect tracer backed by the global OpenTelemetry tracer provider.
|
|
206
|
+
*
|
|
207
|
+
* @category layers
|
|
208
|
+
* @since 4.0.0
|
|
169
209
|
*/
|
|
170
210
|
export const layerGlobal: Layer.Layer<OtelTracer, never, Resource> = Layer.effect(Tracer.Tracer, make).pipe(
|
|
171
211
|
Layer.provideMerge(layerGlobalTracer)
|
|
172
212
|
)
|
|
173
213
|
|
|
174
214
|
/**
|
|
175
|
-
*
|
|
176
|
-
*
|
|
215
|
+
* Layer that installs the Effect tracer using an `OtelTracer` already provided in the environment.
|
|
216
|
+
*
|
|
217
|
+
* @category layers
|
|
218
|
+
* @since 4.0.0
|
|
177
219
|
*/
|
|
178
220
|
export const layerWithoutOtelTracer: Layer.Layer<never, never, OtelTracer> = Layer.effect(Tracer.Tracer, make)
|
|
179
221
|
|
|
180
222
|
/**
|
|
181
|
-
*
|
|
182
|
-
*
|
|
223
|
+
* Layer that creates an OpenTelemetry tracer from a provider and resource, then installs it as the Effect tracer.
|
|
224
|
+
*
|
|
225
|
+
* **When to use**
|
|
226
|
+
*
|
|
227
|
+
* Use when you already provide an `OtelTracerProvider` and a `Resource`, and
|
|
228
|
+
* want Effect spans backed by a tracer derived from them.
|
|
229
|
+
*
|
|
230
|
+
* @see {@link layerTracer} for creating only the OpenTelemetry tracer service
|
|
231
|
+
* @see {@link layerGlobal} for installing the Effect tracer from the global provider
|
|
232
|
+
* @see {@link layerWithoutOtelTracer} for installing an already-provided `OtelTracer`
|
|
233
|
+
*
|
|
234
|
+
* @category layers
|
|
235
|
+
* @since 4.0.0
|
|
183
236
|
*/
|
|
184
237
|
export const layer: Layer.Layer<OtelTracer, never, OtelTracerProvider | Resource> = layerWithoutOtelTracer.pipe(
|
|
185
238
|
Layer.provideMerge(layerTracer)
|
|
@@ -193,16 +246,17 @@ const bigint1e6 = BigInt(1_000_000)
|
|
|
193
246
|
const bigint1e9 = BigInt(1_000_000_000)
|
|
194
247
|
|
|
195
248
|
/**
|
|
196
|
-
*
|
|
249
|
+
* Gets the current OpenTelemetry span.
|
|
197
250
|
*
|
|
198
|
-
*
|
|
199
|
-
* `NodeSdk.layer`, etc.) and the lightweight OTLP module (`OtlpTracer.layer`).
|
|
251
|
+
* **Details**
|
|
200
252
|
*
|
|
201
|
-
*
|
|
202
|
-
*
|
|
253
|
+
* This accessor works with both the official OpenTelemetry API, such as
|
|
254
|
+
* `Tracer.layer` and `NodeSdk.layer`, and the lightweight OTLP module, such as
|
|
255
|
+
* `OtlpTracer.layer`. When using OTLP, the returned span is a wrapper that
|
|
256
|
+
* conforms to the OpenTelemetry `Span` interface.
|
|
203
257
|
*
|
|
204
|
-
* @since 1.0.0
|
|
205
258
|
* @category accessors
|
|
259
|
+
* @since 4.0.0
|
|
206
260
|
*/
|
|
207
261
|
export const currentOtelSpan: Effect.Effect<Otel.Span, Cause.NoSuchElementError> = Effect.clockWith((clock) =>
|
|
208
262
|
Effect.map(Effect.currentSpan, (span) =>
|
|
@@ -299,35 +353,41 @@ const convertOtelTimeInput = (input: Otel.TimeInput | undefined, clock: Clock.Cl
|
|
|
299
353
|
}
|
|
300
354
|
|
|
301
355
|
/**
|
|
302
|
-
*
|
|
356
|
+
* Sets an effect's parent span from the given OpenTelemetry `SpanContext`.
|
|
303
357
|
*
|
|
304
|
-
*
|
|
305
|
-
* attach to a parent span.
|
|
358
|
+
* **When to use**
|
|
306
359
|
*
|
|
307
|
-
*
|
|
308
|
-
*
|
|
360
|
+
* Use when you need an effect to continue a trace from a parent span context
|
|
361
|
+
* produced by OpenTelemetry instrumentation outside Effect.
|
|
362
|
+
*
|
|
363
|
+
* @category propagation
|
|
364
|
+
* @since 4.0.0
|
|
309
365
|
*/
|
|
310
366
|
export const withSpanContext: {
|
|
311
367
|
/**
|
|
312
|
-
*
|
|
368
|
+
* Sets an effect's parent span from the given OpenTelemetry `SpanContext`.
|
|
369
|
+
*
|
|
370
|
+
* **When to use**
|
|
313
371
|
*
|
|
314
|
-
*
|
|
315
|
-
*
|
|
372
|
+
* Use when you need an effect to continue a trace from a parent span context
|
|
373
|
+
* produced by OpenTelemetry instrumentation outside Effect.
|
|
316
374
|
*
|
|
317
|
-
* @
|
|
318
|
-
* @
|
|
375
|
+
* @category propagation
|
|
376
|
+
* @since 4.0.0
|
|
319
377
|
*/
|
|
320
378
|
(spanContext: Otel.SpanContext): <A, E, R>(
|
|
321
379
|
self: Effect.Effect<A, E, R>
|
|
322
380
|
) => Effect.Effect<A, E, Exclude<R, Tracer.ParentSpan>>
|
|
323
381
|
/**
|
|
324
|
-
*
|
|
382
|
+
* Sets an effect's parent span from the given OpenTelemetry `SpanContext`.
|
|
325
383
|
*
|
|
326
|
-
*
|
|
327
|
-
* attach to a parent span.
|
|
384
|
+
* **When to use**
|
|
328
385
|
*
|
|
329
|
-
*
|
|
330
|
-
*
|
|
386
|
+
* Use when you need an effect to continue a trace from a parent span context
|
|
387
|
+
* produced by OpenTelemetry instrumentation outside Effect.
|
|
388
|
+
*
|
|
389
|
+
* @category propagation
|
|
390
|
+
* @since 4.0.0
|
|
331
391
|
*/
|
|
332
392
|
<A, E, R>(self: Effect.Effect<A, E, R>, spanContext: Otel.SpanContext): Effect.Effect<A, E, Exclude<R, Tracer.ParentSpan>>
|
|
333
393
|
} = dual(2, <A, E, R>(
|
|
@@ -356,14 +416,14 @@ export class OtelSpan implements Tracer.Span {
|
|
|
356
416
|
|
|
357
417
|
readonly name: string
|
|
358
418
|
readonly kind: Tracer.SpanKind
|
|
359
|
-
readonly annotations:
|
|
419
|
+
readonly annotations: Context.Context<never>
|
|
360
420
|
readonly links: Array<Tracer.SpanLink>
|
|
361
421
|
readonly span: Otel.Span
|
|
362
422
|
readonly spanId: string
|
|
363
423
|
readonly traceId: string
|
|
364
424
|
readonly attributes = new Map<string, unknown>()
|
|
365
425
|
readonly sampled: boolean
|
|
366
|
-
readonly parent: Tracer.AnySpan
|
|
426
|
+
readonly parent: Option.Option<Tracer.AnySpan>
|
|
367
427
|
status: Tracer.SpanStatus
|
|
368
428
|
|
|
369
429
|
constructor(
|
|
@@ -378,9 +438,9 @@ export class OtelSpan implements Tracer.Span {
|
|
|
378
438
|
this.links = options.links
|
|
379
439
|
this.kind = options.kind
|
|
380
440
|
const active = contextApi.active()
|
|
381
|
-
this.parent = options.
|
|
382
|
-
getOtelParent(traceApi, active, options.annotations)
|
|
383
|
-
|
|
441
|
+
this.parent = options.root !== true
|
|
442
|
+
? Option.orElse(options.parent, () => getOtelParent(traceApi, active, options.annotations))
|
|
443
|
+
: options.parent
|
|
384
444
|
this.span = tracer.startSpan(
|
|
385
445
|
options.name,
|
|
386
446
|
{
|
|
@@ -393,7 +453,9 @@ export class OtelSpan implements Tracer.Span {
|
|
|
393
453
|
: undefined as any,
|
|
394
454
|
kind: kindMap[this.kind]
|
|
395
455
|
},
|
|
396
|
-
|
|
456
|
+
Option.isSome(this.parent) ?
|
|
457
|
+
populateContext(active, this.parent.value, options.annotations) :
|
|
458
|
+
Otel.trace.deleteSpan(active)
|
|
397
459
|
)
|
|
398
460
|
const spanContext = this.span.spanContext()
|
|
399
461
|
this.spanId = spanContext.spanId
|
|
@@ -469,30 +531,24 @@ export class OtelSpan implements Tracer.Span {
|
|
|
469
531
|
const isSampled = (traceFlags: Otel.TraceFlags): boolean =>
|
|
470
532
|
(traceFlags & Otel.TraceFlags.SAMPLED) === Otel.TraceFlags.SAMPLED
|
|
471
533
|
|
|
472
|
-
const nanosToHrTime = (timestamp: bigint): Otel.HrTime => {
|
|
473
|
-
return [Number(timestamp / bigint1e9), Number(timestamp % bigint1e9)]
|
|
474
|
-
}
|
|
475
|
-
|
|
476
534
|
const getOtelParent = (
|
|
477
535
|
tracer: Otel.TraceAPI,
|
|
478
536
|
context: Otel.Context,
|
|
479
|
-
annotations:
|
|
480
|
-
): Tracer.AnySpan
|
|
481
|
-
const
|
|
482
|
-
|
|
483
|
-
return
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
})
|
|
490
|
-
: undefined
|
|
537
|
+
annotations: Context.Context<never>
|
|
538
|
+
): Option.Option<Tracer.AnySpan> => {
|
|
539
|
+
const otelParent = tracer.getSpan(context)?.spanContext()
|
|
540
|
+
if (!otelParent) return Option.none()
|
|
541
|
+
return Option.some(Tracer.externalSpan({
|
|
542
|
+
spanId: otelParent.spanId,
|
|
543
|
+
traceId: otelParent.traceId,
|
|
544
|
+
sampled: (otelParent.traceFlags & 1) === 1,
|
|
545
|
+
annotations
|
|
546
|
+
}))
|
|
491
547
|
}
|
|
492
548
|
|
|
493
549
|
const makeSpanContext = (
|
|
494
550
|
span: Tracer.AnySpan,
|
|
495
|
-
annotations?:
|
|
551
|
+
annotations?: Context.Context<never>
|
|
496
552
|
): Otel.SpanContext => {
|
|
497
553
|
const traceFlags = makeTraceFlags(span, annotations)
|
|
498
554
|
const traceState = makeTraceState(span, annotations)!
|
|
@@ -507,27 +563,27 @@ const makeSpanContext = (
|
|
|
507
563
|
|
|
508
564
|
const makeTraceFlags = (
|
|
509
565
|
span: Tracer.AnySpan,
|
|
510
|
-
annotations:
|
|
566
|
+
annotations: Context.Context<never> | undefined
|
|
511
567
|
): Otel.TraceFlags => {
|
|
512
568
|
let traceFlags: Otel.TraceFlags | undefined
|
|
513
569
|
if (Predicate.isNotUndefined(annotations)) {
|
|
514
570
|
traceFlags = extractTraceService(span, annotations, OtelTraceFlags)
|
|
515
571
|
if (Predicate.isUndefined(traceFlags)) {
|
|
516
|
-
traceFlags =
|
|
572
|
+
traceFlags = Context.getOrUndefined(span.annotations, OtelTraceFlags)
|
|
517
573
|
}
|
|
518
574
|
}
|
|
519
|
-
return traceFlags ?? Otel.TraceFlags.SAMPLED
|
|
575
|
+
return traceFlags ?? (span.sampled ? Otel.TraceFlags.SAMPLED : Otel.TraceFlags.NONE)
|
|
520
576
|
}
|
|
521
577
|
|
|
522
578
|
const makeTraceState = (
|
|
523
579
|
span: Tracer.AnySpan,
|
|
524
|
-
annotations:
|
|
580
|
+
annotations: Context.Context<never> | undefined
|
|
525
581
|
): Otel.TraceState | undefined => {
|
|
526
582
|
let traceState: Otel.TraceState | undefined
|
|
527
583
|
if (Predicate.isNotUndefined(annotations)) {
|
|
528
584
|
traceState = extractTraceService(span, annotations, OtelTraceState)
|
|
529
585
|
if (Predicate.isUndefined(traceState)) {
|
|
530
|
-
traceState =
|
|
586
|
+
traceState = Context.getOrUndefined(span.annotations, OtelTraceState)
|
|
531
587
|
}
|
|
532
588
|
}
|
|
533
589
|
return traceState
|
|
@@ -535,20 +591,20 @@ const makeTraceState = (
|
|
|
535
591
|
|
|
536
592
|
const extractTraceService = <I, S>(
|
|
537
593
|
parent: Tracer.AnySpan,
|
|
538
|
-
annotations:
|
|
539
|
-
service:
|
|
594
|
+
annotations: Context.Context<never>,
|
|
595
|
+
service: Context.Service<I, S>
|
|
540
596
|
) => {
|
|
541
|
-
const instance =
|
|
597
|
+
const instance = Context.getOrUndefined(annotations, service)
|
|
542
598
|
if (Predicate.isNotUndefined(instance)) {
|
|
543
599
|
return instance
|
|
544
600
|
}
|
|
545
|
-
return
|
|
601
|
+
return Context.getOrUndefined(parent.annotations, service)
|
|
546
602
|
}
|
|
547
603
|
|
|
548
604
|
const populateContext = (
|
|
549
605
|
context: Otel.Context,
|
|
550
606
|
span: Tracer.AnySpan,
|
|
551
|
-
annotations?:
|
|
607
|
+
annotations?: Context.Context<never> | undefined
|
|
552
608
|
): Otel.Context =>
|
|
553
609
|
span instanceof OtelSpan ?
|
|
554
610
|
Otel.trace.setSpan(context, span.span) :
|