@mark1russell7/otel-ts 0.7.2 → 0.7.4
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 +19 -2
- package/dist/.tsbuildinfo +1 -1
- package/dist/faro.d.ts.map +1 -1
- package/dist/faro.js +20 -0
- package/dist/faro.js.map +1 -1
- package/dist/lag.d.ts +60 -0
- package/dist/lag.d.ts.map +1 -0
- package/dist/lag.js +40 -0
- package/dist/lag.js.map +1 -0
- package/package.json +10 -1
- package/src/faro.ts +20 -0
- package/src/lag.ts +84 -0
package/dist/faro.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"faro.d.ts","sourceRoot":"","sources":["../src/faro.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,KAAK,IAAI,EACV,MAAM,uBAAuB,CAAC;AAC/B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjD;;;;;;;;;GASG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,cAAc,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI,
|
|
1
|
+
{"version":3,"file":"faro.d.ts","sourceRoot":"","sources":["../src/faro.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,KAAK,IAAI,EACV,MAAM,uBAAuB,CAAC;AAC/B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjD;;;;;;;;;GASG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,cAAc,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI,CAmBnE"}
|
package/dist/faro.js
CHANGED
|
@@ -11,6 +11,7 @@ import { initializeFaro, getWebInstrumentations, } from "@grafana/faro-web-sdk";
|
|
|
11
11
|
* an error or a log of Faro has the trace and the span of its call.
|
|
12
12
|
*/
|
|
13
13
|
export function setupFaro(config, url) {
|
|
14
|
+
const before = globalThis.fetch;
|
|
14
15
|
const faro = initializeFaro({
|
|
15
16
|
url,
|
|
16
17
|
app: {
|
|
@@ -24,6 +25,25 @@ export function setupFaro(config, url) {
|
|
|
24
25
|
}),
|
|
25
26
|
});
|
|
26
27
|
faro.api.initOTEL(trace, context);
|
|
28
|
+
keepFetchOriginal(before);
|
|
27
29
|
return faro;
|
|
28
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* The OTLP exporters send with `fetch.__original` when it exists, so an
|
|
33
|
+
* export makes no fetch span. The request monitor of Faro (user actions,
|
|
34
|
+
* navigation, frustration) replaces `fetch` with a wrapper without
|
|
35
|
+
* `__original`. Then each export made a span, and that span was exported
|
|
36
|
+
* again. This gives the wrapper of Faro the `__original` of the fetch that
|
|
37
|
+
* it replaced.
|
|
38
|
+
*/
|
|
39
|
+
function keepFetchOriginal(before) {
|
|
40
|
+
const after = globalThis.fetch;
|
|
41
|
+
if (!before || !after || after === before)
|
|
42
|
+
return;
|
|
43
|
+
if (typeof before.__original !== "function")
|
|
44
|
+
return;
|
|
45
|
+
if (typeof after.__original === "function")
|
|
46
|
+
return;
|
|
47
|
+
after.__original = before.__original;
|
|
48
|
+
}
|
|
29
49
|
//# sourceMappingURL=faro.js.map
|
package/dist/faro.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"faro.js","sourceRoot":"","sources":["../src/faro.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EACL,cAAc,EACd,sBAAsB,GAEvB,MAAM,uBAAuB,CAAC;AAG/B;;;;;;;;;GASG;AACH,MAAM,UAAU,SAAS,CAAC,MAAsB,EAAE,GAAW;IAC3D,MAAM,IAAI,GAAG,cAAc,CAAC;QAC1B,GAAG;QACH,GAAG,EAAE;YACH,IAAI,EAAE,MAAM,CAAC,WAAW,IAAI,MAAM,CAAC,WAAW;YAC9C,OAAO,EAAE,MAAM,CAAC,cAAc;SAC/B;QACD,gBAAgB,EAAE,sBAAsB,CAAC;YACvC,cAAc,EAAE,IAAI;YACpB,gCAAgC,EAAE,IAAI;YACtC,0CAA0C,EAAE,IAAI;SACjD,CAAC;KACH,CAAC,CAAC;IAEH,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"faro.js","sourceRoot":"","sources":["../src/faro.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EACL,cAAc,EACd,sBAAsB,GAEvB,MAAM,uBAAuB,CAAC;AAG/B;;;;;;;;;GASG;AACH,MAAM,UAAU,SAAS,CAAC,MAAsB,EAAE,GAAW;IAC3D,MAAM,MAAM,GAAG,UAAU,CAAC,KAAK,CAAC;IAChC,MAAM,IAAI,GAAG,cAAc,CAAC;QAC1B,GAAG;QACH,GAAG,EAAE;YACH,IAAI,EAAE,MAAM,CAAC,WAAW,IAAI,MAAM,CAAC,WAAW;YAC9C,OAAO,EAAE,MAAM,CAAC,cAAc;SAC/B;QACD,gBAAgB,EAAE,sBAAsB,CAAC;YACvC,cAAc,EAAE,IAAI;YACpB,gCAAgC,EAAE,IAAI;YACtC,0CAA0C,EAAE,IAAI;SACjD,CAAC;KACH,CAAC,CAAC;IAEH,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAClC,iBAAiB,CAAC,MAAM,CAAC,CAAC;IAE1B,OAAO,IAAI,CAAC;AACd,CAAC;AAID;;;;;;;GAOG;AACH,SAAS,iBAAiB,CAAC,MAAgC;IACzD,MAAM,KAAK,GAAG,UAAU,CAAC,KAAiC,CAAC;IAC3D,IAAI,CAAC,MAAM,IAAI,CAAC,KAAK,IAAI,KAAK,KAAK,MAAM;QAAE,OAAO;IAClD,IAAI,OAAO,MAAM,CAAC,UAAU,KAAK,UAAU;QAAE,OAAO;IACpD,IAAI,OAAO,KAAK,CAAC,UAAU,KAAK,UAAU;QAAE,OAAO;IACnD,KAAK,CAAC,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC;AACvC,CAAC"}
|
package/dist/lag.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The OpenTelemetry dependencies of the monitors of `@mark1russell7/lag`, from one instance of
|
|
3
|
+
* otel-ts (decision D7, roadmap C15). Without this entry, each app connects the meter, the logger,
|
|
4
|
+
* the events, the spans and the flush by hand.
|
|
5
|
+
*
|
|
6
|
+
* The adapters are the adapters of lag. This entry only gives them the providers of the instance.
|
|
7
|
+
* `@mark1russell7/lag` is an optional peer dependency: only an app that imports this entry needs it.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { init } from "@mark1russell7/otel-ts";
|
|
12
|
+
* import { lagDeps } from "@mark1russell7/otel-ts/lag";
|
|
13
|
+
* import { createBrowserDeps, setupAllMonitors } from "@mark1russell7/lag";
|
|
14
|
+
*
|
|
15
|
+
* const otel = init({ serviceName: "checkout-web" });
|
|
16
|
+
* const lag = lagDeps(otel);
|
|
17
|
+
* const monitors = setupAllMonitors(createBrowserDeps(window, lag));
|
|
18
|
+
* lag.connect(monitors);
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @packageDocumentation
|
|
22
|
+
*/
|
|
23
|
+
import { type Tracer } from "@opentelemetry/api";
|
|
24
|
+
import { type EventSink, type Logger, type Meter, type SpanSink } from "@mark1russell7/lag";
|
|
25
|
+
import type { OtelTsInstance } from "./types.js";
|
|
26
|
+
/** The options of `lagDeps` */
|
|
27
|
+
export interface LagDepsOptions {
|
|
28
|
+
/** The name of the meter, the logger and the tracer. Default: `lag`. */
|
|
29
|
+
name?: string;
|
|
30
|
+
/** The tracer of the spans of the monitors. Default: the global tracer of `name`. */
|
|
31
|
+
tracer?: Tracer;
|
|
32
|
+
}
|
|
33
|
+
/** The monitors of lag, as `connect` needs them: the result of `setupAllMonitors()` fits. */
|
|
34
|
+
export interface LagFlushTarget {
|
|
35
|
+
flush(): void;
|
|
36
|
+
}
|
|
37
|
+
/** The OpenTelemetry dependencies of the monitors of lag. Give them to `createBrowserDeps()` of lag. */
|
|
38
|
+
export interface LagDeps {
|
|
39
|
+
/** The meter of the instance, with the name of the options */
|
|
40
|
+
readonly meter: Meter;
|
|
41
|
+
/** The diagnostics of lag, as log records of the instance */
|
|
42
|
+
readonly logger: Logger;
|
|
43
|
+
/** The events of the monitors, as log records with `eventName` */
|
|
44
|
+
readonly events: EventSink;
|
|
45
|
+
/** The spans of the monitors, through the tracer of the options */
|
|
46
|
+
readonly spans: SpanSink;
|
|
47
|
+
/**
|
|
48
|
+
* Record the values that the monitors keep until a checkpoint before each flush of the instance.
|
|
49
|
+
* Thus the final export of a page has the final values of the page view.
|
|
50
|
+
*
|
|
51
|
+
* @returns A function that removes the connection
|
|
52
|
+
*/
|
|
53
|
+
connect(monitors: LagFlushTarget): () => void;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The meter, the logger, the event sink and the span sink of the monitors of lag, from one instance
|
|
57
|
+
* of otel-ts. `connect()` records the values of the monitors before each flush of the instance.
|
|
58
|
+
*/
|
|
59
|
+
export declare function lagDeps(instance: Pick<OtelTsInstance, "getMeter" | "getLogger" | "onBeforeFlush">, options?: LagDepsOptions): LagDeps;
|
|
60
|
+
//# sourceMappingURL=lag.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lag.d.ts","sourceRoot":"","sources":["../src/lag.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,OAAO,EAAuB,KAAK,MAAM,EAAE,MAAM,oBAAoB,CAAC;AACtE,OAAO,EAIL,KAAK,SAAS,EACd,KAAK,MAAM,EACX,KAAK,KAAK,EACV,KAAK,QAAQ,EACd,MAAM,oBAAoB,CAAC;AAC5B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjD,+BAA+B;AAC/B,MAAM,WAAW,cAAc;IAC7B,wEAAwE;IACxE,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,qFAAqF;IACrF,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,6FAA6F;AAC7F,MAAM,WAAW,cAAc;IAC7B,KAAK,IAAI,IAAI,CAAC;CACf;AAED,wGAAwG;AACxG,MAAM,WAAW,OAAO;IACtB,8DAA8D;IAC9D,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,6DAA6D;IAC7D,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,kEAAkE;IAClE,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC;IAC3B,mEAAmE;IACnE,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC;IACzB;;;;;OAKG;IACH,OAAO,CAAC,QAAQ,EAAE,cAAc,GAAG,MAAM,IAAI,CAAC;CAC/C;AAED;;;GAGG;AACH,wBAAgB,OAAO,CACrB,QAAQ,EAAE,IAAI,CAAC,cAAc,EAAE,UAAU,GAAG,WAAW,GAAG,eAAe,CAAC,EAC1E,OAAO,GAAE,cAAmB,GAC3B,OAAO,CAUT"}
|
package/dist/lag.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The OpenTelemetry dependencies of the monitors of `@mark1russell7/lag`, from one instance of
|
|
3
|
+
* otel-ts (decision D7, roadmap C15). Without this entry, each app connects the meter, the logger,
|
|
4
|
+
* the events, the spans and the flush by hand.
|
|
5
|
+
*
|
|
6
|
+
* The adapters are the adapters of lag. This entry only gives them the providers of the instance.
|
|
7
|
+
* `@mark1russell7/lag` is an optional peer dependency: only an app that imports this entry needs it.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { init } from "@mark1russell7/otel-ts";
|
|
12
|
+
* import { lagDeps } from "@mark1russell7/otel-ts/lag";
|
|
13
|
+
* import { createBrowserDeps, setupAllMonitors } from "@mark1russell7/lag";
|
|
14
|
+
*
|
|
15
|
+
* const otel = init({ serviceName: "checkout-web" });
|
|
16
|
+
* const lag = lagDeps(otel);
|
|
17
|
+
* const monitors = setupAllMonitors(createBrowserDeps(window, lag));
|
|
18
|
+
* lag.connect(monitors);
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @packageDocumentation
|
|
22
|
+
*/
|
|
23
|
+
import { ROOT_CONTEXT, trace } from "@opentelemetry/api";
|
|
24
|
+
import { createOtelEventSink, createOtelLoggerAdapter, createOtelSpanSink, } from "@mark1russell7/lag";
|
|
25
|
+
/**
|
|
26
|
+
* The meter, the logger, the event sink and the span sink of the monitors of lag, from one instance
|
|
27
|
+
* of otel-ts. `connect()` records the values of the monitors before each flush of the instance.
|
|
28
|
+
*/
|
|
29
|
+
export function lagDeps(instance, options = {}) {
|
|
30
|
+
const name = options.name ?? "lag";
|
|
31
|
+
const logger = instance.getLogger(name);
|
|
32
|
+
return {
|
|
33
|
+
meter: instance.getMeter(name),
|
|
34
|
+
logger: createOtelLoggerAdapter(logger),
|
|
35
|
+
events: createOtelEventSink(logger),
|
|
36
|
+
spans: createOtelSpanSink(options.tracer ?? trace.getTracer(name), { trace, ROOT_CONTEXT }),
|
|
37
|
+
connect: (monitors) => instance.onBeforeFlush(() => monitors.flush()),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=lag.js.map
|
package/dist/lag.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lag.js","sourceRoot":"","sources":["../src/lag.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,OAAO,EAAE,YAAY,EAAE,KAAK,EAAe,MAAM,oBAAoB,CAAC;AACtE,OAAO,EACL,mBAAmB,EACnB,uBAAuB,EACvB,kBAAkB,GAKnB,MAAM,oBAAoB,CAAC;AAmC5B;;;GAGG;AACH,MAAM,UAAU,OAAO,CACrB,QAA0E,EAC1E,UAA0B,EAAE;IAE5B,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,KAAK,CAAC;IACnC,MAAM,MAAM,GAAG,QAAQ,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;IACxC,OAAO;QACL,KAAK,EAAE,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC;QAC9B,MAAM,EAAE,uBAAuB,CAAC,MAAM,CAAC;QACvC,MAAM,EAAE,mBAAmB,CAAC,MAAM,CAAC;QACnC,KAAK,EAAE,kBAAkB,CAAC,OAAO,CAAC,MAAM,IAAI,KAAK,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC;QAC3F,OAAO,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,aAAa,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;KACtE,CAAC;AACJ,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.7.
|
|
4
|
+
"version": "0.7.4",
|
|
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)",
|
|
@@ -40,6 +40,10 @@
|
|
|
40
40
|
"./node/testing": {
|
|
41
41
|
"types": "./dist/node-testing.d.ts",
|
|
42
42
|
"import": "./dist/node-testing.js"
|
|
43
|
+
},
|
|
44
|
+
"./lag": {
|
|
45
|
+
"types": "./dist/lag.d.ts",
|
|
46
|
+
"import": "./dist/lag.js"
|
|
43
47
|
}
|
|
44
48
|
},
|
|
45
49
|
"files": [
|
|
@@ -71,15 +75,20 @@
|
|
|
71
75
|
"page-lifecycle-tracker": "^0.1.0"
|
|
72
76
|
},
|
|
73
77
|
"peerDependencies": {
|
|
78
|
+
"@mark1russell7/lag": ">=0.4.2 <0.6.0",
|
|
74
79
|
"@opentelemetry/sdk-node": ">=0.200.0"
|
|
75
80
|
},
|
|
76
81
|
"peerDependenciesMeta": {
|
|
82
|
+
"@mark1russell7/lag": {
|
|
83
|
+
"optional": true
|
|
84
|
+
},
|
|
77
85
|
"@opentelemetry/sdk-node": {
|
|
78
86
|
"optional": true
|
|
79
87
|
}
|
|
80
88
|
},
|
|
81
89
|
"devDependencies": {
|
|
82
90
|
"@mark1russell7/grafana-infra": "github:mark1russell7/grafana-infra",
|
|
91
|
+
"@mark1russell7/lag": "^0.4.2",
|
|
83
92
|
"@opentelemetry/sdk-node": "~0.223.0",
|
|
84
93
|
"@types/node": "^26.6.5",
|
|
85
94
|
"@vitest/coverage-v8": "^4.1.12",
|
package/src/faro.ts
CHANGED
|
@@ -17,6 +17,7 @@ import type { ResolvedConfig } from "./types.js";
|
|
|
17
17
|
* an error or a log of Faro has the trace and the span of its call.
|
|
18
18
|
*/
|
|
19
19
|
export function setupFaro(config: ResolvedConfig, url: string): Faro {
|
|
20
|
+
const before = globalThis.fetch;
|
|
20
21
|
const faro = initializeFaro({
|
|
21
22
|
url,
|
|
22
23
|
app: {
|
|
@@ -31,6 +32,25 @@ export function setupFaro(config: ResolvedConfig, url: string): Faro {
|
|
|
31
32
|
});
|
|
32
33
|
|
|
33
34
|
faro.api.initOTEL(trace, context);
|
|
35
|
+
keepFetchOriginal(before);
|
|
34
36
|
|
|
35
37
|
return faro;
|
|
36
38
|
}
|
|
39
|
+
|
|
40
|
+
type WrappedFetch = typeof fetch & { __original?: typeof fetch };
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The OTLP exporters send with `fetch.__original` when it exists, so an
|
|
44
|
+
* export makes no fetch span. The request monitor of Faro (user actions,
|
|
45
|
+
* navigation, frustration) replaces `fetch` with a wrapper without
|
|
46
|
+
* `__original`. Then each export made a span, and that span was exported
|
|
47
|
+
* again. This gives the wrapper of Faro the `__original` of the fetch that
|
|
48
|
+
* it replaced.
|
|
49
|
+
*/
|
|
50
|
+
function keepFetchOriginal(before: WrappedFetch | undefined): void {
|
|
51
|
+
const after = globalThis.fetch as WrappedFetch | undefined;
|
|
52
|
+
if (!before || !after || after === before) return;
|
|
53
|
+
if (typeof before.__original !== "function") return;
|
|
54
|
+
if (typeof after.__original === "function") return;
|
|
55
|
+
after.__original = before.__original;
|
|
56
|
+
}
|
package/src/lag.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The OpenTelemetry dependencies of the monitors of `@mark1russell7/lag`, from one instance of
|
|
3
|
+
* otel-ts (decision D7, roadmap C15). Without this entry, each app connects the meter, the logger,
|
|
4
|
+
* the events, the spans and the flush by hand.
|
|
5
|
+
*
|
|
6
|
+
* The adapters are the adapters of lag. This entry only gives them the providers of the instance.
|
|
7
|
+
* `@mark1russell7/lag` is an optional peer dependency: only an app that imports this entry needs it.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { init } from "@mark1russell7/otel-ts";
|
|
12
|
+
* import { lagDeps } from "@mark1russell7/otel-ts/lag";
|
|
13
|
+
* import { createBrowserDeps, setupAllMonitors } from "@mark1russell7/lag";
|
|
14
|
+
*
|
|
15
|
+
* const otel = init({ serviceName: "checkout-web" });
|
|
16
|
+
* const lag = lagDeps(otel);
|
|
17
|
+
* const monitors = setupAllMonitors(createBrowserDeps(window, lag));
|
|
18
|
+
* lag.connect(monitors);
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @packageDocumentation
|
|
22
|
+
*/
|
|
23
|
+
import { ROOT_CONTEXT, trace, type Tracer } from "@opentelemetry/api";
|
|
24
|
+
import {
|
|
25
|
+
createOtelEventSink,
|
|
26
|
+
createOtelLoggerAdapter,
|
|
27
|
+
createOtelSpanSink,
|
|
28
|
+
type EventSink,
|
|
29
|
+
type Logger,
|
|
30
|
+
type Meter,
|
|
31
|
+
type SpanSink,
|
|
32
|
+
} from "@mark1russell7/lag";
|
|
33
|
+
import type { OtelTsInstance } from "./types.js";
|
|
34
|
+
|
|
35
|
+
/** The options of `lagDeps` */
|
|
36
|
+
export interface LagDepsOptions {
|
|
37
|
+
/** The name of the meter, the logger and the tracer. Default: `lag`. */
|
|
38
|
+
name?: string;
|
|
39
|
+
/** The tracer of the spans of the monitors. Default: the global tracer of `name`. */
|
|
40
|
+
tracer?: Tracer;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The monitors of lag, as `connect` needs them: the result of `setupAllMonitors()` fits. */
|
|
44
|
+
export interface LagFlushTarget {
|
|
45
|
+
flush(): void;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The OpenTelemetry dependencies of the monitors of lag. Give them to `createBrowserDeps()` of lag. */
|
|
49
|
+
export interface LagDeps {
|
|
50
|
+
/** The meter of the instance, with the name of the options */
|
|
51
|
+
readonly meter: Meter;
|
|
52
|
+
/** The diagnostics of lag, as log records of the instance */
|
|
53
|
+
readonly logger: Logger;
|
|
54
|
+
/** The events of the monitors, as log records with `eventName` */
|
|
55
|
+
readonly events: EventSink;
|
|
56
|
+
/** The spans of the monitors, through the tracer of the options */
|
|
57
|
+
readonly spans: SpanSink;
|
|
58
|
+
/**
|
|
59
|
+
* Record the values that the monitors keep until a checkpoint before each flush of the instance.
|
|
60
|
+
* Thus the final export of a page has the final values of the page view.
|
|
61
|
+
*
|
|
62
|
+
* @returns A function that removes the connection
|
|
63
|
+
*/
|
|
64
|
+
connect(monitors: LagFlushTarget): () => void;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The meter, the logger, the event sink and the span sink of the monitors of lag, from one instance
|
|
69
|
+
* of otel-ts. `connect()` records the values of the monitors before each flush of the instance.
|
|
70
|
+
*/
|
|
71
|
+
export function lagDeps(
|
|
72
|
+
instance: Pick<OtelTsInstance, "getMeter" | "getLogger" | "onBeforeFlush">,
|
|
73
|
+
options: LagDepsOptions = {},
|
|
74
|
+
): LagDeps {
|
|
75
|
+
const name = options.name ?? "lag";
|
|
76
|
+
const logger = instance.getLogger(name);
|
|
77
|
+
return {
|
|
78
|
+
meter: instance.getMeter(name),
|
|
79
|
+
logger: createOtelLoggerAdapter(logger),
|
|
80
|
+
events: createOtelEventSink(logger),
|
|
81
|
+
spans: createOtelSpanSink(options.tracer ?? trace.getTracer(name), { trace, ROOT_CONTEXT }),
|
|
82
|
+
connect: (monitors) => instance.onBeforeFlush(() => monitors.flush()),
|
|
83
|
+
};
|
|
84
|
+
}
|