@devopsplaybook.io/otel-utils 1.0.1-beta5 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@devopsplaybook.io/otel-utils",
3
- "version": "1.0.1-beta5",
3
+ "version": "1.0.1-beta6",
4
4
  "description": "Utility to simplify integration with Open Telemetry",
5
5
  "keywords": [
6
6
  "Open",
@@ -1,12 +0,0 @@
1
- import { Span } from "@opentelemetry/sdk-trace-base";
2
- import { ConfigInterfaceOTel } from "./models/ConfigInterfaceOTel";
3
- export declare function LoggerInit(context: Span, config: ConfigInterfaceOTel): 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
- }
@@ -1,131 +0,0 @@
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;
@@ -1,9 +0,0 @@
1
- export declare class PromisePool {
2
- private maxConcurrency;
3
- private currentConcurrency;
4
- private queue;
5
- private timeout;
6
- constructor(maxConcurrency: any, timeout?: number);
7
- add(promiseGenerator: any): Promise<unknown>;
8
- private runNext;
9
- }
@@ -1,54 +0,0 @@
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;
@@ -1,7 +0,0 @@
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[]>;
@@ -1,132 +0,0 @@
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
- }
@@ -1,2 +0,0 @@
1
- export declare function SqlDbUtilsNoTelemetryExecSQL(sql: string, params?: never[]): Promise<void>;
2
- export declare function SqlDbUtilsNoTelemetryQuerySQL(sql: string, params?: never[], debug?: boolean): Promise<any[]>;
@@ -1,38 +0,0 @@
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
- }
@@ -1 +0,0 @@
1
- export declare function SystemCommandExecute(command: string, options?: {}): Promise<string>;
@@ -1,50 +0,0 @@
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
- }
@@ -1 +0,0 @@
1
- export declare function TimeoutWait(duration: number): Promise<void>;
@@ -1,10 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TimeoutWait = TimeoutWait;
4
- function TimeoutWait(duration) {
5
- return new Promise((resolve) => {
6
- setTimeout(() => {
7
- resolve();
8
- }, duration);
9
- });
10
- }
@@ -1,11 +0,0 @@
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
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,11 +0,0 @@
1
- export interface ConfigInterfaceOTel {
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
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });