@devopsplaybook.io/otel-utils 1.0.1-beta4 → 1.0.1-beta6
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/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/{ConfigInterfaceOTel.d.ts → ConfigOTelInterface.d.ts} +2 -2
- package/index.ts +3 -5
- package/package.json +1 -1
- 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/dist/src/Logger.d.ts +0 -12
- package/dist/src/Logger.js +0 -131
- package/dist/src/PromisePool.d.ts +0 -9
- package/dist/src/PromisePool.js +0 -54
- package/dist/src/SqlDbUtils.d.ts +0 -7
- package/dist/src/SqlDbUtils.js +0 -132
- package/dist/src/SqlDbUtilsNoTelemetry.d.ts +0 -2
- package/dist/src/SqlDbUtilsNoTelemetry.js +0 -38
- package/dist/src/SystemCommand.d.ts +0 -1
- package/dist/src/SystemCommand.js +0 -50
- package/dist/src/Timeout.d.ts +0 -1
- package/dist/src/Timeout.js +0 -10
- package/dist/src/models/ConfigInterface.d.ts +0 -11
- package/dist/src/models/ConfigInterfaceOTel.js +0 -2
- package/src/Logger.ts +0 -120
- /package/dist/src/models/{ConfigInterface.js → ConfigOTelInterface.js} +0 -0
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;
|
|
@@ -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/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
|
@@ -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
|
+
}
|