@mark1russell7/otel-ts 0.10.4 → 0.11.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 +51 -4
- package/dist/node-export.d.ts +40 -0
- package/dist/node-export.d.ts.map +1 -0
- package/dist/node-export.js +13 -0
- package/dist/node-export.js.map +1 -0
- package/package.json +88 -7
- package/src/node-export.ts +46 -0
package/README.md
CHANGED
|
@@ -14,6 +14,37 @@ pnpm add @mark1russell7/otel-ts
|
|
|
14
14
|
|
|
15
15
|
The npm package contains the compiled `dist/` folder. You do not need a build step.
|
|
16
16
|
|
|
17
|
+
### The packages of each entry
|
|
18
|
+
|
|
19
|
+
The package depends only on `@opentelemetry/api` and `@mark1russell7/text`. Each other package is an optional peer dependency. Thus a library that imports `/context` installs no SDK, and a program chooses the SDK. Install the packages of each entry that the program imports:
|
|
20
|
+
|
|
21
|
+
<!-- entry-peers:start -->
|
|
22
|
+
| Entry | The packages to install with it |
|
|
23
|
+
| --- | --- |
|
|
24
|
+
| `.` | `@grafana/faro-web-sdk`, `@opentelemetry/auto-instrumentations-web`, `@opentelemetry/core`, `@opentelemetry/exporter-logs-otlp-http`, `@opentelemetry/exporter-metrics-otlp-http`, `@opentelemetry/exporter-trace-otlp-http`, `@opentelemetry/instrumentation`, `@opentelemetry/instrumentation-long-task`, `@opentelemetry/resources`, `@opentelemetry/sdk-logs`, `@opentelemetry/sdk-metrics`, `@opentelemetry/sdk-trace`, `@opentelemetry/semantic-conventions`, `async-browser-context`, `page-lifecycle-tracker` |
|
|
25
|
+
| `./zone` | `@grafana/faro-web-sdk`, `@opentelemetry/auto-instrumentations-web`, `@opentelemetry/context-zone`, `@opentelemetry/core`, `@opentelemetry/exporter-logs-otlp-http`, `@opentelemetry/exporter-metrics-otlp-http`, `@opentelemetry/exporter-trace-otlp-http`, `@opentelemetry/instrumentation`, `@opentelemetry/instrumentation-long-task`, `@opentelemetry/resources`, `@opentelemetry/sdk-logs`, `@opentelemetry/sdk-metrics`, `@opentelemetry/sdk-trace`, `@opentelemetry/semantic-conventions`, `page-lifecycle-tracker` |
|
|
26
|
+
| `./core` | `@grafana/faro-web-sdk`, `@opentelemetry/auto-instrumentations-web`, `@opentelemetry/core`, `@opentelemetry/exporter-logs-otlp-http`, `@opentelemetry/exporter-metrics-otlp-http`, `@opentelemetry/exporter-trace-otlp-http`, `@opentelemetry/instrumentation`, `@opentelemetry/instrumentation-long-task`, `@opentelemetry/resources`, `@opentelemetry/sdk-logs`, `@opentelemetry/sdk-metrics`, `@opentelemetry/sdk-trace`, `@opentelemetry/semantic-conventions`, `page-lifecycle-tracker` |
|
|
27
|
+
| `./context` | none |
|
|
28
|
+
| `./incoming-span` | none |
|
|
29
|
+
| `./node` | `@opentelemetry/context-async-hooks`, `@opentelemetry/core`, `@opentelemetry/exporter-logs-otlp-http`, `@opentelemetry/exporter-logs-otlp-proto`, `@opentelemetry/exporter-trace-otlp-http`, `@opentelemetry/exporter-trace-otlp-proto`, `@opentelemetry/instrumentation`, `@opentelemetry/resources`, `@opentelemetry/sdk-logs`, `@opentelemetry/sdk-metrics`, `@opentelemetry/sdk-node`, `@opentelemetry/sdk-trace`, `@opentelemetry/semantic-conventions` |
|
|
30
|
+
| `./node/context` | `@opentelemetry/context-async-hooks`, `@opentelemetry/core`, `@opentelemetry/resources`, `@opentelemetry/sdk-trace`, `@opentelemetry/semantic-conventions` |
|
|
31
|
+
| `./node/export` | `@opentelemetry/exporter-logs-otlp-http`, `@opentelemetry/exporter-logs-otlp-proto`, `@opentelemetry/exporter-trace-otlp-http`, `@opentelemetry/exporter-trace-otlp-proto`, `@opentelemetry/sdk-logs`, `@opentelemetry/sdk-metrics`, `@opentelemetry/sdk-trace` |
|
|
32
|
+
| `./node/testing` | none |
|
|
33
|
+
| `./lag` | `@mark1russell7/lag`, `page-lifecycle-tracker` |
|
|
34
|
+
| `./pull` | `@opentelemetry/sdk-metrics` |
|
|
35
|
+
| `./error` | none |
|
|
36
|
+
| `./error-view` | none |
|
|
37
|
+
| `./call-trace` | none |
|
|
38
|
+
<!-- entry-peers:end -->
|
|
39
|
+
|
|
40
|
+
For example, a Node service that imports `/node/context` installs these packages:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pnpm add @mark1russell7/otel-ts @opentelemetry/context-async-hooks @opentelemetry/core @opentelemetry/resources @opentelemetry/sdk-trace @opentelemetry/semantic-conventions
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
A test of the package holds this table equal to the imports of each entry.
|
|
47
|
+
|
|
17
48
|
## Quick start
|
|
18
49
|
|
|
19
50
|
```ts
|
|
@@ -180,7 +211,7 @@ const otel = init();
|
|
|
180
211
|
|
|
181
212
|
The zone entry loads zone.js through `@opentelemetry/context-zone`. zone.js patches `Promise`, the timers and the event listeners of the page when it loads. The zone entry does not import async-browser-context.
|
|
182
213
|
|
|
183
|
-
`@opentelemetry/context-zone` is
|
|
214
|
+
`@opentelemetry/context-zone` is an optional peer dependency of otel-ts, as each package of the browser entries. An app that imports the zone entry installs it with the other packages of the entry (see [The packages of each entry](#the-packages-of-each-entry)). A bundle gets zone.js only through the zone entry.
|
|
184
215
|
|
|
185
216
|
### Use no context library
|
|
186
217
|
|
|
@@ -490,10 +521,10 @@ The context managers also operate in a worker. In `@mark1russell7/otel-ts`, the
|
|
|
490
521
|
|
|
491
522
|
The entry `@mark1russell7/otel-ts/node` starts OpenTelemetry in a Node.js service. It is separate from the browser entries and imports no browser library.
|
|
492
523
|
|
|
493
|
-
Install the SDK and the instrumentations in the service.
|
|
524
|
+
Install the SDK, the packages of the entry and the instrumentations in the service. Each OpenTelemetry package is a peer dependency, so the service keeps one version line of them. [The packages of each entry](#the-packages-of-each-entry) gives the full list:
|
|
494
525
|
|
|
495
526
|
```bash
|
|
496
|
-
pnpm add @opentelemetry/sdk-node @opentelemetry/instrumentation @opentelemetry/instrumentation-http
|
|
527
|
+
pnpm add @opentelemetry/sdk-node @opentelemetry/context-async-hooks @opentelemetry/core @opentelemetry/exporter-logs-otlp-http @opentelemetry/exporter-logs-otlp-proto @opentelemetry/exporter-trace-otlp-http @opentelemetry/exporter-trace-otlp-proto @opentelemetry/resources @opentelemetry/sdk-logs @opentelemetry/sdk-metrics @opentelemetry/sdk-trace @opentelemetry/semantic-conventions @opentelemetry/instrumentation @opentelemetry/instrumentation-http
|
|
497
528
|
```
|
|
498
529
|
|
|
499
530
|
Put the start in its own module:
|
|
@@ -551,7 +582,23 @@ export const telemetry = startNodeContext({
|
|
|
551
582
|
- No span leaves the process. A test gives `spanProcessors` with the exporter of its capture.
|
|
552
583
|
- It adds no listener for a signal. Call `telemetry.shutdown()` in the last stage of the service.
|
|
553
584
|
|
|
554
|
-
`startNodeContext()` and `startNodeTelemetry()` refuse each other: only one part of a program registers the context.
|
|
585
|
+
`startNodeContext()` and `startNodeTelemetry()` refuse each other: only one part of a program registers the context.
|
|
586
|
+
|
|
587
|
+
To export the spans with no SDK of `sdk-node`, give the processors of the entry `@mark1russell7/otel-ts/node/export`. The entry loads the exporters, so import it only when the program exports:
|
|
588
|
+
|
|
589
|
+
```ts
|
|
590
|
+
import { startNodeContext } from "@mark1russell7/otel-ts/node/context";
|
|
591
|
+
|
|
592
|
+
const spanProcessors = process.env["OTEL_EXPORTER_OTLP_ENDPOINT"]
|
|
593
|
+
? (await import("@mark1russell7/otel-ts/node/export")).exportSpanProcessors()
|
|
594
|
+
: [];
|
|
595
|
+
export const telemetry = startNodeContext({ serviceName: "my-tool", spanProcessors });
|
|
596
|
+
```
|
|
597
|
+
|
|
598
|
+
- The processors are those of `/node`: the context processor, then one processor for each exporter of `OTEL_TRACES_EXPORTER` (`otlp`, `console` or `none`), with the protocol of `OTEL_EXPORTER_OTLP_PROTOCOL`.
|
|
599
|
+
- With no value in `OTEL_EXPORTER_OTLP_ENDPOINT`, `exportSpanProcessors()` gives no processor. The option `exporter` exports to an exporter of the program.
|
|
600
|
+
- `telemetry.shutdown()` flushes the spans. A command line calls it before it ends.
|
|
601
|
+
- For the logs and the metrics, and for the instrumentations, import `startNodeTelemetry()` from `/node` in place of `startNodeContext()`.
|
|
555
602
|
|
|
556
603
|
### A service of `@mark1russell7/service`
|
|
557
604
|
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The entry `@mark1russell7/otel-ts/node/export`: the span processors that export the spans of a program that
|
|
3
|
+
* starts the context with `startNodeContext()` of the entry `/node/context`, with no SDK of `@opentelemetry/sdk-node`.
|
|
4
|
+
*
|
|
5
|
+
* The processors are those of the entry `/node`: the context processor, then one processor for each exporter of
|
|
6
|
+
* `OTEL_TRACES_EXPORTER`, with the protocol of `OTEL_EXPORTER_OTLP_TRACES_PROTOCOL` or `OTEL_EXPORTER_OTLP_PROTOCOL`.
|
|
7
|
+
* This entry loads the exporters. Thus a program that exports only with an endpoint imports it with `import()`
|
|
8
|
+
* when `OTEL_EXPORTER_OTLP_ENDPOINT` has a value:
|
|
9
|
+
*
|
|
10
|
+
* ```ts no-run
|
|
11
|
+
* import { startNodeContext } from "@mark1russell7/otel-ts/node/context";
|
|
12
|
+
*
|
|
13
|
+
* const spanProcessors = process.env["OTEL_EXPORTER_OTLP_ENDPOINT"]
|
|
14
|
+
* ? (await import("@mark1russell7/otel-ts/node/export")).exportSpanProcessors()
|
|
15
|
+
* : [];
|
|
16
|
+
* const telemetry = startNodeContext({ serviceName: "my-tool", spanProcessors });
|
|
17
|
+
* // The work of the program, then:
|
|
18
|
+
* await telemetry.shutdown();
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @packageDocumentation
|
|
22
|
+
*/
|
|
23
|
+
import type { SpanExporter, SpanProcessor } from "@opentelemetry/sdk-trace";
|
|
24
|
+
/** The options of `exportSpanProcessors()` */
|
|
25
|
+
export interface ExportSpanOptions {
|
|
26
|
+
/**
|
|
27
|
+
* The environment of `OTEL_EXPORTER_OTLP_ENDPOINT`, of the exporter names and of the protocols. Default:
|
|
28
|
+
* `process.env`. An OTLP exporter reads its endpoint, headers and timeout from `process.env`, not from this object.
|
|
29
|
+
*/
|
|
30
|
+
env?: NodeJS.ProcessEnv;
|
|
31
|
+
/** The trace exporter. With it, the processors export to it, also with no endpoint. Default: the exporters of the variables. */
|
|
32
|
+
exporter?: SpanExporter;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The span processors that export the spans, for the option `spanProcessors` of `startNodeContext()`. With no
|
|
36
|
+
* value in `OTEL_EXPORTER_OTLP_ENDPOINT` and no `exporter`, it gives no processor, as the entry `/node` exports
|
|
37
|
+
* nothing then. `OTEL_TRACES_EXPORTER=none` also gives no processor. The `shutdown()` of the context flushes them.
|
|
38
|
+
*/
|
|
39
|
+
export declare function exportSpanProcessors(options?: ExportSpanOptions): SpanProcessor[];
|
|
40
|
+
//# sourceMappingURL=node-export.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node-export.d.ts","sourceRoot":"","sources":["../src/node-export.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAG5E,8CAA8C;AAC9C,MAAM,WAAW,iBAAiB;IAChC;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC;IACxB,gIAAgI;IAChI,QAAQ,CAAC,EAAE,YAAY,CAAC;CACzB;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,GAAE,iBAAsB,GAAG,aAAa,EAAE,CAIrF"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { nodeSpanProcessors } from "./node-exporters.js";
|
|
2
|
+
/**
|
|
3
|
+
* The span processors that export the spans, for the option `spanProcessors` of `startNodeContext()`. With no
|
|
4
|
+
* value in `OTEL_EXPORTER_OTLP_ENDPOINT` and no `exporter`, it gives no processor, as the entry `/node` exports
|
|
5
|
+
* nothing then. `OTEL_TRACES_EXPORTER=none` also gives no processor. The `shutdown()` of the context flushes them.
|
|
6
|
+
*/
|
|
7
|
+
export function exportSpanProcessors(options = {}) {
|
|
8
|
+
const env = options.env ?? process.env;
|
|
9
|
+
if (options.exporter === undefined && !env["OTEL_EXPORTER_OTLP_ENDPOINT"])
|
|
10
|
+
return [];
|
|
11
|
+
return nodeSpanProcessors(env, options.exporter);
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=node-export.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node-export.js","sourceRoot":"","sources":["../src/node-export.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAazD;;;;GAIG;AACH,MAAM,UAAU,oBAAoB,CAAC,UAA6B,EAAE;IAClE,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,IAAI,OAAO,CAAC,GAAG,CAAC;IACvC,IAAI,OAAO,CAAC,QAAQ,KAAK,SAAS,IAAI,CAAC,GAAG,CAAC,6BAA6B,CAAC;QAAE,OAAO,EAAE,CAAC;IACrF,OAAO,kBAAkB,CAAC,GAAG,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;AACnD,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.
|
|
4
|
+
"version": "0.11.0",
|
|
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)",
|
|
@@ -55,6 +55,11 @@
|
|
|
55
55
|
"import": "./dist/node-context.js",
|
|
56
56
|
"default": "./dist/node-context.js"
|
|
57
57
|
},
|
|
58
|
+
"./node/export": {
|
|
59
|
+
"types": "./dist/node-export.d.ts",
|
|
60
|
+
"import": "./dist/node-export.js",
|
|
61
|
+
"default": "./dist/node-export.js"
|
|
62
|
+
},
|
|
58
63
|
"./node/testing": {
|
|
59
64
|
"types": "./dist/node-testing.d.ts",
|
|
60
65
|
"import": "./dist/node-testing.js",
|
|
@@ -93,9 +98,12 @@
|
|
|
93
98
|
],
|
|
94
99
|
"sideEffects": false,
|
|
95
100
|
"dependencies": {
|
|
96
|
-
"@grafana/faro-web-sdk": "^2.13.0",
|
|
97
101
|
"@mark1russell7/text": "^0.0.20",
|
|
98
|
-
"@opentelemetry/api": "^1.10.0"
|
|
102
|
+
"@opentelemetry/api": "^1.10.0"
|
|
103
|
+
},
|
|
104
|
+
"peerDependencies": {
|
|
105
|
+
"@grafana/faro-web-sdk": "^2.13.0",
|
|
106
|
+
"@mark1russell7/lag": ">=0.4.2 <0.6.0",
|
|
99
107
|
"@opentelemetry/auto-instrumentations-web": "^0.70.1",
|
|
100
108
|
"@opentelemetry/context-async-hooks": "^3.1.1",
|
|
101
109
|
"@opentelemetry/context-zone": "^3.1.1",
|
|
@@ -110,34 +118,107 @@
|
|
|
110
118
|
"@opentelemetry/resources": "^3.1.1",
|
|
111
119
|
"@opentelemetry/sdk-logs": "^3.1.1",
|
|
112
120
|
"@opentelemetry/sdk-metrics": "^3.1.1",
|
|
121
|
+
"@opentelemetry/sdk-node": ">=0.300.0 <0.400.0",
|
|
113
122
|
"@opentelemetry/sdk-trace": "^3.1.1",
|
|
114
123
|
"@opentelemetry/semantic-conventions": "^1.40.0",
|
|
115
124
|
"async-browser-context": "^0.3.0",
|
|
116
125
|
"page-lifecycle-tracker": "^0.1.0"
|
|
117
126
|
},
|
|
118
|
-
"peerDependencies": {
|
|
119
|
-
"@mark1russell7/lag": ">=0.4.2 <0.6.0",
|
|
120
|
-
"@opentelemetry/sdk-node": ">=0.300.0 <0.400.0"
|
|
121
|
-
},
|
|
122
127
|
"peerDependenciesMeta": {
|
|
128
|
+
"@grafana/faro-web-sdk": {
|
|
129
|
+
"optional": true
|
|
130
|
+
},
|
|
123
131
|
"@mark1russell7/lag": {
|
|
124
132
|
"optional": true
|
|
125
133
|
},
|
|
134
|
+
"@opentelemetry/auto-instrumentations-web": {
|
|
135
|
+
"optional": true
|
|
136
|
+
},
|
|
137
|
+
"@opentelemetry/context-async-hooks": {
|
|
138
|
+
"optional": true
|
|
139
|
+
},
|
|
140
|
+
"@opentelemetry/context-zone": {
|
|
141
|
+
"optional": true
|
|
142
|
+
},
|
|
143
|
+
"@opentelemetry/core": {
|
|
144
|
+
"optional": true
|
|
145
|
+
},
|
|
146
|
+
"@opentelemetry/exporter-logs-otlp-http": {
|
|
147
|
+
"optional": true
|
|
148
|
+
},
|
|
149
|
+
"@opentelemetry/exporter-logs-otlp-proto": {
|
|
150
|
+
"optional": true
|
|
151
|
+
},
|
|
152
|
+
"@opentelemetry/exporter-metrics-otlp-http": {
|
|
153
|
+
"optional": true
|
|
154
|
+
},
|
|
155
|
+
"@opentelemetry/exporter-trace-otlp-http": {
|
|
156
|
+
"optional": true
|
|
157
|
+
},
|
|
158
|
+
"@opentelemetry/exporter-trace-otlp-proto": {
|
|
159
|
+
"optional": true
|
|
160
|
+
},
|
|
161
|
+
"@opentelemetry/instrumentation": {
|
|
162
|
+
"optional": true
|
|
163
|
+
},
|
|
164
|
+
"@opentelemetry/instrumentation-long-task": {
|
|
165
|
+
"optional": true
|
|
166
|
+
},
|
|
167
|
+
"@opentelemetry/resources": {
|
|
168
|
+
"optional": true
|
|
169
|
+
},
|
|
170
|
+
"@opentelemetry/sdk-logs": {
|
|
171
|
+
"optional": true
|
|
172
|
+
},
|
|
173
|
+
"@opentelemetry/sdk-metrics": {
|
|
174
|
+
"optional": true
|
|
175
|
+
},
|
|
126
176
|
"@opentelemetry/sdk-node": {
|
|
127
177
|
"optional": true
|
|
178
|
+
},
|
|
179
|
+
"@opentelemetry/sdk-trace": {
|
|
180
|
+
"optional": true
|
|
181
|
+
},
|
|
182
|
+
"@opentelemetry/semantic-conventions": {
|
|
183
|
+
"optional": true
|
|
184
|
+
},
|
|
185
|
+
"async-browser-context": {
|
|
186
|
+
"optional": true
|
|
187
|
+
},
|
|
188
|
+
"page-lifecycle-tracker": {
|
|
189
|
+
"optional": true
|
|
128
190
|
}
|
|
129
191
|
},
|
|
130
192
|
"devDependencies": {
|
|
193
|
+
"@grafana/faro-web-sdk": "^2.13.0",
|
|
131
194
|
"@mark1russell7/api-report": "^0.5.22",
|
|
132
195
|
"@mark1russell7/conventions": "^0.52.8",
|
|
133
196
|
"@mark1russell7/grafana-infra": "github:mark1russell7/grafana-infra#618d1ab7042455f81a81e219af6d33022551305e",
|
|
134
197
|
"@mark1russell7/lag": "^0.5.0",
|
|
135
198
|
"@mark1russell7/rigor-bench": "^0.3.2",
|
|
136
199
|
"@mark1russell7/rigor-testkit": "^0.2.0",
|
|
200
|
+
"@opentelemetry/auto-instrumentations-web": "^0.70.1",
|
|
201
|
+
"@opentelemetry/context-async-hooks": "^3.1.1",
|
|
202
|
+
"@opentelemetry/context-zone": "^3.1.1",
|
|
203
|
+
"@opentelemetry/core": "^3.1.1",
|
|
204
|
+
"@opentelemetry/exporter-logs-otlp-http": "^0.301.2",
|
|
205
|
+
"@opentelemetry/exporter-logs-otlp-proto": "^0.301.2",
|
|
206
|
+
"@opentelemetry/exporter-metrics-otlp-http": "^0.301.2",
|
|
207
|
+
"@opentelemetry/exporter-trace-otlp-http": "^0.301.2",
|
|
208
|
+
"@opentelemetry/exporter-trace-otlp-proto": "^0.301.2",
|
|
209
|
+
"@opentelemetry/instrumentation": "^0.301.2",
|
|
210
|
+
"@opentelemetry/instrumentation-long-task": "^0.69.0",
|
|
211
|
+
"@opentelemetry/resources": "^3.1.1",
|
|
212
|
+
"@opentelemetry/sdk-logs": "^3.1.1",
|
|
213
|
+
"@opentelemetry/sdk-metrics": "^3.1.1",
|
|
137
214
|
"@opentelemetry/sdk-node": "~0.301.2",
|
|
215
|
+
"@opentelemetry/sdk-trace": "^3.1.1",
|
|
216
|
+
"@opentelemetry/semantic-conventions": "^1.40.0",
|
|
138
217
|
"@types/node": "^26.6.5",
|
|
139
218
|
"@vitest/coverage-v8": "^4.1.12",
|
|
219
|
+
"async-browser-context": "^0.3.0",
|
|
140
220
|
"happy-dom": "^21.0.4",
|
|
221
|
+
"page-lifecycle-tracker": "^0.1.0",
|
|
141
222
|
"typescript": "^5.9.0",
|
|
142
223
|
"vitest": "^4.1.12"
|
|
143
224
|
},
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The entry `@mark1russell7/otel-ts/node/export`: the span processors that export the spans of a program that
|
|
3
|
+
* starts the context with `startNodeContext()` of the entry `/node/context`, with no SDK of `@opentelemetry/sdk-node`.
|
|
4
|
+
*
|
|
5
|
+
* The processors are those of the entry `/node`: the context processor, then one processor for each exporter of
|
|
6
|
+
* `OTEL_TRACES_EXPORTER`, with the protocol of `OTEL_EXPORTER_OTLP_TRACES_PROTOCOL` or `OTEL_EXPORTER_OTLP_PROTOCOL`.
|
|
7
|
+
* This entry loads the exporters. Thus a program that exports only with an endpoint imports it with `import()`
|
|
8
|
+
* when `OTEL_EXPORTER_OTLP_ENDPOINT` has a value:
|
|
9
|
+
*
|
|
10
|
+
* ```ts no-run
|
|
11
|
+
* import { startNodeContext } from "@mark1russell7/otel-ts/node/context";
|
|
12
|
+
*
|
|
13
|
+
* const spanProcessors = process.env["OTEL_EXPORTER_OTLP_ENDPOINT"]
|
|
14
|
+
* ? (await import("@mark1russell7/otel-ts/node/export")).exportSpanProcessors()
|
|
15
|
+
* : [];
|
|
16
|
+
* const telemetry = startNodeContext({ serviceName: "my-tool", spanProcessors });
|
|
17
|
+
* // The work of the program, then:
|
|
18
|
+
* await telemetry.shutdown();
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @packageDocumentation
|
|
22
|
+
*/
|
|
23
|
+
import type { SpanExporter, SpanProcessor } from "@opentelemetry/sdk-trace";
|
|
24
|
+
import { nodeSpanProcessors } from "./node-exporters.js";
|
|
25
|
+
|
|
26
|
+
/** The options of `exportSpanProcessors()` */
|
|
27
|
+
export interface ExportSpanOptions {
|
|
28
|
+
/**
|
|
29
|
+
* The environment of `OTEL_EXPORTER_OTLP_ENDPOINT`, of the exporter names and of the protocols. Default:
|
|
30
|
+
* `process.env`. An OTLP exporter reads its endpoint, headers and timeout from `process.env`, not from this object.
|
|
31
|
+
*/
|
|
32
|
+
env?: NodeJS.ProcessEnv;
|
|
33
|
+
/** The trace exporter. With it, the processors export to it, also with no endpoint. Default: the exporters of the variables. */
|
|
34
|
+
exporter?: SpanExporter;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The span processors that export the spans, for the option `spanProcessors` of `startNodeContext()`. With no
|
|
39
|
+
* value in `OTEL_EXPORTER_OTLP_ENDPOINT` and no `exporter`, it gives no processor, as the entry `/node` exports
|
|
40
|
+
* nothing then. `OTEL_TRACES_EXPORTER=none` also gives no processor. The `shutdown()` of the context flushes them.
|
|
41
|
+
*/
|
|
42
|
+
export function exportSpanProcessors(options: ExportSpanOptions = {}): SpanProcessor[] {
|
|
43
|
+
const env = options.env ?? process.env;
|
|
44
|
+
if (options.exporter === undefined && !env["OTEL_EXPORTER_OTLP_ENDPOINT"]) return [];
|
|
45
|
+
return nodeSpanProcessors(env, options.exporter);
|
|
46
|
+
}
|