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

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/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  <img alt="codecov" src="https://codecov.io/gh/middleapi/orpc/branch/main/graph/badge.svg">
6
6
  </a>
7
7
  <a href="https://www.npmjs.com/package/@orpc/opentelemetry">
8
- <img alt="weekly downloads" src="https://img.shields.io/npm/dw/%40orpc%2Fopentelemetry?logo=npm" />
8
+ <img alt="monthly downloads" src="https://img.shields.io/npm/dm/%40orpc%2Fopentelemetry?logo=npm" />
9
9
  </a>
10
10
  <a href="https://app.codspeed.io/middleapi/orpc?utm_source=badge">
11
11
  <img alt="CodSpeed" src="https://img.shields.io/endpoint?url=https://codspeed.io/badge.json" />
@@ -41,10 +41,11 @@ export const router = {
41
41
  Then call them from a client that knows every input and output, without generating anything:
42
42
 
43
43
  ```ts
44
- import type { RouterClient } from '@orpc/server'
45
- import type { router } from './router'
46
44
  import { createORPCClient } from '@orpc/client'
47
45
  import { RPCLink } from '@orpc/client/fetch'
46
+ import type { RouterClient } from '@orpc/server'
47
+
48
+ import type { router } from './router'
48
49
 
49
50
  const link = new RPCLink({ origin: 'http://localhost:3000', url: '/rpc' })
50
51
  const client: RouterClient<typeof router> = createORPCClient(link)
@@ -208,7 +209,6 @@ With thanks to [40 past sponsors](https://orpc.dev/sponsors#past-sponsors) who h
208
209
  <tr>
209
210
  <td align="center"><a href="https://claude.com/contact-sales/claude-for-oss" target="_blank" rel="noopener" title="Claude for Open Source"><img src="https://cdn.simpleicons.org/claude" width="64" alt="Claude"/><br />Claude</a></td>
210
211
  <td align="center"><a href="https://pullfrog.com/for-oss" target="_blank" rel="noopener" title="Pullfrog for OSS"><img src="https://avatars.githubusercontent.com/in/1768019?s=128&amp;v=4" width="64" alt="Pullfrog"/><br />Pullfrog</a></td>
211
- <td align="center"><a href="https://macroscope.com/open-source" target="_blank" rel="noopener" title="Macroscope for Open Source"><img src="https://avatars.githubusercontent.com/in/900172?s=128&amp;v=4" width="64" alt="Macroscope"/><br />Macroscope</a></td>
212
212
  </tr>
213
213
  </table>
214
214
 
@@ -0,0 +1,2 @@
1
+ export * from './instrumentation.ts';
2
+ export type { OpenTelemetryPropagationOptions } from './tracer.ts';
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from './instrumentation.js';
@@ -0,0 +1,27 @@
1
+ import type { InstrumentationConfig } from '@opentelemetry/instrumentation';
2
+ import { InstrumentationBase } from '@opentelemetry/instrumentation';
3
+ import type { OpenTelemetryPropagationOptions } from './tracer.ts';
4
+ 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;
14
+ }
15
+ /**
16
+ * OpenTelemetry instrumentation for oRPC. Automatically instruments both
17
+ * client and server for distributed tracing.
18
+ *
19
+ * @see {@link https://orpc.dev/docs/integrations/opentelemetry | OpenTelemetry Integration}
20
+ */
21
+ 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
+ }
@@ -0,0 +1,28 @@
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';
5
+ /**
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
+ }
@@ -0,0 +1,68 @@
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
+ }
28
+ 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
+ }
package/dist/tracer.js ADDED
@@ -0,0 +1,95 @@
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
+ }
78
+ /**
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
+ */
81
+ function contextOf(span) {
82
+ return span instanceof OpenTelemetrySpan ? span.context : context.active();
83
+ }
84
+ /**
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
+ */
88
+ 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()])));
95
+ }
package/package.json CHANGED
@@ -1,52 +1,50 @@
1
1
  {
2
2
  "name": "@orpc/opentelemetry",
3
- "type": "module",
4
- "version": "2.0.0-beta.48",
3
+ "version": "2.0.0-beta.49",
5
4
  "description": "OpenTelemetry integration for oRPC: distributed tracing for typesafe APIs",
6
- "license": "MIT",
7
- "funding": [
8
- "https://github.com/sponsors/dinwwwh",
9
- "https://opencollective.com/middleapi"
5
+ "keywords": [
6
+ "observability",
7
+ "opentelemetry",
8
+ "orpc",
9
+ "otel",
10
+ "tracing",
11
+ "typescript"
10
12
  ],
11
13
  "homepage": "https://orpc.dev",
14
+ "license": "MIT",
12
15
  "repository": {
13
16
  "type": "git",
14
17
  "url": "git+https://github.com/middleapi/orpc.git",
15
18
  "directory": "packages/opentelemetry"
16
19
  },
17
- "keywords": [
18
- "orpc",
19
- "opentelemetry",
20
- "otel",
21
- "tracing",
22
- "observability",
23
- "typescript"
20
+ "funding": [
21
+ "https://github.com/sponsors/dinwwwh",
22
+ "https://opencollective.com/middleapi"
23
+ ],
24
+ "files": [
25
+ "dist"
24
26
  ],
27
+ "type": "module",
25
28
  "sideEffects": false,
26
29
  "exports": {
27
30
  "./package.json": "./package.json",
28
31
  ".": {
29
- "types": "./dist/index.d.mts",
30
- "import": "./dist/index.mjs",
31
- "default": "./dist/index.mjs"
32
+ "types": "./dist/index.d.ts",
33
+ "import": "./dist/index.js",
34
+ "default": "./dist/index.js"
32
35
  }
33
36
  },
34
- "files": [
35
- "dist"
36
- ],
37
- "peerDependencies": {
38
- "@opentelemetry/api": ">=1.9.0",
39
- "@opentelemetry/instrumentation": ">=0.203.0"
40
- },
41
37
  "dependencies": {
42
- "@orpc/shared": "2.0.0-beta.48"
38
+ "@orpc/shared": "2.0.0-beta.49"
43
39
  },
44
40
  "devDependencies": {
45
41
  "@opentelemetry/api": "^1.10.0",
46
42
  "@opentelemetry/instrumentation": "^0.300.0",
47
43
  "@opentelemetry/sdk-trace-node": "^2.12.0"
48
44
  },
49
- "scripts": {
50
- "type:check": "tsc -b"
51
- }
45
+ "peerDependencies": {
46
+ "@opentelemetry/api": ">=1.9.0",
47
+ "@opentelemetry/instrumentation": ">=0.203.0"
48
+ },
49
+ "scripts": {}
52
50
  }
package/dist/index.d.mts DELETED
@@ -1 +0,0 @@
1
- export * from "./instrumentation.mjs";
package/dist/index.mjs DELETED
@@ -1 +0,0 @@
1
- export * from "./instrumentation.mjs";
@@ -1,24 +0,0 @@
1
- import type { InstrumentationConfig, InstrumentationModuleDefinition } from "@opentelemetry/instrumentation";
2
- import { InstrumentationBase } from "@opentelemetry/instrumentation";
3
- export interface ORPCInstrumentationConfig extends InstrumentationConfig {
4
- /**
5
- * Whether to enable automatic OpenTelemetry context/span propagation.
6
- *
7
- * Disable this if propagation is handled elsewhere or managed manually.
8
- *
9
- * @default true
10
- */
11
- propagationEnabled?: boolean | undefined;
12
- }
13
- /**
14
- * OpenTelemetry instrumentation for oRPC. Automatically instruments both
15
- * client and server for distributed tracing.
16
- *
17
- * @see {@link https://orpc.dev/docs/integrations/opentelemetry | OpenTelemetry Integration}
18
- */
19
- export declare class ORPCInstrumentation extends InstrumentationBase<ORPCInstrumentationConfig> {
20
- constructor(config?: ORPCInstrumentationConfig);
21
- protected override init(): InstrumentationModuleDefinition | InstrumentationModuleDefinition[] | void;
22
- override enable(): void;
23
- override disable(): void;
24
- }
@@ -1,23 +0,0 @@
1
- import { context, propagation, trace } from "@opentelemetry/api";
2
- import { InstrumentationBase } from "@opentelemetry/instrumentation";
3
- import { setTracer } from "@orpc/shared";
4
- import pkg from "../package.json" with { type: "json" };
5
- import { OpenTelemetryTracer } from "./tracer.mjs";
6
-
7
- export class ORPCInstrumentation extends InstrumentationBase {
8
- constructor(config = {}) {
9
- super(pkg.name, pkg.version, config);
10
- }
11
- init() {}
12
- enable() {
13
- setTracer(new OpenTelemetryTracer({
14
- tracer: () => this.tracer,
15
- trace,
16
- context,
17
- propagation: this._config.propagationEnabled ?? true ? propagation : undefined
18
- }));
19
- }
20
- disable() {
21
- setTracer(undefined);
22
- }
23
- }
package/dist/tracer.d.mts DELETED
@@ -1,36 +0,0 @@
1
- import type { ContextAPI, Tracer as OpenTelemetryApiTracer, PropagationAPI, Span, TraceAPI } from "@opentelemetry/api";
2
- import type { Promisable, Tracer, TracingAttributeValue, TracingException, TracingExceptionLevel, TracingSpan } from "@orpc/shared";
3
- export declare class OpenTelemetrySpan implements TracingSpan {
4
- readonly span: Span;
5
- constructor(span: Span);
6
- setAttribute(key: string, value: TracingAttributeValue): void;
7
- updateName(name: string): void;
8
- addEvent(name: string): void;
9
- recordException(level: TracingExceptionLevel, exception: TracingException): void;
10
- end(): void;
11
- }
12
- export interface OpenTelemetryTracerOptions {
13
- /**
14
- * Called for every span, so a tracer provider set after construction is picked up.
15
- */
16
- tracer: () => OpenTelemetryApiTracer;
17
- trace: TraceAPI;
18
- context: ContextAPI;
19
- /**
20
- * propagation is optional, can reduce bundle size in some cases.
21
- */
22
- propagation?: PropagationAPI | undefined;
23
- }
24
- export declare class OpenTelemetryTracer implements Tracer {
25
- private readonly tracer;
26
- private readonly trace;
27
- private readonly context;
28
- readonly inject: Tracer["inject"];
29
- readonly extract: Tracer["extract"];
30
- constructor(options: OpenTelemetryTracerOptions);
31
- startSpan(name: string, parent?: TracingSpan): TracingSpan;
32
- startActiveSpan<T>(name: string, parent: TracingSpan | undefined, fn: (span: TracingSpan) => Promise<T>): Promise<T>;
33
- getActiveSpan(): TracingSpan | undefined;
34
- withActiveSpan<T>(span: TracingSpan, fn: () => Promisable<T>): Promisable<T>;
35
- private contextOf;
36
- }
package/dist/tracer.mjs DELETED
@@ -1,71 +0,0 @@
1
- import { SpanStatusCode } from "@opentelemetry/api";
2
- export class OpenTelemetrySpan {
3
- span;
4
- constructor(span) {
5
- this.span = span;
6
- }
7
- setAttribute(key, value) {
8
- this.span.setAttribute(key, value);
9
- }
10
- updateName(name) {
11
- this.span.updateName(name);
12
- }
13
- addEvent(name) {
14
- this.span.addEvent(name);
15
- }
16
- recordException(level, exception) {
17
- this.span.recordException(exception);
18
- if (level === "error") {
19
- this.span.setStatus({
20
- code: SpanStatusCode.ERROR,
21
- message: exception.message
22
- });
23
- }
24
- }
25
- end() {
26
- this.span.end();
27
- }
28
- }
29
- export class OpenTelemetryTracer {
30
- tracer;
31
- trace;
32
- context;
33
- inject;
34
- extract;
35
- constructor(options) {
36
- this.tracer = options.tracer;
37
- this.trace = options.trace;
38
- this.context = options.context;
39
- const propagation = options.propagation;
40
- if (propagation !== undefined) {
41
- this.inject = (span, headers) => {
42
- propagation.inject(this.contextOf(span), headers);
43
- };
44
- this.extract = (headers) => {
45
- const span = this.trace.getSpan(propagation.extract(this.context.active(), headers));
46
- return span === undefined ? undefined : new OpenTelemetrySpan(span);
47
- };
48
- }
49
- }
50
- startSpan(name, parent) {
51
- const span = parent === undefined ? this.tracer().startSpan(name) : this.tracer().startSpan(name, undefined, this.contextOf(parent));
52
- return new OpenTelemetrySpan(span);
53
- }
54
- startActiveSpan(name, parent, fn) {
55
- const callback = (span) => fn(new OpenTelemetrySpan(span));
56
- if (parent === undefined) {
57
- return this.tracer().startActiveSpan(name, callback);
58
- }
59
- return this.tracer().startActiveSpan(name, {}, this.contextOf(parent), callback);
60
- }
61
- getActiveSpan() {
62
- const span = this.trace.getActiveSpan();
63
- return span === undefined ? undefined : new OpenTelemetrySpan(span);
64
- }
65
- withActiveSpan(span, fn) {
66
- return this.context.with(this.contextOf(span), fn);
67
- }
68
- contextOf(parent) {
69
- return this.trace.setSpan(this.context.active(), parent.span);
70
- }
71
- }