@mark1russell7/otel-ts 0.10.0 → 0.10.1

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
@@ -234,6 +234,7 @@ The rules:
234
234
  - `"all"`: the trace and the baggage, for a caller that passed its check.
235
235
  - `"trace"` (the default): the trace only. The baggage of the caller goes, and each key that a propagator of the program read from the headers goes too.
236
236
  - `"none"`: nothing. The call starts a new trace, for a public edge.
237
+ - `runInIncomingSpan(name, headers, fn, { trust, tracer, attributes })` of the entry `@mark1russell7/otel-ts/incoming-span` starts `fn` in a span of the kind SERVER. The parent of the span is `incomingContext(headers, { trust })`, so the span continues the trace of the caller. Use it for a route that no instrumentation covers, for example a raw route of `node:http`. The entry is apart from `/context`, so `/node/context` does not load it. The span ends when `fn` returns or its promise settles. An error of `fn` gives the span the status ERROR and passes through.
237
238
 
238
239
  In the browser, `init()` registers the context when `tracing` is on (the default):
239
240
 
@@ -0,0 +1,39 @@
1
+ /**
2
+ * The entry `@mark1russell7/otel-ts/incoming-span`: the span of the server for an incoming call, in the trace of
3
+ * its caller (docs/CONTEXT-DESIGN.md, section 5).
4
+ *
5
+ * This entry depends on `@opentelemetry/api` only. It is not in `/context`, because `/node/context` loads `/context`
6
+ * in each service. Thus only a program that imports this entry pays for its code.
7
+ *
8
+ * @packageDocumentation
9
+ */
10
+ import { type Attributes, type Tracer } from "@opentelemetry/api";
11
+ import { type IncomingContextOptions, type IncomingHeaders } from "./context.js";
12
+ /** The options of `runInIncomingSpan` */
13
+ export interface IncomingSpanOptions extends IncomingContextOptions {
14
+ /** The attributes of the span at its start, for example `http.request.method` and `url.path`. */
15
+ readonly attributes?: Attributes;
16
+ /** The tracer of the span, for example `trace.getTracer("my-service")`. Default: the global tracer `@mark1russell7/otel-ts`. */
17
+ readonly tracer?: Tracer;
18
+ }
19
+ /**
20
+ * This function runs the handler of an incoming call in a span of the server. The span is of the kind SERVER, and its
21
+ * parent is the context of `incomingContext(headers, options)`. Thus with the default trust "trace", the span continues
22
+ * the trace of the `traceparent` header of the caller, and it takes no baggage. With no valid `traceparent`, the span
23
+ * starts a new trace. It is for a route that no instrumentation of the framework covers, for example a raw route of
24
+ * `node:http` or a handler of a Fetch `Request`.
25
+ *
26
+ * The span ends when `fn` returns, or when the promise of `fn` settles. An error of `fn` sets the status of the span to
27
+ * ERROR, and passes through.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * const answer = await runInIncomingSpan(`POST ${path}`, req.headers, () => handle(body), {
32
+ * tracer: trace.getTracer("my-service"),
33
+ * attributes: { "http.request.method": "POST", "url.path": path },
34
+ * });
35
+ * ```
36
+ */
37
+ export declare function runInIncomingSpan<T>(name: string, headers: IncomingHeaders, fn: () => T, options?: IncomingSpanOptions): T;
38
+ export type { IncomingContextOptions, IncomingHeaders, IncomingTrust } from "./context.js";
39
+ //# sourceMappingURL=incoming-span.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"incoming-span.d.ts","sourceRoot":"","sources":["../src/incoming-span.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAmB,KAAK,UAAU,EAAE,KAAK,MAAM,EAAE,MAAM,oBAAoB,CAAC;AACnF,OAAO,EAAmB,KAAK,sBAAsB,EAAE,KAAK,eAAe,EAAE,MAAM,cAAc,CAAC;AAGlG,yCAAyC;AACzC,MAAM,WAAW,mBAAoB,SAAQ,sBAAsB;IACjE,iGAAiG;IACjG,QAAQ,CAAC,UAAU,CAAC,EAAE,UAAU,CAAC;IACjC,gIAAgI;IAChI,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,eAAe,EAAE,EAAE,EAAE,MAAM,CAAC,EAAE,OAAO,GAAE,mBAAwB,GAAG,CAAC,CAK9H;AAED,YAAY,EAAE,sBAAsB,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC"}
@@ -0,0 +1,37 @@
1
+ /**
2
+ * The entry `@mark1russell7/otel-ts/incoming-span`: the span of the server for an incoming call, in the trace of
3
+ * its caller (docs/CONTEXT-DESIGN.md, section 5).
4
+ *
5
+ * This entry depends on `@opentelemetry/api` only. It is not in `/context`, because `/node/context` loads `/context`
6
+ * in each service. Thus only a program that imports this entry pays for its code.
7
+ *
8
+ * @packageDocumentation
9
+ */
10
+ import { SpanKind, trace } from "@opentelemetry/api";
11
+ import { incomingContext } from "./context.js";
12
+ import { runInSpan } from "./span-run.js";
13
+ /**
14
+ * This function runs the handler of an incoming call in a span of the server. The span is of the kind SERVER, and its
15
+ * parent is the context of `incomingContext(headers, options)`. Thus with the default trust "trace", the span continues
16
+ * the trace of the `traceparent` header of the caller, and it takes no baggage. With no valid `traceparent`, the span
17
+ * starts a new trace. It is for a route that no instrumentation of the framework covers, for example a raw route of
18
+ * `node:http` or a handler of a Fetch `Request`.
19
+ *
20
+ * The span ends when `fn` returns, or when the promise of `fn` settles. An error of `fn` sets the status of the span to
21
+ * ERROR, and passes through.
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * const answer = await runInIncomingSpan(`POST ${path}`, req.headers, () => handle(body), {
26
+ * tracer: trace.getTracer("my-service"),
27
+ * attributes: { "http.request.method": "POST", "url.path": path },
28
+ * });
29
+ * ```
30
+ */
31
+ export function runInIncomingSpan(name, headers, fn, options = {}) {
32
+ const parent = incomingContext(headers, options.trust === undefined ? {} : { trust: options.trust });
33
+ const tracer = options.tracer ?? trace.getTracer("@mark1russell7/otel-ts");
34
+ const span = tracer.startSpan(name, { kind: SpanKind.SERVER, ...(options.attributes ? { attributes: options.attributes } : {}) }, parent);
35
+ return runInSpan(span, parent, fn);
36
+ }
37
+ //# sourceMappingURL=incoming-span.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"incoming-span.js","sourceRoot":"","sources":["../src/incoming-span.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAgC,MAAM,oBAAoB,CAAC;AACnF,OAAO,EAAE,eAAe,EAAqD,MAAM,cAAc,CAAC;AAClG,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAU1C;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,iBAAiB,CAAI,IAAY,EAAE,OAAwB,EAAE,EAAW,EAAE,UAA+B,EAAE;IACzH,MAAM,MAAM,GAAG,eAAe,CAAC,OAAO,EAAE,OAAO,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC;IACrG,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,KAAK,CAAC,SAAS,CAAC,wBAAwB,CAAC,CAAC;IAC3E,MAAM,IAAI,GAAG,MAAM,CAAC,SAAS,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,CAAC,MAAM,EAAE,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;IAC1I,OAAO,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC;AACrC,CAAC"}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * The run of a function in a span that is already started: the one rule of `runInRemoteTrace` and
3
+ * `runInIncomingSpan`. This module is internal. No entry exports it.
4
+ *
5
+ * @packageDocumentation
6
+ */
7
+ import { type Context, type Span } from "@opentelemetry/api";
8
+ /**
9
+ * This function runs `fn` with `span` active in `parent`, and ends the span. The span ends when `fn` returns, or when
10
+ * the promise of `fn` settles. An error of `fn` sets the status of the span to ERROR, records the error and passes through.
11
+ */
12
+ export declare function runInSpan<T>(span: Span, parent: Context, fn: () => T): T;
13
+ //# sourceMappingURL=span-run.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"span-run.d.ts","sourceRoot":"","sources":["../src/span-run.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAkC,KAAK,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAE7F;;;GAGG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,CA4BxE"}
@@ -0,0 +1,39 @@
1
+ /**
2
+ * The run of a function in a span that is already started: the one rule of `runInRemoteTrace` and
3
+ * `runInIncomingSpan`. This module is internal. No entry exports it.
4
+ *
5
+ * @packageDocumentation
6
+ */
7
+ import { context, SpanStatusCode, trace } from "@opentelemetry/api";
8
+ /**
9
+ * This function runs `fn` with `span` active in `parent`, and ends the span. The span ends when `fn` returns, or when
10
+ * the promise of `fn` settles. An error of `fn` sets the status of the span to ERROR, records the error and passes through.
11
+ */
12
+ export function runInSpan(span, parent, fn) {
13
+ const fail = (error) => {
14
+ span.recordException(error instanceof Error ? error : String(error));
15
+ span.setStatus({ code: SpanStatusCode.ERROR, ...(error instanceof Error ? { message: error.message } : {}) });
16
+ };
17
+ let result;
18
+ try {
19
+ result = context.with(trace.setSpan(parent, span), fn);
20
+ }
21
+ catch (error) {
22
+ fail(error);
23
+ span.end();
24
+ throw error;
25
+ }
26
+ if (result && typeof result.then === "function") {
27
+ return result.then((value) => {
28
+ span.end();
29
+ return value;
30
+ }, (error) => {
31
+ fail(error);
32
+ span.end();
33
+ throw error;
34
+ });
35
+ }
36
+ span.end();
37
+ return result;
38
+ }
39
+ //# sourceMappingURL=span-run.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"span-run.js","sourceRoot":"","sources":["../src/span-run.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,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9
9
  * @packageDocumentation
