@orpc/opentelemetry 2.0.0-beta.49 → 2.0.0-beta.50

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/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
- export * from './instrumentation.ts';
2
- export type { OpenTelemetryPropagationOptions } from './tracer.ts';
1
+ import { ORPCInstrumentation, ORPCInstrumentationConfig } from "./instrumentation.js";
2
+ import { OpenTelemetryPropagationOptions } from "./tracer.js";
3
+ export { ORPCInstrumentation, ORPCInstrumentationConfig, type OpenTelemetryPropagationOptions };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- export * from './instrumentation.js';
1
+ export { ORPCInstrumentation } from "./instrumentation.js";
@@ -1,16 +1,15 @@
1
- import type { InstrumentationConfig } from '@opentelemetry/instrumentation';
2
- import { InstrumentationBase } from '@opentelemetry/instrumentation';
3
- import type { OpenTelemetryPropagationOptions } from './tracer.ts';
1
+ import { OpenTelemetryPropagationOptions } from "./tracer.js";
2
+ import { InstrumentationBase, InstrumentationConfig } from "@opentelemetry/instrumentation";
4
3
  export interface ORPCInstrumentationConfig extends InstrumentationConfig {
5
- /**
6
- * Automatic OpenTelemetry context/span propagation. `false` disables it,
7
- * {@link OpenTelemetryPropagationOptions} controls each direction separately.
8
- *
9
- * Disable this if propagation is handled elsewhere or managed manually.
10
- *
11
- * @default true
12
- */
13
- propagation?: boolean | OpenTelemetryPropagationOptions | undefined;
4
+ /**
5
+ * Automatic OpenTelemetry context/span propagation. `false` disables it,
6
+ * {@link OpenTelemetryPropagationOptions} controls each direction separately.
7
+ *
8
+ * Disable this if propagation is handled elsewhere or managed manually.
9
+ *
10
+ * @default true
11
+ */
12
+ propagation?: boolean | OpenTelemetryPropagationOptions | undefined;
14
13
  }
15
14
  /**
16
15
  * OpenTelemetry instrumentation for oRPC. Automatically instruments both
@@ -19,9 +18,9 @@ export interface ORPCInstrumentationConfig extends InstrumentationConfig {
19
18
  * @see {@link https://orpc.dev/docs/integrations/opentelemetry | OpenTelemetry Integration}
20
19
  */
