@routr/common 2.32.0 → 2.32.2

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,15 @@
1
+ import { Sampler } from "@opentelemetry/sdk-trace";
2
+ /**
3
+ * The trace sampler named by the standard OTEL_TRACES_SAMPLER and
4
+ * OTEL_TRACES_SAMPLER_ARG variables, as the OpenTelemetry specification defines
5
+ * them (and as the EdgePort's Java SDK reads them): always_on, always_off,
6
+ * traceidratio, parentbased_always_on (the default), parentbased_always_off and
7
+ * parentbased_traceidratio. The ratio is a number from 0 to 1 and defaults to 1.
8
+ * An unknown sampler or a bad ratio falls back to the default with a warning, so
9
+ * a typo never stops a service.
10
+ *
11
+ * @param {NodeJS.ProcessEnv} env - The environment to read.
12
+ * @param {Function} warn - Receives a message for each value that was ignored.
13
+ * @return {Sampler} The sampler for the tracer provider.
14
+ */
15
+ export declare function samplerFromEnv(env?: NodeJS.ProcessEnv, warn?: (message: string) => void): Sampler;
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.samplerFromEnv = samplerFromEnv;
4
+ /*
5
+ * Copyright (C) 2026 by Fonoster Inc (https://fonoster.com)
6
+ * http://github.com/fonoster/routr
7
+ *
8
+ * This file is part of Routr.
9
+ *
10
+ * Licensed under the MIT License (the "License");
11
+ * you may not use this file except in compliance with
12
+ * the License. You may obtain a copy of the License at
13
+ *
14
+ * https://opensource.org/licenses/MIT
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ */
22
+ const sdk_trace_1 = require("@opentelemetry/sdk-trace");
23
+ const DEFAULT_SAMPLER = "parentbased_always_on";
24
+ /**
25
+ * The trace sampler named by the standard OTEL_TRACES_SAMPLER and
26
+ * OTEL_TRACES_SAMPLER_ARG variables, as the OpenTelemetry specification defines
27
+ * them (and as the EdgePort's Java SDK reads them): always_on, always_off,
28
+ * traceidratio, parentbased_always_on (the default), parentbased_always_off and
29
+ * parentbased_traceidratio. The ratio is a number from 0 to 1 and defaults to 1.
30
+ * An unknown sampler or a bad ratio falls back to the default with a warning, so
31
+ * a typo never stops a service.
32
+ *
33
+ * @param {NodeJS.ProcessEnv} env - The environment to read.
34
+ * @param {Function} warn - Receives a message for each value that was ignored.
35
+ * @return {Sampler} The sampler for the tracer provider.
36
+ */
37
+ function samplerFromEnv(env = process.env, warn = (message) => process.emitWarning(message)) {
38
+ var _a;
39
+ const name = (((_a = env.OTEL_TRACES_SAMPLER) === null || _a === void 0 ? void 0 : _a.trim()) || DEFAULT_SAMPLER).toLowerCase();
40
+ const ratio = () => {
41
+ var _a;
42
+ const arg = (_a = env.OTEL_TRACES_SAMPLER_ARG) === null || _a === void 0 ? void 0 : _a.trim();
43
+ if (!arg)
44
+ return 1;
45
+ const value = Number(arg);
46
+ if (Number.isFinite(value) && value >= 0 && value <= 1)
47
+ return value;
48
+ warn(`OTEL_TRACES_SAMPLER_ARG "${arg}" is not a number from 0 to 1; using 1`);
49
+ return 1;
50
+ };
51
+ switch (name) {
52
+ case "always_on":
53
+ return new sdk_trace_1.AlwaysOnSampler();
54
+ case "always_off":
55
+ return new sdk_trace_1.AlwaysOffSampler();
56
+ case "traceidratio":
57
+ return new sdk_trace_1.TraceIdRatioBasedSampler(ratio());
58
+ case "parentbased_always_on":
59
+ return new sdk_trace_1.ParentBasedSampler({ root: new sdk_trace_1.AlwaysOnSampler() });
60
+ case "parentbased_always_off":
61
+ return new sdk_trace_1.ParentBasedSampler({ root: new sdk_trace_1.AlwaysOffSampler() });
62
+ case "parentbased_traceidratio":
63
+ return new sdk_trace_1.ParentBasedSampler({
64
+ root: new sdk_trace_1.TraceIdRatioBasedSampler(ratio())
65
+ });
66
+ default:
67
+ warn(`OTEL_TRACES_SAMPLER "${name}" is not supported; using ${DEFAULT_SAMPLER}`);
68
+ return new sdk_trace_1.ParentBasedSampler({ root: new sdk_trace_1.AlwaysOnSampler() });
69
+ }
70
+ }
package/dist/tracer.js CHANGED
@@ -78,6 +78,7 @@ const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
78
78
  const exporter_trace_otlp_http_1 = require("@opentelemetry/exporter-trace-otlp-http");
79
79
  const exporter_metrics_otlp_http_1 = require("@opentelemetry/exporter-metrics-otlp-http");
80
80
  const exporter_logs_otlp_http_1 = require("@opentelemetry/exporter-logs-otlp-http");
81
+ const sampler_from_env_1 = require("./sampler_from_env");
81
82
  /**
82
83
  * Whether telemetry is on: an OTLP endpoint is configured and the SDK is not disabled.
83
84
  *
@@ -133,6 +134,8 @@ function init(serviceName, env = process.env) {
133
134
  }));
134
135
  const tracerProvider = new sdk_trace_1.TracerProvider({
135
136
  resource,
137
+ // The SDK does not read OTEL_TRACES_SAMPLER itself: without this every span is sampled.
138
+ sampler: (0, sampler_from_env_1.samplerFromEnv)(env),
136
139
  spanProcessors: [
137
140
  new sdk_trace_1.BatchSpanProcessor({
138
141
  exporter: new exporter_trace_otlp_http_1.OTLPTraceExporter({ url: signalUrl(env, "traces") })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@routr/common",
3
- "version": "2.32.0",
3
+ "version": "2.32.2",
4
4
  "description": "Common package",
5
5
  "author": "Pedro Sanders <psanders@fonoster.com>",
6
6
  "homepage": "https://github.com/fonoster/routr#readme",
@@ -61,5 +61,5 @@
61
61
  "devDependencies": {
62
62
  "@types/js-yaml": "^4.0.5"
63
63
  },
64
- "gitHead": "369849c10e84d6fac55051c6cfbf1ee01478c122"
64
+ "gitHead": "4781133850af7e3cf9daa3b538c385836422d27e"
65
65
  }