10
10
  */
11
- import { context, defaultTextMapGetter, isSpanContextValid, propagation, ROOT_CONTEXT, SpanKind, SpanStatusCode, trace, } from "@opentelemetry/api";
11
+ import { context, defaultTextMapGetter, isSpanContextValid, propagation, ROOT_CONTEXT, SpanKind, trace, } from "@opentelemetry/api";
12
12
  import { W3CTraceContextPropagator } from "@opentelemetry/core";
13
+ import { runInSpan } from "./span-run.js";
13
14
  /** The W3C propagator of this module: it reads the traceparent also when no global propagator is set. */
14
15
  const W3C = new W3CTraceContextPropagator();
15
16
  /**
@@ -105,30 +106,6 @@ export function runInRemoteTrace(traceparent, fn, options = {}) {
105
106
  return fn();
106
107
  const tracer = options.tracer ?? trace.getTracer("@mark1russell7/otel-ts");
107
108
  const span = tracer.startSpan(options.name ?? "remote.apply", { kind: SpanKind.CONSUMER, ...(options.attributes ? { attributes: options.attributes } : {}) }, parent);
108
- const fail = (error) => {
109
- span.recordException(error instanceof Error ? error : String(error));
110
- span.setStatus({ code: SpanStatusCode.ERROR });
111
- };
112
- let result;
113
- try {
114
- result = context.with(trace.setSpan(parent, span), fn);
115
- }
116
- catch (error) {
117
- fail(error);
118
- span.end();
119
- throw error;
120
- }
121
- if (result && typeof result.then === "function") {
122
- return result.then((value) => {
123
- span.end();
124
- return value;
125
- }, (error) => {
126
- fail(error);
127
- span.end();
128
- throw error;
129
- });
130
- }
131
- span.end();
132
- return result;
109
+ return runInSpan(span, parent, fn);
133
110
  }
134
111
  //# sourceMappingURL=trace-echo.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"trace-echo.js","sourceRoot":"","sources":["../src/trace-echo.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EACL,OAAO,EACP,oBAAoB,EACpB,kBAAkB,EAClB,WAAW,EACX,YAAY,EACZ,QAAQ,EACR,cAAc,EACd,KAAK,GAIN,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,yBAAyB,EAAE,MAAM,qBAAqB,CAAC;AAE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CAAC,CAAC;YAC7B,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YAC7B,IAAI,CAAC,GAAG;gBAAE,OAAO,SAAS,CAAC;YAC3B,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;YACpB,OAAO,GAAG,GAAG,GAAG,CAAC,EAAE,IAAI,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC,SAAS,CAAC;QAC3D,CAAC;QACD,IAAI,IAAI;YACN,OAAO,OAAO,CAAC,IAAI,CAAC;QACtB,CAAC;KACF,CAAC;AACJ,CAAC;AAYD;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,gBAAgB,CAAI,WAA+B,EAAE,EAAW,EAAE,UAA8B,EAAE;IAChH,MAAM,MAAM,GAAG,YAAY,CAAC,WAAW,CAAC,CAAC;IACzC,IAAI,CAAC,MAAM;QAAE,OAAO,EAAE,EAAE,CAAC;IACzB,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,KAAK,CAAC,SAAS,CAAC,wBAAwB,CAAC,CAAC;IAC3E,MAAM,IAAI,GAAG,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,IAAI,cAAc,EAAE,EAAE,IAAI,EAAE,QAAQ,CAAC,QAAQ,EAAE,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;IACtK,OAAO,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC;AACrC,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@mark1russell7/otel-ts",
4
- "version": "0.10.0",
4
+ "version": "0.10.1",
5
5
  "description": "Frontend observability in one init() call — OpenTelemetry + Grafana Faro for any TypeScript web app",
6
6
  "license": "MIT",
7
7
  "author": "Mark Russell (https://github.com/mark1russell7)",
@@ -36,6 +36,10 @@
36
36
  "types": "./dist/context.d.ts",
37
37
  "import": "./dist/context.js"
38
38
  },
39
+ "./incoming-span": {
40
+ "types": "./dist/incoming-span.d.ts",
41
+ "import": "./dist/incoming-span.js"
42
+ },
39
43
  "./node": {
40
44
  "types": "./dist/node.d.ts",
41
45
  "import": "./dist/node.js"
@@ -0,0 +1,47 @@
1
+ /**
2
+ * The entry `@mark1russell7/otel-ts/incoming-span`: the span of the server for an incoming call, in the trace of
3
+ * its caller (docs/CONTEXT-DESIGN.md, section 5).
4
+ *
5
+ * This entry depends on `@opentelemetry/api` only. It is not in `/context`, because `/node/context` loads `/context`
6
+ * in each service. Thus only a program that imports this entry pays for its code.
7
+ *
8
+ * @packageDocumentation
9
+ */
10
+ import { SpanKind, trace, type Attributes, type Tracer } from "@opentelemetry/api";
11
+ import { incomingContext, type IncomingContextOptions, type IncomingHeaders } from "./context.js";
12
+ import { runInSpan } from "./span-run.js";
13
+
14
+ /** The options of `runInIncomingSpan` */
15
+ export interface IncomingSpanOptions extends IncomingContextOptions {
16
+ /** The attributes of the span at its start, for example `http.request.method` and `url.path`. */
17
+ readonly attributes?: Attributes;
18
+ /** The tracer of the span, for example `trace.getTracer("my-service")`. Default: the global tracer `@mark1russell7/otel-ts`. */
19
+ readonly tracer?: Tracer;
20
+ }
21
+
22
+ /**
23
+ * This function runs the handler of an incoming call in a span of the server. The span is of the kind SERVER, and its
24
+ * parent is the context of `incomingContext(headers, options)`. Thus with the default trust "trace", the span continues
25
+ * the trace of the `traceparent` header of the caller, and it takes no baggage. With no valid `traceparent`, the span
26
+ * starts a new trace. It is for a route that no instrumentation of the framework covers, for example a raw route of
27
+ * `node:http` or a handler of a Fetch `Request`.
28
+ *
29
+ * The span ends when `fn` returns, or when the promise of `fn` settles. An error of `fn` sets the status of the span to
30
+ * ERROR, and passes through.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * const answer = await runInIncomingSpan(`POST ${path}`, req.headers, () => handle(body), {
35
+ * tracer: trace.getTracer("my-service"),
36
+ * attributes: { "http.request.method": "POST", "url.path": path },
37
+ * });
38
+ * ```
39
+ */
40
+ export function runInIncomingSpan<T>(name: string, headers: IncomingHeaders, fn: () => T, options: IncomingSpanOptions = {}): T {
41
+ const parent = incomingContext(headers, options.trust === undefined ? {} : { trust: options.trust });
42
+ const tracer = options.tracer ?? trace.getTracer("@mark1russell7/otel-ts");
43
+ const span = tracer.startSpan(name, { kind: SpanKind.SERVER, ...(options.attributes ? { attributes: options.attributes } : {}) }, parent);
44
+ return runInSpan(span, parent, fn);
45
+ }
46
+
47
+ export type { IncomingContextOptions, IncomingHeaders, IncomingTrust } from "./context.js";
@@ -0,0 +1,41 @@
1
+ /**
2
+ * The run of a function in a span that is already started: the one rule of `runInRemoteTrace` and
3
+ * `runInIncomingSpan`. This module is internal. No entry exports it.
4
+ *
5
+ * @packageDocumentation
6
+ */
7
+ import { context, SpanStatusCode, trace, type Context, type Span } from "@opentelemetry/api";
8
+
9
+ /**
10
+ * This function runs `fn` with `span` active in `parent`, and ends the span. The span ends when `fn` returns, or when
11
+ * the promise of `fn` settles. An error of `fn` sets the status of the span to ERROR, records the error and passes through.
12
+ */
13
+ export function runInSpan<T>(span: Span, parent: Context, fn: () => T): T {
14
+ const fail = (error: unknown): void => {
15
+ span.recordException(error instanceof Error ? error : String(error));
16
+ span.setStatus({ code: SpanStatusCode.ERROR, ...(error instanceof Error ? { message: error.message } : {}) });
17
+ };
18
+ let result: T;
19
+ try {
20
+ result = context.with(trace.setSpan(parent, span), fn);
21
+ } catch (error) {
22
+ fail(error);
23
+ span.end();
24
+ throw error;
25
+ }
26
+ if (result && typeof (result as { then?: unknown }).then === "function") {
27
+ return (result as unknown as Promise<unknown>).then(
28
+ (value) => {
29
+ span.end();
30
+ return value;
31
+ },
32
+ (error: unknown) => {
33
+ fail(error);
34
+ span.end();
35
+ throw error;
36
+ },
37
+ ) as T;
38
+ }
39
+ span.end();
40
+ return result;
41
+ }
package/src/trace-echo.ts CHANGED
@@ -15,13 +15,13 @@ import {
15
15
  propagation,
16
16
  ROOT_CONTEXT,
17
17
  SpanKind,
18
- SpanStatusCode,
19
18
  trace,
20
19
  type Attributes,
21
20
  type Context,
22
21
  type Tracer,
23
22
  } from "@opentelemetry/api";
