@effect/opentelemetry 4.0.0-beta.7 → 4.0.0-beta.70

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/src/index.ts CHANGED
@@ -1,35 +1,154 @@
1
1
  /**
2
- * @since 1.0.0
2
+ * @since 4.0.0
3
3
  */
4
4
 
5
5
  // @barrel: Auto-generated exports. Do not edit manually.
6
6
 
7
7
  /**
8
- * @since 1.0.0
8
+ * Connects Effect's logging system to the OpenTelemetry Logs SDK.
9
+ *
10
+ * This module provides a logger provider service, an Effect `Logger` that
11
+ * emits OpenTelemetry log records, and layers for installing that logger in an
12
+ * application. It is commonly used to send Effect logs to OTLP, console, or
13
+ * vendor-specific exporters through OpenTelemetry `LogRecordProcessor`s while
14
+ * keeping logs correlated with Effect fibers and spans. Emitted records include
15
+ * the current fiber id, span identifiers when a parent span is present, log
16
+ * annotations, log spans, severity text, and the matching OpenTelemetry
17
+ * severity number.
18
+ *
19
+ * Log export depends on the configured OpenTelemetry processors and exporters;
20
+ * this module creates the provider and logger, but does not choose an exporter.
21
+ * Use the `Resource` layer to attach service and deployment metadata to the
22
+ * provider rather than repeating that data on every log record. When using
23
+ * `layerLoggerProvider`, the provider is scoped and is force-flushed and shut
24
+ * down when the layer is released, with a configurable shutdown timeout. If you
25
+ * supply or manage an OpenTelemetry provider yourself, make sure it is flushed
26
+ * and shut down during application shutdown, especially when using batching
27
+ * processors that may otherwise drop buffered logs.
28
+ *
29
+ * @since 4.0.0
9
30
  */
10
31
  export * as Logger from "./Logger.ts"
11
32
 
12
33
  /**
13
- * @since 1.0.0
34
+ * Bridges Effect metrics into OpenTelemetry by exposing the current Effect
35
+ * metric snapshot as an OpenTelemetry `MetricProducer` and registering it with
36
+ * one or more SDK `MetricReader`s. Use this module when an application already
37
+ * records metrics with Effect and needs those counters, gauges, histograms,
38
+ * frequencies, or summaries exported through OTLP, Prometheus, or another
39
+ * OpenTelemetry-compatible reader/exporter.
40
+ *
41
+ * The `layer` constructor is the usual entry point, and is also used by the
42
+ * Node and Web SDK layers when `metricReader` configuration is supplied. Metric
43
+ * readers are acquired inside the layer scope and shut down when the scope is
44
+ * released, so periodic exporters need the runtime to stay alive long enough to
45
+ * collect and export data. The exporter or backend determines whether
46
+ * cumulative or delta aggregation is expected; this module defaults to
47
+ * cumulative temporality and can be configured with `temporality: "delta"` for
48
+ * backends that require interval-based values.
49
+ *
50
+ * @since 4.0.0
14
51
  */
15
52
  export * as Metrics from "./Metrics.ts"
16
53
 
17
54
  /**
18
- * @since 1.0.0
55
+ * Provides an Effect layer for configuring OpenTelemetry in Node.js
56
+ * processes. The module wires the Effect tracer, metrics producer, and logger
57
+ * into OpenTelemetry SDK providers when span processors, metric readers, or log
58
+ * record processors are supplied, and it builds the shared resource from
59
+ * `OTEL_SERVICE_NAME`, `OTEL_RESOURCE_ATTRIBUTES`, and optional explicit
60
+ * service metadata.
61
+ *
62
+ * Use this module in Node services, workers, CLIs, or server runtimes that need
63
+ * Effect spans, metrics, and logs exported through OpenTelemetry processors and
64
+ * exporters. Telemetry is enabled only for the configured signal types, so an
65
+ * application can install tracing alone, metrics alone, logging alone, or any
66
+ * combination of them from the same layer.
67
+ *
68
+ * The layer is scoped. Tracer and logger providers are force-flushed and shut
69
+ * down when the scope is released, metric readers are shut down with the same
70
+ * lifecycle, and all shutdown waits are bounded by `shutdownTimeout` with a
71
+ * default of three seconds. Keep the layer scope alive for the lifetime of the
72
+ * process and release it during graceful shutdown so batched exporters have a
73
+ * chance to export final telemetry. When combining this layer with Node
74
+ * auto-instrumentations, register instrumentation before importing modules that
75
+ * should be patched, because many Node instrumentations hook module loading.
76
+ *
77
+ * @since 4.0.0
19
78
  */