21
20
  export declare class ORPCInstrumentation extends InstrumentationBase<ORPCInstrumentationConfig> {
22
- constructor(config?: ORPCInstrumentationConfig);
23
- protected init(): void;
24
- enable(): void;
25
- disable(): void;
26
- isEnabled(): boolean;
27
- }
21
+ constructor(config?: ORPCInstrumentationConfig);
22
+ protected override init(): void;
23
+ override enable(): void;
24
+ override disable(): void;
25
+ override isEnabled(): boolean;
26
+ }
@@ -1,28 +1,30 @@
1
- import { InstrumentationBase } from '@opentelemetry/instrumentation';
2
- import { getTracer, setTracer } from '@orpc/shared';
3
- import pkg from '../package.json' with { type: 'json' };
4
- import { OpenTelemetryTracer } from './tracer.js';
1
+ import { name, version } from "./package.json.js";
2
+ import { OpenTelemetryTracer } from "./tracer.js";
3
+ import { InstrumentationBase } from "@opentelemetry/instrumentation";
4
+ import { getTracer, setTracer } from "@orpc/shared";
5
+ // src/instrumentation.ts
5
6
  /**
6
- * OpenTelemetry instrumentation for oRPC. Automatically instruments both
7
- * client and server for distributed tracing.
8
- *
9
- * @see {@link https://orpc.dev/docs/integrations/opentelemetry | OpenTelemetry Integration}
10
- */
11
- export class ORPCInstrumentation extends InstrumentationBase {
12
- constructor(config = {}) {
13
- super(pkg.name, pkg.version, config);
14
- }
15
- init() { }
16
- enable() {
17
- setTracer(new OpenTelemetryTracer({
18
- tracer: () => this.tracer,
19
- propagation: this.getConfig().propagation,
20
- }));
21
- }
22
- disable() {
23
- setTracer(undefined);
24
- }
25
- isEnabled() {
26
- return getTracer() !== undefined;
27
- }
28
- }
7
+ * OpenTelemetry instrumentation for oRPC. Automatically instruments both
8
+ * client and server for distributed tracing.
9
+ *
10
+ * @see {@link https://orpc.dev/docs/integrations/opentelemetry | OpenTelemetry Integration}
11
+ */
12
+ var ORPCInstrumentation = class extends InstrumentationBase {
13
+ constructor(config = {}) {
14
+ super(name, version, config);
15
+ }
16
+ init() {}
17
+ enable() {
18
+ setTracer(new OpenTelemetryTracer({
19
+ tracer: () => this.tracer,
20
+ propagation: this.getConfig().propagation
21
+ }));
22
+ }
23
+ disable() {
24
+ setTracer(void 0);
25
+ }
26
+ isEnabled() {
27
+ return getTracer() !== void 0;
28
+ }
29
+ };
30
+ export { ORPCInstrumentation };
@@ -0,0 +1,4 @@
1
+ // package.json
2
+ var name = "@orpc/opentelemetry";
3
+ var version = "2.0.0-beta.50";
4
+ export { name, version };
package/dist/tracer.d.ts CHANGED
@@ -1,68 +1,18 @@
1
- import type { Context, Tracer as OpenTelemetryApiTracer, Span } from '@opentelemetry/api';
2
- import type { Promisable, Tracer, TracingAttributeValue, TracingException, TracingExceptionLevel, TracingSpan } from '@orpc/shared';
3
- export declare class OpenTelemetrySpan implements TracingSpan {
4
- /**
5
- * The context the span was started or extracted in, with the span set as its active span.
6
- * Children and `withActiveSpan` use it as-is, so baggage and other context values are kept
7
- * even when the span is used later, e.g. while a streamed response is consumed.
8
- *
9
- * An extracted parent with only baggage has no span here, so its children start a new trace.
10
- */
11
- readonly context: Context;
12
- readonly span: Span;
13
- constructor(
14
- /**
15
- * The context the span was started or extracted in, with the span set as its active span.
16
- * Children and `withActiveSpan` use it as-is, so baggage and other context values are kept
17
- * even when the span is used later, e.g. while a streamed response is consumed.
18
- *
19
- * An extracted parent with only baggage has no span here, so its children start a new trace.
20
- */
21
- context: Context);
22
- setAttribute(key: string, value: TracingAttributeValue): void;
23
- updateName(name: string): void;
24
- addEvent(name: string): void;
25
- recordException(level: TracingExceptionLevel, exception: TracingException): void;
26
- end(): void;
27
- }
1
+ import "@orpc/shared";
2
+ import "@opentelemetry/api";
28
3
  export interface OpenTelemetryPropagationOptions {
29
- /**
30
- * Whether oRPC links write the trace context and baggage of the active span into outgoing request headers.
31
- *
32
- * @default true
33
- */
34
- inject?: boolean | undefined;
35
- /**
36
- * Whether oRPC handlers read the trace context and baggage from incoming request headers and continue that trace.
37
- *
38
- * Disable this when callers are untrusted, e.g. public web clients.
39
- *
40
- * @default true
41
- */
42
- extract?: boolean | undefined;
43
- }
44
- export interface OpenTelemetryTracerOptions {
45
- /**
46
- * Called for every span, so a tracer provider set after construction is picked up.
47
- */
48
- tracer: () => OpenTelemetryApiTracer;
49
- /**
50
- * Automatic OpenTelemetry context propagation. `false` disables it,
51
- * {@link OpenTelemetryPropagationOptions} controls each direction separately.
52
- *
53
- * Disable this if propagation is handled elsewhere or managed manually.
54
- *
55
- * @default true
56
- */
57
- propagation?: boolean | OpenTelemetryPropagationOptions | undefined;
58
- }
59
- export declare class OpenTelemetryTracer implements Tracer {
60
- private readonly tracer;
61
- readonly inject: Tracer['inject'];
62
- readonly extract: Tracer['extract'];
63
- constructor(options: OpenTelemetryTracerOptions);
64
- startSpan(name: string, parent?: TracingSpan): TracingSpan;
65
- startActiveSpan<T>(name: string, parent: TracingSpan | undefined, fn: (span: TracingSpan) => Promise<T>): Promise<T>;
66
- getActiveSpan(): TracingSpan | undefined;
67
- withActiveSpan<T>(span: TracingSpan, fn: () => Promisable<T>): Promisable<T>;
68
- }
4
+ /**
5
+ * Whether oRPC links write the trace context and baggage of the active span into outgoing request headers.
6
+ *
7
+ * @default true
8
+ */
9
+ inject?: boolean | undefined;
10
+ /**
11
+ * Whether oRPC handlers read the trace context and baggage from incoming request headers and continue that trace.
12
+ *
13
+ * Disable this when callers are untrusted, e.g. public web clients.
14
+ *
15
+ * @default true
16
+ */
17
+ extract?: boolean | undefined;
18
+ }
package/dist/tracer.js CHANGED
@@ -1,95 +1,82 @@
1
- import { context, INVALID_SPAN_CONTEXT, propagation, SpanStatusCode, trace, } from '@opentelemetry/api';
2
- export class OpenTelemetrySpan {
3
- context;
4
- span;
5
- constructor(
6
- /**
7
- * The context the span was started or extracted in, with the span set as its active span.
8
- * Children and `withActiveSpan` use it as-is, so baggage and other context values are kept
9
- * even when the span is used later, e.g. while a streamed response is consumed.
10
- *
11
- * An extracted parent with only baggage has no span here, so its children start a new trace.
12
- */
13
- context) {
14
- this.context = context;
15
- this.span = trace.getSpan(context) ?? trace.wrapSpanContext(INVALID_SPAN_CONTEXT);
16
- }
17
- setAttribute(key, value) {
18
- this.span.setAttribute(key, value);
19
- }
20
- updateName(name) {
21
- this.span.updateName(name);
22
- }
23
- addEvent(name) {
24
- this.span.addEvent(name);
25
- }
26
- recordException(level, exception) {
27
- this.span.recordException(exception);
28
- if (level === 'error') {
29
- this.span.setStatus({ code: SpanStatusCode.ERROR, message: exception.message });
30
- }
31
- }
32
- end() {
33
- this.span.end();
34
- }
35
- }
36
- export class OpenTelemetryTracer {
37
- tracer;
38
- inject;
39
- extract;
40
- constructor(options) {
41
- this.tracer = options.tracer;
42
- const enabled = typeof options.propagation === 'object'
43
- ? options.propagation
44
- : { inject: options.propagation, extract: options.propagation };
45
- if (enabled.inject ?? true) {
46
- this.inject = (span, headers) => {
47
- propagation.inject(contextOf(span), headers);
48
- };
49
- }
50
- if (enabled.extract ?? true) {
51
- this.extract = (headers) => {
52
- const active = context.active();
53
- const extracted = withActiveBaggage(propagation.extract(active, headers), active);
54
- return extracted === active && trace.getSpan(active) === undefined
55
- ? undefined
56
- : new OpenTelemetrySpan(extracted);
57
- };
58
- }
59
- }
60
- startSpan(name, parent) {
61
- const parentContext = contextOf(parent);
62
- const span = this.tracer().startSpan(name, undefined, parentContext);
63
- return new OpenTelemetrySpan(trace.setSpan(parentContext, span));
64
- }
65
- startActiveSpan(name, parent, fn) {
66
- // Same as the OpenTelemetry SDK's startActiveSpan, but the stored context does not depend on a context manager
67
- const span = this.startSpan(name, parent);
68
- return context.with(span.context, fn, undefined, span);
69
- }
70
- getActiveSpan() {
71
- const active = context.active();
72
- return trace.getSpan(active) === undefined ? undefined : new OpenTelemetrySpan(active);
73
- }
74
- withActiveSpan(span, fn) {
75
- return context.with(contextOf(span), fn);
76
- }
77
- }
1
+ import { INVALID_SPAN_CONTEXT, SpanStatusCode, context, propagation, trace } from "@opentelemetry/api";
2
+ // src/tracer.ts
3
+ var OpenTelemetrySpan = class {
4
+ context;
5
+ span;
6
+ constructor(context) {
7
+ this.context = context;
8
+ this.span = trace.getSpan(context) ?? trace.wrapSpanContext(INVALID_SPAN_CONTEXT);
9
+ }
10
+ setAttribute(key, value) {
11
+ this.span.setAttribute(key, value);
12
+ }
13
+ updateName(name) {
14
+ this.span.updateName(name);
15
+ }
16
+ addEvent(name) {
17
+ this.span.addEvent(name);
18
+ }
19
+ recordException(level, exception) {
20
+ this.span.recordException(exception);
21
+ if (level === "error") this.span.setStatus({
22
+ code: SpanStatusCode.ERROR,
23
+ message: exception.message
24
+ });
25
+ }
26
+ end() {
27
+ this.span.end();
28
+ }
29
+ };
30
+ var OpenTelemetryTracer = class {
31
+ tracer;
32
+ inject;
33
+ extract;
34
+ constructor(options) {
35
+ this.tracer = options.tracer;
36
+ const enabled = typeof options.propagation === "object" ? options.propagation : {
37
+ inject: options.propagation,
38
+ extract: options.propagation
39
+ };
40
+ if (enabled.inject ?? true) this.inject = (span, headers) => {
41
+ propagation.inject(contextOf(span), headers);
42
+ };
43
+ if (enabled.extract ?? true) this.extract = (headers) => {
44
+ const active = context.active();
45
+ const extracted = withActiveBaggage(propagation.extract(active, headers), active);
46
+ return extracted === active && trace.getSpan(active) === void 0 ? void 0 : new OpenTelemetrySpan(extracted);
47
+ };
48
+ }
49
+ startSpan(name, parent) {
50
+ const parentContext = contextOf(parent);
51
+ const span = this.tracer().startSpan(name, void 0, parentContext);
52
+ return new OpenTelemetrySpan(trace.setSpan(parentContext, span));
53
+ }
54
+ startActiveSpan(name, parent, fn) {
55
+ const span = this.startSpan(name, parent);
56
+ return context.with(span.context, fn, void 0, span);
57
+ }
58
+ getActiveSpan() {
59
+ const active = context.active();
60
+ return trace.getSpan(active) === void 0 ? void 0 : new OpenTelemetrySpan(active);
61
+ }
62
+ withActiveSpan(span, fn) {
63
+ return context.with(contextOf(span), fn);
64
+ }
65
+ };
78
66
  /**
79
- * No span, or a span of another tracer (e.g. one set while a request was running), falls back to the active context.
80
- */
67
+ * No span, or a span of another tracer (e.g. one set while a request was running), falls back to the active context.
68
+ */
81
69
  function contextOf(span) {
82
- return span instanceof OpenTelemetrySpan ? span.context : context.active();
70
+ return span instanceof OpenTelemetrySpan ? span.context : context.active();
83
71
  }
