@devopsplaybook.io/otel-utils 1.0.1-beta3 → 1.0.1-beta5
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/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -4
- package/dist/src/ModuleLogger.d.ts +10 -0
- package/dist/src/ModuleLogger.js +50 -0
- package/dist/src/StandardLogger.d.ts +9 -0
- package/dist/src/StandardLogger.js +77 -0
- package/dist/src/StandardMeter.d.ts +11 -6
- package/dist/src/StandardMeter.js +57 -64
- package/dist/src/StandardTracer.d.ts +9 -5
- package/dist/src/StandardTracer.js +49 -58
- package/dist/src/models/ConfigOTelInterface.d.ts +11 -0
- package/dist/src/models/ConfigOTelInterface.js +2 -0
- package/eslint.config.mjs +10 -0
- package/index.ts +3 -5
- package/package.json +6 -2
- package/prettierrc.json +5 -0
- package/src/ModuleLogger.ts +54 -0
- package/src/StandardLogger.ts +65 -0
- package/src/StandardMeter.ts +68 -70
- package/src/StandardTracer.ts +64 -72
- package/src/models/{ConfigInterfaceOTel.ts → ConfigOTelInterface.ts} +2 -2
- package/src/Logger.ts +0 -120
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 devopsplaybook.io
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# otel-utils
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export * from "./src/
|
|
1
|
+
export * from "./src/models/ConfigOTelInterface";
|
|
2
|
+
export * from "./src/ModuleLogger";
|
|
3
|
+
export * from "./src/StandardLogger";
|
|
2
4
|
export * from "./src/StandardMeter";
|
|
3
5
|
export * from "./src/StandardTracer";
|
|
4
|
-
export * from "./src/models/ConfigInterfaceOTel";
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
// Entry point for the otel-utils npm package
|
|
3
|
-
// Export all modules from src
|
|
4
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
5
3
|
if (k2 === undefined) k2 = k;
|
|
6
4
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
@@ -16,7 +14,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
17
15
|
};
|
|
18
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
-
__exportStar(require("./src/
|
|
17
|
+
__exportStar(require("./src/models/ConfigOTelInterface"), exports);
|
|
18
|
+
__exportStar(require("./src/ModuleLogger"), exports);
|
|
19
|
+
__exportStar(require("./src/StandardLogger"), exports);
|
|
20
20
|
__exportStar(require("./src/StandardMeter"), exports);
|
|
21
21
|
__exportStar(require("./src/StandardTracer"), exports);
|
|
22
|
-
__exportStar(require("./src/models/ConfigInterfaceOTel"), exports);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Logger } from "@opentelemetry/api-logs";
|
|
2
|
+
export declare class ModuleLogger {
|
|
3
|
+
private module;
|
|
4
|
+
private logger?;
|
|
5
|
+
constructor(module: string, logger?: Logger);
|
|
6
|
+
info(message: Error | string | any): void;
|
|
7
|
+
warn(message: Error | string | any): void;
|
|
8
|
+
error(message: Error | string | any): void;
|
|
9
|
+
private display;
|
|
10
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ModuleLogger = void 0;
|
|
4
|
+
const api_logs_1 = require("@opentelemetry/api-logs");
|
|
5
|
+
class ModuleLogger {
|
|
6
|
+
constructor(module, logger) {
|
|
7
|
+
this.module = module;
|
|
8
|
+
this.logger = logger;
|
|
9
|
+
}
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
11
|
+
info(message) {
|
|
12
|
+
this.display("info", message, api_logs_1.SeverityNumber.WARN);
|
|
13
|
+
}
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
15
|
+
warn(message) {
|
|
16
|
+
this.display("warn", message, api_logs_1.SeverityNumber.WARN);
|
|
17
|
+
}
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
19
|
+
error(message) {
|
|
20
|
+
this.display("error", message, api_logs_1.SeverityNumber.ERROR);
|
|
21
|
+
}
|
|
22
|
+
display(level,
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
24
|
+
message, severityNumber = api_logs_1.SeverityNumber.INFO) {
|
|
25
|
+
if (typeof message === "string") {
|
|
26
|
+
// eslint:disable-next-line:no-console
|
|
27
|
+
console.log(`[${level}] [${this.module}] ${message}`);
|
|
28
|
+
}
|
|
29
|
+
else if (message instanceof Error) {
|
|
30
|
+
// eslint:disable-next-line:no-console
|
|
31
|
+
console.log(`${level} [${this.module}] ${message}`);
|
|
32
|
+
// eslint:disable-next-line:no-console
|
|
33
|
+
console.log(message.stack);
|
|
34
|
+
}
|
|
35
|
+
else if (typeof message === "object") {
|
|
36
|
+
// eslint:disable-next-line:no-console
|
|
37
|
+
console.log(`${level} [${this.module}] ${JSON.stringify(message)}`);
|
|
38
|
+
}
|
|
39
|
+
if (!this.logger) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
this.logger.emit({
|
|
43
|
+
severityNumber,
|
|
44
|
+
severityText: level,
|
|
45
|
+
body: message,
|
|
46
|
+
attributes: { "log.type": "custom" },
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.ModuleLogger = ModuleLogger;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ConfigOTelInterface } from "./models/ConfigOTelInterface";
|
|
2
|
+
import { ModuleLogger } from "./ModuleLogger";
|
|
3
|
+
export declare class StandardLogger {
|
|
4
|
+
private logger?;
|
|
5
|
+
private serviceVersion;
|
|
6
|
+
private serviceName;
|
|
7
|
+
constructor(config: ConfigOTelInterface);
|
|
8
|
+
createModuleLogger(moduleName: string): ModuleLogger;
|
|
9
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.StandardLogger = void 0;
|
|
37
|
+
const exporter_logs_otlp_http_1 = require("@opentelemetry/exporter-logs-otlp-http");
|
|
38
|
+
const resources_1 = require("@opentelemetry/resources");
|
|
39
|
+
const sdk_logs_1 = require("@opentelemetry/sdk-logs");
|
|
40
|
+
const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
|
|
41
|
+
const os = __importStar(require("os"));
|
|
42
|
+
const ModuleLogger_1 = require("./ModuleLogger");
|
|
43
|
+
class StandardLogger {
|
|
44
|
+
constructor(config) {
|
|
45
|
+
this.serviceName = config.SERVICE_ID;
|
|
46
|
+
this.serviceVersion = config.VERSION;
|
|
47
|
+
if (config.OPENTELEMETRY_COLLECTOR_HTTP_LOGS) {
|
|
48
|
+
const exporterHeaders = {};
|
|
49
|
+
if (config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER) {
|
|
50
|
+
exporterHeaders["Authorization"] = `Bearer ${config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER}`;
|
|
51
|
+
}
|
|
52
|
+
const exporter = new exporter_logs_otlp_http_1.OTLPLogExporter({
|
|
53
|
+
url: config.OPENTELEMETRY_COLLECTOR_HTTP_LOGS,
|
|
54
|
+
headers: exporterHeaders,
|
|
55
|
+
});
|
|
56
|
+
const loggerProvider = new sdk_logs_1.LoggerProvider({
|
|
57
|
+
processors: [
|
|
58
|
+
new sdk_logs_1.BatchLogRecordProcessor(exporter, {
|
|
59
|
+
maxQueueSize: 100,
|
|
60
|
+
scheduledDelayMillis: config.OPENTELEMETRY_COLLECTOR_EXPORT_LOGS_INTERVAL_SECONDS *
|
|
61
|
+
1000,
|
|
62
|
+
}),
|
|
63
|
+
],
|
|
64
|
+
resource: (0, resources_1.resourceFromAttributes)({
|
|
65
|
+
[semantic_conventions_1.ATTR_SERVICE_NAME]: `${this.serviceName}`,
|
|
66
|
+
[semantic_conventions_1.ATTR_SERVICE_VERSION]: `${this.serviceVersion}`,
|
|
67
|
+
[semantic_conventions_1.ATTR_NETWORK_LOCAL_ADDRESS]: os.hostname(),
|
|
68
|
+
}),
|
|
69
|
+
});
|
|
70
|
+
this.logger = loggerProvider.getLogger(`${this.serviceName}:${this.serviceVersion}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
createModuleLogger(moduleName) {
|
|
74
|
+
return new ModuleLogger_1.ModuleLogger(moduleName, this.logger);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
exports.StandardLogger = StandardLogger;
|
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import { Counter, Histogram, ObservableGauge } from "@opentelemetry/api";
|
|
2
|
-
import {
|
|
3
|
-
export declare
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
import { ConfigOTelInterface } from "./models/ConfigOTelInterface";
|
|
3
|
+
export declare class StandardMeter {
|
|
4
|
+
private meter;
|
|
5
|
+
private serviceVersion;
|
|
6
|
+
private serviceName;
|
|
7
|
+
constructor(config: ConfigOTelInterface);
|
|
8
|
+
createCounter(key: string): Counter;
|
|
9
|
+
createUpDownCounter(key: string): Counter;
|
|
10
|
+
createHistogram(key: string): Histogram;
|
|
11
|
+
createObservableGauge(key: string, callback: (observableResult: any) => void, description?: any): ObservableGauge;
|
|
12
|
+
}
|
|
@@ -33,77 +33,70 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.
|
|
37
|
-
exports.StandardMeterCreateCounter = StandardMeterCreateCounter;
|
|
38
|
-
exports.StandardMeterCreateUpDownCounter = StandardMeterCreateUpDownCounter;
|
|
39
|
-
exports.StandardMeterCreateHistorgram = StandardMeterCreateHistorgram;
|
|
40
|
-
exports.StandardMeterCreateObservableGauge = StandardMeterCreateObservableGauge;
|
|
36
|
+
exports.StandardMeter = void 0;
|
|
41
37
|
const exporter_metrics_otlp_http_1 = require("@opentelemetry/exporter-metrics-otlp-http");
|
|
42
38
|
const resources_1 = require("@opentelemetry/resources");
|
|
43
39
|
const sdk_metrics_1 = require("@opentelemetry/sdk-metrics");
|
|
44
40
|
const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
|
|
45
41
|
const os = __importStar(require("os"));
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
42
|
+
class StandardMeter {
|
|
43
|
+
constructor(config) {
|
|
44
|
+
this.serviceName = config.SERVICE_ID;
|
|
45
|
+
this.serviceVersion = config.VERSION;
|
|
46
|
+
let meterProvider;
|
|
47
|
+
if (config.OPENTELEMETRY_COLLECTOR_HTTP_METRICS) {
|
|
48
|
+
const collectorOptions = {
|
|
49
|
+
url: config.OPENTELEMETRY_COLLECTOR_HTTP_METRICS,
|
|
50
|
+
headers: {},
|
|
51
|
+
concurrencyLimit: 1,
|
|
52
|
+
};
|
|
53
|
+
if (config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER) {
|
|
54
|
+
collectorOptions.headers["Authorization"] = `Bearer ${config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER}`;
|
|
55
|
+
}
|
|
56
|
+
const metricExporter = new exporter_metrics_otlp_http_1.OTLPMetricExporter(collectorOptions);
|
|
57
|
+
meterProvider = new sdk_metrics_1.MeterProvider({
|
|
58
|
+
resource: (0, resources_1.resourceFromAttributes)({
|
|
59
|
+
[semantic_conventions_1.ATTR_SERVICE_NAME]: `${this.serviceName}`,
|
|
60
|
+
[semantic_conventions_1.ATTR_SERVICE_VERSION]: `${this.serviceVersion}`,
|
|
61
|
+
[semantic_conventions_1.ATTR_NETWORK_LOCAL_ADDRESS]: os.hostname(),
|
|
62
|
+
}),
|
|
63
|
+
readers: [
|
|
64
|
+
new sdk_metrics_1.PeriodicExportingMetricReader({
|
|
65
|
+
exporter: metricExporter,
|
|
66
|
+
exportIntervalMillis: config.OPENTELEMETRY_COLLECTOR_EXPORT_METRICS_INTERVAL_SECONDS *
|
|
67
|
+
1000,
|
|
68
|
+
}),
|
|
69
|
+
],
|
|
70
|
+
});
|
|
61
71
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}),
|
|
69
|
-
readers: [
|
|
70
|
-
new sdk_metrics_1.PeriodicExportingMetricReader({
|
|
71
|
-
exporter: metricExporter,
|
|
72
|
-
exportIntervalMillis: config.OPENTELEMETRY_COLLECTOR_EXPORT_METRICS_INTERVAL_SECONDS *
|
|
73
|
-
1000,
|
|
72
|
+
else {
|
|
73
|
+
meterProvider = new sdk_metrics_1.MeterProvider({
|
|
74
|
+
resource: (0, resources_1.resourceFromAttributes)({
|
|
75
|
+
[semantic_conventions_1.ATTR_SERVICE_NAME]: `${this.serviceName}`,
|
|
76
|
+
[semantic_conventions_1.ATTR_SERVICE_VERSION]: `${this.serviceVersion}`,
|
|
77
|
+
[semantic_conventions_1.ATTR_NETWORK_LOCAL_ADDRESS]: os.hostname(),
|
|
74
78
|
}),
|
|
75
|
-
|
|
76
|
-
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
this.meter = meterProvider.getMeter(`${this.serviceName}:${this.serviceVersion}`);
|
|
77
82
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
});
|
|
83
|
+
createCounter(key) {
|
|
84
|
+
return this.meter.createCounter(`${this.serviceName}.${key}`);
|
|
85
|
+
}
|
|
86
|
+
createUpDownCounter(key) {
|
|
87
|
+
return this.meter.createUpDownCounter(`${this.serviceName}.${key}`);
|
|
88
|
+
}
|
|
89
|
+
createHistogram(key) {
|
|
90
|
+
return this.meter.createHistogram(`${this.serviceName}.${key}`);
|
|
91
|
+
}
|
|
92
|
+
createObservableGauge(key,
|
|
93
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
94
|
+
callback,
|
|
95
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
96
|
+
description = null) {
|
|
97
|
+
const observableGauge = this.meter.createObservableGauge(key, description);
|
|
98
|
+
observableGauge.addCallback(callback);
|
|
99
|
+
return observableGauge;
|
|
86
100
|
}
|
|
87
101
|
}
|
|
88
|
-
|
|
89
|
-
const meter = meterProvider.getMeter(METER_NAME);
|
|
90
|
-
return meter.createCounter(`${config.SERVICE_ID}.${key}`);
|
|
91
|
-
}
|
|
92
|
-
function StandardMeterCreateUpDownCounter(key) {
|
|
93
|
-
const meter = meterProvider.getMeter(METER_NAME);
|
|
94
|
-
return meter.createUpDownCounter(`${config.SERVICE_ID}.${key}`);
|
|
95
|
-
}
|
|
96
|
-
function StandardMeterCreateHistorgram(key) {
|
|
97
|
-
const meter = meterProvider.getMeter(METER_NAME);
|
|
98
|
-
return meter.createHistogram(`${config.SERVICE_ID}.${key}`);
|
|
99
|
-
}
|
|
100
|
-
function StandardMeterCreateObservableGauge(key,
|
|
101
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
102
|
-
callback,
|
|
103
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
104
|
-
description = null) {
|
|
105
|
-
const meter = meterProvider.getMeter(METER_NAME);
|
|
106
|
-
const observableGauge = meter.createObservableGauge(key, description);
|
|
107
|
-
observableGauge.addCallback(callback);
|
|
108
|
-
return observableGauge;
|
|
109
|
-
}
|
|
102
|
+
exports.StandardMeter = StandardMeter;
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { Span } from "@opentelemetry/sdk-trace-base";
|
|
2
|
-
import {
|
|
3
|
-
export declare
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
import { ConfigOTelInterface } from "./models/ConfigOTelInterface";
|
|
3
|
+
export declare class StandardTracer {
|
|
4
|
+
private tracer;
|
|
5
|
+
private serviceVersion;
|
|
6
|
+
private serviceName;
|
|
7
|
+
constructor(config: ConfigOTelInterface);
|
|
8
|
+
startSpan(name: string, parentSpan?: Span): Span;
|
|
9
|
+
static updateHttpHeader(context: Span, headers?: {}): any;
|
|
10
|
+
}
|
|
@@ -33,10 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.
|
|
37
|
-
exports.StandardTracerStartSpan = StandardTracerStartSpan;
|
|
38
|
-
exports.StandardTracerGetTracer = StandardTracerGetTracer;
|
|
39
|
-
exports.StandardTracerAppendHeader = StandardTracerAppendHeader;
|
|
36
|
+
exports.StandardTracer = void 0;
|
|
40
37
|
const api_1 = __importStar(require("@opentelemetry/api"));
|
|
41
38
|
const context_async_hooks_1 = require("@opentelemetry/context-async-hooks");
|
|
42
39
|
const core_1 = require("@opentelemetry/core");
|
|
@@ -47,63 +44,57 @@ const sdk_trace_base_1 = require("@opentelemetry/sdk-trace-base");
|
|
|
47
44
|
const sdk_trace_node_1 = require("@opentelemetry/sdk-trace-node");
|
|
48
45
|
const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
|
|
49
46
|
const os = __importStar(require("os"));
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
47
|
+
class StandardTracer {
|
|
48
|
+
constructor(config) {
|
|
49
|
+
this.serviceName = config.SERVICE_ID;
|
|
50
|
+
this.serviceVersion = config.VERSION;
|
|
51
|
+
const spanProcessors = [];
|
|
52
|
+
if (config.OPENTELEMETRY_COLLECTOR_HTTP_TRACES) {
|
|
53
|
+
const exporterHeaders = {};
|
|
54
|
+
if (config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER) {
|
|
55
|
+
exporterHeaders["Authorization"] = `Bearer ${config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER}`;
|
|
56
|
+
}
|
|
57
|
+
const exporter = new exporter_trace_otlp_http_1.OTLPTraceExporter({
|
|
58
|
+
url: config.OPENTELEMETRY_COLLECTOR_HTTP_TRACES,
|
|
59
|
+
headers: exporterHeaders,
|
|
60
|
+
});
|
|
61
|
+
spanProcessors.push(new sdk_trace_base_1.BatchSpanProcessor(exporter));
|
|
61
62
|
}
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
63
|
+
const traceProvider = new sdk_trace_node_1.NodeTracerProvider({
|
|
64
|
+
idGenerator: new id_generator_aws_xray_1.AWSXRayIdGenerator(),
|
|
65
|
+
resource: (0, resources_1.resourceFromAttributes)({
|
|
66
|
+
[semantic_conventions_1.ATTR_SERVICE_NAME]: `${this.serviceName}`,
|
|
67
|
+
[semantic_conventions_1.ATTR_SERVICE_VERSION]: `${this.serviceVersion}`,
|
|
68
|
+
[semantic_conventions_1.ATTR_NETWORK_LOCAL_ADDRESS]: os.hostname(),
|
|
69
|
+
}),
|
|
70
|
+
spanProcessors,
|
|
65
71
|
});
|
|
66
|
-
|
|
72
|
+
traceProvider.register();
|
|
73
|
+
const contextManager = new context_async_hooks_1.AsyncHooksContextManager();
|
|
74
|
+
contextManager.enable();
|
|
75
|
+
api_1.default.context.setGlobalContextManager(contextManager);
|
|
76
|
+
this.tracer = api_1.default.trace.getTracer(`${this.serviceName}:${this.serviceVersion}`);
|
|
67
77
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
traceProvider.register();
|
|
78
|
-
const contextManager = new context_async_hooks_1.AsyncHooksContextManager();
|
|
79
|
-
contextManager.enable();
|
|
80
|
-
api_1.default.context.setGlobalContextManager(contextManager);
|
|
81
|
-
}
|
|
82
|
-
function StandardTracerStartSpan(name, parentSpan) {
|
|
83
|
-
const sanitizedName = String(name).replace(/[^a-zA-Z0-9-_/]/g, "_");
|
|
84
|
-
const tracer = StandardTracerGetTracer();
|
|
85
|
-
if (parentSpan) {
|
|
86
|
-
return tracer.startSpan(sanitizedName, undefined, api_1.default.trace.setSpan(api_1.default.context.active(), parentSpan));
|
|
87
|
-
}
|
|
88
|
-
const span = tracer.startSpan(sanitizedName);
|
|
89
|
-
span.setAttribute(semantic_conventions_1.ATTR_HTTP_REQUEST_METHOD, `BACKEND`);
|
|
90
|
-
span.setAttribute(semantic_conventions_1.ATTR_HTTP_ROUTE, `${config.SERVICE_ID}-${config.VERSION}-${sanitizedName}`);
|
|
91
|
-
return span;
|
|
92
|
-
}
|
|
93
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
94
|
-
function StandardTracerGetTracer() {
|
|
95
|
-
if (!tracerInstance) {
|
|
96
|
-
tracerInstance = api_1.default.trace.getTracer(`${config.SERVICE_ID}:${config.VERSION}`);
|
|
97
|
-
}
|
|
98
|
-
return tracerInstance;
|
|
99
|
-
}
|
|
100
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
101
|
-
function StandardTracerAppendHeader(context, headers = {}) {
|
|
102
|
-
if (!headers) {
|
|
103
|
-
headers = {};
|
|
78
|
+
startSpan(name, parentSpan) {
|
|
79
|
+
const sanitizedName = String(name).replace(/[^a-zA-Z0-9-_/]/g, "_");
|
|
80
|
+
if (parentSpan) {
|
|
81
|
+
return this.tracer.startSpan(sanitizedName, undefined, api_1.default.trace.setSpan(api_1.default.context.active(), parentSpan));
|
|
82
|
+
}
|
|
83
|
+
const span = this.tracer.startSpan(sanitizedName);
|
|
84
|
+
span.setAttribute(semantic_conventions_1.ATTR_HTTP_REQUEST_METHOD, `BACKEND`);
|
|
85
|
+
span.setAttribute(semantic_conventions_1.ATTR_HTTP_ROUTE, `${this.serviceName}-${this.serviceVersion}-${sanitizedName}`);
|
|
86
|
+
return span;
|
|
104
87
|
}
|
|
105
|
-
propagator.inject(api_1.trace.setSpanContext(api_1.ROOT_CONTEXT, context.spanContext()),
|
|
106
88
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
107
|
-
|
|
108
|
-
|
|
89
|
+
static updateHttpHeader(context, headers = {}) {
|
|
90
|
+
if (!headers) {
|
|
91
|
+
headers = {};
|
|
92
|
+
}
|
|
93
|
+
const propagator = new core_1.W3CTraceContextPropagator();
|
|
94
|
+
propagator.inject(api_1.trace.setSpanContext(api_1.ROOT_CONTEXT, context.spanContext()),
|
|
95
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
96
|
+
headers, api_1.defaultTextMapSetter);
|
|
97
|
+
return headers;
|
|
98
|
+
}
|
|
109
99
|
}
|
|
100
|
+
exports.StandardTracer = StandardTracer;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface ConfigOTelInterface {
|
|
2
|
+
SERVICE_ID: string;
|
|
3
|
+
VERSION: string;
|
|
4
|
+
OPENTELEMETRY_COLLECTOR_HTTP_TRACES: string;
|
|
5
|
+
OPENTELEMETRY_COLLECTOR_HTTP_METRICS: string;
|
|
6
|
+
OPENTELEMETRY_COLLECTOR_HTTP_LOGS: string;
|
|
7
|
+
OPENTELEMETRY_COLLECTOR_EXPORT_LOGS_INTERVAL_SECONDS: number;
|
|
8
|
+
OPENTELEMETRY_COLLECTOR_EXPORT_METRICS_INTERVAL_SECONDS: number;
|
|
9
|
+
OPENTELEMETRY_COLLECTOR_AWS: boolean;
|
|
10
|
+
OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER: string;
|
|
11
|
+
}
|
package/index.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export * from "./src/Logger";
|
|
1
|
+
export * from "./src/models/ConfigOTelInterface";
|
|
2
|
+
export * from "./src/ModuleLogger";
|
|
3
|
+
export * from "./src/StandardLogger";
|
|
5
4
|
export * from "./src/StandardMeter";
|
|
6
5
|
export * from "./src/StandardTracer";
|
|
7
|
-
export * from "./src/models/ConfigInterfaceOTel";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devopsplaybook.io/otel-utils",
|
|
3
|
-
"version": "1.0.1-
|
|
3
|
+
"version": "1.0.1-beta5",
|
|
4
4
|
"description": "Utility to simplify integration with Open Telemetry",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Open",
|
|
@@ -33,8 +33,12 @@
|
|
|
33
33
|
"@opentelemetry/semantic-conventions": "^1.36.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
+
"@eslint/js": "^9.34.0",
|
|
36
37
|
"@types/node": "^24.3.0",
|
|
37
|
-
"@types/sqlite3": "^5.1.0"
|
|
38
|
+
"@types/sqlite3": "^5.1.0",
|
|
39
|
+
"ts-node": "^10.9.2",
|
|
40
|
+
"typescript-eslint": "^8.40.0",
|
|
41
|
+
"typescript": "^5.9.2"
|
|
38
42
|
},
|
|
39
43
|
"publishConfig": {
|
|
40
44
|
"access": "public"
|
package/prettierrc.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { Logger } from "@opentelemetry/api-logs";
|
|
2
|
+
import { SeverityNumber } from "@opentelemetry/api-logs";
|
|
3
|
+
|
|
4
|
+
export class ModuleLogger {
|
|
5
|
+
private module: string;
|
|
6
|
+
private logger?: Logger;
|
|
7
|
+
|
|
8
|
+
constructor(module: string, logger?: Logger) {
|
|
9
|
+
this.module = module;
|
|
10
|
+
this.logger = logger;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
14
|
+
public info(message: Error | string | any): void {
|
|
15
|
+
this.display("info", message, SeverityNumber.WARN);
|
|
16
|
+
}
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
|
+
public warn(message: Error | string | any): void {
|
|
19
|
+
this.display("warn", message, SeverityNumber.WARN);
|
|
20
|
+
}
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
22
|
+
public error(message: Error | string | any): void {
|
|
23
|
+
this.display("error", message, SeverityNumber.ERROR);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
private display(
|
|
27
|
+
level: string,
|
|
28
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
29
|
+
message: any,
|
|
30
|
+
severityNumber = SeverityNumber.INFO
|
|
31
|
+
): void {
|
|
32
|
+
if (typeof message === "string") {
|
|
33
|
+
// eslint:disable-next-line:no-console
|
|
34
|
+
console.log(`[${level}] [${this.module}] ${message}`);
|
|
35
|
+
} else if (message instanceof Error) {
|
|
36
|
+
// eslint:disable-next-line:no-console
|
|
37
|
+
console.log(`${level} [${this.module}] ${message}`);
|
|
38
|
+
// eslint:disable-next-line:no-console
|
|
39
|
+
console.log((message as Error).stack);
|
|
40
|
+
} else if (typeof message === "object") {
|
|
41
|
+
// eslint:disable-next-line:no-console
|
|
42
|
+
console.log(`${level} [${this.module}] ${JSON.stringify(message)}`);
|
|
43
|
+
}
|
|
44
|
+
if (!this.logger) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
this.logger.emit({
|
|
48
|
+
severityNumber,
|
|
49
|
+
severityText: level,
|
|
50
|
+
body: message,
|
|
51
|
+
attributes: { "log.type": "custom" },
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { Logger, Logger as OTelLogger } from "@opentelemetry/api-logs";
|
|
2
|
+
import { SeverityNumber } from "@opentelemetry/api-logs";
|
|
3
|
+
import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-http";
|
|
4
|
+
import { resourceFromAttributes } from "@opentelemetry/resources";
|
|
5
|
+
import {
|
|
6
|
+
BatchLogRecordProcessor,
|
|
7
|
+
LoggerProvider,
|
|
8
|
+
} from "@opentelemetry/sdk-logs";
|
|
9
|
+
import { Span } from "@opentelemetry/sdk-trace-base";
|
|
10
|
+
import {
|
|
11
|
+
ATTR_NETWORK_LOCAL_ADDRESS,
|
|
12
|
+
ATTR_SERVICE_NAME,
|
|
13
|
+
ATTR_SERVICE_VERSION,
|
|
14
|
+
} from "@opentelemetry/semantic-conventions";
|
|
15
|
+
import * as os from "os";
|
|
16
|
+
import { ConfigOTelInterface } from "./models/ConfigOTelInterface";
|
|
17
|
+
import { ModuleLogger } from "./ModuleLogger";
|
|
18
|
+
|
|
19
|
+
export class StandardLogger {
|
|
20
|
+
private logger?: Logger;
|
|
21
|
+
private serviceVersion: string;
|
|
22
|
+
private serviceName: string;
|
|
23
|
+
|
|
24
|
+
constructor(config: ConfigOTelInterface) {
|
|
25
|
+
this.serviceName = config.SERVICE_ID;
|
|
26
|
+
this.serviceVersion = config.VERSION;
|
|
27
|
+
|
|
28
|
+
if (config.OPENTELEMETRY_COLLECTOR_HTTP_LOGS) {
|
|
29
|
+
const exporterHeaders: Record<string, string> = {};
|
|
30
|
+
if (config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER) {
|
|
31
|
+
exporterHeaders[
|
|
32
|
+
"Authorization"
|
|
33
|
+
] = `Bearer ${config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER}`;
|
|
34
|
+
}
|
|
35
|
+
const exporter = new OTLPLogExporter({
|
|
36
|
+
url: config.OPENTELEMETRY_COLLECTOR_HTTP_LOGS,
|
|
37
|
+
headers: exporterHeaders,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const loggerProvider = new LoggerProvider({
|
|
41
|
+
processors: [
|
|
42
|
+
new BatchLogRecordProcessor(exporter, {
|
|
43
|
+
maxQueueSize: 100,
|
|
44
|
+
scheduledDelayMillis:
|
|
45
|
+
config.OPENTELEMETRY_COLLECTOR_EXPORT_LOGS_INTERVAL_SECONDS *
|
|
46
|
+
1000,
|
|
47
|
+
}),
|
|
48
|
+
],
|
|
49
|
+
resource: resourceFromAttributes({
|
|
50
|
+
[ATTR_SERVICE_NAME]: `${this.serviceName}`,
|
|
51
|
+
[ATTR_SERVICE_VERSION]: `${this.serviceVersion}`,
|
|
52
|
+
[ATTR_NETWORK_LOCAL_ADDRESS]: os.hostname(),
|
|
53
|
+
}),
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
this.logger = loggerProvider.getLogger(
|
|
57
|
+
`${this.serviceName}:${this.serviceVersion}`
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public createModuleLogger(moduleName: string): ModuleLogger {
|
|
63
|
+
return new ModuleLogger(moduleName, this.logger);
|
|
64
|
+
}
|
|
65
|
+
}
|
package/src/StandardMeter.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Counter, Histogram, ObservableGauge } from "@opentelemetry/api";
|
|
1
|
+
import { Counter, Histogram, Meter, ObservableGauge } from "@opentelemetry/api";
|
|
2
2
|
import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-http";
|
|
3
3
|
import { resourceFromAttributes } from "@opentelemetry/resources";
|
|
4
4
|
import {
|
|
@@ -11,81 +11,79 @@ import {
|
|
|
11
11
|
ATTR_SERVICE_VERSION,
|
|
12
12
|
} from "@opentelemetry/semantic-conventions";
|
|
13
13
|
import * as os from "os";
|
|
14
|
-
import {
|
|
14
|
+
import { ConfigOTelInterface } from "./models/ConfigOTelInterface";
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
export class StandardMeter {
|
|
17
|
+
private meter: Meter;
|
|
18
|
+
private serviceVersion: string;
|
|
19
|
+
private serviceName: string;
|
|
19
20
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
[ATTR_NETWORK_LOCAL_ADDRESS]: os.hostname(),
|
|
42
|
-
}),
|
|
43
|
-
readers: [
|
|
44
|
-
new PeriodicExportingMetricReader({
|
|
45
|
-
exporter: metricExporter,
|
|
46
|
-
exportIntervalMillis:
|
|
47
|
-
config.OPENTELEMETRY_COLLECTOR_EXPORT_METRICS_INTERVAL_SECONDS *
|
|
48
|
-
1000,
|
|
21
|
+
constructor(config: ConfigOTelInterface) {
|
|
22
|
+
this.serviceName = config.SERVICE_ID;
|
|
23
|
+
this.serviceVersion = config.VERSION;
|
|
24
|
+
let meterProvider;
|
|
25
|
+
if (config.OPENTELEMETRY_COLLECTOR_HTTP_METRICS) {
|
|
26
|
+
const collectorOptions = {
|
|
27
|
+
url: config.OPENTELEMETRY_COLLECTOR_HTTP_METRICS,
|
|
28
|
+
headers: {} as Record<string, string>,
|
|
29
|
+
concurrencyLimit: 1,
|
|
30
|
+
};
|
|
31
|
+
if (config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER) {
|
|
32
|
+
collectorOptions.headers[
|
|
33
|
+
"Authorization"
|
|
34
|
+
] = `Bearer ${config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER}`;
|
|
35
|
+
}
|
|
36
|
+
const metricExporter = new OTLPMetricExporter(collectorOptions);
|
|
37
|
+
meterProvider = new MeterProvider({
|
|
38
|
+
resource: resourceFromAttributes({
|
|
39
|
+
[ATTR_SERVICE_NAME]: `${this.serviceName}`,
|
|
40
|
+
[ATTR_SERVICE_VERSION]: `${this.serviceVersion}`,
|
|
41
|
+
[ATTR_NETWORK_LOCAL_ADDRESS]: os.hostname(),
|
|
49
42
|
}),
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
})
|
|
59
|
-
}
|
|
43
|
+
readers: [
|
|
44
|
+
new PeriodicExportingMetricReader({
|
|
45
|
+
exporter: metricExporter,
|
|
46
|
+
exportIntervalMillis:
|
|
47
|
+
config.OPENTELEMETRY_COLLECTOR_EXPORT_METRICS_INTERVAL_SECONDS *
|
|
48
|
+
1000,
|
|
49
|
+
}),
|
|
50
|
+
],
|
|
51
|
+
});
|
|
52
|
+
} else {
|
|
53
|
+
meterProvider = new MeterProvider({
|
|
54
|
+
resource: resourceFromAttributes({
|
|
55
|
+
[ATTR_SERVICE_NAME]: `${this.serviceName}`,
|
|
56
|
+
[ATTR_SERVICE_VERSION]: `${this.serviceVersion}`,
|
|
57
|
+
[ATTR_NETWORK_LOCAL_ADDRESS]: os.hostname(),
|
|
58
|
+
}),
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
this.meter = meterProvider.getMeter(
|
|
62
|
+
`${this.serviceName}:${this.serviceVersion}`
|
|
63
|
+
);
|
|
60
64
|
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export function StandardMeterCreateCounter(key: string): Counter {
|
|
64
|
-
const meter = meterProvider.getMeter(METER_NAME);
|
|
65
|
-
return meter.createCounter(`${config.SERVICE_ID}.${key}`);
|
|
66
|
-
}
|
|
67
65
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export function StandardMeterCreateHistorgram(key: string): Histogram {
|
|
74
|
-
const meter = meterProvider.getMeter(METER_NAME);
|
|
75
|
-
return meter.createHistogram(`${config.SERVICE_ID}.${key}`);
|
|
76
|
-
}
|
|
66
|
+
public createCounter(key: string): Counter {
|
|
67
|
+
return this.meter.createCounter(`${this.serviceName}.${key}`);
|
|
68
|
+
}
|
|
77
69
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
callback: (observableResult: any) => void,
|
|
82
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
83
|
-
description: any = null
|
|
84
|
-
): ObservableGauge {
|
|
85
|
-
const meter = meterProvider.getMeter(METER_NAME);
|
|
86
|
-
const observableGauge = meter.createObservableGauge(key, description);
|
|
70
|
+
public createUpDownCounter(key: string): Counter {
|
|
71
|
+
return this.meter.createUpDownCounter(`${this.serviceName}.${key}`);
|
|
72
|
+
}
|
|
87
73
|
|
|
88
|
-
|
|
74
|
+
public createHistogram(key: string): Histogram {
|
|
75
|
+
return this.meter.createHistogram(`${this.serviceName}.${key}`);
|
|
76
|
+
}
|
|
89
77
|
|
|
90
|
-
|
|
78
|
+
public createObservableGauge(
|
|
79
|
+
key: string,
|
|
80
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
81
|
+
callback: (observableResult: any) => void,
|
|
82
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
83
|
+
description: any = null
|
|
84
|
+
): ObservableGauge {
|
|
85
|
+
const observableGauge = this.meter.createObservableGauge(key, description);
|
|
86
|
+
observableGauge.addCallback(callback);
|
|
87
|
+
return observableGauge;
|
|
88
|
+
}
|
|
91
89
|
}
|
package/src/StandardTracer.ts
CHANGED
|
@@ -19,87 +19,79 @@ import {
|
|
|
19
19
|
ATTR_SERVICE_VERSION,
|
|
20
20
|
} from "@opentelemetry/semantic-conventions";
|
|
21
21
|
import * as os from "os";
|
|
22
|
-
import {
|
|
22
|
+
import { ConfigOTelInterface } from "./models/ConfigOTelInterface";
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
export class StandardTracer {
|
|
25
|
+
private tracer: Tracer;
|
|
26
|
+
private serviceVersion: string;
|
|
27
|
+
private serviceName: string;
|
|
27
28
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
constructor(config: ConfigOTelInterface) {
|
|
30
|
+
this.serviceName = config.SERVICE_ID;
|
|
31
|
+
this.serviceVersion = config.VERSION;
|
|
32
|
+
const spanProcessors = [];
|
|
32
33
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
34
|
+
if (config.OPENTELEMETRY_COLLECTOR_HTTP_TRACES) {
|
|
35
|
+
const exporterHeaders: Record<string, string> = {};
|
|
36
|
+
if (config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER) {
|
|
37
|
+
exporterHeaders[
|
|
38
|
+
"Authorization"
|
|
39
|
+
] = `Bearer ${config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER}`;
|
|
40
|
+
}
|
|
41
|
+
const exporter = new OTLPTraceExporter({
|
|
42
|
+
url: config.OPENTELEMETRY_COLLECTOR_HTTP_TRACES,
|
|
43
|
+
headers: exporterHeaders,
|
|
44
|
+
});
|
|
45
|
+
spanProcessors.push(new BatchSpanProcessor(exporter));
|
|
39
46
|
}
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
47
|
+
const traceProvider = new NodeTracerProvider({
|
|
48
|
+
idGenerator: new AWSXRayIdGenerator(),
|
|
49
|
+
resource: resourceFromAttributes({
|
|
50
|
+
[ATTR_SERVICE_NAME]: `${this.serviceName}`,
|
|
51
|
+
[ATTR_SERVICE_VERSION]: `${this.serviceVersion}`,
|
|
52
|
+
[ATTR_NETWORK_LOCAL_ADDRESS]: os.hostname(),
|
|
53
|
+
}),
|
|
54
|
+
spanProcessors,
|
|
43
55
|
});
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
[ATTR_NETWORK_LOCAL_ADDRESS]: os.hostname(),
|
|
52
|
-
}),
|
|
53
|
-
spanProcessors,
|
|
54
|
-
});
|
|
55
|
-
traceProvider.register();
|
|
56
|
-
const contextManager = new AsyncHooksContextManager();
|
|
57
|
-
contextManager.enable();
|
|
58
|
-
opentelemetry.context.setGlobalContextManager(contextManager);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export function StandardTracerStartSpan(name: string, parentSpan?: Span): Span {
|
|
62
|
-
const sanitizedName = String(name).replace(/[^a-zA-Z0-9-_/]/g, "_");
|
|
63
|
-
const tracer = StandardTracerGetTracer();
|
|
64
|
-
|
|
65
|
-
if (parentSpan) {
|
|
66
|
-
return tracer.startSpan(
|
|
67
|
-
sanitizedName,
|
|
68
|
-
undefined,
|
|
69
|
-
opentelemetry.trace.setSpan(opentelemetry.context.active(), parentSpan)
|
|
70
|
-
) as Span;
|
|
56
|
+
traceProvider.register();
|
|
57
|
+
const contextManager = new AsyncHooksContextManager();
|
|
58
|
+
contextManager.enable();
|
|
59
|
+
opentelemetry.context.setGlobalContextManager(contextManager);
|
|
60
|
+
this.tracer = opentelemetry.trace.getTracer(
|
|
61
|
+
`${this.serviceName}:${this.serviceVersion}`
|
|
62
|
+
);
|
|
71
63
|
}
|
|
72
64
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
`${config.SERVICE_ID}:${config.VERSION}`
|
|
65
|
+
public startSpan(name: string, parentSpan?: Span): Span {
|
|
66
|
+
const sanitizedName = String(name).replace(/[^a-zA-Z0-9-_/]/g, "_");
|
|
67
|
+
if (parentSpan) {
|
|
68
|
+
return this.tracer.startSpan(
|
|
69
|
+
sanitizedName,
|
|
70
|
+
undefined,
|
|
71
|
+
opentelemetry.trace.setSpan(opentelemetry.context.active(), parentSpan)
|
|
72
|
+
) as Span;
|
|
73
|
+
}
|
|
74
|
+
const span = this.tracer.startSpan(sanitizedName) as Span;
|
|
75
|
+
span.setAttribute(ATTR_HTTP_REQUEST_METHOD, `BACKEND`);
|
|
76
|
+
span.setAttribute(
|
|
77
|
+
ATTR_HTTP_ROUTE,
|
|
78
|
+
`${this.serviceName}-${this.serviceVersion}-${sanitizedName}`
|
|
88
79
|
);
|
|
80
|
+
return span;
|
|
89
81
|
}
|
|
90
|
-
return tracerInstance;
|
|
91
|
-
}
|
|
92
82
|
|
|
93
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
83
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
84
|
+
public static updateHttpHeader(context: Span, headers = {}): any {
|
|
85
|
+
if (!headers) {
|
|
86
|
+
headers = {};
|
|
87
|
+
}
|
|
88
|
+
const propagator = new W3CTraceContextPropagator();
|
|
89
|
+
propagator.inject(
|
|
90
|
+
trace.setSpanContext(ROOT_CONTEXT, context.spanContext()),
|
|
91
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
92
|
+
headers as any,
|
|
93
|
+
defaultTextMapSetter
|
|
94
|
+
);
|
|
95
|
+
return headers;
|
|
97
96
|
}
|
|
98
|
-
propagator.inject(
|
|
99
|
-
trace.setSpanContext(ROOT_CONTEXT, context.spanContext()),
|
|
100
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
101
|
-
headers as any,
|
|
102
|
-
defaultTextMapSetter
|
|
103
|
-
);
|
|
104
|
-
return headers;
|
|
105
97
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export interface
|
|
1
|
+
export interface ConfigOTelInterface {
|
|
2
2
|
SERVICE_ID: string;
|
|
3
|
-
VERSION:
|
|
3
|
+
VERSION: string;
|
|
4
4
|
OPENTELEMETRY_COLLECTOR_HTTP_TRACES: string;
|
|
5
5
|
OPENTELEMETRY_COLLECTOR_HTTP_METRICS: string;
|
|
6
6
|
OPENTELEMETRY_COLLECTOR_HTTP_LOGS: string;
|
package/src/Logger.ts
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
import type { Logger as OTelLogger } from "@opentelemetry/api-logs";
|
|
2
|
-
import { SeverityNumber } from "@opentelemetry/api-logs";
|
|
3
|
-
import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-http";
|
|
4
|
-
import { resourceFromAttributes } from "@opentelemetry/resources";
|
|
5
|
-
import {
|
|
6
|
-
BatchLogRecordProcessor,
|
|
7
|
-
LoggerProvider,
|
|
8
|
-
} from "@opentelemetry/sdk-logs";
|
|
9
|
-
import { Span } from "@opentelemetry/sdk-trace-base";
|
|
10
|
-
import {
|
|
11
|
-
ATTR_NETWORK_LOCAL_ADDRESS,
|
|
12
|
-
ATTR_SERVICE_NAME,
|
|
13
|
-
ATTR_SERVICE_VERSION,
|
|
14
|
-
} from "@opentelemetry/semantic-conventions";
|
|
15
|
-
import * as os from "os";
|
|
16
|
-
import { ConfigInterfaceOTel } from "./models/ConfigInterfaceOTel";
|
|
17
|
-
import { StandardTracerStartSpan } from "./StandardTracer";
|
|
18
|
-
|
|
19
|
-
let loggerOTel: OTelLogger;
|
|
20
|
-
|
|
21
|
-
export function LoggerInit(context: Span, config: ConfigInterfaceOTel) {
|
|
22
|
-
const span = StandardTracerStartSpan("LoggerInit", context);
|
|
23
|
-
|
|
24
|
-
if (config.OPENTELEMETRY_COLLECTOR_HTTP_LOGS) {
|
|
25
|
-
const exporterHeaders: Record<string, string> = {};
|
|
26
|
-
if (config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER) {
|
|
27
|
-
exporterHeaders[
|
|
28
|
-
"Authorization"
|
|
29
|
-
] = `Bearer ${config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER}`;
|
|
30
|
-
}
|
|
31
|
-
const exporter = new OTLPLogExporter({
|
|
32
|
-
url: config.OPENTELEMETRY_COLLECTOR_HTTP_LOGS,
|
|
33
|
-
headers: exporterHeaders,
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
const loggerProvider = new LoggerProvider({
|
|
37
|
-
processors: [
|
|
38
|
-
new BatchLogRecordProcessor(exporter, {
|
|
39
|
-
maxQueueSize: 100,
|
|
40
|
-
scheduledDelayMillis:
|
|
41
|
-
config.OPENTELEMETRY_COLLECTOR_EXPORT_LOGS_INTERVAL_SECONDS * 1000,
|
|
42
|
-
}),
|
|
43
|
-
],
|
|
44
|
-
resource: resourceFromAttributes({
|
|
45
|
-
[ATTR_SERVICE_NAME]: `${config.SERVICE_ID}`,
|
|
46
|
-
[ATTR_SERVICE_VERSION]: `${config.VERSION}`,
|
|
47
|
-
[ATTR_NETWORK_LOCAL_ADDRESS]: os.hostname(),
|
|
48
|
-
}),
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
loggerOTel = loggerProvider.getLogger(
|
|
52
|
-
`${config.SERVICE_ID}:${config.VERSION}`
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
span.end();
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const DEV_MODE = (() => {
|
|
59
|
-
if (process.env.NODE_ENV === "dev") {
|
|
60
|
-
return true;
|
|
61
|
-
}
|
|
62
|
-
return false;
|
|
63
|
-
})();
|
|
64
|
-
|
|
65
|
-
export class Logger {
|
|
66
|
-
private module: string;
|
|
67
|
-
|
|
68
|
-
constructor(module: string) {
|
|
69
|
-
this.module = `${module}`;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
73
|
-
public debug(message: Error | string | any): void {
|
|
74
|
-
if (DEV_MODE) {
|
|
75
|
-
this.display("debug", message, SeverityNumber.DEBUG);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
79
|
-
public info(message: Error | string | any): void {
|
|
80
|
-
this.display("info", message, SeverityNumber.WARN);
|
|
81
|
-
}
|
|
82
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
83
|
-
public warn(message: Error | string | any): void {
|
|
84
|
-
this.display("warn", message, SeverityNumber.WARN);
|
|
85
|
-
}
|
|
86
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
87
|
-
public error(message: Error | string | any): void {
|
|
88
|
-
this.display("error", message, SeverityNumber.ERROR);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
private display(
|
|
92
|
-
level: string,
|
|
93
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
94
|
-
message: any,
|
|
95
|
-
severityNumber = SeverityNumber.INFO
|
|
96
|
-
): void {
|
|
97
|
-
if (typeof message === "string") {
|
|
98
|
-
// eslint:disable-next-line:no-console
|
|
99
|
-
console.log(`[${level}] [${this.module}] ${message}`);
|
|
100
|
-
} else if (message instanceof Error) {
|
|
101
|
-
// eslint:disable-next-line:no-console
|
|
102
|
-
console.log(`${level} [${this.module}] ${message}`);
|
|
103
|
-
// eslint:disable-next-line:no-console
|
|
104
|
-
console.log((message as Error).stack);
|
|
105
|
-
} else if (typeof message === "object") {
|
|
106
|
-
// eslint:disable-next-line:no-console
|
|
107
|
-
console.log(`${level} [${this.module}] ${JSON.stringify(message)}`);
|
|
108
|
-
}
|
|
109
|
-
if (loggerOTel) {
|
|
110
|
-
{
|
|
111
|
-
loggerOTel.emit({
|
|
112
|
-
severityNumber,
|
|
113
|
-
severityText: level,
|
|
114
|
-
body: message,
|
|
115
|
-
attributes: { "log.type": "custom" },
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
}
|