@devopsplaybook.io/otel-utils 1.0.0
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 +4 -0
- package/dist/index.js +45 -0
- package/dist/src/Logger.d.ts +12 -0
- package/dist/src/Logger.js +131 -0
- package/dist/src/PromisePool.d.ts +9 -0
- package/dist/src/PromisePool.js +54 -0
- package/dist/src/SqlDbUtils.d.ts +7 -0
- package/dist/src/SqlDbUtils.js +132 -0
- package/dist/src/SqlDbUtilsNoTelemetry.d.ts +2 -0
- package/dist/src/SqlDbUtilsNoTelemetry.js +38 -0
- package/dist/src/StandardMeter.d.ts +7 -0
- package/dist/src/StandardMeter.js +109 -0
- package/dist/src/StandardTracer.d.ts +7 -0
- package/dist/src/StandardTracer.js +115 -0
- package/dist/src/SystemCommand.d.ts +1 -0
- package/dist/src/SystemCommand.js +50 -0
- package/dist/src/Timeout.d.ts +1 -0
- package/dist/src/Timeout.js +10 -0
- package/dist/src/models/ConfigInterface.d.ts +11 -0
- package/dist/src/models/ConfigInterface.js +2 -0
- package/index.ts +7 -0
- package/package.json +42 -0
- package/src/Logger.ts +120 -0
- package/src/StandardMeter.ts +91 -0
- package/src/StandardTracer.ts +111 -0
- package/src/models/ConfigInterface.ts +11 -0
- package/tsconfig.json +13 -0
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Entry point for the otel-utils npm package
|
|
3
|
+
// Export all modules from src
|
|
4
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
5
|
+
if (k2 === undefined) k2 = k;
|
|
6
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
7
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
8
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
9
|
+
}
|
|
10
|
+
Object.defineProperty(o, k2, desc);
|
|
11
|
+
}) : (function(o, m, k, k2) {
|
|
12
|
+
if (k2 === undefined) k2 = k;
|
|
13
|
+
o[k2] = m[k];
|
|
14
|
+
}));
|
|
15
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
16
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
17
|
+
}) : function(o, v) {
|
|
18
|
+
o["default"] = v;
|
|
19
|
+
});
|
|
20
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
21
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
22
|
+
};
|
|
23
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
24
|
+
var ownKeys = function(o) {
|
|
25
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
26
|
+
var ar = [];
|
|
27
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
28
|
+
return ar;
|
|
29
|
+
};
|
|
30
|
+
return ownKeys(o);
|
|
31
|
+
};
|
|
32
|
+
return function (mod) {
|
|
33
|
+
if (mod && mod.__esModule) return mod;
|
|
34
|
+
var result = {};
|
|
35
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
36
|
+
__setModuleDefault(result, mod);
|
|
37
|
+
return result;
|
|
38
|
+
};
|
|
39
|
+
})();
|
|
40
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
+
exports.models = void 0;
|
|
42
|
+
__exportStar(require("./src/Logger"), exports);
|
|
43
|
+
__exportStar(require("./src/StandardMeter"), exports);
|
|
44
|
+
__exportStar(require("./src/StandardTracer"), exports);
|
|
45
|
+
exports.models = __importStar(require("./src/models/ConfigInterface"));
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Span } from "@opentelemetry/sdk-trace-base";
|
|
2
|
+
import { ConfigInterface } from "./models/ConfigInterface";
|
|
3
|
+
export declare function LoggerInit(context: Span, config: ConfigInterface): void;
|
|
4
|
+
export declare class Logger {
|
|
5
|
+
private module;
|
|
6
|
+
constructor(module: string);
|
|
7
|
+
debug(message: Error | string | any): void;
|
|
8
|
+
info(message: Error | string | any): void;
|
|
9
|
+
warn(message: Error | string | any): void;
|
|
10
|
+
error(message: Error | string | any): void;
|
|
11
|
+
private display;
|
|
12
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
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.Logger = void 0;
|
|
37
|
+
exports.LoggerInit = LoggerInit;
|
|
38
|
+
const api_logs_1 = require("@opentelemetry/api-logs");
|
|
39
|
+
const exporter_logs_otlp_http_1 = require("@opentelemetry/exporter-logs-otlp-http");
|
|
40
|
+
const resources_1 = require("@opentelemetry/resources");
|
|
41
|
+
const sdk_logs_1 = require("@opentelemetry/sdk-logs");
|
|
42
|
+
const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
|
|
43
|
+
const os = __importStar(require("os"));
|
|
44
|
+
const StandardTracer_1 = require("./StandardTracer");
|
|
45
|
+
let loggerOTel;
|
|
46
|
+
function LoggerInit(context, config) {
|
|
47
|
+
const span = (0, StandardTracer_1.StandardTracerStartSpan)("LoggerInit", context);
|
|
48
|
+
if (config.OPENTELEMETRY_COLLECTOR_HTTP_LOGS) {
|
|
49
|
+
const exporterHeaders = {};
|
|
50
|
+
if (config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER) {
|
|
51
|
+
exporterHeaders["Authorization"] = `Bearer ${config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER}`;
|
|
52
|
+
}
|
|
53
|
+
const exporter = new exporter_logs_otlp_http_1.OTLPLogExporter({
|
|
54
|
+
url: config.OPENTELEMETRY_COLLECTOR_HTTP_LOGS,
|
|
55
|
+
headers: exporterHeaders,
|
|
56
|
+
});
|
|
57
|
+
const loggerProvider = new sdk_logs_1.LoggerProvider({
|
|
58
|
+
processors: [
|
|
59
|
+
new sdk_logs_1.BatchLogRecordProcessor(exporter, {
|
|
60
|
+
maxQueueSize: 100,
|
|
61
|
+
scheduledDelayMillis: config.OPENTELEMETRY_COLLECTOR_EXPORT_LOGS_INTERVAL_SECONDS * 1000,
|
|
62
|
+
}),
|
|
63
|
+
],
|
|
64
|
+
resource: (0, resources_1.resourceFromAttributes)({
|
|
65
|
+
[semantic_conventions_1.ATTR_SERVICE_NAME]: `${config.SERVICE_ID}`,
|
|
66
|
+
[semantic_conventions_1.ATTR_SERVICE_VERSION]: `${config.VERSION}`,
|
|
67
|
+
[semantic_conventions_1.ATTR_NETWORK_LOCAL_ADDRESS]: os.hostname(),
|
|
68
|
+
}),
|
|
69
|
+
});
|
|
70
|
+
loggerOTel = loggerProvider.getLogger(`${config.SERVICE_ID}:${config.VERSION}`);
|
|
71
|
+
}
|
|
72
|
+
span.end();
|
|
73
|
+
}
|
|
74
|
+
const DEV_MODE = (() => {
|
|
75
|
+
if (process.env.NODE_ENV === "dev") {
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
return false;
|
|
79
|
+
})();
|
|
80
|
+
class Logger {
|
|
81
|
+
constructor(module) {
|
|
82
|
+
this.module = `${module}`;
|
|
83
|
+
}
|
|
84
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
85
|
+
debug(message) {
|
|
86
|
+
if (DEV_MODE) {
|
|
87
|
+
this.display("debug", message, api_logs_1.SeverityNumber.DEBUG);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
91
|
+
info(message) {
|
|
92
|
+
this.display("info", message, api_logs_1.SeverityNumber.WARN);
|
|
93
|
+
}
|
|
94
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
95
|
+
warn(message) {
|
|
96
|
+
this.display("warn", message, api_logs_1.SeverityNumber.WARN);
|
|
97
|
+
}
|
|
98
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
99
|
+
error(message) {
|
|
100
|
+
this.display("error", message, api_logs_1.SeverityNumber.ERROR);
|
|
101
|
+
}
|
|
102
|
+
display(level,
|
|
103
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
104
|
+
message, severityNumber = api_logs_1.SeverityNumber.INFO) {
|
|
105
|
+
if (typeof message === "string") {
|
|
106
|
+
// eslint:disable-next-line:no-console
|
|
107
|
+
console.log(`[${level}] [${this.module}] ${message}`);
|
|
108
|
+
}
|
|
109
|
+
else if (message instanceof Error) {
|
|
110
|
+
// eslint:disable-next-line:no-console
|
|
111
|
+
console.log(`${level} [${this.module}] ${message}`);
|
|
112
|
+
// eslint:disable-next-line:no-console
|
|
113
|
+
console.log(message.stack);
|
|
114
|
+
}
|
|
115
|
+
else if (typeof message === "object") {
|
|
116
|
+
// eslint:disable-next-line:no-console
|
|
117
|
+
console.log(`${level} [${this.module}] ${JSON.stringify(message)}`);
|
|
118
|
+
}
|
|
119
|
+
if (loggerOTel) {
|
|
120
|
+
{
|
|
121
|
+
loggerOTel.emit({
|
|
122
|
+
severityNumber,
|
|
123
|
+
severityText: level,
|
|
124
|
+
body: message,
|
|
125
|
+
attributes: { "log.type": "custom" },
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
exports.Logger = Logger;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PromisePool = void 0;
|
|
4
|
+
class PromisePool {
|
|
5
|
+
constructor(maxConcurrency, timeout = 3600000) {
|
|
6
|
+
this.maxConcurrency = maxConcurrency;
|
|
7
|
+
this.currentConcurrency = 0;
|
|
8
|
+
this.queue = [];
|
|
9
|
+
this.timeout = timeout;
|
|
10
|
+
}
|
|
11
|
+
add(promiseGenerator) {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
const controller = new AbortController();
|
|
14
|
+
const signal = controller.signal;
|
|
15
|
+
const wrappedPromise = () => {
|
|
16
|
+
return new Promise((innerResolve, innerReject) => {
|
|
17
|
+
const timeoutId = setTimeout(() => {
|
|
18
|
+
controller.abort();
|
|
19
|
+
innerReject(new Error("Promise cancelled due to timeout"));
|
|
20
|
+
}, this.timeout);
|
|
21
|
+
promiseGenerator(signal)
|
|
22
|
+
.then((result) => {
|
|
23
|
+
clearTimeout(timeoutId);
|
|
24
|
+
innerResolve(result);
|
|
25
|
+
})
|
|
26
|
+
.catch((error) => {
|
|
27
|
+
clearTimeout(timeoutId);
|
|
28
|
+
innerReject(error);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
this.queue.push({ wrappedPromise, resolve, reject });
|
|
33
|
+
this.runNext();
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
runNext() {
|
|
37
|
+
if (this.currentConcurrency < this.maxConcurrency && this.queue.length > 0) {
|
|
38
|
+
const { wrappedPromise, resolve, reject } = this.queue.shift();
|
|
39
|
+
this.currentConcurrency++;
|
|
40
|
+
wrappedPromise()
|
|
41
|
+
.then((result) => {
|
|
42
|
+
resolve(result);
|
|
43
|
+
this.currentConcurrency--;
|
|
44
|
+
this.runNext();
|
|
45
|
+
})
|
|
46
|
+
.catch((error) => {
|
|
47
|
+
reject(error);
|
|
48
|
+
this.currentConcurrency--;
|
|
49
|
+
this.runNext();
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
exports.PromisePool = PromisePool;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Config } from "../Config";
|
|
2
|
+
import { Span } from "@opentelemetry/sdk-trace-base";
|
|
3
|
+
export declare function SqlDbUtilsInit(context: Span, config: Config): Promise<void>;
|
|
4
|
+
export declare function SqlDbUtilsInitGetDatabase(): any;
|
|
5
|
+
export declare function SqlDbUtilsExecSQL(context: Span, sql: string, params?: never[]): Promise<number>;
|
|
6
|
+
export declare function SqlDbUtilsExecSQLFile(context: Span, filename: string): Promise<void>;
|
|
7
|
+
export declare function SqlDbUtilsQuerySQL(context: Span, sql: string, params?: never[], debug?: boolean): Promise<any[]>;
|
|
@@ -0,0 +1,132 @@
|
|
|
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.SqlDbUtilsInit = SqlDbUtilsInit;
|
|
37
|
+
exports.SqlDbUtilsInitGetDatabase = SqlDbUtilsInitGetDatabase;
|
|
38
|
+
exports.SqlDbUtilsExecSQL = SqlDbUtilsExecSQL;
|
|
39
|
+
exports.SqlDbUtilsExecSQLFile = SqlDbUtilsExecSQLFile;
|
|
40
|
+
exports.SqlDbUtilsQuerySQL = SqlDbUtilsQuerySQL;
|
|
41
|
+
const sqlite3_1 = require("sqlite3");
|
|
42
|
+
const fs = __importStar(require("fs-extra"));
|
|
43
|
+
const Logger_1 = require("./Logger");
|
|
44
|
+
const api_1 = require("@opentelemetry/api");
|
|
45
|
+
const StandardTracer_1 = require("./StandardTracer");
|
|
46
|
+
const logger = new Logger_1.Logger("SqlDbutils");
|
|
47
|
+
const SQL_DIR = `${__dirname}/../../sql`;
|
|
48
|
+
let database;
|
|
49
|
+
async function SqlDbUtilsInit(context, config) {
|
|
50
|
+
const span = (0, StandardTracer_1.StandardTracerStartSpan)("SqlDbUtilsInit", context);
|
|
51
|
+
await fs.ensureDir(config.DATA_DIR);
|
|
52
|
+
database = new sqlite3_1.Database(`${config.DATA_DIR}/database.db`);
|
|
53
|
+
await SqlDbUtilsExecSQLFile(span, `${SQL_DIR}/init-0000.sql`);
|
|
54
|
+
const initFiles = (await await fs.readdir(`${SQL_DIR}`)).sort();
|
|
55
|
+
let dbVersionApplied = 0;
|
|
56
|
+
const dbVersionQuery = await SqlDbUtilsQuerySQL(span, "SELECT MAX(value) as maxVerion FROM metadata WHERE type='db_version'");
|
|
57
|
+
if (dbVersionQuery[0].maxVerion) {
|
|
58
|
+
dbVersionApplied = Number(dbVersionQuery[0].maxVerion);
|
|
59
|
+
}
|
|
60
|
+
logger.info(`Current DB Version: ${dbVersionApplied}`);
|
|
61
|
+
for (const initFile of initFiles) {
|
|
62
|
+
const regex = /init-(\d+).sql/g;
|
|
63
|
+
const match = regex.exec(initFile);
|
|
64
|
+
if (match) {
|
|
65
|
+
const dbVersionInitFile = Number(match[1]);
|
|
66
|
+
if (dbVersionInitFile > dbVersionApplied) {
|
|
67
|
+
logger.info(`Loading init file: ${initFile}`);
|
|
68
|
+
await SqlDbUtilsExecSQLFile(span, `${SQL_DIR}/${initFile}`);
|
|
69
|
+
await SqlDbUtilsQuerySQL(span, 'INSERT INTO metadata (type, value, dateCreated) VALUES ("db_version",?,?)', [dbVersionInitFile, new Date().toISOString()]);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
span.end();
|
|
74
|
+
}
|
|
75
|
+
function SqlDbUtilsInitGetDatabase() {
|
|
76
|
+
return database;
|
|
77
|
+
}
|
|
78
|
+
function SqlDbUtilsExecSQL(context, sql, params = []) {
|
|
79
|
+
const span = (0, StandardTracer_1.StandardTracerStartSpan)("SqlDbUtilsExecSQL", context);
|
|
80
|
+
return new Promise((resolve, reject) => {
|
|
81
|
+
database.run(sql, params, function (error) {
|
|
82
|
+
if (error) {
|
|
83
|
+
span.setStatus({ code: api_1.SpanStatusCode.ERROR, message: error.message });
|
|
84
|
+
span.end();
|
|
85
|
+
reject(error);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
span.addEvent(`Impacted Rows: ${this.changes}`);
|
|
89
|
+
span.end();
|
|
90
|
+
resolve(this.changes);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
async function SqlDbUtilsExecSQLFile(context, filename) {
|
|
96
|
+
const span = (0, StandardTracer_1.StandardTracerStartSpan)("SqlDbUtilsExecSQLFile", context);
|
|
97
|
+
const sql = (await fs.readFile(filename)).toString();
|
|
98
|
+
return new Promise((resolve, reject) => {
|
|
99
|
+
database.exec(sql, (error) => {
|
|
100
|
+
if (error) {
|
|
101
|
+
span.setStatus({ code: api_1.SpanStatusCode.ERROR, message: error.message });
|
|
102
|
+
span.end();
|
|
103
|
+
reject(error);
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
span.end();
|
|
107
|
+
resolve();
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
function SqlDbUtilsQuerySQL(context, sql, params = [], debug = false
|
|
113
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
114
|
+
) {
|
|
115
|
+
const span = (0, StandardTracer_1.StandardTracerStartSpan)("SqlDbUtilsQuerySQL", context);
|
|
116
|
+
if (debug) {
|
|
117
|
+
console.log(sql);
|
|
118
|
+
}
|
|
119
|
+
return new Promise((resolve, reject) => {
|
|
120
|
+
database.all(sql, params, (error, rows) => {
|
|
121
|
+
if (error) {
|
|
122
|
+
span.setStatus({ code: api_1.SpanStatusCode.ERROR, message: error.message });
|
|
123
|
+
span.end();
|
|
124
|
+
reject(error);
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
span.end();
|
|
128
|
+
resolve(rows);
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SqlDbUtilsNoTelemetryExecSQL = SqlDbUtilsNoTelemetryExecSQL;
|
|
4
|
+
exports.SqlDbUtilsNoTelemetryQuerySQL = SqlDbUtilsNoTelemetryQuerySQL;
|
|
5
|
+
const Logger_1 = require("./Logger");
|
|
6
|
+
const SqlDbUtils_1 = require("./SqlDbUtils");
|
|
7
|
+
const logger = new Logger_1.Logger("SqlDbUtilsNoTelemetry");
|
|
8
|
+
function SqlDbUtilsNoTelemetryExecSQL(sql, params = []) {
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
(0, SqlDbUtils_1.SqlDbUtilsInitGetDatabase)().run(sql, params, (error) => {
|
|
11
|
+
if (error) {
|
|
12
|
+
reject(error);
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
resolve();
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
function SqlDbUtilsNoTelemetryQuerySQL(sql, params = [], debug = false
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
22
|
+
) {
|
|
23
|
+
if (debug) {
|
|
24
|
+
console.log(sql);
|
|
25
|
+
}
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
(0, SqlDbUtils_1.SqlDbUtilsInitGetDatabase)().all(sql, params, (error, rows) => {
|
|
28
|
+
if (error) {
|
|
29
|
+
console.log(error);
|
|
30
|
+
logger.error(`SQL ERROR: ${sql}`);
|
|
31
|
+
reject(error);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
resolve(rows);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Counter, Histogram, ObservableGauge } from "@opentelemetry/api";
|
|
2
|
+
import { ConfigInterface } from "./models/ConfigInterface";
|
|
3
|
+
export declare function StandardMeterInitTelemetry(initConfig: ConfigInterface): void;
|
|
4
|
+
export declare function StandardMeterCreateCounter(key: string): Counter;
|
|
5
|
+
export declare function StandardMeterCreateUpDownCounter(key: string): Counter;
|
|
6
|
+
export declare function StandardMeterCreateHistorgram(key: string): Histogram;
|
|
7
|
+
export declare function StandardMeterCreateObservableGauge(key: string, callback: (observableResult: any) => void, description?: any): ObservableGauge;
|
|
@@ -0,0 +1,109 @@
|
|
|
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.StandardMeterInitTelemetry = StandardMeterInitTelemetry;
|
|
37
|
+
exports.StandardMeterCreateCounter = StandardMeterCreateCounter;
|
|
38
|
+
exports.StandardMeterCreateUpDownCounter = StandardMeterCreateUpDownCounter;
|
|
39
|
+
exports.StandardMeterCreateHistorgram = StandardMeterCreateHistorgram;
|
|
40
|
+
exports.StandardMeterCreateObservableGauge = StandardMeterCreateObservableGauge;
|
|
41
|
+
const exporter_metrics_otlp_http_1 = require("@opentelemetry/exporter-metrics-otlp-http");
|
|
42
|
+
const resources_1 = require("@opentelemetry/resources");
|
|
43
|
+
const sdk_metrics_1 = require("@opentelemetry/sdk-metrics");
|
|
44
|
+
const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
|
|
45
|
+
const os = __importStar(require("os"));
|
|
46
|
+
let meterProvider;
|
|
47
|
+
let config;
|
|
48
|
+
const METER_NAME = "default";
|
|
49
|
+
//
|
|
50
|
+
function StandardMeterInitTelemetry(initConfig) {
|
|
51
|
+
config = initConfig;
|
|
52
|
+
// Metrics
|
|
53
|
+
if (config.OPENTELEMETRY_COLLECTOR_HTTP_METRICS) {
|
|
54
|
+
const collectorOptions = {
|
|
55
|
+
url: config.OPENTELEMETRY_COLLECTOR_HTTP_METRICS,
|
|
56
|
+
headers: {},
|
|
57
|
+
concurrencyLimit: 1,
|
|
58
|
+
};
|
|
59
|
+
if (config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER) {
|
|
60
|
+
collectorOptions.headers["Authorization"] = `Bearer ${config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER}`;
|
|
61
|
+
}
|
|
62
|
+
const metricExporter = new exporter_metrics_otlp_http_1.OTLPMetricExporter(collectorOptions);
|
|
63
|
+
meterProvider = new sdk_metrics_1.MeterProvider({
|
|
64
|
+
resource: (0, resources_1.resourceFromAttributes)({
|
|
65
|
+
[semantic_conventions_1.ATTR_SERVICE_NAME]: `${config.SERVICE_ID}`,
|
|
66
|
+
[semantic_conventions_1.ATTR_SERVICE_VERSION]: `${config.VERSION}`,
|
|
67
|
+
[semantic_conventions_1.ATTR_NETWORK_LOCAL_ADDRESS]: os.hostname(),
|
|
68
|
+
}),
|
|
69
|
+
readers: [
|
|
70
|
+
new sdk_metrics_1.PeriodicExportingMetricReader({
|
|
71
|
+
exporter: metricExporter,
|
|
72
|
+
exportIntervalMillis: config.OPENTELEMETRY_COLLECTOR_EXPORT_METRICS_INTERVAL_SECONDS *
|
|
73
|
+
1000,
|
|
74
|
+
}),
|
|
75
|
+
],
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
meterProvider = new sdk_metrics_1.MeterProvider({
|
|
80
|
+
resource: (0, resources_1.resourceFromAttributes)({
|
|
81
|
+
[semantic_conventions_1.ATTR_SERVICE_NAME]: `${config.SERVICE_ID}`,
|
|
82
|
+
[semantic_conventions_1.ATTR_SERVICE_VERSION]: `${config.VERSION}`,
|
|
83
|
+
[semantic_conventions_1.ATTR_NETWORK_LOCAL_ADDRESS]: os.hostname(),
|
|
84
|
+
}),
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function StandardMeterCreateCounter(key) {
|
|
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
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Span } from "@opentelemetry/sdk-trace-base";
|
|
2
|
+
import { ConfigInterface } from "./models/ConfigInterface";
|
|
3
|
+
export declare function StandardTracerInitTelemetry(initConfig: ConfigInterface): void;
|
|
4
|
+
export declare function StandardTracerGetSpanFromRequest(req: any): Span;
|
|
5
|
+
export declare function StandardTracerStartSpan(name: string, parentSpan?: Span): Span;
|
|
6
|
+
export declare function StandardTracerGetTracer(): any;
|
|
7
|
+
export declare function StandardTracerAppendHeader(context: Span, headers?: {}): any;
|
|
@@ -0,0 +1,115 @@
|
|
|
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.StandardTracerInitTelemetry = StandardTracerInitTelemetry;
|
|
37
|
+
exports.StandardTracerGetSpanFromRequest = StandardTracerGetSpanFromRequest;
|
|
38
|
+
exports.StandardTracerStartSpan = StandardTracerStartSpan;
|
|
39
|
+
exports.StandardTracerGetTracer = StandardTracerGetTracer;
|
|
40
|
+
exports.StandardTracerAppendHeader = StandardTracerAppendHeader;
|
|
41
|
+
const api_1 = __importStar(require("@opentelemetry/api"));
|
|
42
|
+
const context_async_hooks_1 = require("@opentelemetry/context-async-hooks");
|
|
43
|
+
const core_1 = require("@opentelemetry/core");
|
|
44
|
+
const exporter_trace_otlp_http_1 = require("@opentelemetry/exporter-trace-otlp-http");
|
|
45
|
+
const id_generator_aws_xray_1 = require("@opentelemetry/id-generator-aws-xray");
|
|
46
|
+
const resources_1 = require("@opentelemetry/resources");
|
|
47
|
+
const sdk_trace_base_1 = require("@opentelemetry/sdk-trace-base");
|
|
48
|
+
const sdk_trace_node_1 = require("@opentelemetry/sdk-trace-node");
|
|
49
|
+
const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
|
|
50
|
+
const os = __importStar(require("os"));
|
|
51
|
+
let tracerInstance;
|
|
52
|
+
const propagator = new core_1.W3CTraceContextPropagator();
|
|
53
|
+
let config;
|
|
54
|
+
//
|
|
55
|
+
function StandardTracerInitTelemetry(initConfig) {
|
|
56
|
+
config = initConfig;
|
|
57
|
+
const spanProcessors = [];
|
|
58
|
+
if (config.OPENTELEMETRY_COLLECTOR_HTTP_TRACES) {
|
|
59
|
+
const exporterHeaders = {};
|
|
60
|
+
if (config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER) {
|
|
61
|
+
exporterHeaders["Authorization"] = `Bearer ${config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER}`;
|
|
62
|
+
}
|
|
63
|
+
const exporter = new exporter_trace_otlp_http_1.OTLPTraceExporter({
|
|
64
|
+
url: config.OPENTELEMETRY_COLLECTOR_HTTP_TRACES,
|
|
65
|
+
headers: exporterHeaders,
|
|
66
|
+
});
|
|
67
|
+
spanProcessors.push(new sdk_trace_base_1.BatchSpanProcessor(exporter));
|
|
68
|
+
}
|
|
69
|
+
const traceProvider = new sdk_trace_node_1.NodeTracerProvider({
|
|
70
|
+
idGenerator: new id_generator_aws_xray_1.AWSXRayIdGenerator(),
|
|
71
|
+
resource: (0, resources_1.resourceFromAttributes)({
|
|
72
|
+
[semantic_conventions_1.ATTR_SERVICE_NAME]: `${config.SERVICE_ID}`,
|
|
73
|
+
[semantic_conventions_1.ATTR_SERVICE_VERSION]: `${config.VERSION}`,
|
|
74
|
+
[semantic_conventions_1.ATTR_NETWORK_LOCAL_ADDRESS]: os.hostname(),
|
|
75
|
+
}),
|
|
76
|
+
spanProcessors,
|
|
77
|
+
});
|
|
78
|
+
traceProvider.register();
|
|
79
|
+
const contextManager = new context_async_hooks_1.AsyncHooksContextManager();
|
|
80
|
+
contextManager.enable();
|
|
81
|
+
api_1.default.context.setGlobalContextManager(contextManager);
|
|
82
|
+
}
|
|
83
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
84
|
+
function StandardTracerGetSpanFromRequest(req) {
|
|
85
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
86
|
+
return req.tracerSpanApi;
|
|
87
|
+
}
|
|
88
|
+
function StandardTracerStartSpan(name, parentSpan) {
|
|
89
|
+
const sanitizedName = String(name).replace(/[^a-zA-Z0-9-_/]/g, "_");
|
|
90
|
+
const tracer = StandardTracerGetTracer();
|
|
91
|
+
if (parentSpan) {
|
|
92
|
+
return tracer.startSpan(sanitizedName, undefined, api_1.default.trace.setSpan(api_1.default.context.active(), parentSpan));
|
|
93
|
+
}
|
|
94
|
+
const span = tracer.startSpan(sanitizedName);
|
|
95
|
+
span.setAttribute(semantic_conventions_1.ATTR_HTTP_REQUEST_METHOD, `BACKEND`);
|
|
96
|
+
span.setAttribute(semantic_conventions_1.ATTR_HTTP_ROUTE, `${config.SERVICE_ID}-${config.VERSION}-${sanitizedName}`);
|
|
97
|
+
return span;
|
|
98
|
+
}
|
|
99
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
100
|
+
function StandardTracerGetTracer() {
|
|
101
|
+
if (!tracerInstance) {
|
|
102
|
+
tracerInstance = api_1.default.trace.getTracer(`${config.SERVICE_ID}:${config.VERSION}`);
|
|
103
|
+
}
|
|
104
|
+
return tracerInstance;
|
|
105
|
+
}
|
|
106
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
107
|
+
function StandardTracerAppendHeader(context, headers = {}) {
|
|
108
|
+
if (!headers) {
|
|
109
|
+
headers = {};
|
|
110
|
+
}
|
|
111
|
+
propagator.inject(api_1.trace.setSpanContext(api_1.ROOT_CONTEXT, context.spanContext()),
|
|
112
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
113
|
+
headers, api_1.defaultTextMapSetter);
|
|
114
|
+
return headers;
|
|
115
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function SystemCommandExecute(command: string, options?: {}): Promise<string>;
|
|
@@ -0,0 +1,50 @@
|
|
|
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.SystemCommandExecute = SystemCommandExecute;
|
|
37
|
+
const childProcess = __importStar(require("child_process"));
|
|
38
|
+
function SystemCommandExecute(command, options = {}) {
|
|
39
|
+
const exec = childProcess.exec;
|
|
40
|
+
return new Promise((resolve, reject) => {
|
|
41
|
+
exec(command, options, (error, stdout) => {
|
|
42
|
+
if (error) {
|
|
43
|
+
reject(error);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
resolve(stdout);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function TimeoutWait(duration: number): Promise<void>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface ConfigInterface {
|
|
2
|
+
SERVICE_ID: string;
|
|
3
|
+
VERSION: number;
|
|
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
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@devopsplaybook.io/otel-utils",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Utility to simplify integration with Open Telemetry",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"Open",
|
|
7
|
+
"Telemetry",
|
|
8
|
+
"Traces",
|
|
9
|
+
"Logs",
|
|
10
|
+
"Metrics"
|
|
11
|
+
],
|
|
12
|
+
"license": "ISC",
|
|
13
|
+
"author": "",
|
|
14
|
+
"type": "commonjs",
|
|
15
|
+
"main": "dist/index.js",
|
|
16
|
+
"types": "dist/index.d.ts",
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsc",
|
|
19
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@opentelemetry/api": "^1.9.0",
|
|
23
|
+
"@opentelemetry/api-logs": "^0.203.0",
|
|
24
|
+
"@opentelemetry/auto-instrumentations-node": "^0.62.0",
|
|
25
|
+
"@opentelemetry/exporter-logs-otlp-http": "^0.203.0",
|
|
26
|
+
"@opentelemetry/exporter-trace-otlp-http": "^0.203.0",
|
|
27
|
+
"@opentelemetry/id-generator-aws-xray": "^2.0.0",
|
|
28
|
+
"@opentelemetry/resources": "^2.0.1",
|
|
29
|
+
"@opentelemetry/sdk-logs": "^0.203.0",
|
|
30
|
+
"@opentelemetry/sdk-node": "^0.203.0",
|
|
31
|
+
"@opentelemetry/sdk-trace-base": "^2.0.1",
|
|
32
|
+
"@opentelemetry/sdk-trace-web": "^2.0.1",
|
|
33
|
+
"@opentelemetry/semantic-conventions": "^1.36.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/node": "^24.3.0",
|
|
37
|
+
"@types/sqlite3": "^3.1.11"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/src/Logger.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
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 { ConfigInterface } from "./models/ConfigInterface";
|
|
17
|
+
import { StandardTracerStartSpan } from "./StandardTracer";
|
|
18
|
+
|
|
19
|
+
let loggerOTel: OTelLogger;
|
|
20
|
+
|
|
21
|
+
export function LoggerInit(context: Span, config: ConfigInterface) {
|
|
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
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { Counter, Histogram, ObservableGauge } from "@opentelemetry/api";
|
|
2
|
+
import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-http";
|
|
3
|
+
import { resourceFromAttributes } from "@opentelemetry/resources";
|
|
4
|
+
import {
|
|
5
|
+
MeterProvider,
|
|
6
|
+
PeriodicExportingMetricReader,
|
|
7
|
+
} from "@opentelemetry/sdk-metrics";
|
|
8
|
+
import {
|
|
9
|
+
ATTR_NETWORK_LOCAL_ADDRESS,
|
|
10
|
+
ATTR_SERVICE_NAME,
|
|
11
|
+
ATTR_SERVICE_VERSION,
|
|
12
|
+
} from "@opentelemetry/semantic-conventions";
|
|
13
|
+
import * as os from "os";
|
|
14
|
+
import { ConfigInterface } from "./models/ConfigInterface";
|
|
15
|
+
|
|
16
|
+
let meterProvider: MeterProvider;
|
|
17
|
+
let config: ConfigInterface;
|
|
18
|
+
const METER_NAME = "default";
|
|
19
|
+
|
|
20
|
+
//
|
|
21
|
+
export function StandardMeterInitTelemetry(initConfig: ConfigInterface) {
|
|
22
|
+
config = initConfig;
|
|
23
|
+
|
|
24
|
+
// Metrics
|
|
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]: `${config.SERVICE_ID}`,
|
|
40
|
+
[ATTR_SERVICE_VERSION]: `${config.VERSION}`,
|
|
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,
|
|
49
|
+
}),
|
|
50
|
+
],
|
|
51
|
+
});
|
|
52
|
+
} else {
|
|
53
|
+
meterProvider = new MeterProvider({
|
|
54
|
+
resource: resourceFromAttributes({
|
|
55
|
+
[ATTR_SERVICE_NAME]: `${config.SERVICE_ID}`,
|
|
56
|
+
[ATTR_SERVICE_VERSION]: `${config.VERSION}`,
|
|
57
|
+
[ATTR_NETWORK_LOCAL_ADDRESS]: os.hostname(),
|
|
58
|
+
}),
|
|
59
|
+
});
|
|
60
|
+
}
|
|
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
|
+
|
|
68
|
+
export function StandardMeterCreateUpDownCounter(key: string): Counter {
|
|
69
|
+
const meter = meterProvider.getMeter(METER_NAME);
|
|
70
|
+
return meter.createUpDownCounter(`${config.SERVICE_ID}.${key}`);
|
|
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
|
+
}
|
|
77
|
+
|
|
78
|
+
export function StandardMeterCreateObservableGauge(
|
|
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 meter = meterProvider.getMeter(METER_NAME);
|
|
86
|
+
const observableGauge = meter.createObservableGauge(key, description);
|
|
87
|
+
|
|
88
|
+
observableGauge.addCallback(callback);
|
|
89
|
+
|
|
90
|
+
return observableGauge;
|
|
91
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import opentelemetry, {
|
|
2
|
+
defaultTextMapSetter,
|
|
3
|
+
ROOT_CONTEXT,
|
|
4
|
+
trace,
|
|
5
|
+
Tracer,
|
|
6
|
+
} from "@opentelemetry/api";
|
|
7
|
+
import { AsyncHooksContextManager } from "@opentelemetry/context-async-hooks";
|
|
8
|
+
import { W3CTraceContextPropagator } from "@opentelemetry/core";
|
|
9
|
+
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
|
|
10
|
+
import { AWSXRayIdGenerator } from "@opentelemetry/id-generator-aws-xray";
|
|
11
|
+
import { resourceFromAttributes } from "@opentelemetry/resources";
|
|
12
|
+
import { BatchSpanProcessor, Span } from "@opentelemetry/sdk-trace-base";
|
|
13
|
+
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
|
|
14
|
+
import {
|
|
15
|
+
ATTR_HTTP_REQUEST_METHOD,
|
|
16
|
+
ATTR_HTTP_ROUTE,
|
|
17
|
+
ATTR_NETWORK_LOCAL_ADDRESS,
|
|
18
|
+
ATTR_SERVICE_NAME,
|
|
19
|
+
ATTR_SERVICE_VERSION,
|
|
20
|
+
} from "@opentelemetry/semantic-conventions";
|
|
21
|
+
import * as os from "os";
|
|
22
|
+
import { ConfigInterface } from "./models/ConfigInterface";
|
|
23
|
+
|
|
24
|
+
let tracerInstance: Tracer;
|
|
25
|
+
const propagator = new W3CTraceContextPropagator();
|
|
26
|
+
let config: ConfigInterface;
|
|
27
|
+
|
|
28
|
+
//
|
|
29
|
+
export function StandardTracerInitTelemetry(initConfig: ConfigInterface) {
|
|
30
|
+
config = initConfig;
|
|
31
|
+
const spanProcessors = [];
|
|
32
|
+
|
|
33
|
+
if (config.OPENTELEMETRY_COLLECTOR_HTTP_TRACES) {
|
|
34
|
+
const exporterHeaders: Record<string, string> = {};
|
|
35
|
+
if (config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER) {
|
|
36
|
+
exporterHeaders[
|
|
37
|
+
"Authorization"
|
|
38
|
+
] = `Bearer ${config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER}`;
|
|
39
|
+
}
|
|
40
|
+
const exporter = new OTLPTraceExporter({
|
|
41
|
+
url: config.OPENTELEMETRY_COLLECTOR_HTTP_TRACES,
|
|
42
|
+
headers: exporterHeaders,
|
|
43
|
+
});
|
|
44
|
+
spanProcessors.push(new BatchSpanProcessor(exporter));
|
|
45
|
+
}
|
|
46
|
+
const traceProvider = new NodeTracerProvider({
|
|
47
|
+
idGenerator: new AWSXRayIdGenerator(),
|
|
48
|
+
resource: resourceFromAttributes({
|
|
49
|
+
[ATTR_SERVICE_NAME]: `${config.SERVICE_ID}`,
|
|
50
|
+
[ATTR_SERVICE_VERSION]: `${config.VERSION}`,
|
|
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
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
62
|
+
export function StandardTracerGetSpanFromRequest(req: any): Span {
|
|
63
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
64
|
+
return (req as any).tracerSpanApi as Span;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function StandardTracerStartSpan(name: string, parentSpan?: Span): Span {
|
|
68
|
+
const sanitizedName = String(name).replace(/[^a-zA-Z0-9-_/]/g, "_");
|
|
69
|
+
const tracer = StandardTracerGetTracer();
|
|
70
|
+
|
|
71
|
+
if (parentSpan) {
|
|
72
|
+
return tracer.startSpan(
|
|
73
|
+
sanitizedName,
|
|
74
|
+
undefined,
|
|
75
|
+
opentelemetry.trace.setSpan(opentelemetry.context.active(), parentSpan)
|
|
76
|
+
) as Span;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const span = tracer.startSpan(sanitizedName) as Span;
|
|
80
|
+
|
|
81
|
+
span.setAttribute(ATTR_HTTP_REQUEST_METHOD, `BACKEND`);
|
|
82
|
+
span.setAttribute(
|
|
83
|
+
ATTR_HTTP_ROUTE,
|
|
84
|
+
`${config.SERVICE_ID}-${config.VERSION}-${sanitizedName}`
|
|
85
|
+
);
|
|
86
|
+
return span;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
90
|
+
export function StandardTracerGetTracer(): any {
|
|
91
|
+
if (!tracerInstance) {
|
|
92
|
+
tracerInstance = opentelemetry.trace.getTracer(
|
|
93
|
+
`${config.SERVICE_ID}:${config.VERSION}`
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
return tracerInstance;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
100
|
+
export function StandardTracerAppendHeader(context: Span, headers = {}): any {
|
|
101
|
+
if (!headers) {
|
|
102
|
+
headers = {};
|
|
103
|
+
}
|
|
104
|
+
propagator.inject(
|
|
105
|
+
trace.setSpanContext(ROOT_CONTEXT, context.spanContext()),
|
|
106
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
107
|
+
headers as any,
|
|
108
|
+
defaultTextMapSetter
|
|
109
|
+
);
|
|
110
|
+
return headers;
|
|
111
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface ConfigInterface {
|
|
2
|
+
SERVICE_ID: string;
|
|
3
|
+
VERSION: number;
|
|
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/tsconfig.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
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
|
+
}
|