20
79
  export * as NodeSdk from "./NodeSdk.ts"
21
80
 
22
81
  /**
23
- * @since 1.0.0
82
+ * Provides the OpenTelemetry resource used by the Effect OpenTelemetry layers.
83
+ *
84
+ * A resource describes the entity that produces telemetry, such as a service,
85
+ * process, deployment, or browser application. The tracing, metrics, logging,
86
+ * and SDK layers use this module's `Resource` service to configure providers
87
+ * and identify emitted telemetry with service-level metadata.
88
+ *
89
+ * Use `layer` when service metadata is known in code, `layerFromEnv` when
90
+ * deploying with `OTEL_SERVICE_NAME` and `OTEL_RESOURCE_ATTRIBUTES`, and
91
+ * `layerEmpty` when no resource attributes should be provided. Resource
92
+ * attributes are for stable process or service metadata, not per-span or
93
+ * per-log data. The explicit `layer` helper sets `service.name` and the
94
+ * `telemetry.sdk.*` attributes after merging custom attributes, so those keys
95
+ * are controlled by this integration. With `layerFromEnv`, `OTEL_SERVICE_NAME`
96
+ * overrides `service.name` from `OTEL_RESOURCE_ATTRIBUTES`, and additional
97
+ * attributes passed to the layer are merged last.
98
+ *
99
+ * @since 4.0.0
24
100
  */
25
101
  export * as Resource from "./Resource.ts"
26
102
 
27
103
  /**
28
- * @since 1.0.0
104
+ * Bridges Effect tracing into OpenTelemetry by installing an Effect `Tracer`
105
+ * that creates OpenTelemetry spans, records attributes, events, links, errors,
106
+ * and status, and keeps OpenTelemetry context active while traced effects run.
107
+ * Use this module when an application already has an OpenTelemetry
108
+ * `TracerProvider`, or when the Node and Web SDK layers should expose Effect
109
+ * spans to OTLP, console, or other OpenTelemetry-compatible exporters.
110
+ *
111
+ * The layer constructors wire Effect's tracer service to either the global
112
+ * OpenTelemetry tracer provider or an explicitly provided `OtelTracer`. This
113
+ * module does not create exporters or span processors by itself, so spans are
114
+ * exported only when the provider has been configured by the application or by
115
+ * the Node/Web SDK layers. Parentage is taken from Effect spans first and can
116
+ * also attach to the active OpenTelemetry context, while `makeExternalSpan` and
117
+ * `withSpanContext` are the entry points for continuing an incoming remote
118
+ * trace. Preserve `traceFlags` and `traceState` when building external spans;
119
+ * otherwise sampling defaults to sampled and trace state cannot be propagated.
120
+ *
121
+ * @since 4.0.0
29
122
  */
30
123
  export * as Tracer from "./Tracer.ts"
31
124
 
32
125
  /**
33
- * @since 1.0.0
126
+ * Provides an Effect layer for configuring OpenTelemetry in browser
127
+ * applications. The module builds a shared resource from explicit service
128
+ * metadata and wires Effect tracing, metrics, and logging into OpenTelemetry
129
+ * SDK providers when span processors, metric readers, or log record processors
130
+ * are supplied.
131
+ *
132
+ * Use this module in client-side applications that need Effect spans, metrics,
133
+ * and logs exported from browser runtimes, such as single-page apps,
134
+ * multi-page apps with hydrated Effect code, frontend workers, or UI flows
135
+ * that should be correlated with backend traces. Telemetry is enabled only for
136
+ * the configured signal types, so tracing, metrics, and logging can be
137
+ * installed independently from the same layer.
138
+ *
139
+ * Browser SDKs cannot rely on process environment resource configuration, so
140
+ * provide stable service metadata explicitly and use resource attributes for
141
+ * application, release, deployment, or page-shell identity rather than
142
+ * per-event data. This module does not create exporters; supply
143
+ * browser-compatible processors, readers, and exporters yourself, and make sure
144
+ * their endpoints are reachable from the browser with the required CORS and
145
+ * authentication behavior. The layer is scoped: tracer providers are
146
+ * force-flushed and shut down when the scope is released, while metric readers
147
+ * and logger providers follow their respective layer lifecycles. Keep the
148
+ * scope alive for the lifetime of the browser application and release it during
149
+ * application teardown when possible so batched exporters and periodic metric
150
+ * readers can deliver buffered telemetry before the page is unloaded.
151
+ *
152
+ * @since 4.0.0
34
153
  */
