@devopsplaybook.io/otel-utils 1.8.0 → 1.9.0-beta.40.3ebb823
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 +36 -29
- package/dist/src/StandardLogger.js +3 -1
- package/dist/src/StandardTracer.d.ts +8 -2
- package/dist/src/StandardTracer.js +7 -4
- package/package.json +18 -5
- package/.github/workflows/main-build.yml +0 -16
- package/.github/workflows/pr-check.yml +0 -25
- package/index.ts +0 -7
- package/jest.config.js +0 -17
- package/prettierrc.json +0 -5
- package/src/ModuleLogger.spec.ts +0 -253
- package/src/ModuleLogger.ts +0 -137
- package/src/ShutdownHooks.spec.ts +0 -75
- package/src/ShutdownHooks.ts +0 -45
- package/src/StandardLogger.spec.ts +0 -479
- package/src/StandardLogger.ts +0 -149
- package/src/StandardMeter.spec.ts +0 -446
- package/src/StandardMeter.ts +0 -167
- package/src/StandardTracer.spec.ts +0 -349
- package/src/StandardTracer.ts +0 -190
- package/src/createResource.spec.ts +0 -20
- package/src/models/ConfigOTelInterface.ts +0 -16
- package/src/models/StandardLoggerInterface.ts +0 -5
- package/src/utils/configValidation.spec.ts +0 -141
- package/src/utils/configValidation.ts +0 -50
- package/src/utils/createResource.ts +0 -20
- package/tsconfig.json +0 -14
- package/tsconfig.spec.json +0 -8
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import type { ConfigOTelInterface } from "../models/ConfigOTelInterface";
|
|
2
|
-
import {
|
|
3
|
-
normalizeConsoleLogMode,
|
|
4
|
-
normalizeExportIntervalSeconds,
|
|
5
|
-
normalizePositiveInteger,
|
|
6
|
-
validateServiceIdentity,
|
|
7
|
-
} from "./configValidation";
|
|
8
|
-
|
|
9
|
-
type ServiceIdentity = Pick<ConfigOTelInterface, "SERVICE_ID" | "VERSION">;
|
|
10
|
-
|
|
11
|
-
function identity(serviceId: unknown, version: unknown): ServiceIdentity {
|
|
12
|
-
return {
|
|
13
|
-
SERVICE_ID: serviceId,
|
|
14
|
-
VERSION: version,
|
|
15
|
-
} as ServiceIdentity;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
describe("configValidation", () => {
|
|
19
|
-
let warnSpy: jest.SpyInstance;
|
|
20
|
-
|
|
21
|
-
beforeEach(() => {
|
|
22
|
-
warnSpy = jest.spyOn(console, "warn").mockImplementation(() => {});
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
afterEach(() => {
|
|
26
|
-
warnSpy.mockRestore();
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
describe("validateServiceIdentity", () => {
|
|
30
|
-
it("accepts non-empty SERVICE_ID and VERSION", () => {
|
|
31
|
-
expect(() =>
|
|
32
|
-
validateServiceIdentity(identity("my-service", "1.2.3")),
|
|
33
|
-
).not.toThrow();
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it("rejects an empty SERVICE_ID", () => {
|
|
37
|
-
expect(() => validateServiceIdentity(identity("", "1.2.3"))).toThrow(
|
|
38
|
-
"[otel-utils] SERVICE_ID must be a non-empty string",
|
|
39
|
-
);
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it("rejects a whitespace-only SERVICE_ID", () => {
|
|
43
|
-
expect(() => validateServiceIdentity(identity(" ", "1.2.3"))).toThrow(
|
|
44
|
-
"[otel-utils] SERVICE_ID must be a non-empty string",
|
|
45
|
-
);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it("rejects a missing SERVICE_ID", () => {
|
|
49
|
-
expect(() =>
|
|
50
|
-
validateServiceIdentity(identity(undefined, "1.2.3")),
|
|
51
|
-
).toThrow("[otel-utils] SERVICE_ID must be a non-empty string");
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it("rejects a non-string SERVICE_ID", () => {
|
|
55
|
-
expect(() => validateServiceIdentity(identity(42, "1.2.3"))).toThrow(
|
|
56
|
-
"[otel-utils] SERVICE_ID must be a non-empty string",
|
|
57
|
-
);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it("rejects an empty VERSION", () => {
|
|
61
|
-
expect(() => validateServiceIdentity(identity("my-service", ""))).toThrow(
|
|
62
|
-
"[otel-utils] VERSION must be a non-empty string",
|
|
63
|
-
);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it("rejects a whitespace-only VERSION", () => {
|
|
67
|
-
expect(() =>
|
|
68
|
-
validateServiceIdentity(identity("my-service", " ")),
|
|
69
|
-
).toThrow("[otel-utils] VERSION must be a non-empty string");
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it("rejects a missing VERSION", () => {
|
|
73
|
-
expect(() =>
|
|
74
|
-
validateServiceIdentity(identity("my-service", undefined)),
|
|
75
|
-
).toThrow("[otel-utils] VERSION must be a non-empty string");
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
it("rejects a non-string VERSION", () => {
|
|
79
|
-
expect(() => validateServiceIdentity(identity("my-service", 1))).toThrow(
|
|
80
|
-
"[otel-utils] VERSION must be a non-empty string",
|
|
81
|
-
);
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
describe("normalizeExportIntervalSeconds", () => {
|
|
86
|
-
it("returns the default for missing or non-finite values", () => {
|
|
87
|
-
expect(normalizeExportIntervalSeconds(undefined, 60)).toBe(60);
|
|
88
|
-
expect(normalizeExportIntervalSeconds(NaN, 60)).toBe(60);
|
|
89
|
-
expect(normalizeExportIntervalSeconds(Infinity, 60)).toBe(60);
|
|
90
|
-
expect(normalizeExportIntervalSeconds(-Infinity, 60)).toBe(60);
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
it("clamps finite values below one second", () => {
|
|
94
|
-
expect(normalizeExportIntervalSeconds(0, 60)).toBe(1);
|
|
95
|
-
expect(normalizeExportIntervalSeconds(-1, 60)).toBe(1);
|
|
96
|
-
expect(normalizeExportIntervalSeconds(0.5, 60)).toBe(1);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
it("keeps values of at least one second", () => {
|
|
100
|
-
expect(normalizeExportIntervalSeconds(1, 60)).toBe(1);
|
|
101
|
-
expect(normalizeExportIntervalSeconds(5, 60)).toBe(5);
|
|
102
|
-
});
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
describe("normalizePositiveInteger", () => {
|
|
106
|
-
it("returns the default for missing or invalid values", () => {
|
|
107
|
-
expect(normalizePositiveInteger(undefined, 2048)).toBe(2048);
|
|
108
|
-
expect(normalizePositiveInteger(NaN, 2048)).toBe(2048);
|
|
109
|
-
expect(normalizePositiveInteger(Infinity, 2048)).toBe(2048);
|
|
110
|
-
expect(normalizePositiveInteger(0, 2048)).toBe(2048);
|
|
111
|
-
expect(normalizePositiveInteger(-1, 2048)).toBe(2048);
|
|
112
|
-
expect(normalizePositiveInteger(1.5, 2048)).toBe(2048);
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
it("keeps positive integers", () => {
|
|
116
|
-
expect(normalizePositiveInteger(1024, 2048)).toBe(1024);
|
|
117
|
-
});
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
describe("normalizeConsoleLogMode", () => {
|
|
121
|
-
it("passes the three valid modes through", () => {
|
|
122
|
-
expect(normalizeConsoleLogMode("always")).toBe("always");
|
|
123
|
-
expect(normalizeConsoleLogMode("fallback")).toBe("fallback");
|
|
124
|
-
expect(normalizeConsoleLogMode("never")).toBe("never");
|
|
125
|
-
expect(warnSpy).not.toHaveBeenCalled();
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it("defaults to always when undefined without warning", () => {
|
|
129
|
-
expect(normalizeConsoleLogMode(undefined)).toBe("always");
|
|
130
|
-
expect(warnSpy).not.toHaveBeenCalled();
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
it("warns once and defaults to always for invalid values", () => {
|
|
134
|
-
expect(normalizeConsoleLogMode("sometimes")).toBe("always");
|
|
135
|
-
expect(warnSpy).toHaveBeenCalledTimes(1);
|
|
136
|
-
expect(warnSpy).toHaveBeenCalledWith(
|
|
137
|
-
expect.stringContaining("OPENTELEMETRY_CONSOLE_LOG_MODE"),
|
|
138
|
-
);
|
|
139
|
-
});
|
|
140
|
-
});
|
|
141
|
-
});
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ConfigOTelInterface,
|
|
3
|
-
ConsoleLogMode,
|
|
4
|
-
} from "../models/ConfigOTelInterface";
|
|
5
|
-
|
|
6
|
-
export function validateServiceIdentity(
|
|
7
|
-
config: Pick<ConfigOTelInterface, "SERVICE_ID" | "VERSION">,
|
|
8
|
-
): void {
|
|
9
|
-
if (
|
|
10
|
-
typeof config.SERVICE_ID !== "string" ||
|
|
11
|
-
config.SERVICE_ID.trim() === ""
|
|
12
|
-
) {
|
|
13
|
-
throw new Error("[otel-utils] SERVICE_ID must be a non-empty string");
|
|
14
|
-
}
|
|
15
|
-
if (typeof config.VERSION !== "string" || config.VERSION.trim() === "") {
|
|
16
|
-
throw new Error("[otel-utils] VERSION must be a non-empty string");
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function normalizeExportIntervalSeconds(
|
|
21
|
-
value: number | undefined,
|
|
22
|
-
defaultSeconds: number,
|
|
23
|
-
): number {
|
|
24
|
-
if (value === undefined || !Number.isFinite(value)) {
|
|
25
|
-
return defaultSeconds;
|
|
26
|
-
}
|
|
27
|
-
return Math.max(1, value);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function normalizePositiveInteger(
|
|
31
|
-
value: number | undefined,
|
|
32
|
-
defaultValue: number,
|
|
33
|
-
): number {
|
|
34
|
-
if (value === undefined || !Number.isInteger(value) || value <= 0) {
|
|
35
|
-
return defaultValue;
|
|
36
|
-
}
|
|
37
|
-
return value;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function normalizeConsoleLogMode(value: unknown): ConsoleLogMode {
|
|
41
|
-
if (value === "always" || value === "fallback" || value === "never") {
|
|
42
|
-
return value;
|
|
43
|
-
}
|
|
44
|
-
if (value !== undefined) {
|
|
45
|
-
console.warn(
|
|
46
|
-
`[otel-utils] Invalid OPENTELEMETRY_CONSOLE_LOG_MODE value "${String(value)}"; expected "always", "fallback" or "never". Defaulting to "always".`,
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
return "always";
|
|
50
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { resourceFromAttributes } from "@opentelemetry/resources";
|
|
2
|
-
import {
|
|
3
|
-
ATTR_NETWORK_LOCAL_ADDRESS,
|
|
4
|
-
ATTR_SERVICE_NAME,
|
|
5
|
-
ATTR_SERVICE_VERSION,
|
|
6
|
-
} from "@opentelemetry/semantic-conventions";
|
|
7
|
-
import * as os from "os";
|
|
8
|
-
|
|
9
|
-
const CACHED_HOSTNAME = os.hostname();
|
|
10
|
-
|
|
11
|
-
export function createOTelResource(
|
|
12
|
-
serviceName: string,
|
|
13
|
-
serviceVersion: string,
|
|
14
|
-
) {
|
|
15
|
-
return resourceFromAttributes({
|
|
16
|
-
[ATTR_SERVICE_NAME]: serviceName,
|
|
17
|
-
[ATTR_SERVICE_VERSION]: serviceVersion,
|
|
18
|
-
[ATTR_NETWORK_LOCAL_ADDRESS]: CACHED_HOSTNAME,
|
|
19
|
-
});
|
|
20
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2019",
|
|
4
|
-
"module": "commonjs",
|
|
5
|
-
"declaration": true,
|
|
6
|
-
"outDir": "./dist",
|
|
7
|
-
"strict": true,
|
|
8
|
-
"esModuleInterop": true,
|
|
9
|
-
"skipLibCheck": true,
|
|
10
|
-
"forceConsistentCasingInFileNames": true
|
|
11
|
-
},
|
|
12
|
-
"include": ["index.ts", "src/**/*"],
|
|
13
|
-
"exclude": ["src/**/*.spec.ts"]
|
|
14
|
-
}
|