@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.
@@ -1,4 +1,4 @@
1
- import { Counter, Histogram, ObservableGauge } from "@opentelemetry/api";
1
+ import { Counter, Histogram, Meter, ObservableGauge } from "@opentelemetry/api";
2
2
  import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-http";
3
3
  import { resourceFromAttributes } from "@opentelemetry/resources";
4
4
  import {
@@ -11,81 +11,79 @@ import {
11
11
  ATTR_SERVICE_VERSION,
12
12
  } from "@opentelemetry/semantic-conventions";
13
13
  import * as os from "os";
14
- import { ConfigInterfaceOTel } from "./models/ConfigInterfaceOTel";
14
+ import { ConfigOTelInterface } from "./models/ConfigOTelInterface";
15
15
 
16
- let meterProvider: MeterProvider;
17
- let config: ConfigInterfaceOTel;
18
- const METER_NAME = "default";
16
+ export class StandardMeter {
17
+ private meter: Meter;
18
+ private serviceVersion: string;
19
+ private serviceName: string;
19
20
 
20
- //
21
- export function StandardMeterInitTelemetry(initConfig: ConfigInterfaceOTel) {
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,
21
+ constructor(config: ConfigOTelInterface) {
22
+ this.serviceName = config.SERVICE_ID;
23
+ this.serviceVersion = config.VERSION;
24
+ let meterProvider;
25
+ if (config.OPENTELEMETRY_COLLECTOR_HTTP_METRICS) {
26
+ const collectorOptions = {
27
+ url: config.OPENTELEMETRY_COLLECTOR_HTTP_METRICS,
28
+ headers: {} as Record<string, string>,
29
+ concurrencyLimit: 1,
30
+ };
31
+ if (config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER) {
32
+ collectorOptions.headers[
33
+ "Authorization"
34
+ ] = `Bearer ${config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER}`;
35
+ }
36
+ const metricExporter = new OTLPMetricExporter(collectorOptions);
37
+ meterProvider = new MeterProvider({
38
+ resource: resourceFromAttributes({
39
+ [ATTR_SERVICE_NAME]: `${this.serviceName}`,
40
+ [ATTR_SERVICE_VERSION]: `${this.serviceVersion}`,
41
+ [ATTR_NETWORK_LOCAL_ADDRESS]: os.hostname(),
49
42
  }),
50
- ],
51
- });
52
- } 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
- });
43
+ readers: [
44
+ new PeriodicExportingMetricReader({
45
+ exporter: metricExporter,
46
+ exportIntervalMillis:
47
+ config.OPENTELEMETRY_COLLECTOR_EXPORT_METRICS_INTERVAL_SECONDS *
48
+ 1000,
49
+ }),
50
+ ],
51
+ });
52
+ } else {
53
+ meterProvider = new MeterProvider({
54
+ resource: resourceFromAttributes({
55
+ [ATTR_SERVICE_NAME]: `${this.serviceName}`,
56
+ [ATTR_SERVICE_VERSION]: `${this.serviceVersion}`,
57
+ [ATTR_NETWORK_LOCAL_ADDRESS]: os.hostname(),
58
+ }),
59
+ });
60
+ }
61
+ this.meter = meterProvider.getMeter(
62
+ `${this.serviceName}:${this.serviceVersion}`
63
+ );
60
64
  }
61
- }
62
-
63
- export function StandardMeterCreateCounter(key: string): Counter {
64
- const meter = meterProvider.getMeter(METER_NAME);
65
- return meter.createCounter(`${config.SERVICE_ID}.${key}`);
66
- }
67
65
 
68
- 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
- }
66
+ public createCounter(key: string): Counter {
67
+ return this.meter.createCounter(`${this.serviceName}.${key}`);
68
+ }
77
69
 
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);
70
+ public createUpDownCounter(key: string): Counter {
71
+ return this.meter.createUpDownCounter(`${this.serviceName}.${key}`);
72
+ }
87
73
 
88
- observableGauge.addCallback(callback);
74
+ public createHistogram(key: string): Histogram {
75
+ return this.meter.createHistogram(`${this.serviceName}.${key}`);
76
+ }
89
77
 
90
- return observableGauge;
78
+ public createObservableGauge(
79
+ key: string,
80
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
81
+ callback: (observableResult: any) => void,
82
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
83
+ description: any = null
84
+ ): ObservableGauge {
85
+ const observableGauge = this.meter.createObservableGauge(key, description);
86
+ observableGauge.addCallback(callback);
87
+ return observableGauge;
88
+ }
91
89
  }
@@ -19,87 +19,79 @@ import {
19
19
  ATTR_SERVICE_VERSION,
20
20
  } from "@opentelemetry/semantic-conventions";
21
21
  import * as os from "os";
22
- import { ConfigInterfaceOTel } from "./models/ConfigInterfaceOTel";
22
+ import { ConfigOTelInterface } from "./models/ConfigOTelInterface";
23
23
 
24
- let tracerInstance: Tracer;
25
- const propagator = new W3CTraceContextPropagator();
26
- let config: ConfigInterfaceOTel;
24
+ export class StandardTracer {
25
+ private tracer: Tracer;
26
+ private serviceVersion: string;
27
+ private serviceName: string;
27
28
 
28
- //
29
- export function StandardTracerInitTelemetry(initConfig: ConfigInterfaceOTel) {
30
- config = initConfig;
31
- const spanProcessors = [];
29
+ constructor(config: ConfigOTelInterface) {
30
+ this.serviceName = config.SERVICE_ID;
31
+ this.serviceVersion = config.VERSION;
32
+ const spanProcessors = [];
32
33
 
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}`;
34
+ if (config.OPENTELEMETRY_COLLECTOR_HTTP_TRACES) {
35
+ const exporterHeaders: Record<string, string> = {};
36
+ if (config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER) {
37
+ exporterHeaders[
38
+ "Authorization"
39
+ ] = `Bearer ${config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER}`;
40
+ }
41
+ const exporter = new OTLPTraceExporter({
42
+ url: config.OPENTELEMETRY_COLLECTOR_HTTP_TRACES,
43
+ headers: exporterHeaders,
44
+ });
45
+ spanProcessors.push(new BatchSpanProcessor(exporter));
39
46
  }
40
- const exporter = new OTLPTraceExporter({
41
- url: config.OPENTELEMETRY_COLLECTOR_HTTP_TRACES,
42
- headers: exporterHeaders,
47
+ const traceProvider = new NodeTracerProvider({
48
+ idGenerator: new AWSXRayIdGenerator(),
49
+ resource: resourceFromAttributes({
50
+ [ATTR_SERVICE_NAME]: `${this.serviceName}`,
51
+ [ATTR_SERVICE_VERSION]: `${this.serviceVersion}`,
52
+ [ATTR_NETWORK_LOCAL_ADDRESS]: os.hostname(),
53
+ }),
54
+ spanProcessors,
43
55
  });
44
- 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
- export function StandardTracerStartSpan(name: string, parentSpan?: Span): Span {
62
- const sanitizedName = String(name).replace(/[^a-zA-Z0-9-_/]/g, "_");
63
- const tracer = StandardTracerGetTracer();
64
-
65
- if (parentSpan) {
66
- return tracer.startSpan(
67
- sanitizedName,
68
- undefined,
69
- opentelemetry.trace.setSpan(opentelemetry.context.active(), parentSpan)
70
- ) as Span;
56
+ traceProvider.register();
57
+ const contextManager = new AsyncHooksContextManager();
58
+ contextManager.enable();
59
+ opentelemetry.context.setGlobalContextManager(contextManager);
60
+ this.tracer = opentelemetry.trace.getTracer(
61
+ `${this.serviceName}:${this.serviceVersion}`
62
+ );
71
63
  }
72
64
 
73
- const span = tracer.startSpan(sanitizedName) as Span;
74
-
75
- span.setAttribute(ATTR_HTTP_REQUEST_METHOD, `BACKEND`);
76
- span.setAttribute(
77
- ATTR_HTTP_ROUTE,
78
- `${config.SERVICE_ID}-${config.VERSION}-${sanitizedName}`
79
- );
80
- return span;
81
- }
82
-
83
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
84
- export function StandardTracerGetTracer(): any {
85
- if (!tracerInstance) {
86
- tracerInstance = opentelemetry.trace.getTracer(
87
- `${config.SERVICE_ID}:${config.VERSION}`
65
+ public startSpan(name: string, parentSpan?: Span): Span {
66
+ const sanitizedName = String(name).replace(/[^a-zA-Z0-9-_/]/g, "_");
67
+ if (parentSpan) {
68
+ return this.tracer.startSpan(
69
+ sanitizedName,
70
+ undefined,
71
+ opentelemetry.trace.setSpan(opentelemetry.context.active(), parentSpan)
72
+ ) as Span;
73
+ }
74
+ const span = this.tracer.startSpan(sanitizedName) as Span;
75
+ span.setAttribute(ATTR_HTTP_REQUEST_METHOD, `BACKEND`);
76
+ span.setAttribute(
77
+ ATTR_HTTP_ROUTE,
78
+ `${this.serviceName}-${this.serviceVersion}-${sanitizedName}`
88
79
  );
80
+ return span;
89
81
  }
90
- return tracerInstance;
91
- }
92
82
 
93
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
94
- export function StandardTracerAppendHeader(context: Span, headers = {}): any {
95
- if (!headers) {
96
- headers = {};
83
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
84
+ public static updateHttpHeader(context: Span, headers = {}): any {
85
+ if (!headers) {
86
+ headers = {};
87
+ }
88
+ const propagator = new W3CTraceContextPropagator();
89
+ propagator.inject(
90
+ trace.setSpanContext(ROOT_CONTEXT, context.spanContext()),
91
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
92
+ headers as any,
93
+ defaultTextMapSetter
94
+ );
95
+ return headers;
97
96
  }
98
- propagator.inject(
99
- trace.setSpanContext(ROOT_CONTEXT, context.spanContext()),
100
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
101
- headers as any,
102
- defaultTextMapSetter
103
- );
104
- return headers;
105
97
  }
@@ -1,6 +1,6 @@
1
- export interface ConfigInterfaceOTel {
1
+ export interface ConfigOTelInterface {
2
2
  SERVICE_ID: string;
3
- VERSION: number;
3
+ VERSION: string;
4
4
  OPENTELEMETRY_COLLECTOR_HTTP_TRACES: string;
5
5
  OPENTELEMETRY_COLLECTOR_HTTP_METRICS: string;
6
6
  OPENTELEMETRY_COLLECTOR_HTTP_LOGS: string;
@@ -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[]>;