@peerkit/metrics 0.1.0-alpha.1 → 0.1.0-alpha.9

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.
@@ -0,0 +1,49 @@
1
+ import { DiagLogLevel } from "@opentelemetry/api";
2
+ import { type MetricReader } from "@opentelemetry/sdk-metrics";
3
+ export interface MetricsConfig {
4
+ /** `service.name` resource attribute. Required. */
5
+ serviceName: string;
6
+ /** Optional `service.version` resource attribute. */
7
+ serviceVersion?: string;
8
+ /** OTLP/HTTP metrics endpoint. Default: `http://localhost:4318/v1/metrics`. */
9
+ otlpEndpoint?: string;
10
+ /** Export interval. Default: 60 000 ms. */
11
+ exportIntervalMillis?: number;
12
+ /** Export timeout. Default: 30 000 ms. */
13
+ exportTimeoutMillis?: number;
14
+ /** Extra OTLP request headers (auth, tenant, …). */
15
+ headers?: Record<string, string>;
16
+ /** Additional resource attributes. */
17
+ resourceAttributes?: Record<string, string>;
18
+ /**
19
+ * Optional metric reader to use instead of the default OTLP/HTTP reader.
20
+ * Primarily for tests with `InMemoryMetricExporter`.
21
+ */
22
+ reader?: MetricReader;
23
+ /**
24
+ * Minimum level of OpenTelemetry SDK diagnostic messages forwarded to the
25
+ * `peerkit.metrics` logtape logger. Default: {@link DiagLogLevel.WARN}.
26
+ * Lower (e.g. {@link DiagLogLevel.DEBUG}) is helpful when investigating
27
+ * why metrics are not being exported.
28
+ */
29
+ diagLogLevel?: DiagLogLevel;
30
+ }
31
+ /**
32
+ * Initialise the global metrics pipeline.
33
+ *
34
+ * Builds a {@link MeterProvider} that exports to the configured OTLP endpoint
35
+ * (or to a caller-supplied {@link MetricReader}) and registers it as the
36
+ * global meter provider for `@opentelemetry/api`.
37
+ *
38
+ * Must be called at most once between {@link shutdownMetrics} calls; a second
39
+ * call without prior shutdown rejects.
40
+ */
41
+ export declare function initMetrics(cfg: MetricsConfig): Promise<void>;
42
+ /**
43
+ * Flush pending metrics, shut the provider down, and restore the OpenTelemetry
44
+ * default no-op meter provider.
45
+ *
46
+ * Safe to call when metrics have not been initialised.
47
+ */
48
+ export declare function shutdownMetrics(): Promise<void>;
49
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAW,MAAM,oBAAoB,CAAC;AAG3D,OAAO,EAGL,KAAK,YAAY,EAClB,MAAM,4BAA4B,CAAC;AAOpC,MAAM,WAAW,aAAa;IAC5B,mDAAmD;IACnD,WAAW,EAAE,MAAM,CAAC;IACpB,qDAAqD;IACrD,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,+EAA+E;IAC/E,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,2CAA2C;IAC3C,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,0CAA0C;IAC1C,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B,oDAAoD;IACpD,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,sCAAsC;IACtC,kBAAkB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC5C;;;OAGG;IACH,MAAM,CAAC,EAAE,YAAY,CAAC;IACtB;;;;;OAKG;IACH,YAAY,CAAC,EAAE,YAAY,CAAC;CAC7B;AAID;;;;;;;;;GASG;AACH,wBAAsB,WAAW,CAAC,GAAG,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,CA8BnE;AAED;;;;;GAKG;AACH,wBAAsB,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC,CAgBrD"}
package/dist/config.js ADDED
@@ -0,0 +1,67 @@
1
+ import { DiagLogLevel, metrics } from "@opentelemetry/api";
2
+ import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-http";
3
+ import { resourceFromAttributes } from "@opentelemetry/resources";
4
+ import { MeterProvider, PeriodicExportingMetricReader, } from "@opentelemetry/sdk-metrics";
5
+ import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION, } from "@opentelemetry/semantic-conventions";
6
+ import { installDiagLogger, uninstallDiagLogger } from "./diag.js";
7
+ let activeProvider;
8
+ /**
9
+ * Initialise the global metrics pipeline.
10
+ *
11
+ * Builds a {@link MeterProvider} that exports to the configured OTLP endpoint
12
+ * (or to a caller-supplied {@link MetricReader}) and registers it as the
13
+ * global meter provider for `@opentelemetry/api`.
14
+ *
15
+ * Must be called at most once between {@link shutdownMetrics} calls; a second
16
+ * call without prior shutdown rejects.
17
+ */
18
+ export async function initMetrics(cfg) {
19
+ if (activeProvider) {
20
+ throw new Error("Metrics already initialised. Call shutdownMetrics() before re-initialising.");
21
+ }
22
+ installDiagLogger(cfg.diagLogLevel);
23
+ const resourceAttrs = {
24
+ [ATTR_SERVICE_NAME]: cfg.serviceName,
25
+ ...(cfg.serviceVersion && { [ATTR_SERVICE_VERSION]: cfg.serviceVersion }),
26
+ ...(cfg.resourceAttributes ?? {}),
27
+ };
28
+ const resource = resourceFromAttributes(resourceAttrs);
29
+ const reader = cfg.reader ??
30
+ new PeriodicExportingMetricReader({
31
+ exporter: new OTLPMetricExporter({
32
+ url: cfg.otlpEndpoint ?? "http://localhost:4318/v1/metrics",
33
+ headers: cfg.headers,
34
+ timeoutMillis: cfg.exportTimeoutMillis ?? 30_000,
35
+ }),
36
+ exportIntervalMillis: cfg.exportIntervalMillis ?? 60_000,
37
+ });
38
+ const provider = new MeterProvider({ resource, readers: [reader] });
39
+ metrics.setGlobalMeterProvider(provider);
40
+ activeProvider = provider;
41
+ }
42
+ /**
43
+ * Flush pending metrics, shut the provider down, and restore the OpenTelemetry
44
+ * default no-op meter provider.
45
+ *
46
+ * Safe to call when metrics have not been initialised.
47
+ */
48
+ export async function shutdownMetrics() {
49
+ if (!activeProvider) {
50
+ return;
51
+ }
52
+ const provider = activeProvider;
53
+ activeProvider = undefined;
54
+ try {
55
+ await provider.forceFlush();
56
+ }
57
+ finally {
58
+ try {
59
+ await provider.shutdown();
60
+ }
61
+ finally {
62
+ metrics.disable();
63
+ uninstallDiagLogger();
64
+ }
65
+ }
66
+ }
67
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,EAAE,kBAAkB,EAAE,MAAM,2CAA2C,CAAC;AAC/E,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,EACL,aAAa,EACb,6BAA6B,GAE9B,MAAM,4BAA4B,CAAC;AACpC,OAAO,EACL,iBAAiB,EACjB,oBAAoB,GACrB,MAAM,qCAAqC,CAAC;AAC7C,OAAO,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AA+BnE,IAAI,cAAyC,CAAC;AAE9C;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,GAAkB;IAClD,IAAI,cAAc,EAAE,CAAC;QACnB,MAAM,IAAI,KAAK,CACb,6EAA6E,CAC9E,CAAC;IACJ,CAAC;IAED,iBAAiB,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;IAEpC,MAAM,aAAa,GAA2B;QAC5C,CAAC,iBAAiB,CAAC,EAAE,GAAG,CAAC,WAAW;QACpC,GAAG,CAAC,GAAG,CAAC,cAAc,IAAI,EAAE,CAAC,oBAAoB,CAAC,EAAE,GAAG,CAAC,cAAc,EAAE,CAAC;QACzE,GAAG,CAAC,GAAG,CAAC,kBAAkB,IAAI,EAAE,CAAC;KAClC,CAAC;IACF,MAAM,QAAQ,GAAG,sBAAsB,CAAC,aAAa,CAAC,CAAC;IAEvD,MAAM,MAAM,GACV,GAAG,CAAC,MAAM;QACV,IAAI,6BAA6B,CAAC;YAChC,QAAQ,EAAE,IAAI,kBAAkB,CAAC;gBAC/B,GAAG,EAAE,GAAG,CAAC,YAAY,IAAI,kCAAkC;gBAC3D,OAAO,EAAE,GAAG,CAAC,OAAO;gBACpB,aAAa,EAAE,GAAG,CAAC,mBAAmB,IAAI,MAAM;aACjD,CAAC;YACF,oBAAoB,EAAE,GAAG,CAAC,oBAAoB,IAAI,MAAM;SACzD,CAAC,CAAC;IAEL,MAAM,QAAQ,GAAG,IAAI,aAAa,CAAC,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IACpE,OAAO,CAAC,sBAAsB,CAAC,QAAQ,CAAC,CAAC;IACzC,cAAc,GAAG,QAAQ,CAAC;AAC5B,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe;IACnC,IAAI,CAAC,cAAc,EAAE,CAAC;QACpB,OAAO;IACT,CAAC;IACD,MAAM,QAAQ,GAAG,cAAc,CAAC;IAChC,cAAc,GAAG,SAAS,CAAC;IAC3B,IAAI,CAAC;QACH,MAAM,QAAQ,CAAC,UAAU,EAAE,CAAC;IAC9B,CAAC;YAAS,CAAC;QACT,IAAI,CAAC;YACH,MAAM,QAAQ,CAAC,QAAQ,EAAE,CAAC;QAC5B,CAAC;gBAAS,CAAC;YACT,OAAO,CAAC,OAAO,EAAE,CAAC;YAClB,mBAAmB,EAAE,CAAC;QACxB,CAAC;IACH,CAAC;AACH,CAAC"}
package/dist/diag.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ import { DiagLogLevel } from "@opentelemetry/api";
2
+ /**
3
+ * Bridge OpenTelemetry's `diag` channel to the peerkit logtape logger,
4
+ * so exporter errors and SDK diagnostics surface through the standard
5
+ * peerkit logging pipeline.
6
+ *
7
+ * `level` filters messages on the OTel side before they reach logtape:
8
+ * - `ERROR` — only outright failures.
9
+ * - `WARN` (default) — failures + retries / dropped data.
10
+ * - `INFO` / `DEBUG` / `VERBOSE` — increasingly chatty; useful when
11
+ * diagnosing why metrics are not exporting.
12
+ */
13
+ export declare function installDiagLogger(level?: DiagLogLevel): void;
14
+ /**
15
+ * Restore the OpenTelemetry default diag logger.
16
+ */
17
+ export declare function uninstallDiagLogger(): void;
18
+ //# sourceMappingURL=diag.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"diag.d.ts","sourceRoot":"","sources":["../src/diag.ts"],"names":[],"mappings":"AACA,OAAO,EAAQ,YAAY,EAAmB,MAAM,oBAAoB,CAAC;AAIzE;;;;;;;;;;GAUG;AACH,wBAAgB,iBAAiB,CAC/B,KAAK,GAAE,YAAgC,GACtC,IAAI,CASN;AAED;;GAEG;AACH,wBAAgB,mBAAmB,IAAI,IAAI,CAE1C"}
@@ -1,8 +1,6 @@
1
1
  import { getLogger } from "@logtape/logtape";
