@mark1russell7/otel-ts 0.3.0 → 0.4.0
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 +50 -2
- package/dist/.tsbuildinfo +1 -1
- package/dist/context-processors.d.ts +38 -0
- package/dist/context-processors.d.ts.map +1 -0
- package/dist/context-processors.js +76 -0
- package/dist/context-processors.js.map +1 -0
- package/dist/context-propagator.d.ts +39 -0
- package/dist/context-propagator.d.ts.map +1 -0
- package/dist/context-propagator.js +88 -0
- package/dist/context-propagator.js.map +1 -0
- package/dist/context.d.ts +90 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +169 -0
- package/dist/context.js.map +1 -0
- package/dist/node-exporters.d.ts +33 -0
- package/dist/node-exporters.d.ts.map +1 -0
- package/dist/node-exporters.js +70 -0
- package/dist/node-exporters.js.map +1 -0
- package/dist/node.d.ts +28 -10
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +51 -12
- package/dist/node.js.map +1 -1
- package/dist/session-processors.d.ts.map +1 -1
- package/dist/session-processors.js +6 -19
- package/dist/session-processors.js.map +1 -1
- package/package.json +7 -1
- package/src/context-processors.ts +84 -0
- package/src/context-propagator.ts +96 -0
- package/src/context.ts +207 -0
- package/src/node-exporters.ts +88 -0
- package/src/node.ts +75 -22
- package/src/session-processors.ts +6 -19
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The processors of the context keys (docs/CONTEXT-DESIGN.md, section 4).
|
|
3
|
+
*
|
|
4
|
+
* - Each span and each log record gets the value of each defined context key
|
|
5
|
+
* of its context, as an attribute.
|
|
6
|
+
* - A metric gets no attribute of a key with `cardinality: "high"`: one time
|
|
7
|
+
* series for each user id would fill the store.
|
|
8
|
+
*
|
|
9
|
+
* The `session.id` processors (session-processors.ts) are a case of the same
|
|
10
|
+
* two processors. This file imports only types of the SDK packages.
|
|
11
|
+
*/
|
|
12
|
+
import { type Context } from "@opentelemetry/api";
|
|
13
|
+
import type { LogRecordProcessor } from "@opentelemetry/sdk-logs";
|
|
14
|
+
import type { IAttributesProcessor, ViewOptions } from "@opentelemetry/sdk-metrics";
|
|
15
|
+
import type { SpanProcessor } from "@opentelemetry/sdk-trace-base";
|
|
16
|
+
/** The attributes to add to a record that starts in the context `from` */
|
|
17
|
+
export type ContextAttributesReader = (from: Context) => Readonly<Record<string, string>>;
|
|
18
|
+
/** Sets the attributes of `read` on each span as it starts. An attribute that the caller set is kept. */
|
|
19
|
+
export declare function createAttributesSpanProcessor(read: ContextAttributesReader): SpanProcessor;
|
|
20
|
+
/** Sets the attributes of `read` on each log record as it is emitted. An attribute that the caller set is kept. */
|
|
21
|
+
export declare function createAttributesLogRecordProcessor(read: ContextAttributesReader): LogRecordProcessor;
|
|
22
|
+
/** Sets the value of each defined context key on each span */
|
|
23
|
+
export declare function createContextSpanProcessor(): SpanProcessor;
|
|
24
|
+
/** Sets the value of each defined context key on each log record */
|
|
25
|
+
export declare function createContextLogRecordProcessor(): LogRecordProcessor;
|
|
26
|
+
/**
|
|
27
|
+
* Removes each attribute of a defined key with `cardinality: "high"` from a
|
|
28
|
+
* measurement. It reads the rules at each measurement, so a key defined after
|
|
29
|
+
* the start of the meter provider is removed too.
|
|
30
|
+
*/
|
|
31
|
+
export declare function createContextAttributesProcessor(): IAttributesProcessor;
|
|
32
|
+
/**
|
|
33
|
+
* The view for each instrument that removes the keys of high cardinality.
|
|
34
|
+
* Give it as the only view of the meter provider: a second view that matches
|
|
35
|
+
* an instrument makes a second stream of it.
|
|
36
|
+
*/
|
|
37
|
+
export declare function contextMetricView(): ViewOptions;
|
|
38
|
+
//# sourceMappingURL=context-processors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-processors.d.ts","sourceRoot":"","sources":["../src/context-processors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,EAA4B,KAAK,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC5E,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAClE,OAAO,KAAK,EAAE,oBAAoB,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AACpF,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAGnE,0EAA0E;AAC1E,MAAM,MAAM,uBAAuB,GAAG,CAAC,IAAI,EAAE,OAAO,KAAK,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AAE1F,yGAAyG;AACzG,wBAAgB,6BAA6B,CAAC,IAAI,EAAE,uBAAuB,GAAG,aAAa,CAW1F;AAED,mHAAmH;AACnH,wBAAgB,kCAAkC,CAAC,IAAI,EAAE,uBAAuB,GAAG,kBAAkB,CAUpG;AAED,8DAA8D;AAC9D,wBAAgB,0BAA0B,IAAI,aAAa,CAE1D;AAED,oEAAoE;AACpE,wBAAgB,+BAA+B,IAAI,kBAAkB,CAEpE;AAED;;;;GAIG;AACH,wBAAgB,gCAAgC,IAAI,oBAAoB,CAYvE;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,IAAI,WAAW,CAE/C"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The processors of the context keys (docs/CONTEXT-DESIGN.md, section 4).
|
|
3
|
+
*
|
|
4
|
+
* - Each span and each log record gets the value of each defined context key
|
|
5
|
+
* of its context, as an attribute.
|
|
6
|
+
* - A metric gets no attribute of a key with `cardinality: "high"`: one time
|
|
7
|
+
* series for each user id would fill the store.
|
|
8
|
+
*
|
|
9
|
+
* The `session.id` processors (session-processors.ts) are a case of the same
|
|
10
|
+
* two processors. This file imports only types of the SDK packages.
|
|
11
|
+
*/
|
|
12
|
+
import { context } from "@opentelemetry/api";
|
|
13
|
+
import { contextKeyRule, contextValues } from "./context.js";
|
|
14
|
+
/** Sets the attributes of `read` on each span as it starts. An attribute that the caller set is kept. */
|
|
15
|
+
export function createAttributesSpanProcessor(read) {
|
|
16
|
+
return {
|
|
17
|
+
onStart(span, parentContext) {
|
|
18
|
+
for (const [key, value] of Object.entries(read(parentContext))) {
|
|
19
|
+
if (span.attributes[key] === undefined)
|
|
20
|
+
span.setAttribute(key, value);
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
onEnd() { },
|
|
24
|
+
forceFlush: () => Promise.resolve(),
|
|
25
|
+
shutdown: () => Promise.resolve(),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/** Sets the attributes of `read` on each log record as it is emitted. An attribute that the caller set is kept. */
|
|
29
|
+
export function createAttributesLogRecordProcessor(read) {
|
|
30
|
+
return {
|
|
31
|
+
onEmit(logRecord, from) {
|
|
32
|
+
for (const [key, value] of Object.entries(read(from ?? context.active()))) {
|
|
33
|
+
if (logRecord.attributes[key] === undefined)
|
|
34
|
+
logRecord.setAttribute(key, value);
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
forceFlush: () => Promise.resolve(),
|
|
38
|
+
shutdown: () => Promise.resolve(),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/** Sets the value of each defined context key on each span */
|
|
42
|
+
export function createContextSpanProcessor() {
|
|
43
|
+
return createAttributesSpanProcessor(contextValues);
|
|
44
|
+
}
|
|
45
|
+
/** Sets the value of each defined context key on each log record */
|
|
46
|
+
export function createContextLogRecordProcessor() {
|
|
47
|
+
return createAttributesLogRecordProcessor(contextValues);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Removes each attribute of a defined key with `cardinality: "high"` from a
|
|
51
|
+
* measurement. It reads the rules at each measurement, so a key defined after
|
|
52
|
+
* the start of the meter provider is removed too.
|
|
53
|
+
*/
|
|
54
|
+
export function createContextAttributesProcessor() {
|
|
55
|
+
return {
|
|
56
|
+
process(incoming) {
|
|
57
|
+
let result;
|
|
58
|
+
for (const key of Object.keys(incoming)) {
|
|
59
|
+
if (contextKeyRule(key)?.cardinality !== "high")
|
|
60
|
+
continue;
|
|
61
|
+
result ??= { ...incoming };
|
|
62
|
+
delete result[key];
|
|
63
|
+
}
|
|
64
|
+
return result ?? incoming;
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The view for each instrument that removes the keys of high cardinality.
|
|
70
|
+
* Give it as the only view of the meter provider: a second view that matches
|
|
71
|
+
* an instrument makes a second stream of it.
|
|
72
|
+
*/
|
|
73
|
+
export function contextMetricView() {
|
|
74
|
+
return { instrumentName: "*", attributesProcessors: [createContextAttributesProcessor()] };
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=context-processors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-processors.js","sourceRoot":"","sources":["../src/context-processors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,EAAE,OAAO,EAAiC,MAAM,oBAAoB,CAAC;AAI5E,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAK7D,yGAAyG;AACzG,MAAM,UAAU,6BAA6B,CAAC,IAA6B;IACzE,OAAO;QACL,OAAO,CAAC,IAAI,EAAE,aAAa;YACzB,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,EAAE,CAAC;gBAC/D,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,KAAK,SAAS;oBAAE,IAAI,CAAC,YAAY,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YACxE,CAAC;QACH,CAAC;QACD,KAAK,KAAI,CAAC;QACV,UAAU,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE;QACnC,QAAQ,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE;KAClC,CAAC;AACJ,CAAC;AAED,mHAAmH;AACnH,MAAM,UAAU,kCAAkC,CAAC,IAA6B;IAC9E,OA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CAAC,EAAE,CAAC;AAC7F,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The propagator of the one context (docs/CONTEXT-DESIGN.md, section 5), and
|
|
3
|
+
* the guard that lets only one part of a program register the context.
|
|
4
|
+
*
|
|
5
|
+
* - The W3C trace context (`traceparent`, `tracestate`) goes on each call. It
|
|
6
|
+
* holds only random ids.
|
|
7
|
+
* - The W3C baggage goes only on a call that starts in a context with the
|
|
8
|
+
* internal mark (`withInternalCall` of `/context`), and only with the keys
|
|
9
|
+
* that `defineContextKeys` defined with `propagate: "internal"`. A value
|
|
10
|
+
* longer than the `maxLength` of its key is dropped, not cut.
|
|
11
|
+
* - On extract, the baggage keeps only those keys too, so an entry from
|
|
12
|
+
* outside does not travel on. The W3C limits (180 entries, 8192 bytes) are
|
|
13
|
+
* the limits of the W3C baggage propagator of `@opentelemetry/core`.
|
|
14
|
+
*
|
|
15
|
+
* A server that does not trust its caller removes the extracted baggage with
|
|
16
|
+
* `propagation.deleteBaggage(context)`, and keeps the trace context.
|
|
17
|
+
*/
|
|
18
|
+
import { type Baggage, type Context, type TextMapGetter, type TextMapPropagator, type TextMapSetter } from "@opentelemetry/api";
|
|
19
|
+
/** The baggage with only the defined internal keys, each value in its maxLength; undefined when no entry stays */
|
|
20
|
+
export declare function allowedBaggage(baggage: Baggage): Baggage | undefined;
|
|
21
|
+
/** The W3C trace context on each call, and the allowed baggage on internal calls only */
|
|
22
|
+
export declare class ContextPropagator implements TextMapPropagator {
|
|
23
|
+
private readonly traceContext;
|
|
24
|
+
private readonly baggage;
|
|
25
|
+
inject(context: Context, carrier: unknown, setter: TextMapSetter): void;
|
|
26
|
+
extract(context: Context, carrier: unknown, getter: TextMapGetter): Context;
|
|
27
|
+
fields(): string[];
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Take the registration of the context for `caller` (for example "init()" or
|
|
31
|
+
* "startNodeTelemetry()"), and give the function that gives it back. A second
|
|
32
|
+
* registration while the first holds is an error that names the first caller.
|
|
33
|
+
* The OpenTelemetry API itself keeps the first registration and only writes a
|
|
34
|
+
* diagnostic message.
|
|
35
|
+
*/
|
|
36
|
+
export declare function claimContextRegistration(caller: string): () => void;
|
|
37
|
+
/** The caller that holds the registration of the context, or undefined */
|
|
38
|
+
export declare function contextRegistrationOwner(): string | undefined;
|
|
39
|
+
//# sourceMappingURL=context-propagator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-propagator.d.ts","sourceRoot":"","sources":["../src/context-propagator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,EAEL,KAAK,OAAO,EACZ,KAAK,OAAO,EACZ,KAAK,aAAa,EAClB,KAAK,iBAAiB,EACtB,KAAK,aAAa,EACnB,MAAM,oBAAoB,CAAC;AAI5B,kHAAkH;AAClH,wBAAgB,cAAc,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,GAAG,SAAS,CAOpE;AAED,yFAAyF;AACzF,qBAAa,iBAAkB,YAAW,iBAAiB;IACzD,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAmC;IAChE,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA8B;IAEtD,MAAM,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,aAAa,GAAG,IAAI;IAQvE,OAAO,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,aAAa,GAAG,OAAO;IAQ3E,MAAM,IAAI,MAAM,EAAE;CAGnB;AAQD;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,IAAI,CAanE;AAED,0EAA0E;AAC1E,wBAAgB,wBAAwB,IAAI,MAAM,GAAG,SAAS,CAE7D"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The propagator of the one context (docs/CONTEXT-DESIGN.md, section 5), and
|
|
3
|
+
* the guard that lets only one part of a program register the context.
|
|
4
|
+
*
|
|
5
|
+
* - The W3C trace context (`traceparent`, `tracestate`) goes on each call. It
|
|
6
|
+
* holds only random ids.
|
|
7
|
+
* - The W3C baggage goes only on a call that starts in a context with the
|
|
8
|
+
* internal mark (`withInternalCall` of `/context`), and only with the keys
|
|
9
|
+
* that `defineContextKeys` defined with `propagate: "internal"`. A value
|
|
10
|
+
* longer than the `maxLength` of its key is dropped, not cut.
|
|
11
|
+
* - On extract, the baggage keeps only those keys too, so an entry from
|
|
12
|
+
* outside does not travel on. The W3C limits (180 entries, 8192 bytes) are
|
|
13
|
+
* the limits of the W3C baggage propagator of `@opentelemetry/core`.
|
|
14
|
+
*
|
|
15
|
+
* A server that does not trust its caller removes the extracted baggage with
|
|
16
|
+
* `propagation.deleteBaggage(context)`, and keeps the trace context.
|
|
17
|
+
*/
|
|
18
|
+
import { propagation, } from "@opentelemetry/api";
|
|
19
|
+
import { W3CBaggagePropagator, W3CTraceContextPropagator } from "@opentelemetry/core";
|
|
20
|
+
import { contextKeyRule, isInternalCall } from "./context.js";
|
|
21
|
+
/** The baggage with only the defined internal keys, each value in its maxLength; undefined when no entry stays */
|
|
22
|
+
export function allowedBaggage(baggage) {
|
|
23
|
+
let kept = baggage;
|
|
24
|
+
for (const [key, entry] of baggage.getAllEntries()) {
|
|
25
|
+
const rule = contextKeyRule(key);
|
|
26
|
+
if (rule?.propagate !== "internal" || entry.value.length > rule.maxLength)
|
|
27
|
+
kept = kept.removeEntry(key);
|
|
28
|
+
}
|
|
29
|
+
return kept.getAllEntries().length > 0 ? kept : undefined;
|
|
30
|
+
}
|
|
31
|
+
/** The W3C trace context on each call, and the allowed baggage on internal calls only */
|
|
32
|
+
export class ContextPropagator {
|
|
33
|
+
traceContext = new W3CTraceContextPropagator();
|
|
34
|
+
baggage = new W3CBaggagePropagator();
|
|
35
|
+
inject(context, carrier, setter) {
|
|
36
|
+
this.traceContext.inject(context, carrier, setter);
|
|
37
|
+
if (!isInternalCall(context))
|
|
38
|
+
return;
|
|
39
|
+
const baggage = propagation.getBaggage(context);
|
|
40
|
+
const allowed = baggage && allowedBaggage(baggage);
|
|
41
|
+
if (allowed)
|
|
42
|
+
this.baggage.inject(propagation.setBaggage(context, allowed), carrier, setter);
|
|
43
|
+
}
|
|
44
|
+
extract(context, carrier, getter) {
|
|
45
|
+
const next = this.baggage.extract(this.traceContext.extract(context, carrier, getter), carrier, getter);
|
|
46
|
+
const baggage = propagation.getBaggage(next);
|
|
47
|
+
if (!baggage)
|
|
48
|
+
return next;
|
|
49
|
+
const allowed = allowedBaggage(baggage);
|
|
50
|
+
return allowed ? propagation.setBaggage(next, allowed) : propagation.deleteBaggage(next);
|
|
51
|
+
}
|
|
52
|
+
fields() {
|
|
53
|
+
return [...this.traceContext.fields(), ...this.baggage.fields()];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/*
|
|
57
|
+
* The owner of the registration is on globalThis, so two copies of otel-ts in
|
|
58
|
+
* one program also refuse each other.
|
|
59
|
+
*/
|
|
60
|
+
const OWNER = Symbol.for("@mark1russell7/otel-ts/context owner");
|
|
61
|
+
/**
|
|
62
|
+
* Take the registration of the context for `caller` (for example "init()" or
|
|
63
|
+
* "startNodeTelemetry()"), and give the function that gives it back. A second
|
|
64
|
+
* registration while the first holds is an error that names the first caller.
|
|
65
|
+
* The OpenTelemetry API itself keeps the first registration and only writes a
|
|
66
|
+
* diagnostic message.
|
|
67
|
+
*/
|
|
68
|
+
export function claimContextRegistration(caller) {
|
|
69
|
+
const holder = globalThis;
|
|
70
|
+
const owner = holder[OWNER];
|
|
71
|
+
if (owner !== undefined) {
|
|
72
|
+
throw new Error(`The context is registered already by ${owner}. Only one part of a program registers it: call the shutdown() of ${owner} first.`);
|
|
73
|
+
}
|
|
74
|
+
holder[OWNER] = caller;
|
|
75
|
+
let released = false;
|
|
76
|
+
return () => {
|
|
77
|
+
if (released)
|
|
78
|
+
return;
|
|
79
|
+
released = true;
|
|
80
|
+
if (holder[OWNER] === caller)
|
|
81
|
+
delete holder[OWNER];
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/** The caller that holds the registration of the context, or undefined */
|
|
85
|
+
export function contextRegistrationOwner() {
|
|
86
|
+
return globalThis[OWNER];
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=context-propagator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-propagator.js","sourceRoot":"","sources":["../src/context-propagator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,EACL,WAAW,GAMZ,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,oBAAoB,EAAE,yBAAyB,EAAE,MAAM,qBAAqB,CAAC;AACtF,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAE9D,kHAAkH;AAClH,MAAM,UAAU,cAAc,CAAC,OAAgB;IAC7C,IAAI,IAAI,GAAG,OAAO,CAAC;IACnB,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,OAAO,CAAC,aAAa,EAAE,EAAE,CAAC;QACnD,MAAM,IAAI,GAAG,cAAc,CAAC,GAAG,CAAC,CAAC;QACjC,IAAI,IAAI,EAAE,SAAS,KAAK,UAAU,IAAI,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC,SAAS;YAAE,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;IAC1G,CAAC;IACD,OAAO,IAAI,CAAC,aAAa,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;AAC5D,CAAC;AAED,yFAAyF;AACzF,MAAM,OAAO,iBAAiB;IACX,YAAY,GAAG,IAAI,yBAAyB,EAAE,CAAC;IAC/C,OAAO,GAAG,IAAI,oBAAoB,EAAE,CAAC;IAEtD,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sCAAsC,CAAC,CAAC;AAEjE;;;;;;GAMG;AACH,MAAM,UAAU,wBAAwB,CAAC,MAAc;IACrD,MAAM,MAAM,GAAG,UAAkC,CAAC;IAClD,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IAC5B,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACxB,MAAM,IAAI,KAAK,CAAC,wCAAwC,KAAK,qEAAqE,KAAK,SAAS,CAAC,CAAC;IACpJ,CAAC;IACD,MAAM,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC;IACvB,IAAI,QAAQ,GAAG,KAAK,CAAC;IACrB,OAAO,GAAG,EAAE;QACV,IAAI,QAAQ;YAAE,OAAO;QACrB,QAAQ,GAAG,IAAI,CAAC;QAChB,IAAI,MAAM,CAAC,KAAK,CAAC,KAAK,MAAM;YAAE,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;IACrD,CAAC,CAAC;AACJ,CAAC;AAED,0EAA0E;AAC1E,MAAM,UAAU,wBAAwB;IACtC,OAAQ,UAAmC,CAAC,KAAK,CAAC,CAAC;AACrD,CAAC"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The entry `@mark1russell7/otel-ts/context`: the one call that adds context
|
|
3
|
+
* ids, for browsers and Node.js (docs/CONTEXT-DESIGN.md, section 3).
|
|
4
|
+
*
|
|
5
|
+
* This entry depends on `@opentelemetry/api` only. It registers nothing: the
|
|
6
|
+
* context manager, the propagators and the processors come from `init()` or
|
|
7
|
+
* `startNodeTelemetry()`. Without a registered context manager, `withContext`
|
|
8
|
+
* still runs its function, but the values do not stay after an `await`.
|
|
9
|
+
*
|
|
10
|
+
* - The keys are data: `defineContextKeys` names each key, its cardinality and
|
|
11
|
+
* where it may go. A key that no call defined is refused.
|
|
12
|
+
* - A key with `propagate: "internal"` goes in the W3C baggage, so the
|
|
13
|
+
* propagator of otel-ts can send it on an internal call.
|
|
14
|
+
* - A key with `propagate: "none"` stays in a context entry of its own. No
|
|
15
|
+
* baggage propagator sees it, so it never leaves the process.
|
|
16
|
+
*
|
|
17
|
+
* @packageDocumentation
|
|
18
|
+
*/
|
|
19
|
+
import { type Context } from "@opentelemetry/api";
|
|
20
|
+
/** How many distinct values a key has: "low" may become a metric attribute, "high" never does */
|
|
21
|
+
export type ContextKeyCardinality = "low" | "high";
|
|
22
|
+
/** Where a key may go: "internal" on internal calls (in the baggage), "none" never out of the process */
|
|
23
|
+
export type ContextKeyPropagation = "internal" | "none";
|
|
24
|
+
/** The rules of one context key */
|
|
25
|
+
export interface ContextKeySpec {
|
|
26
|
+
readonly cardinality: ContextKeyCardinality;
|
|
27
|
+
readonly propagate: ContextKeyPropagation;
|
|
28
|
+
/** The longest value, in UTF-16 code units. Default: 256. */
|
|
29
|
+
readonly maxLength?: number;
|
|
30
|
+
}
|
|
31
|
+
/** The rules of one key, with each default filled in */
|
|
32
|
+
export interface ContextKeyRule {
|
|
33
|
+
readonly key: string;
|
|
34
|
+
readonly cardinality: ContextKeyCardinality;
|
|
35
|
+
readonly propagate: ContextKeyPropagation;
|
|
36
|
+
readonly maxLength: number;
|
|
37
|
+
}
|
|
38
|
+
/** The values of a set of keys. An `undefined` or `null` value adds nothing. */
|
|
39
|
+
export type ContextValues<K extends string = string> = {
|
|
40
|
+
readonly [P in K]?: string | null | undefined;
|
|
41
|
+
};
|
|
42
|
+
/** The keys that one call of `defineContextKeys` defined, with typed calls */
|
|
43
|
+
export interface ContextKeys<K extends string> {
|
|
44
|
+
/** The rules of each key of this set */
|
|
45
|
+
readonly rules: ReadonlyMap<K, ContextKeyRule>;
|
|
46
|
+
/** `withContext`, with the key names of this set */
|
|
47
|
+
with<T>(values: ContextValues<K>, fn: () => T): T;
|
|
48
|
+
/** `contextValue`, with the key names of this set */
|
|
49
|
+
get(key: K, from?: Context): string | undefined;
|
|
50
|
+
}
|
|
51
|
+
/** The default of `maxLength` */
|
|
52
|
+
export declare const DEFAULT_MAX_LENGTH = 256;
|
|
53
|
+
/**
|
|
54
|
+
* Define context keys, one time for each program part. A second definition of
|
|
55
|
+
* a key with the same rules is accepted, so a library and an app can both
|
|
56
|
+
* define `request.id`. A second definition with other rules is an error.
|
|
57
|
+
*/
|
|
58
|
+
export declare function defineContextKeys<K extends string>(specs: {
|
|
59
|
+
readonly [P in K]: ContextKeySpec;
|
|
60
|
+
}): ContextKeys<K>;
|
|
61
|
+
/** The rules of a defined key, or undefined. The processors and the propagator read them. */
|
|
62
|
+
export declare function contextKeyRule(key: string): ContextKeyRule | undefined;
|
|
63
|
+
/** The rules of each defined key */
|
|
64
|
+
export declare function contextKeyRules(): readonly ContextKeyRule[];
|
|
65
|
+
/**
|
|
66
|
+
* Run `fn` in a new context with these values, on top of the values of the
|
|
67
|
+
* active context. The async storage keeps the context through `await`,
|
|
68
|
+
* timers and callbacks. A key that is not defined, a value that is not a
|
|
69
|
+
* string and a value longer than the `maxLength` of its key are errors.
|
|
70
|
+
*/
|
|
71
|
+
export declare function withContext<T>(values: ContextValues, fn: () => T): T;
|
|
72
|
+
/** The active context (or `from`) with these values: `withContext` without the call */
|
|
73
|
+
export declare function contextWith(values: ContextValues, from?: Context): Context;
|
|
74
|
+
/** The value of a defined key in the active context (or `from`), or undefined */
|
|
75
|
+
export declare function contextValue(key: string, from?: Context): string | undefined;
|
|
76
|
+
/**
|
|
77
|
+
* The value of each defined key in the active context (or `from`). A baggage
|
|
78
|
+
* entry of a key that is not defined is not in the result: an entry from
|
|
79
|
+
* outside does not become an attribute.
|
|
80
|
+
*/
|
|
81
|
+
export declare function contextValues(from?: Context): Readonly<Record<string, string>>;
|
|
82
|
+
/**
|
|
83
|
+
* Run `fn` with the internal mark: the propagator of otel-ts sends the baggage
|
|
84
|
+
* only on a call that starts in such a context. `@procedures/client` sets the
|
|
85
|
+
* mark around the send of each of its transports.
|
|
86
|
+
*/
|
|
87
|
+
export declare function withInternalCall<T>(fn: () => T): T;
|
|
88
|
+
/** True when the active context (or `from`) has the internal mark */
|
|
89
|
+
export declare function isInternalCall(from?: Context): boolean;
|
|
90
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,EAA0C,KAAK,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAE1F,iGAAiG;AACjG,MAAM,MAAM,qBAAqB,GAAG,KAAK,GAAG,MAAM,CAAC;AAEnD,yGAAyG;AACzG,MAAM,MAAM,qBAAqB,GAAG,UAAU,GAAG,MAAM,CAAC;AAExD,mCAAmC;AACnC,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,WAAW,EAAE,qBAAqB,CAAC;IAC5C,QAAQ,CAAC,SAAS,EAAE,qBAAqB,CAAC;IAC1C,6DAA6D;IAC7D,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED,wDAAwD;AACxD,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,WAAW,EAAE,qBAAqB,CAAC;IAC5C,QAAQ,CAAC,SAAS,EAAE,qBAAqB,CAAC;IAC1C,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B;AAED,gFAAgF;AAChF,MAAM,MAAM,aAAa,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,IAAI;IAAE,QAAQ,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS;CAAE,CAAC;AAEzG,8EAA8E;AAC9E,MAAM,WAAW,WAAW,CAAC,CAAC,SAAS,MAAM;IAC3C,wCAAwC;IACxC,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC,EAAE,cAAc,CAAC,CAAC;IAC/C,oDAAoD;IACpD,IAAI,CAAC,CAAC,EAAE,MAAM,EAAE,aAAa,CAAC,C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 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The entry `@mark1russell7/otel-ts/context`: the one call that adds context
|
|
3
|
+
* ids, for browsers and Node.js (docs/CONTEXT-DESIGN.md, section 3).
|
|
4
|
+
*
|
|
5
|
+
* This entry depends on `@opentelemetry/api` only. It registers nothing: the
|
|
6
|
+
* context manager, the propagators and the processors come from `init()` or
|
|
7
|
+
* `startNodeTelemetry()`. Without a registered context manager, `withContext`
|
|
8
|
+
* still runs its function, but the values do not stay after an `await`.
|
|
9
|
+
*
|
|
10
|
+
* - The keys are data: `defineContextKeys` names each key, its cardinality and
|
|
11
|
+
* where it may go. A key that no call defined is refused.
|
|
12
|
+
* - A key with `propagate: "internal"` goes in the W3C baggage, so the
|
|
13
|
+
* propagator of otel-ts can send it on an internal call.
|
|
14
|
+
* - A key with `propagate: "none"` stays in a context entry of its own. No
|
|
15
|
+
* baggage propagator sees it, so it never leaves the process.
|
|
16
|
+
*
|
|
17
|
+
* @packageDocumentation
|
|
18
|
+
*/
|
|
19
|
+
import { context, createContextKey, propagation } from "@opentelemetry/api";
|
|
20
|
+
/** The default of `maxLength` */
|
|
21
|
+
export const DEFAULT_MAX_LENGTH = 256;
|
|
22
|
+
/*
|
|
23
|
+
* The W3C baggage key is a token (RFC 7230): letters, digits and
|
|
24
|
+
* !#$%&'*+-.^_`|~. A dot is the separator of the semantic conventions.
|
|
25
|
+
*/
|
|
26
|
+
const KEY_PATTERN = /^[A-Za-z0-9!#$%&'*+\-.^_`|~]+$/;
|
|
27
|
+
/*
|
|
28
|
+
* The registry is on globalThis, so two copies of this package in one program
|
|
29
|
+
* share their keys, as the copies of @opentelemetry/api share their globals.
|
|
30
|
+
*/
|
|
31
|
+
const REGISTRY = Symbol.for("@mark1russell7/otel-ts/context-keys");
|
|
32
|
+
function registry() {
|
|
33
|
+
const holder = globalThis;
|
|
34
|
+
return (holder[REGISTRY] ??= new Map());
|
|
35
|
+
}
|
|
36
|
+
/** The context entry of the keys with `propagate: "none"` */
|
|
37
|
+
const LOCAL_VALUES = createContextKey("@mark1russell7/otel-ts/context local values");
|
|
38
|
+
/** The context entry of the internal mark */
|
|
39
|
+
const INTERNAL_CALL = createContextKey("@mark1russell7/otel-ts/context internal call");
|
|
40
|
+
function ruleOf(key, spec) {
|
|
41
|
+
if (!KEY_PATTERN.test(key))
|
|
42
|
+
throw new TypeError(`The context key "${key}" is not a W3C baggage key: use letters, digits and . _ -`);
|
|
43
|
+
if (spec.cardinality !== "low" && spec.cardinality !== "high")
|
|
44
|
+
throw new TypeError(`The context key "${key}" has no cardinality "low" or "high"`);
|
|
45
|
+
if (spec.propagate !== "internal" && spec.propagate !== "none")
|
|
46
|
+
throw new TypeError(`The context key "${key}" has no propagate "internal" or "none"`);
|
|
47
|
+
const maxLength = spec.maxLength ?? DEFAULT_MAX_LENGTH;
|
|
48
|
+
if (!Number.isInteger(maxLength) || maxLength < 1)
|
|
49
|
+
throw new TypeError(`The maxLength of the context key "${key}" is not a positive integer`);
|
|
50
|
+
return { key, cardinality: spec.cardinality, propagate: spec.propagate, maxLength };
|
|
51
|
+
}
|
|
52
|
+
function sameRule(a, b) {
|
|
53
|
+
return a.cardinality === b.cardinality && a.propagate === b.propagate && a.maxLength === b.maxLength;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Define context keys, one time for each program part. A second definition of
|
|
57
|
+
* a key with the same rules is accepted, so a library and an app can both
|
|
58
|
+
* define `request.id`. A second definition with other rules is an error.
|
|
59
|
+
*/
|
|
60
|
+
export function defineContextKeys(specs) {
|
|
61
|
+
const rules = new Map();
|
|
62
|
+
for (const key of Object.keys(specs))
|
|
63
|
+
rules.set(key, ruleOf(key, specs[key]));
|
|
64
|
+
const known = registry();
|
|
65
|
+
// Check all keys first, so a refused set registers no key
|
|
66
|
+
for (const rule of rules.values()) {
|
|
67
|
+
const earlier = known.get(rule.key);
|
|
68
|
+
if (earlier && !sameRule(earlier, rule)) {
|
|
69
|
+
throw new Error(`The context key "${rule.key}" is defined already with other rules (${describe(earlier)}), not ${describe(rule)}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
for (const rule of rules.values())
|
|
73
|
+
known.set(rule.key, rule);
|
|
74
|
+
return {
|
|
75
|
+
rules,
|
|
76
|
+
with: (values, fn) => withContext(values, fn),
|
|
77
|
+
get: (key, from) => contextValue(key, from),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function describe(rule) {
|
|
81
|
+
return `cardinality ${rule.cardinality}, propagate ${rule.propagate}, maxLength ${rule.maxLength}`;
|
|
82
|
+
}
|
|
83
|
+
function knownRule(key) {
|
|
84
|
+
const rule = registry().get(key);
|
|
85
|
+
if (!rule)
|
|
86
|
+
throw new Error(`The context key "${key}" is not defined: call defineContextKeys first`);
|
|
87
|
+
return rule;
|
|
88
|
+
}
|
|
89
|
+
/** The rules of a defined key, or undefined. The processors and the propagator read them. */
|
|
90
|
+
export function contextKeyRule(key) {
|
|
91
|
+
return registry().get(key);
|
|
92
|
+
}
|
|
93
|
+
/** The rules of each defined key */
|
|
94
|
+
export function contextKeyRules() {
|
|
95
|
+
return [...registry().values()];
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Run `fn` in a new context with these values, on top of the values of the
|
|
99
|
+
* active context. The async storage keeps the context through `await`,
|
|
100
|
+
* timers and callbacks. A key that is not defined, a value that is not a
|
|
101
|
+
* string and a value longer than the `maxLength` of its key are errors.
|
|
102
|
+
*/
|
|
103
|
+
export function withContext(values, fn) {
|
|
104
|
+
return context.with(contextWith(values), fn);
|
|
105
|
+
}
|
|
106
|
+
/** The active context (or `from`) with these values: `withContext` without the call */
|
|
107
|
+
export function contextWith(values, from = context.active()) {
|
|
108
|
+
let baggage = propagation.getBaggage(from) ?? propagation.createBaggage();
|
|
109
|
+
let local;
|
|
110
|
+
for (const [key, value] of Object.entries(values)) {
|
|
111
|
+
if (value === undefined || value === null)
|
|
112
|
+
continue;
|
|
113
|
+
const rule = knownRule(key);
|
|
114
|
+
if (typeof value !== "string")
|
|
115
|
+
throw new TypeError(`The value of the context key "${key}" is not a string`);
|
|
116
|
+
if (value.length > rule.maxLength)
|
|
117
|
+
throw new RangeError(`The value of the context key "${key}" has ${value.length} characters, more than its maxLength ${rule.maxLength}`);
|
|
118
|
+
if (rule.propagate === "internal") {
|
|
119
|
+
baggage = baggage.setEntry(key, { value });
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
local ??= new Map(localValues(from));
|
|
123
|
+
local.set(key, value);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
let next = propagation.setBaggage(from, baggage);
|
|
127
|
+
if (local)
|
|
128
|
+
next = next.setValue(LOCAL_VALUES, local);
|
|
129
|
+
return next;
|
|
130
|
+
}
|
|
131
|
+
function localValues(from) {
|
|
132
|
+
return from.getValue(LOCAL_VALUES) ?? new Map();
|
|
133
|
+
}
|
|
134
|
+
/** The value of a defined key in the active context (or `from`), or undefined */
|
|
135
|
+
export function contextValue(key, from = context.active()) {
|
|
136
|
+
const rule = knownRule(key);
|
|
137
|
+
if (rule.propagate === "none")
|
|
138
|
+
return localValues(from).get(key);
|
|
139
|
+
return propagation.getBaggage(from)?.getEntry(key)?.value;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* The value of each defined key in the active context (or `from`). A baggage
|
|
143
|
+
* entry of a key that is not defined is not in the result: an entry from
|
|
144
|
+
* outside does not become an attribute.
|
|
145
|
+
*/
|
|
146
|
+
export function contextValues(from = context.active()) {
|
|
147
|
+
const result = {};
|
|
148
|
+
const known = registry();
|
|
149
|
+
for (const [key, entry] of propagation.getBaggage(from)?.getAllEntries() ?? []) {
|
|
150
|
+
if (known.get(key)?.propagate === "internal")
|
|
151
|
+
result[key] = entry.value;
|
|
152
|
+
}
|
|
153
|
+
for (const [key, value] of localValues(from))
|
|
154
|
+
result[key] = value;
|
|
155
|
+
return result;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Run `fn` with the internal mark: the propagator of otel-ts sends the baggage
|
|
159
|
+
* only on a call that starts in such a context. `@procedures/client` sets the
|
|
160
|
+
* mark around the send of each of its transports.
|
|
161
|
+
*/
|
|
162
|
+
export function withInternalCall(fn) {
|
|
163
|
+
return context.with(context.active().setValue(INTERNAL_CALL, true), fn);
|
|
164
|
+
}
|
|
165
|
+
/** True when the active context (or `from`) has the internal mark */
|
|
166
|
+
export function isInternalCall(from = context.active()) {
|
|
167
|
+
return from.getValue(INTERNAL_CALL) === true;
|
|
168
|
+
}
|
|
169
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,EAAE,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAgB,MAAM,oBAAoB,CAAC;AAqC1F,iCAAiC;AACjC,MAAM,CAAC,MAAM,kBAAkB,GAAG,GAAG,CAAC;AAEtC;;;GAGG;AACH,MAAM,WAAW,GAAG,gCAAgC,CAAC;AAErD;;;GAGG;AACH,MAAM,QAAQ,GAAG,MAAM,CAAC,GAAG,CAAC,qCAAqC,CAAC,CAAC;AAEnE,SAAS,QAAQ;IACf,MAAM,MAAM,GAAG,UAAuC,CAAC;IACvD,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,IAAI,GAAG,EAAE,CAAC,CAAC;AAC1C,CAAC;AAED,6DAA6D;AAC7D,MAAM,YAAY,GAAG,gBAAgB,CAAC,6CAA6C,CAAC,CAAC;AACrF,6CAA6C;AAC7C,MAAM,aAAa,GAAG,gBAAgB,CAAC,8CAA8C,CAAC,CAAC;AAEvF,SAAS,MAAM,CAAC,GAAW,EAAE,IAAoB;IAC/C,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC;QAAE,MAAM,IAAI,SAAS,CAAC,oBAAoB,GAAG,2DAA2D,CAAC,CAAC;IACpI,IA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AAC;QAAE,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;IAClE,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAI,EAAW;IAC7C,OAAO,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,aAAa,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC;AAC1E,CAAC;AAED,qEAAqE;AACrE,MAAM,UAAU,cAAc,CAAC,OAAgB,OAAO,CAAC,MAAM,EAAE;IAC7D,OAAO,IAAI,CAAC,QAAQ,CAAC,aAAa,CAAC,KAAK,IAAI,CAAC;AAC/C,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type LogRecordExporter, type LogRecordProcessor } from "@opentelemetry/sdk-logs";
|
|
2
|
+
import { type SpanExporter, type SpanProcessor } from "@opentelemetry/sdk-trace-base";
|
|
3
|
+
type Env = Readonly<Record<string, string | undefined>>;
|
|
4
|
+
/** A signal that has processors */
|
|
5
|
+
export type OtlpSignal = "traces" | "logs";
|
|
6
|
+
/** An exporter name of `OTEL_TRACES_EXPORTER` and `OTEL_LOGS_EXPORTER` that otel-ts makes */
|
|
7
|
+
export type ExporterName = "otlp" | "console";
|
|
8
|
+
/** An OTLP protocol that otel-ts sends */
|
|
9
|
+
export type OtlpProtocol = "http/protobuf" | "http/json";
|
|
10
|
+
/**
|
|
11
|
+
* The exporters of `OTEL_<SIGNAL>_EXPORTER`, a list with commas. Default:
|
|
12
|
+
* `otlp`. With `none` in the list: no exporter. Another name is an error.
|
|
13
|
+
*/
|
|
14
|
+
export declare function exporterNames(signal: OtlpSignal, env: Env): ExporterName[];
|
|
15
|
+
/**
|
|
16
|
+
* The protocol of `OTEL_EXPORTER_OTLP_<SIGNAL>_PROTOCOL`, else of
|
|
17
|
+
* `OTEL_EXPORTER_OTLP_PROTOCOL`. Default: `http/protobuf`, as the SDK.
|
|
18
|
+
*/
|
|
19
|
+
export declare function otlpProtocol(signal: OtlpSignal, env: Env): OtlpProtocol;
|
|
20
|
+
/**
|
|
21
|
+
* The span processors: the context processor, then a processor for `exporter`,
|
|
22
|
+
* or for each exporter of the variables. No exporter gives no processor.
|
|
23
|
+
* An OTLP exporter reads its endpoint, headers and timeout from `process.env`.
|
|
24
|
+
*/
|
|
25
|
+
export declare function nodeSpanProcessors(env: Env, exporter?: SpanExporter): SpanProcessor[];
|
|
26
|
+
/**
|
|
27
|
+
* The log record processors: the context processor, then a processor for
|
|
28
|
+
* `exporter`, or for each exporter of the variables. No exporter gives no processor.
|
|
29
|
+
* An OTLP exporter reads its endpoint, headers and timeout from `process.env`.
|
|
30
|
+
*/
|
|
31
|
+
export declare function nodeLogRecordProcessors(env: Env, exporter?: LogRecordExporter): LogRecordProcessor[];
|
|
32
|
+
export {};
|
|
33
|
+
//# sourceMappingURL=node-exporters.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node-exporters.d.ts","sourceRoot":"","sources":["../src/node-exporters.ts"],"names":[],"mappings":"AAYA,OAAO,EAIL,KAAK,iBAAiB,EACtB,KAAK,kBAAkB,EACxB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAgE,KAAK,YAAY,EAAE,KAAK,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAGpJ,KAAK,GAAG,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC;AAExD,mCAAmC;AACnC,MAAM,MAAM,UAAU,GAAG,QAAQ,GAAG,MAAM,CAAC;AAC3C,6FAA6F;AAC7F,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,SAAS,CAAC;AAC9C,0CAA0C;AAC1C,MAAM,MAAM,YAAY,GAAG,eAAe,GAAG,WAAW,CAAC;AAEzD;;;GAGG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,UAAU,EAAE,GAAG,EAAE,GAAG,GAAG,YAAY,EAAE,CAS1E;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,UAAU,EAAE,GAAG,EAAE,GAAG,GAAG,YAAY,CAKvE;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,GAAG,EAAE,QAAQ,CAAC,EAAE,YAAY,GAAG,aAAa,EAAE,CASrF;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,GAAG,EAAE,QAAQ,CAAC,EAAE,iBAAiB,GAAG,kBAAkB,EAAE,CASpG"}
|