@contrail/telemetry 2.0.4-beta.0 → 2.0.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/CHANGELOG.md CHANGED
@@ -7,6 +7,12 @@ Versioning follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [2.0.4] - 2026-04-16
11
+
12
+ ### Added
13
+
14
+ - **Duplicate module detection** — logs a `stderr` warning at cold start if two copies of `@contrail/telemetry` are loaded, so silent log loss and broken context propagation are immediately visible.
15
+
10
16
  ## [2.0.3] - 2026-04-15
11
17
 
12
18
  ### Changed
package/lib/index.d.ts CHANGED
@@ -1,3 +1,2 @@
1
1
  export * from './logger';
2
- export * from './metrics';
3
2
  export * from './semantic-conventions';
package/lib/index.js CHANGED
@@ -15,5 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./logger"), exports);
18
- __exportStar(require("./metrics"), exports);
19
18
  __exportStar(require("./semantic-conventions"), exports);
@@ -32,6 +32,31 @@ Object.defineProperty(exports, "ATTR_LOG_PAYLOAD", { enumerable: true, get: func
32
32
  var log_context_1 = require("./log-context");
33
33
  Object.defineProperty(exports, "loggerStorage", { enumerable: true, get: function () { return log_context_1.loggerStorage; } });
34
34
  Object.defineProperty(exports, "withLogAttributes", { enumerable: true, get: function () { return log_context_1.withLogAttributes; } });
35
+ // --- Singleton Guard ---
36
+ // The logger uses AsyncLocalStorage for request context propagation. If two copies
37
+ // of this module are loaded (e.g. due to a version mismatch causing npm to install
38
+ // a nested duplicate), each copy has its own AsyncLocalStorage instance and its own
39
+ // OTel LoggerProvider. Context set in one copy won't be visible in the other, and
40
+ // flushLogs() will only flush the provider it closes over — silently dropping logs.
41
+ //
42
+ // Symbol.for uses the global symbol registry, which is shared across all module
43
+ // instances, so this check fires correctly even when two copies are loaded.
44
+ //
45
+ // Local dev note: running `npm i` inside a lib package directory causes npm v7+ to
46
+ // auto-install peer deps locally. If that package is then symlinked into a service,
47
+ // the nested node_modules travels with it and re-creates the duplicate. Published
48
+ // tarballs don't ship node_modules, so this is local-only.
49
+ const TELEMETRY_SINGLETON_KEY = Symbol.for('@contrail/telemetry/logger-initialized');
50
+ const globalRecord = globalThis;
51
+ if (globalRecord[TELEMETRY_SINGLETON_KEY]) {
52
+ process.stderr.write('[WARN] @contrail/telemetry loaded twice — duplicate module instance detected. ' +
53
+ 'flushLogs() will only flush one provider and log context may not propagate correctly. ' +
54
+ 'Ensure @contrail/telemetry is listed as a peerDependency in any library that re-exports ' +
55
+ 'it, and that the host service lists it in its own package.json.\n');
56
+ }
57
+ else {
58
+ globalRecord[TELEMETRY_SINGLETON_KEY] = true;
59
+ }
35
60
  // --- Environment & Resource Setup ---
36
61
  const semantic_conventions_4 = require("../semantic-conventions");
37
62
  const LAMBDA_ENV_OTEL_ATTRIBUTES = getLambdaEnvAttributes();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/telemetry",
3
- "version": "2.0.4-beta.0",
3
+ "version": "2.0.4",
4
4
  "description": "Telemetry and monitoring utilities for contrail services",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -20,10 +20,8 @@
20
20
  "@opentelemetry/api-logs": "^0.211.0",
21
21
  "@opentelemetry/core": "^2.5.0",
22
22
  "@opentelemetry/exporter-logs-otlp-http": "^0.211.0",
23
- "@opentelemetry/exporter-metrics-otlp-http": "^0.211.0",
24
23
  "@opentelemetry/resources": "^2.5.0",
25
24
  "@opentelemetry/sdk-logs": "^0.211.0",
26
- "@opentelemetry/sdk-metrics": "^2.5.0",
27
25
  "@opentelemetry/sdk-trace-base": "^2.5.0",
28
26
  "@opentelemetry/semantic-conventions": "^1.39.0",
29
27
  "pino": "^10.1.0"
@@ -1,4 +0,0 @@
1
- export declare function getMeter(name: string): import("@opentelemetry/api").Meter;
2
- export declare function flushMetrics(options?: {
3
- timeoutMs?: number;
4
- }): Promise<void>;
@@ -1,40 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getMeter = getMeter;
4
- exports.flushMetrics = flushMetrics;
5
- const sdk_metrics_1 = require("@opentelemetry/sdk-metrics");
6
- const exporter_metrics_otlp_http_1 = require("@opentelemetry/exporter-metrics-otlp-http");
7
- const resources_1 = require("@opentelemetry/resources");
8
- const parse_otel_resource_attributes_1 = require("../logger/parse-otel-resource-attributes");
9
- const otlpEndpoint = process.env.OTEL_EXPORTER_OTLP_ENDPOINT;
10
- const shouldExportToOtlp = Boolean(otlpEndpoint);
11
- const resource = (0, resources_1.resourceFromAttributes)((0, parse_otel_resource_attributes_1.parseOtelResourceAttributes)());
12
- const readers = shouldExportToOtlp
13
- ? [
14
- new sdk_metrics_1.PeriodicExportingMetricReader({
15
- exporter: new exporter_metrics_otlp_http_1.OTLPMetricExporter(),
16
- exportIntervalMillis: 30000,
17
- }),
18
- ]
19
- : [];
20
- const meterProvider = new sdk_metrics_1.MeterProvider({ resource, readers });
21
- function getMeter(name) {
22
- return meterProvider.getMeter(name);
23
- }
24
- async function flushMetrics(options) {
25
- var _a;
26
- const timeoutMs = (_a = options === null || options === void 0 ? void 0 : options.timeoutMs) !== null && _a !== void 0 ? _a : 5000;
27
- try {
28
- await Promise.race([
29
- meterProvider.forceFlush(),
30
- new Promise((_, reject) => setTimeout(() => reject(new Error(`Metrics flush timed out after ${timeoutMs}ms`)), timeoutMs)),
31
- ]);
32
- }
33
- catch (error) {
34
- process.stderr.write(`[OTel Metrics Flush Failed] ${JSON.stringify({
35
- error: error instanceof Error ? error.message : String(error),
36
- timeoutMs,
37
- timestamp: new Date().toISOString(),
38
- })}\n`);
39
- }
40
- }