24
23
  import { W3CTraceContextPropagator } from "@opentelemetry/core";
24
+ import { runInSpan } from "./span-run.js";
25
25
 
26
26
  /** The W3C propagator of this module: it reads the traceparent also when no global propagator is set. */
27
27
  const W3C = new W3CTraceContextPropagator();
@@ -133,31 +133,5 @@ export function runInRemoteTrace<T>(traceparent: string | undefined, fn: () => T
133
133
  if (!parent) return fn();
134
134
  const tracer = options.tracer ?? trace.getTracer("@mark1russell7/otel-ts");
135
135
  const span = tracer.startSpan(options.name ?? "remote.apply", { kind: SpanKind.CONSUMER, ...(options.attributes ? { attributes: options.attributes } : {}) }, parent);
136
- const fail = (error: unknown): void => {
137
- span.recordException(error instanceof Error ? error : String(error));
138
- span.setStatus({ code: SpanStatusCode.ERROR });
139
- };
140
- let result: T;
141
- try {
142
- result = context.with(trace.setSpan(parent, span), fn);
143
- } catch (error) {
144
- fail(error);
145
- span.end();
146
- throw error;
147
- }
148
- if (result && typeof (result as { then?: unknown }).then === "function") {
149
- return (result as unknown as Promise<unknown>).then(
150
- (value) => {
151
- span.end();
152
- return value;
153
- },
154
- (error: unknown) => {
155
- fail(error);
156
- span.end();
157
- throw error;
158
- },
159
- ) as T;
160
- }
161
- span.end();
162
- return result;
136
+ return runInSpan(span, parent, fn);
163
137
  }