2
- import { diag, DiagLogLevel, type DiagLogger } from "@opentelemetry/api";
3
-
2
+ import { diag, DiagLogLevel } from "@opentelemetry/api";
4
3
  const logger = getLogger(["peerkit", "metrics"]);
5
-
6
4
  /**
7
5
  * Bridge OpenTelemetry's `diag` channel to the peerkit logtape logger,
8
6
  * so exporter errors and SDK diagnostics surface through the standard
@@ -14,22 +12,20 @@ const logger = getLogger(["peerkit", "metrics"]);
14
12
  * - `INFO` / `DEBUG` / `VERBOSE` — increasingly chatty; useful when
15
13
  * diagnosing why metrics are not exporting.
16
14
  */
17
- export function installDiagLogger(
18
- level: DiagLogLevel = DiagLogLevel.WARN,
19
- ): void {
20
- const diagLogger: DiagLogger = {
21
- error: (message, ...args) => logger.error(message, { args }),
22
- warn: (message, ...args) => logger.warn(message, { args }),
23
- info: (message, ...args) => logger.info(message, { args }),
24
- debug: (message, ...args) => logger.debug(message, { args }),
25
- verbose: (message, ...args) => logger.debug(message, { args }),
26
- };
27
- diag.setLogger(diagLogger, level);
15
+ export function installDiagLogger(level = DiagLogLevel.WARN) {
16
+ const diagLogger = {
17
+ error: (message, ...args) => logger.error(message, { args }),
18
+ warn: (message, ...args) => logger.warn(message, { args }),
19
+ info: (message, ...args) => logger.info(message, { args }),
20
+ debug: (message, ...args) => logger.debug(message, { args }),
21
+ verbose: (message, ...args) => logger.debug(message, { args }),
22
+ };
23
+ diag.setLogger(diagLogger, level);
28
24
  }
29
-
30
25
  /**
31
26
  * Restore the OpenTelemetry default diag logger.
32
27
  */
33
- export function uninstallDiagLogger(): void {
34
- diag.disable();
28
+ export function uninstallDiagLogger() {
29
+ diag.disable();
35
30
  }
31
+ //# sourceMappingURL=diag.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"diag.js","sourceRoot":"","sources":["../src/diag.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,IAAI,EAAE,YAAY,EAAmB,MAAM,oBAAoB,CAAC;AAEzE,MAAM,MAAM,GAAG,SAAS,CAAC,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC;AAEjD;;;;;;;;;;GAUG;AACH,MAAM,UAAU,iBAAiB,CAC/B,QAAsB,YAAY,CAAC,IAAI;IAEvC,MAAM,UAAU,GAAe;QAC7B,KAAK,EAAE,CAAC,OAAO,EAAE,GAAG,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,EAAE,IAAI,EAAE,CAAC;QAC5D,IAAI,EAAE,CAAC,OAAO,EAAE,GAAG,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,EAAE,IAAI,EAAE,CAAC;QAC1D,IAAI,EAAE,CAAC,OAAO,EAAE,GAAG,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,EAAE,IAAI,EAAE,CAAC;QAC1D,KAAK,EAAE,CAAC,OAAO,EAAE,GAAG,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,EAAE,IAAI,EAAE,CAAC;QAC5D,OAAO,EAAE,CAAC,OAAO,EAAE,GAAG,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,EAAE,IAAI,EAAE,CAAC;KAC/D,CAAC;IACF,IAAI,CAAC,SAAS,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;AACpC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,mBAAmB;IACjC,IAAI,CAAC,OAAO,EAAE,CAAC;AACjB,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from "./config.js";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./config.js";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peerkit/metrics",
3
- "version": "0.1.0-alpha.1",
3
+ "version": "0.1.0-alpha.9",
4
4
  "description": "OpenTelemetry-based metric collection for peerkit",
5
5
  "keywords": [
6
6
  "peerkit",
@@ -10,6 +10,9 @@
10
10
  ],
11
11
  "author": "Holochain Dev Team <devcore@holochain.org>",
12
12
  "license": "CAL-1.0",
13
+ "files": [
14
+ "dist"
15
+ ],
13
16
  "bugs": {
14
17
  "url": "https://github.com/holochain/peerkit/issues"
15
18
  },
@@ -21,8 +24,8 @@
21
24
  "type": "module",
22
25
  "exports": {
23
26
  ".": {
24
- "import": "./dist/index.js",
25
- "types": "./dist/index.d.ts"
27
+ "types": "./dist/index.d.ts",
28
+ "import": "./dist/index.js"
26
29
  }
27
30
  },
28
31
  "scripts": {
@@ -31,10 +34,9 @@
31
34
  "dependencies": {
32
35
  "@logtape/logtape": "^2.0.5",
33
36
  "@opentelemetry/api": "^1.9.0",
34
- "@opentelemetry/exporter-metrics-otlp-http": "^0.217.0",
37
+ "@opentelemetry/exporter-metrics-otlp-http": "^0.218.0",
35
38
  "@opentelemetry/resources": "^2.7.0",
36
39
  "@opentelemetry/sdk-metrics": "^2.7.0",
37
40
  "@opentelemetry/semantic-conventions": "^1.40.0"
38
- },
39
- "devDependencies": {}
41
+ }
40
42
  }
package/src/config.ts DELETED
@@ -1,110 +0,0 @@
1
- import { DiagLogLevel, metrics } from "@opentelemetry/api";
2
- import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-http";
3
- import { resourceFromAttributes } from "@opentelemetry/resources";
4
- import {
5
- MeterProvider,
6
- PeriodicExportingMetricReader,
7
- type MetricReader,
8
- } from "@opentelemetry/sdk-metrics";
9
- import {
10
- ATTR_SERVICE_NAME,
11
- ATTR_SERVICE_VERSION,
12
- } from "@opentelemetry/semantic-conventions";
13
- import { installDiagLogger, uninstallDiagLogger } from "./diag.js";
14
-
15
- export interface MetricsConfig {
16
- /** `service.name` resource attribute. Required. */
17
- serviceName: string;
18
- /** Optional `service.version` resource attribute. */
19
- serviceVersion?: string;
20
- /** OTLP/HTTP metrics endpoint. Default: `http://localhost:4318/v1/metrics`. */
21
- otlpEndpoint?: string;
22
- /** Export interval. Default: 60 000 ms. */
23
- exportIntervalMillis?: number;
24
- /** Export timeout. Default: 30 000 ms. */
25
- exportTimeoutMillis?: number;
26
- /** Extra OTLP request headers (auth, tenant, …). */
27
- headers?: Record<string, string>;
28
- /** Additional resource attributes. */
29
- resourceAttributes?: Record<string, string>;
30
- /**
31
- * Optional metric reader to use instead of the default OTLP/HTTP reader.
32
- * Primarily for tests with `InMemoryMetricExporter`.
33
- */
34
- reader?: MetricReader;
35
- /**
36
- * Minimum level of OpenTelemetry SDK diagnostic messages forwarded to the
37
- * `peerkit.metrics` logtape logger. Default: {@link DiagLogLevel.WARN}.
38
- * Lower (e.g. {@link DiagLogLevel.DEBUG}) is helpful when investigating
39
- * why metrics are not being exported.
40
- */
41
- diagLogLevel?: DiagLogLevel;
42
- }
43
-
44
- let activeProvider: MeterProvider | undefined;
45
-
46
- /**
47
- * Initialise the global metrics pipeline.
48
- *
49
- * Builds a {@link MeterProvider} that exports to the configured OTLP endpoint
50
- * (or to a caller-supplied {@link MetricReader}) and registers it as the
51
- * global meter provider for `@opentelemetry/api`.
52
- *
53
- * Must be called at most once between {@link shutdownMetrics} calls; a second
54
- * call without prior shutdown rejects.
55
- */
56
- export async function initMetrics(cfg: MetricsConfig): Promise<void> {
57
- if (activeProvider) {
58
- throw new Error(
59
- "Metrics already initialised. Call shutdownMetrics() before re-initialising.",
60
- );
61
- }
62
-
63
- installDiagLogger(cfg.diagLogLevel);
64
-
65
- const resourceAttrs: Record<string, string> = {
66
- [ATTR_SERVICE_NAME]: cfg.serviceName,
67
- ...(cfg.serviceVersion && { [ATTR_SERVICE_VERSION]: cfg.serviceVersion }),
68
- ...(cfg.resourceAttributes ?? {}),
69
- };
70
- const resource = resourceFromAttributes(resourceAttrs);
71
-
72
- const reader =
73
- cfg.reader ??
74
- new PeriodicExportingMetricReader({
75
- exporter: new OTLPMetricExporter({
76
- url: cfg.otlpEndpoint ?? "http://localhost:4318/v1/metrics",
77
- headers: cfg.headers,
78
- timeoutMillis: cfg.exportTimeoutMillis ?? 30_000,
79
- }),
80
- exportIntervalMillis: cfg.exportIntervalMillis ?? 60_000,
81
- });
82
-
83
- const provider = new MeterProvider({ resource, readers: [reader] });
84
- metrics.setGlobalMeterProvider(provider);
85
- activeProvider = provider;
86
- }
87
-
88
- /**
89
- * Flush pending metrics, shut the provider down, and restore the OpenTelemetry
90
- * default no-op meter provider.
91
- *
92
- * Safe to call when metrics have not been initialised.
93
- */
94
- export async function shutdownMetrics(): Promise<void> {
95
- if (!activeProvider) {
96
- return;
97
- }
98
- const provider = activeProvider;
99
- activeProvider = undefined;
100
- try {
101
- await provider.forceFlush();
102
- } finally {
103
- try {
104
- await provider.shutdown();
105
- } finally {
106
- metrics.disable();
107
- uninstallDiagLogger();
108
- }
109
- }
110
- }
package/src/index.ts DELETED
@@ -1 +0,0 @@
1
- export * from "./config.js";
@@ -1,64 +0,0 @@
1
- import {
2
- AggregationTemporality,
3
- InMemoryMetricExporter,
4
- PeriodicExportingMetricReader,
5
- } from "@opentelemetry/sdk-metrics";
6
- import { metrics } from "@opentelemetry/api";
7
- import { afterEach, describe, expect, test } from "vitest";
8
- import { initMetrics, shutdownMetrics } from "../src/index.js";
9
-
10
- const makeReader = () => {
11
- const exporter = new InMemoryMetricExporter(
12
- AggregationTemporality.CUMULATIVE,
13
- );
14
- const reader = new PeriodicExportingMetricReader({
15
- exporter,
16
- exportIntervalMillis: 60_000,
17
- });
18
- return { exporter, reader };
19
- };
20
-
21
- describe("initMetrics", () => {
22
- afterEach(async () => {
23
- await shutdownMetrics();
24
- });
25
-
26
- test("records a counter that the configured reader observes", async () => {
27
- const { exporter, reader } = makeReader();
28
- await initMetrics({ serviceName: "test-service", reader });
29
-
30
- const counter = metrics.getMeter("test").createCounter("test.counter");
31
- counter.add(7, { kind: "unit" });
32
-
33
- await reader.forceFlush();
34
- const collected = exporter.getMetrics();
35
- const found = collected
36
- .flatMap((rm) => rm.scopeMetrics)
37
- .flatMap((sm) => sm.metrics)
38
- .find((m) => m.descriptor.name === "test.counter");
39
- expect(found).toBeDefined();
40
- expect(found?.dataPoints[0]?.value).toBe(7);
41
- });
42
-
43
- test("calling initMetrics twice without shutdown rejects", async () => {
44
- const { reader: r1 } = makeReader();
45
- const { reader: r2 } = makeReader();
46
- await initMetrics({ serviceName: "svc", reader: r1 });
47
- await expect(
48
- initMetrics({ serviceName: "svc", reader: r2 }),
49
- ).rejects.toThrow(/already initialised/i);
50
- });
51
-
52
- test("shutdownMetrics reverts the global provider to the OTel no-op", async () => {
53
- const { reader } = makeReader();
54
- await initMetrics({ serviceName: "svc", reader });
55
- const beforeProvider = metrics.getMeterProvider();
56
- await shutdownMetrics();
57
- const afterProvider = metrics.getMeterProvider();
58
- expect(afterProvider).not.toBe(beforeProvider);
59
- });
60
-
61
- test("shutdownMetrics is safe to call when not initialised", async () => {
62
- await expect(shutdownMetrics()).resolves.toBeUndefined();
63
- });
64
- });
package/tsconfig.json DELETED
@@ -1,10 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "compilerOptions": {
4
- "composite": true,
5
- "rootDir": "src",
6
- "outDir": "dist"
7
- },
8
- "include": ["src"],
9
- "references": []
10
- }