84
72
  /**
85
- * The baggage propagator replaces the baggage of the context it extracts into,
86
- * so entries already set (e.g. by an HTTP instrumentation or middleware) are added back, and win on conflict.
87
- */
73
+ * The baggage propagator replaces the baggage of the context it extracts into,
74
+ * so entries already set (e.g. by an HTTP instrumentation or middleware) are added back, and win on conflict.
75
+ */
88
76
  function withActiveBaggage(extracted, active) {
89
- const fromHeaders = propagation.getBaggage(extracted);
90
- const current = propagation.getBaggage(active);
91
- if (fromHeaders === undefined || current === undefined || fromHeaders === current) {
92
- return extracted;
93
- }
94
- return propagation.setBaggage(extracted, propagation.createBaggage(Object.fromEntries([...fromHeaders.getAllEntries(), ...current.getAllEntries()])));
77
+ const fromHeaders = propagation.getBaggage(extracted);
78
+ const current = propagation.getBaggage(active);
79
+ if (fromHeaders === void 0 || current === void 0 || fromHeaders === current) return extracted;
80
+ return propagation.setBaggage(extracted, propagation.createBaggage(Object.fromEntries([...fromHeaders.getAllEntries(), ...current.getAllEntries()])));
95
81
  }
82
+ export { OpenTelemetrySpan, OpenTelemetryTracer };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orpc/opentelemetry",
3
- "version": "2.0.0-beta.49",
3
+ "version": "2.0.0-beta.50",
4
4
  "description": "OpenTelemetry integration for oRPC: distributed tracing for typesafe APIs",
5
5
  "keywords": [
6
6
  "observability",
@@ -35,7 +35,7 @@
35
35
  }
36
36
  },
37
37
  "dependencies": {
38
- "@orpc/shared": "2.0.0-beta.49"
38
+ "@orpc/shared": "2.0.0-beta.50"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@opentelemetry/api": "^1.10.0",