@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 +3 -2
- package/dist/index.js +1 -1
- package/dist/instrumentation.d.ts +17 -18
- package/dist/instrumentation.js +29 -27
- package/dist/package.json.js +4 -0
- package/dist/tracer.d.ts +17 -67
- package/dist/tracer.js +76 -89
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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
|
|
1
|
+
export { ORPCInstrumentation } from "./instrumentation.js";
|
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { InstrumentationBase } from
|
|
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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
21
|
+
constructor(config?: ORPCInstrumentationConfig);
|
|
22
|
+
protected override init(): void;
|
|
23
|
+
override enable(): void;
|
|
24
|
+
override disable(): void;
|
|
25
|
+
override isEnabled(): boolean;
|
|
26
|
+
}
|
package/dist/instrumentation.js
CHANGED
|
@@ -1,28 +1,30 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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 };
|
package/dist/tracer.d.ts
CHANGED
|
@@ -1,68 +1,18 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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 {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
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
|
-
|
|
70
|
+
return span instanceof OpenTelemetrySpan ? span.context : context.active();
|
|
83
71
|
}
|
|
84
72
|
/**
|
|
85
|
-
|
|
86
|
-
|
|
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
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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.
|
|
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.
|
|
38
|
+
"@orpc/shared": "2.0.0-beta.50"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@opentelemetry/api": "^1.10.0",
|