35
154
  export * as WebSdk from "./WebSdk.ts"
@@ -1,6 +1,13 @@
1
1
  import type * as Otel from "@opentelemetry/api"
2
2
  import * as Inspectable from "effect/Inspectable"
3
3
 
4
+ const bigint1e9 = BigInt(1_000_000_000)
5
+
6
+ /** @internal */
7
+ export const nanosToHrTime = (timestamp: bigint): Otel.HrTime => {
8
+ return [Number(timestamp / bigint1e9), Number(timestamp % bigint1e9)]
9
+ }
10
+
4
11
  /** @internal */
5
12
  export const recordToAttributes = (record: Record<string, unknown>): Otel.Attributes => {
6
13
  const attributes: Otel.Attributes = {}
@@ -11,8 +11,8 @@ import type {
11
11
  import { AggregationTemporality, DataPointType, InstrumentType } from "@opentelemetry/sdk-metrics"
12
12
  import type { InstrumentDescriptor } from "@opentelemetry/sdk-metrics/build/src/InstrumentDescriptor.js"
13
13
  import * as Arr from "effect/Array"
14
+ import type * as Context from "effect/Context"
14
15
  import * as Metric from "effect/Metric"
15
- import type * as ServiceMap from "effect/ServiceMap"
16
16
  import type * as Metrics from "../Metrics.ts"
17
17
 
18
18
  const sdkName = "@effect/opentelemetry/Metrics"
@@ -37,7 +37,7 @@ interface PreviousSummaryState {
37
37
  /** @internal */
38
38
  export class MetricProducerImpl implements MetricProducer {
39
39
  resource: Resources.Resource
40
- services: ServiceMap.ServiceMap<never>
40
+ context: Context.Context<never>
41
41
  temporality: Metrics.TemporalityPreference
42
42
  startTimes: Map<string, HrTime>
43
43
  startTimeNanos: HrTime
@@ -49,11 +49,11 @@ export class MetricProducerImpl implements MetricProducer {
49
49
 
50
50
  constructor(
51
51
  resource: Resources.Resource,
52
- services: ServiceMap.ServiceMap<never>,
52
+ context: Context.Context<never>,
53
53
  temporality: Metrics.TemporalityPreference = "cumulative"
54
54
  ) {
55
55
  this.resource = resource
56
- this.services = services
56
+ this.context = context
57
57
  this.temporality = temporality
58
58
  this.startTimes = new Map()
59
59
  this.startTimeNanos = currentHrTime()
@@ -73,7 +73,7 @@ export class MetricProducerImpl implements MetricProducer {
73
73
  }
74
74
 
75
75
  collect(_options?: MetricCollectOptions): Promise<CollectionResult> {
76
- const snapshot = Metric.snapshotUnsafe(this.services)
76
+ const snapshot = Metric.snapshotUnsafe(this.context)
77
77
  const hrTimeNow = currentHrTime()
78
78
  const metricData: Array<MetricData> = []
79
79
  const metricDataByName = new Map<string, MetricData>()
@@ -111,7 +111,7 @@ export class MetricProducerImpl implements MetricProducer {
111
111
  if (typeof currentCount === "bigint" && typeof previousCount === "bigint") {
112
112
  reportValue = currentCount - previousCount
113
113
  // Handle reset: if current < previous, report current value
114
- if (reportValue < 0n) {
114
+ if (reportValue < BigInt(0)) {
115
115
  reportValue = currentCount
116
116
  }
117
117
  } else {