@qrvey/telemetry 0.0.1

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/.eslintrc ADDED
@@ -0,0 +1,88 @@
1
+ {
2
+ "parser": "@typescript-eslint/parser",
3
+ "plugins": [
4
+ "import"
5
+ ],
6
+ "extends": [
7
+ "../../.eslintrc",
8
+ "plugin:import/recommended",
9
+ "plugin:import/typescript"
10
+ ],
11
+ "rules": {
12
+ "sort-imports": [
13
+ "error",
14
+ {
15
+ "ignoreCase": false,
16
+ "ignoreDeclarationSort": true,
17
+ "ignoreMemberSort": false,
18
+ "memberSyntaxSortOrder": [
19
+ "none",
20
+ "all",
21
+ "multiple",
22
+ "single"
23
+ ],
24
+ "allowSeparatedGroups": true
25
+ }
26
+ ],
27
+ "import/no-unresolved": "warn",
28
+ "import/order": [
29
+ "error",
30
+ {
31
+ "groups": [
32
+ "builtin",
33
+ "external",
34
+ "internal",
35
+ [
36
+ "sibling",
37
+ "parent"
38
+ ],
39
+ "index",
40
+ "unknown"
41
+ ],
42
+ "newlines-between": "always",
43
+ "alphabetize": {
44
+ "order": "asc",
45
+ "caseInsensitive": true
46
+ }
47
+ }
48
+ ],
49
+ "padding-line-between-statements": [
50
+ "error",
51
+ {
52
+ "blankLine": "always",
53
+ "prev": "function",
54
+ "next": "*"
55
+ },
56
+ {
57
+ "blankLine": "always",
58
+ "prev": "*",
59
+ "next": "function"
60
+ },
61
+ {
62
+ "blankLine": "always",
63
+ "prev": "class",
64
+ "next": "*"
65
+ },
66
+ {
67
+ "blankLine": "always",
68
+ "prev": "*",
69
+ "next": "class"
70
+ }
71
+ ],
72
+ "lines-between-class-members": [
73
+ "error",
74
+ "always",
75
+ {
76
+ "exceptAfterSingleLine": true
77
+ }
78
+ ],
79
+ "no-console": "error"
80
+ },
81
+ "settings": {
82
+ "import/resolver": {
83
+ "typescript": {
84
+ "project": "./tsconfig.json"
85
+ }
86
+ }
87
+ }
88
+ }
package/.prettierrc.js ADDED
@@ -0,0 +1,7 @@
1
+ module.exports = {
2
+ bracketSpacing: true,
3
+ jsxBracketSameLine: true,
4
+ singleQuote: true,
5
+ trailingComma: 'all',
6
+ printWidth: 200,
7
+ };
package/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Telemetry Package
2
+
3
+ This package provides telemetry instrumentation and logging for Qrvey services.
4
+
5
+ ## Environment Variables
6
+
7
+ Set the following environment variables to configure telemetry:
8
+
9
+ ```env
10
+ ENABLE_OTLP_LOG=true # Enable OpenTelemetry logging
11
+ ENABLE_OTLP_TRACE=true # Enable OpenTelemetry tracing
12
+ ENABLE_OTLP_METRIC=true # Enable OpenTelemetry metrics
13
+ QRVEY_SERVICE_NAME=Qrvey-Service # Name of the service for telemetry identification
14
+ QRVEY_SERVICE_VERSION=1.0.0 # Version of the service
15
+ OTLP_TRACE_URL=http://localhost:4320/v1/traces # Endpoint for sending trace data
16
+ OTLP_LOG_URL=http://localhost:4320/v1/logs # Endpoint for sending log data
17
+ OTLP_METRIC_URL=http://otel-collector:4318/v1/metrics # Endpoint for sending metric data
18
+ ENABLE_CONSOLE_LOGS=false # Enable or disable console logging
19
+ NODE_ENV=development # Node.js environment (development, production, etc.)
20
+ LOG_LEVEL=debug # Minimum log level (debug, info, warn, error)
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ At the beginning of each service, start the `InstrumentationService` and use the `LoggerService` for logging purposes:
26
+
27
+ ```js
28
+ const { InstrumentationService, LoggerService } = require("@qrvey/telemetry");
29
+
30
+ // Initialize instrumentation
31
+ new InstrumentationService();
32
+
33
+ // Initialize logger
34
+ const logger = new LoggerService();
35
+
36
+ logger.info("this is info message");
37
+ logger.debug("this is a debug message");
38
+ logger.warn("this is a warning message");
39
+ logger.error("this is an error message");
40
+ ```
41
+
42
+ ## Notes
43
+
44
+ - Ensure all environment variables are set before starting your service.
45
+ - Use the logger for consistent log formatting and output.
46
+ - InstrumentationService should be initialized once at service startup.
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.InstrumentationService = exports.LoggerService = void 0;
7
+ var loggerService_1 = require("./lib/services/loggerService");
8
+ Object.defineProperty(exports, "LoggerService", { enumerable: true, get: function () { return __importDefault(loggerService_1).default; } });
9
+ var instrumentationService_1 = require("./lib/services/instrumentationService");
10
+ Object.defineProperty(exports, "InstrumentationService", { enumerable: true, get: function () { return __importDefault(instrumentationService_1).default; } });
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,8DAAwE;AAA/D,+HAAA,OAAO,OAAiB;AACjC,gFAA0F;AAAjF,iJAAA,OAAO,OAA0B"}
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ENVIRONMENT = exports.LOG_LEVELS = exports.DEFAULT_LOGGER_SERVICE_NAME = void 0;
4
+ exports.DEFAULT_LOGGER_SERVICE_NAME = 'pino-otel-service';
5
+ exports.LOG_LEVELS = {
6
+ DEBUG: { name: 'debug', severity: 5 },
7
+ INFO: { name: 'info', severity: 9 },
8
+ WARN: { name: 'warn', severity: 13 },
9
+ ERROR: { name: 'error', severity: 17 },
10
+ };
11
+ exports.ENVIRONMENT = {
12
+ LOG_LEVEL: process.env.LOG_LEVEL || exports.LOG_LEVELS.DEBUG.name,
13
+ ENABLE_CONSOLE_LOGS: process.env.ENABLE_CONSOLE_LOGS ? Boolean(process.env.ENABLE_CONSOLE_LOGS) : true,
14
+ ENABLE_OTLP_TRACE: process.env.ENABLE_OTLP_TRACE ? Boolean(process.env.ENABLE_OTLP_TRACE) : true,
15
+ ENABLE_OTLP_METRIC: process.env.ENABLE_OTLP_METRIC ? Boolean(process.env.ENABLE_OTLP_METRIC) : true,
16
+ ENABLE_OTLP_LOG: process.env.ENABLE_OTLP_LOG ? Boolean(process.env.ENABLE_OTLP_LOG) : true,
17
+ OTLP_TRACE_URL: process.env.OTLP_TRACE_URL || 'http://localhost:4320/v1/traces',
18
+ OTLP_METRIC_URL: process.env.OTLP_METRIC_URL || 'http://localhost:4318/v1/metrics',
19
+ OTLP_LOG_URL: process.env.OTLP_LOG_URL || 'http://localhost:4320/v1/logs',
20
+ QRVEY_SERVICE_NAME: process.env.QRVEY_SERVICE_NAME || 'Default-Service-name',
21
+ QRVEY_SERVICE_VERSION: process.env.QRVEY_SERVICE_VERSION || '0.0.0',
22
+ EXPORT_METRICS_INTERVAL_MILLIS: process.env.EXPORT_METRICS_INTERVAL_MILLIS ? parseInt(process.env.EXPORT_METRICS_INTERVAL_MILLIS, 10) : 10000,
23
+ };
24
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/lib/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,2BAA2B,GAAG,mBAAmB,CAAC;AAElD,QAAA,UAAU,GAAG;IACtB,KAAK,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC,EAAE;IACrC,IAAI,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,EAAE;IACnC,IAAI,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,EAAE;IACpC,KAAK,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE;CAChC,CAAC;AAEE,QAAA,WAAW,GAAG;IACvB,SAAS,EAAE,OAAO,CAAC,GAAG,CAAC,SAAS,IAAI,kBAAU,CAAC,KAAK,CAAC,IAAI;IACzD,mBAAmB,EAAE,OAAO,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC,IAAI;IACtG,iBAAiB,EAAE,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC,IAAI;IAChG,kBAAkB,EAAE,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC,CAAC,CAAC,IAAI;IACnG,eAAe,EAAE,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,IAAI;IAC1F,cAAc,EAAE,OAAO,CAAC,GAAG,CAAC,cAAc,IAAI,iCAAiC;IAC/E,eAAe,EAAE,OAAO,CAAC,GAAG,CAAC,eAAe,IAAI,kCAAkC;IAClF,YAAY,EAAE,OAAO,CAAC,GAAG,CAAC,YAAY,IAAI,+BAA+B;IACzE,kBAAkB,EAAE,OAAO,CAAC,GAAG,CAAC,kBAAkB,IAAI,sBAAsB;IAC5E,qBAAqB,EAAE,OAAO,CAAC,GAAG,CAAC,qBAAqB,IAAI,OAAO;IACnE,8BAA8B,EAAE,OAAO,CAAC,GAAG,CAAC,8BAA8B,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,8BAA8B,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK;CACvI,CAAC"}
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const auto_instrumentations_node_1 = require("@opentelemetry/auto-instrumentations-node");
4
+ const exporter_logs_otlp_proto_1 = require("@opentelemetry/exporter-logs-otlp-proto");
5
+ const exporter_metrics_otlp_proto_1 = require("@opentelemetry/exporter-metrics-otlp-proto");
6
+ const exporter_trace_otlp_proto_1 = require("@opentelemetry/exporter-trace-otlp-proto");
7
+ const resources_1 = require("@opentelemetry/resources");
8
+ const sdk_logs_1 = require("@opentelemetry/sdk-logs");
9
+ const sdk_metrics_1 = require("@opentelemetry/sdk-metrics");
10
+ const sdk_node_1 = require("@opentelemetry/sdk-node");
11
+ const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
12
+ const constants_1 = require("../constants");
13
+ class InstrumentationService {
14
+ constructor() {
15
+ this.enableOtlpTrace = constants_1.ENVIRONMENT.ENABLE_OTLP_TRACE;
16
+ this.enableOtlpMetric = constants_1.ENVIRONMENT.ENABLE_OTLP_METRIC;
17
+ this.enableOtlpLog = constants_1.ENVIRONMENT.ENABLE_OTLP_LOG;
18
+ this.otlpTraceUrl = constants_1.ENVIRONMENT.OTLP_TRACE_URL;
19
+ this.otlpMetricUrl = constants_1.ENVIRONMENT.OTLP_METRIC_URL;
20
+ this.otlpLogUrl = constants_1.ENVIRONMENT.OTLP_LOG_URL;
21
+ this.serviceName = constants_1.ENVIRONMENT.QRVEY_SERVICE_NAME;
22
+ this.serviceVersion = constants_1.ENVIRONMENT.QRVEY_SERVICE_VERSION;
23
+ this.exportMetricsIntervalMillis = constants_1.ENVIRONMENT.EXPORT_METRICS_INTERVAL_MILLIS;
24
+ const sdkConfig = {
25
+ resource: (0, resources_1.resourceFromAttributes)({
26
+ [semantic_conventions_1.ATTR_SERVICE_NAME]: this.serviceName,
27
+ [semantic_conventions_1.ATTR_SERVICE_VERSION]: this.serviceVersion,
28
+ }),
29
+ instrumentations: [(0, auto_instrumentations_node_1.getNodeAutoInstrumentations)()],
30
+ };
31
+ if (this.enableOtlpTrace) {
32
+ sdkConfig.traceExporter = new exporter_trace_otlp_proto_1.OTLPTraceExporter({
33
+ url: this.otlpTraceUrl,
34
+ headers: {
35
+ 'Content-Type': 'application/json',
36
+ },
37
+ });
38
+ }
39
+ if (this.enableOtlpMetric) {
40
+ sdkConfig.metricReader = new sdk_metrics_1.PeriodicExportingMetricReader({
41
+ exporter: new exporter_metrics_otlp_proto_1.OTLPMetricExporter({
42
+ url: this.otlpMetricUrl,
43
+ }),
44
+ exportIntervalMillis: this.exportMetricsIntervalMillis, // Export every 10 seconds
45
+ });
46
+ }
47
+ if (this.enableOtlpLog) {
48
+ sdkConfig.logRecordProcessor = new sdk_logs_1.SimpleLogRecordProcessor(new exporter_logs_otlp_proto_1.OTLPLogExporter({
49
+ url: this.otlpLogUrl,
50
+ }));
51
+ }
52
+ this.sdk = new sdk_node_1.NodeSDK(sdkConfig);
53
+ this.sdk.start();
54
+ }
55
+ async shutdown() {
56
+ await this.sdk.shutdown();
57
+ }
58
+ }
59
+ exports.default = InstrumentationService;
60
+ //# sourceMappingURL=instrumentationService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"instrumentationService.js","sourceRoot":"","sources":["../../../../src/lib/services/instrumentationService.ts"],"names":[],"mappings":";;AAAA,0FAAwF;AACxF,sFAA0E;AAC1E,4FAAgF;AAChF,wFAA6E;AAC7E,wDAAkE;AAClE,sDAAmE;AACnE,4DAA2E;AAC3E,sDAAkD;AAClD,8EAA8F;AAE9F,4CAA2C;AAE3C,MAAM,sBAAsB;IAaxB;QAVQ,oBAAe,GAAY,uBAAW,CAAC,iBAAiB,CAAC;QACzD,qBAAgB,GAAY,uBAAW,CAAC,kBAAkB,CAAC;QAC3D,kBAAa,GAAY,uBAAW,CAAC,eAAe,CAAC;QACrD,iBAAY,GAAW,uBAAW,CAAC,cAAc,CAAC;QAClD,kBAAa,GAAW,uBAAW,CAAC,eAAe,CAAC;QACpD,eAAU,GAAW,uBAAW,CAAC,YAAY,CAAC;QAC9C,gBAAW,GAAW,uBAAW,CAAC,kBAAkB,CAAC;QACrD,mBAAc,GAAW,uBAAW,CAAC,qBAAqB,CAAC;QAC3D,gCAA2B,GAAW,uBAAW,CAAC,8BAA8B,CAAC;QAGrF,MAAM,SAAS,GAAQ;YACnB,QAAQ,EAAE,IAAA,kCAAsB,EAAC;gBAC7B,CAAC,wCAAiB,CAAC,EAAE,IAAI,CAAC,WAAW;gBACrC,CAAC,2CAAoB,CAAC,EAAE,IAAI,CAAC,cAAc;aAC9C,CAAC;YACF,gBAAgB,EAAE,CAAC,IAAA,wDAA2B,GAAE,CAAC;SACpD,CAAC;QAEF,IAAI,IAAI,CAAC,eAAe,EAAE,CAAC;YACvB,SAAS,CAAC,aAAa,GAAG,IAAI,6CAAiB,CAAC;gBAC5C,GAAG,EAAE,IAAI,CAAC,YAAY;gBACtB,OAAO,EAAE;oBACL,cAAc,EAAE,kBAAkB;iBACrC;aACJ,CAAC,CAAC;QACP,CAAC;QAED,IAAI,IAAI,CAAC,gBAAgB,EAAE,CAAC;YACxB,SAAS,CAAC,YAAY,GAAG,IAAI,2CAA6B,CAAC;gBACvD,QAAQ,EAAE,IAAI,gDAAkB,CAAC;oBAC7B,GAAG,EAAE,IAAI,CAAC,aAAa;iBAC1B,CAAC;gBACF,oBAAoB,EAAE,IAAI,CAAC,2BAA2B,EAAE,0BAA0B;aACrF,CAAC,CAAC;QACP,CAAC;QAED,IAAI,IAAI,CAAC,aAAa,EAAE,CAAC;YACrB,SAAS,CAAC,kBAAkB,GAAG,IAAI,mCAAwB,CACvD,IAAI,0CAAe,CAAC;gBAChB,GAAG,EAAE,IAAI,CAAC,UAAU;aACvB,CAAC,CACL,CAAC;QACN,CAAC;QAED,IAAI,CAAC,GAAG,GAAG,IAAI,kBAAO,CAAC,SAAS,CAAC,CAAC;QAClC,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC;IACrB,CAAC;IAEM,KAAK,CAAC,QAAQ;QACjB,MAAM,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC;IAC9B,CAAC;CACJ;AAED,kBAAe,sBAAsB,CAAC"}
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const api_logs_1 = require("@opentelemetry/api-logs");
7
+ const pino_1 = __importDefault(require("pino"));
8
+ const constants_1 = require("../constants");
9
+ class LoggerService {
10
+ constructor(serviceName = constants_1.DEFAULT_LOGGER_SERVICE_NAME) {
11
+ this.enableConsole = constants_1.ENVIRONMENT.ENABLE_CONSOLE_LOGS;
12
+ this.pinoLogger = (0, pino_1.default)({
13
+ level: constants_1.ENVIRONMENT.LOG_LEVEL,
14
+ formatters: {
15
+ level: (label) => {
16
+ return { level: label.toUpperCase() };
17
+ },
18
+ },
19
+ timestamp: () => `,"timestamp":"${new Date(Date.now()).toISOString()}"`,
20
+ transport: constants_1.ENVIRONMENT.LOG_LEVEL === constants_1.LOG_LEVELS.DEBUG.name
21
+ ? {
22
+ target: 'pino-pretty',
23
+ options: { colorize: true },
24
+ }
25
+ : undefined,
26
+ });
27
+ this.otelLogger = api_logs_1.logs.getLogger(serviceName);
28
+ }
29
+ info(message, meta) {
30
+ if (this.enableConsole)
31
+ this.pinoLogger.info(meta || {}, message);
32
+ this.otelLogger.emit({
33
+ body: message,
34
+ severityNumber: constants_1.LOG_LEVELS.INFO.severity,
35
+ attributes: meta,
36
+ });
37
+ }
38
+ error(message, meta) {
39
+ if (this.enableConsole)
40
+ this.pinoLogger.error(meta || {}, message);
41
+ this.otelLogger.emit({
42
+ body: message,
43
+ severityNumber: constants_1.LOG_LEVELS.ERROR.severity,
44
+ attributes: meta,
45
+ });
46
+ }
47
+ warn(message, meta) {
48
+ if (this.enableConsole)
49
+ this.pinoLogger.warn(meta || {}, message);
50
+ this.otelLogger.emit({
51
+ body: message,
52
+ severityNumber: constants_1.LOG_LEVELS.WARN.severity,
53
+ attributes: meta,
54
+ });
55
+ }
56
+ debug(message, meta) {
57
+ if (this.enableConsole)
58
+ this.pinoLogger.debug(meta || {}, message);
59
+ if (constants_1.ENVIRONMENT.LOG_LEVEL === constants_1.LOG_LEVELS.DEBUG.name)
60
+ this.otelLogger.emit({
61
+ body: message,
62
+ severityNumber: constants_1.LOG_LEVELS.DEBUG.severity,
63
+ attributes: meta,
64
+ });
65
+ }
66
+ }
67
+ exports.default = LoggerService;
68
+ //# sourceMappingURL=loggerService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loggerService.js","sourceRoot":"","sources":["../../../../src/lib/services/loggerService.ts"],"names":[],"mappings":";;;;;AAAA,sDAAoF;AACpF,gDAAwB;AAExB,4CAAoF;AAIpF,MAAM,aAAa;IAKf,YAAY,WAAW,GAAG,uCAA2B;QAF7C,kBAAa,GAAY,uBAAW,CAAC,mBAAmB,CAAC;QAG7D,IAAI,CAAC,UAAU,GAAG,IAAA,cAAI,EAAC;YACnB,KAAK,EAAE,uBAAW,CAAC,SAAS;YAC5B,UAAU,EAAE;gBACR,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE;oBACb,OAAO,EAAE,KAAK,EAAE,KAAK,CAAC,WAAW,EAAE,EAAE,CAAC;gBAC1C,CAAC;aACJ;YACD,SAAS,EAAE,GAAG,EAAE,CAAC,iBAAiB,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,WAAW,EAAE,GAAG;YACvE,SAAS,EACL,uBAAW,CAAC,SAAS,KAAK,sBAAU,CAAC,KAAK,CAAC,IAAI;gBAC3C,CAAC,CAAC;oBACI,MAAM,EAAE,aAAa;oBACrB,OAAO,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE;iBAC9B;gBACH,CAAC,CAAC,SAAS;SACtB,CAAC,CAAC;QACH,IAAI,CAAC,UAAU,GAAG,eAAI,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC;IAClD,CAAC;IAED,IAAI,CAAC,OAAe,EAAE,IAAoB;QACtC,IAAI,IAAI,CAAC,aAAa;YAAE,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,IAAI,EAAE,EAAE,OAAO,CAAC,CAAC;QAClE,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC;YACjB,IAAI,EAAE,OAAO;YACb,cAAc,EAAE,sBAA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entelemetry/api-logs';
2
+ import pino from 'pino';
3
+ import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
4
+ import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-proto';
5
+ import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-proto';
6
+ import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
7
+ import { resourceFromAttributes } from '@opentelemetry/resources';
8
+ import { SimpleLogRecordProcessor } from '@opentelemetry/sdk-logs';
9
+ import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
10
+ import { NodeSDK } from '@opentelemetry/sdk-node';
11
+ import { ATTR_SERVICE_VERSION, ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
12
+
13
+ // src/lib/services/loggerService.ts
14
+
15
+ // src/lib/constants.ts
16
+ var DEFAULT_LOGGER_SERVICE_NAME = "pino-otel-service";
17
+ var LOG_LEVELS = {
18
+ DEBUG: { name: "debug", severity: 5 },
19
+ INFO: { severity: 9 },
20
+ WARN: { severity: 13 },
21
+ ERROR: { severity: 17 }
22
+ };
23
+ var ENVIRONMENT = {
24
+ LOG_LEVEL: process.env.LOG_LEVEL || LOG_LEVELS.DEBUG.name,
25
+ ENABLE_CONSOLE_LOGS: process.env.ENABLE_CONSOLE_LOGS ? Boolean(process.env.ENABLE_CONSOLE_LOGS) : true,
26
+ ENABLE_OTLP_TRACE: process.env.ENABLE_OTLP_TRACE ? Boolean(process.env.ENABLE_OTLP_TRACE) : true,
27
+ ENABLE_OTLP_METRIC: process.env.ENABLE_OTLP_METRIC ? Boolean(process.env.ENABLE_OTLP_METRIC) : true,
28
+ ENABLE_OTLP_LOG: process.env.ENABLE_OTLP_LOG ? Boolean(process.env.ENABLE_OTLP_LOG) : true,
29
+ OTLP_TRACE_URL: process.env.OTLP_TRACE_URL || "http://localhost:4320/v1/traces",
30
+ OTLP_METRIC_URL: process.env.OTLP_METRIC_URL || "http://localhost:4318/v1/metrics",
31
+ OTLP_LOG_URL: process.env.OTLP_LOG_URL || "http://localhost:4320/v1/logs",
32
+ QRVEY_SERVICE_NAME: process.env.QRVEY_SERVICE_NAME || "Default-Service-name",
33
+ QRVEY_SERVICE_VERSION: process.env.QRVEY_SERVICE_VERSION || "0.0.0",
34
+ EXPORT_METRICS_INTERVAL_MILLIS: process.env.EXPORT_METRICS_INTERVAL_MILLIS ? parseInt(process.env.EXPORT_METRICS_INTERVAL_MILLIS, 10) : 1e4
35
+ };
36
+
37
+ // src/lib/services/loggerService.ts
38
+ var LoggerService = class {
39
+ constructor(serviceName = DEFAULT_LOGGER_SERVICE_NAME) {
40
+ this.enableConsole = ENVIRONMENT.ENABLE_CONSOLE_LOGS;
41
+ this.pinoLogger = pino({
42
+ level: ENVIRONMENT.LOG_LEVEL,
43
+ formatters: {
44
+ level: (label) => {
45
+ return { level: label.toUpperCase() };
46
+ }
47
+ },
48
+ timestamp: () => `,"timestamp":"${new Date(Date.now()).toISOString()}"`,
49
+ transport: ENVIRONMENT.LOG_LEVEL === LOG_LEVELS.DEBUG.name ? {
50
+ target: "pino-pretty",
51
+ options: { colorize: true }
52
+ } : void 0
53
+ });
54
+ this.otelLogger = logs.getLogger(serviceName);
55
+ }
56
+ info(message, meta) {
57
+ if (this.enableConsole)
58
+ this.pinoLogger.info(meta || {}, message);
59
+ this.otelLogger.emit({
60
+ body: message,
61
+ severityNumber: LOG_LEVELS.INFO.severity,
62
+ attributes: meta
63
+ });
64
+ }
65
+ error(message, meta) {
66
+ if (this.enableConsole)
67
+ this.pinoLogger.error(meta || {}, message);
68
+ this.otelLogger.emit({
69
+ body: message,
70
+ severityNumber: LOG_LEVELS.ERROR.severity,
71
+ attributes: meta
72
+ });
73
+ }
74
+ warn(message, meta) {
75
+ if (this.enableConsole)
76
+ this.pinoLogger.warn(meta || {}, message);
77
+ this.otelLogger.emit({
78
+ body: message,
79
+ severityNumber: LOG_LEVELS.WARN.severity,
80
+ attributes: meta
81
+ });
82
+ }
83
+ debug(message, meta) {
84
+ if (this.enableConsole)
85
+ this.pinoLogger.debug(meta || {}, message);
86
+ if (ENVIRONMENT.LOG_LEVEL === LOG_LEVELS.DEBUG.name)
87
+ this.otelLogger.emit({
88
+ body: message,
89
+ severityNumber: LOG_LEVELS.DEBUG.severity,
90
+ attributes: meta
91
+ });
92
+ }
93
+ };
94
+ var loggerService_default = LoggerService;
95
+ var InstrumentationService = class {
96
+ constructor() {
97
+ this.enableOtlpTrace = ENVIRONMENT.ENABLE_OTLP_TRACE;
98
+ this.enableOtlpMetric = ENVIRONMENT.ENABLE_OTLP_METRIC;
99
+ this.enableOtlpLog = ENVIRONMENT.ENABLE_OTLP_LOG;
100
+ this.otlpTraceUrl = ENVIRONMENT.OTLP_TRACE_URL;
101
+ this.otlpMetricUrl = ENVIRONMENT.OTLP_METRIC_URL;
102
+ this.otlpLogUrl = ENVIRONMENT.OTLP_LOG_URL;
103
+ this.serviceName = ENVIRONMENT.QRVEY_SERVICE_NAME;
104
+ this.serviceVersion = ENVIRONMENT.QRVEY_SERVICE_VERSION;
105
+ this.exportMetricsIntervalMillis = ENVIRONMENT.EXPORT_METRICS_INTERVAL_MILLIS;
106
+ const sdkConfig = {
107
+ resource: resourceFromAttributes({
108
+ [ATTR_SERVICE_NAME]: this.serviceName,
109
+ [ATTR_SERVICE_VERSION]: this.serviceVersion
110
+ }),
111
+ instrumentations: [getNodeAutoInstrumentations()]
112
+ };
113
+ if (this.enableOtlpTrace) {
114
+ sdkConfig.traceExporter = new OTLPTraceExporter({
115
+ url: this.otlpTraceUrl,
116
+ headers: {
117
+ "Content-Type": "application/json"
118
+ }
119
+ });
120
+ }
121
+ if (this.enableOtlpMetric) {
122
+ sdkConfig.metricReader = new PeriodicExportingMetricReader({
123
+ exporter: new OTLPMetricExporter({
124
+ url: this.otlpMetricUrl
125
+ }),
126
+ exportIntervalMillis: this.exportMetricsIntervalMillis
127
+ // Export every 10 seconds
128
+ });
129
+ }
130
+ if (this.enableOtlpLog) {
131
+ sdkConfig.logRecordProcessor = new SimpleLogRecordProcessor(
132
+ new OTLPLogExporter({
133
+ url: this.otlpLogUrl
134
+ })
135
+ );
136
+ }
137
+ this.sdk = new NodeSDK(sdkConfig);
138
+ this.sdk.start();
139
+ }
140
+ async shutdown() {
141
+ await this.sdk.shutdown();
142
+ }
143
+ };
144
+ var instrumentationService_default = InstrumentationService;
145
+
146
+ export { instrumentationService_default as InstrumentationService, loggerService_default as LoggerService };
147
+ //# sourceMappingURL=out.js.map
148
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/lib/services/loggerService.ts","../../src/lib/constants.ts","../../src/lib/services/instrumentationService.ts"],"names":[],"mappings":";AAAA,SAA8C,YAAY;AAC1D,OAAO,UAAU;;;ACDV,IAAM,8BAA8B;AAEpC,IAAM,aAAa;AAAA,EACtB,OAAO,EAAE,MAAM,SAAS,UAAU,EAAE;AAAA,EACpC,MAAM,EAAE,MAAM,QAAQ,UAAU,EAAE;AAAA,EAClC,MAAM,EAAE,MAAM,QAAQ,UAAU,GAAG;AAAA,EACnC,OAAO,EAAE,MAAM,SAAS,UAAU,GAAG;AACzC;AAEO,IAAM,cAAc;AAAA,EACvB,WAAW,QAAQ,IAAI,aAAa,WAAW,MAAM;AAAA,EACrD,qBAAqB,QAAQ,IAAI,sBAAsB,QAAQ,QAAQ,IAAI,mBAAmB,IAAI;AAAA,EAClG,mBAAmB,QAAQ,IAAI,oBAAoB,QAAQ,QAAQ,IAAI,iBAAiB,IAAI;AAAA,EAC5F,oBAAoB,QAAQ,IAAI,qBAAqB,QAAQ,QAAQ,IAAI,kBAAkB,IAAI;AAAA,EAC/F,iBAAiB,QAAQ,IAAI,kBAAkB,QAAQ,QAAQ,IAAI,eAAe,IAAI;AAAA,EACtF,gBAAgB,QAAQ,IAAI,kBAAkB;AAAA,EAC9C,iBAAiB,QAAQ,IAAI,mBAAmB;AAAA,EAChD,cAAc,QAAQ,IAAI,gBAAgB;AAAA,EAC1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AAC;AAAA,IACL;AAEA,QAAI,KAAK,kBAAkB;AACvB,gBAAU,eAAe,IAAI,8BAA8B;AAAA,QACvD,UAAU,IAAI,mBAAmB;AAAA,UAC7B,KAAK,KAAK;AAAA,QACd,CAAC;AAAA,QACD,sBAAsB,KAAK;AAAA;AAAA,MAC/B,CAAC;AAAA,IACL;AAEA,QAAI,KAAK,eAAe;AACpB,gBAAU,qBAAqB,IAAI;AAAA,QAC/B,IAAI,gBAAgB;AAAA,UAChB,KAAK,KAAK;AAAA,QACd,CAAC;AAAA,MACL;AAAA,IACJ;AAEA,SAAK,MAAM,IAAI,QAAQ,SAAS;AAChC,SAAK,IAAI,MAAM;AAAA,EACnB;AAAA,EAEA,MAAa,WAA0B;AACnC,UAAM,KAAK,IAAI,SAAS;AAAA,EAC5B;AACJ;AAEA,IAAO,iCAAQ","sourcesContent":["import { LogAttributes, Logger as OtelLogger, logs } from '@opentelemetry/api-logs';\nimport pino from 'pino';\n\nimport { DEFAULT_LOGGER_SERVICE_NAME, ENVIRONMENT, LOG_LEVELS } from '../constants';\n\ntype PinoLogger = ReturnType<typeof pino>;\n\nclass LoggerService {\n private pinoLogger: PinoLogger;\n private otelLogger: OtelLogger;\n private enableConsole: boolean = ENVIRONMENT.ENABLE_CONSOLE_LOGS;\n\n constructor(serviceName = DEFAULT_LOGGER_SERVICE_NAME) {\n this.pinoLogger = pino({\n level: ENVIRONMENT.LOG_LEVEL,\n formatters: {\n level: (label) => {\n return { level: label.toUpperCase() };\n },\n },\n timestamp: () => `,\"timestamp\":\"${new Date(Date.now()).toISOString()}\"`,\n transport:\n ENVIRONMENT.LOG_LEVEL === LOG_LEVELS.DEBUG.name\n ? {\n target: 'pino-pretty',\n options: { colorize: true },\n }\n : undefined,\n });\n this.otelLogger = logs.getLogger(serviceName);\n }\n\n info(message: string, meta?: LogAttributes) {\n if (this.enableConsole) this.pinoLogger.info(meta || {}, message);\n this.otelLogger.emit({\n body: message,\n severityNumber: LOG_LEVELS.INFO.severity,\n attributes: meta,\n });\n }\n\n error(message: string, meta?: LogAttributes) {\n if (this.enableConsole) this.pinoLogger.error(meta || {}, message);\n this.otelLogger.emit({\n body: message,\n severityNumber: LOG_LEVELS.ERROR.severity,\n attributes: meta,\n });\n }\n\n warn(message: string, meta?: LogAttributes) {\n if (this.enableConsole) this.pinoLogger.warn(meta || {}, message);\n this.otelLogger.emit({\n body: message,\n severityNumber: LOG_LEVELS.WARN.severity,\n attributes: meta,\n });\n }\n\n debug(message: string, meta?: LogAttributes) {\n if (this.enableConsole) this.pinoLogger.debug(meta || {}, message);\n if (ENVIRONMENT.LOG_LEVEL === LOG_LEVELS.DEBUG.name)\n this.otelLogger.emit({\n body: message,\n severityNumber: LOG_LEVELS.DEBUG.severity,\n attributes: meta,\n });\n }\n}\n\nexport default LoggerService;\n","export const DEFAULT_LOGGER_SERVICE_NAME = 'pino-otel-service';\n\nexport const LOG_LEVELS = {\n DEBUG: { name: 'debug', severity: 5 },\n INFO: { name: 'info', severity: 9 },\n WARN: { name: 'warn', severity: 13 },\n ERROR: { name: 'error', severity: 17 },\n} as const;\n\nexport const ENVIRONMENT = {\n LOG_LEVEL: process.env.LOG_LEVEL || LOG_LEVELS.DEBUG.name,\n ENABLE_CONSOLE_LOGS: process.env.ENABLE_CONSOLE_LOGS ? Boolean(process.env.ENABLE_CONSOLE_LOGS) : true,\n ENABLE_OTLP_TRACE: process.env.ENABLE_OTLP_TRACE ? Boolean(process.env.ENABLE_OTLP_TRACE) : true,\n ENABLE_OTLP_METRIC: process.env.ENABLE_OTLP_METRIC ? Boolean(process.env.ENABLE_OTLP_METRIC) : true,\n ENABLE_OTLP_LOG: process.env.ENABLE_OTLP_LOG ? Boolean(process.env.ENABLE_OTLP_LOG) : true,\n OTLP_TRACE_URL: process.env.OTLP_TRACE_URL || 'http://localhost:4320/v1/traces',\n OTLP_METRIC_URL: process.env.OTLP_METRIC_URL || 'http://localhost:4318/v1/metrics',\n OTLP_LOG_URL: process.env.OTLP_LOG_URL || 'http://localhost:4320/v1/logs',\n QRVEY_SERVICE_NAME: process.env.QRVEY_SERVICE_NAME || 'Default-Service-name',\n QRVEY_SERVICE_VERSION: process.env.QRVEY_SERVICE_VERSION || '0.0.0',\n EXPORT_METRICS_INTERVAL_MILLIS: process.env.EXPORT_METRICS_INTERVAL_MILLIS ? parseInt(process.env.EXPORT_METRICS_INTERVAL_MILLIS, 10) : 10000,\n} as const;\n","import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';\nimport { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-proto';\nimport { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-proto';\nimport { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';\nimport { resourceFromAttributes } from '@opentelemetry/resources';\nimport { SimpleLogRecordProcessor } from '@opentelemetry/sdk-logs';\nimport { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';\nimport { NodeSDK } from '@opentelemetry/sdk-node';\nimport { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from '@opentelemetry/semantic-conventions';\n\nimport { ENVIRONMENT } from '../constants';\n\nclass InstrumentationService {\n private sdk: NodeSDK;\n\n private enableOtlpTrace: boolean = ENVIRONMENT.ENABLE_OTLP_TRACE;\n private enableOtlpMetric: boolean = ENVIRONMENT.ENABLE_OTLP_METRIC;\n private enableOtlpLog: boolean = ENVIRONMENT.ENABLE_OTLP_LOG;\n private otlpTraceUrl: string = ENVIRONMENT.OTLP_TRACE_URL;\n private otlpMetricUrl: string = ENVIRONMENT.OTLP_METRIC_URL;\n private otlpLogUrl: string = ENVIRONMENT.OTLP_LOG_URL;\n private serviceName: string = ENVIRONMENT.QRVEY_SERVICE_NAME;\n private serviceVersion: string = ENVIRONMENT.QRVEY_SERVICE_VERSION;\n private exportMetricsIntervalMillis: number = ENVIRONMENT.EXPORT_METRICS_INTERVAL_MILLIS;\n\n constructor() {\n const sdkConfig: any = {\n resource: resourceFromAttributes({\n [ATTR_SERVICE_NAME]: this.serviceName,\n [ATTR_SERVICE_VERSION]: this.serviceVersion,\n }),\n instrumentations: [getNodeAutoInstrumentations()],\n };\n\n if (this.enableOtlpTrace) {\n sdkConfig.traceExporter = new OTLPTraceExporter({\n url: this.otlpTraceUrl,\n headers: {\n 'Content-Type': 'application/json',\n },\n });\n }\n\n if (this.enableOtlpMetric) {\n sdkConfig.metricReader = new PeriodicExportingMetricReader({\n exporter: new OTLPMetricExporter({\n url: this.otlpMetricUrl,\n }),\n exportIntervalMillis: this.exportMetricsIntervalMillis, // Export every 10 seconds\n });\n }\n\n if (this.enableOtlpLog) {\n sdkConfig.logRecordProcessor = new SimpleLogRecordProcessor(\n new OTLPLogExporter({\n url: this.otlpLogUrl,\n }),\n );\n }\n\n this.sdk = new NodeSDK(sdkConfig);\n this.sdk.start();\n }\n\n public async shutdown(): Promise<void> {\n await this.sdk.shutdown();\n }\n}\n\nexport default InstrumentationService;\n"]}
@@ -0,0 +1,29 @@
1
+ import { LogAttributes } from '@opentelemetry/api-logs';
2
+
3
+ declare class LoggerService {
4
+ private pinoLogger;
5
+ private otelLogger;
6
+ private enableConsole;
7
+ constructor(serviceName?: string);
8
+ info(message: string, meta?: LogAttributes): void;
9
+ error(message: string, meta?: LogAttributes): void;
10
+ warn(message: string, meta?: LogAttributes): void;
11
+ debug(message: string, meta?: LogAttributes): void;
12
+ }
13
+
14
+ declare class InstrumentationService {
15
+ private sdk;
16
+ private enableOtlpTrace;
17
+ private enableOtlpMetric;
18
+ private enableOtlpLog;
19
+ private otlpTraceUrl;
20
+ private otlpMetricUrl;
21
+ private otlpLogUrl;
22
+ private serviceName;
23
+ private serviceVersion;
24
+ private exportMetricsIntervalMillis;
25
+ constructor();
26
+ shutdown(): Promise<void>;
27
+ }
28
+
29
+ export { InstrumentationService, LoggerService };
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@qrvey/telemetry",
3
+ "version": "0.0.1",
4
+ "types": "dist/types/index.d.ts",
5
+ "main": "dist/cjs/index.js",
6
+ "exports": {
7
+ ".": {
8
+ "import": "./dist/esm/index.mjs",
9
+ "require": "./dist/cjs/index.js",
10
+ "default": "./dist/cjs/index.js"
11
+ }
12
+ },
13
+ "scripts": {
14
+ "test": "echo \"Error: no test specified\" && exit 1",
15
+ "lint": "eslint \"{src,apps,libs,test}/**/*.ts\"",
16
+ "lint:fix": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
17
+ "type-check": "tsc",
18
+ "build:clean": "rm -rf dist",
19
+ "build:compile:original": "tsc -b ./tsconfig.cjs.json ./tsconfig.esm.json ./tsconfig.types.json",
20
+ "build:compile:cjs:original": "tsup --config ./tsup.config.cjs.ts",
21
+ "build:compile:cjs": "tsc -b ./tsconfig.cjs.json",
22
+ "build:compile:esm": "tsup --config ./tsup.config.esm.ts",
23
+ "build:compile:types": "tsup --config ./tsup.config.types.ts",
24
+ "build:compile": "yarn build:compile:cjs && yarn build:compile:esm && yarn build:compile:types",
25
+ "build": "yarn build:clean && yarn build:compile",
26
+ "prepare-publish": "yarn install && yarn build",
27
+ "publish-package": "yarn prepare-publish && npm publish"
28
+ },
29
+ "dependencies": {
30
+ "@opentelemetry/api-logs": "0.205.0",
31
+ "@opentelemetry/auto-instrumentations-node": "0.64.1",
32
+ "@opentelemetry/exporter-logs-otlp-proto": "0.205.0",
33
+ "@opentelemetry/exporter-metrics-otlp-proto": "0.205.0",
34
+ "@opentelemetry/exporter-trace-otlp-proto": "0.205.0",
35
+ "@opentelemetry/resources": "2.1.0",
36
+ "@opentelemetry/sdk-logs": "0.205.0",
37
+ "@opentelemetry/sdk-metrics": "2.1.0",
38
+ "@opentelemetry/sdk-node": "0.205.0",
39
+ "@opentelemetry/semantic-conventions": "1.37.0",
40
+ "pino": "9.11.0",
41
+ "pino-pretty": "13.1.1"
42
+ },
43
+ "devDependencies": {
44
+ "eslint": "8.48.0",
45
+ "eslint-import-resolver-typescript": "3.8.0",
46
+ "eslint-plugin-import": "2.31.0",
47
+ "tsup": "8.0.2",
48
+ "typescript": "5.9.2"
49
+ },
50
+ "keywords": [
51
+ "telemetry",
52
+ "logs",
53
+ "qrvey"
54
+ ],
55
+ "author": "Qrvey",
56
+ "license": "ISC",
57
+ "publishConfig": {
58
+ "access": "public",
59
+ "registry": "https://registry.npmjs.org"
60
+ }
61
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { default as LoggerService } from './lib/services/loggerService';
2
+ export { default as InstrumentationService } from './lib/services/instrumentationService';
@@ -0,0 +1,22 @@
1
+ export const DEFAULT_LOGGER_SERVICE_NAME = 'pino-otel-service';
2
+
3
+ export const LOG_LEVELS = {
4
+ DEBUG: { name: 'debug', severity: 5 },
5
+ INFO: { name: 'info', severity: 9 },
6
+ WARN: { name: 'warn', severity: 13 },
7
+ ERROR: { name: 'error', severity: 17 },
8
+ } as const;
9
+
10
+ export const ENVIRONMENT = {
11
+ LOG_LEVEL: process.env.LOG_LEVEL || LOG_LEVELS.DEBUG.name,
12
+ ENABLE_CONSOLE_LOGS: process.env.ENABLE_CONSOLE_LOGS ? Boolean(process.env.ENABLE_CONSOLE_LOGS) : true,
13
+ ENABLE_OTLP_TRACE: process.env.ENABLE_OTLP_TRACE ? Boolean(process.env.ENABLE_OTLP_TRACE) : true,
14
+ ENABLE_OTLP_METRIC: process.env.ENABLE_OTLP_METRIC ? Boolean(process.env.ENABLE_OTLP_METRIC) : true,
15
+ ENABLE_OTLP_LOG: process.env.ENABLE_OTLP_LOG ? Boolean(process.env.ENABLE_OTLP_LOG) : true,
16
+ OTLP_TRACE_URL: process.env.OTLP_TRACE_URL || 'http://localhost:4320/v1/traces',
17
+ OTLP_METRIC_URL: process.env.OTLP_METRIC_URL || 'http://localhost:4318/v1/metrics',
18
+ OTLP_LOG_URL: process.env.OTLP_LOG_URL || 'http://localhost:4320/v1/logs',
19
+ QRVEY_SERVICE_NAME: process.env.QRVEY_SERVICE_NAME || 'Default-Service-name',
20
+ QRVEY_SERVICE_VERSION: process.env.QRVEY_SERVICE_VERSION || '0.0.0',
21
+ EXPORT_METRICS_INTERVAL_MILLIS: process.env.EXPORT_METRICS_INTERVAL_MILLIS ? parseInt(process.env.EXPORT_METRICS_INTERVAL_MILLIS, 10) : 10000,
22
+ } as const;
@@ -0,0 +1,70 @@
1
+ import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
2
+ import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-proto';
3
+ import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-proto';
4
+ import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
5
+ import { resourceFromAttributes } from '@opentelemetry/resources';
6
+ import { SimpleLogRecordProcessor } from '@opentelemetry/sdk-logs';
7
+ import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
8
+ import { NodeSDK } from '@opentelemetry/sdk-node';
9
+ import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from '@opentelemetry/semantic-conventions';
10
+
11
+ import { ENVIRONMENT } from '../constants';
12
+
13
+ class InstrumentationService {
14
+ private sdk: NodeSDK;
15
+
16
+ private enableOtlpTrace: boolean = ENVIRONMENT.ENABLE_OTLP_TRACE;
17
+ private enableOtlpMetric: boolean = ENVIRONMENT.ENABLE_OTLP_METRIC;
18
+ private enableOtlpLog: boolean = ENVIRONMENT.ENABLE_OTLP_LOG;
19
+ private otlpTraceUrl: string = ENVIRONMENT.OTLP_TRACE_URL;
20
+ private otlpMetricUrl: string = ENVIRONMENT.OTLP_METRIC_URL;
21
+ private otlpLogUrl: string = ENVIRONMENT.OTLP_LOG_URL;
22
+ private serviceName: string = ENVIRONMENT.QRVEY_SERVICE_NAME;
23
+ private serviceVersion: string = ENVIRONMENT.QRVEY_SERVICE_VERSION;
24
+ private exportMetricsIntervalMillis: number = ENVIRONMENT.EXPORT_METRICS_INTERVAL_MILLIS;
25
+
26
+ constructor() {
27
+ const sdkConfig: any = {
28
+ resource: resourceFromAttributes({
29
+ [ATTR_SERVICE_NAME]: this.serviceName,
30
+ [ATTR_SERVICE_VERSION]: this.serviceVersion,
31
+ }),
32
+ instrumentations: [getNodeAutoInstrumentations()],
33
+ };
34
+
35
+ if (this.enableOtlpTrace) {
36
+ sdkConfig.traceExporter = new OTLPTraceExporter({
37
+ url: this.otlpTraceUrl,
38
+ headers: {
39
+ 'Content-Type': 'application/json',
40
+ },
41
+ });
42
+ }
43
+
44
+ if (this.enableOtlpMetric) {
45
+ sdkConfig.metricReader = new PeriodicExportingMetricReader({
46
+ exporter: new OTLPMetricExporter({
47
+ url: this.otlpMetricUrl,
48
+ }),
49
+ exportIntervalMillis: this.exportMetricsIntervalMillis, // Export every 10 seconds
50
+ });
51
+ }
52
+
53
+ if (this.enableOtlpLog) {
54
+ sdkConfig.logRecordProcessor = new SimpleLogRecordProcessor(
55
+ new OTLPLogExporter({
56
+ url: this.otlpLogUrl,
57
+ }),
58
+ );
59
+ }
60
+
61
+ this.sdk = new NodeSDK(sdkConfig);
62
+ this.sdk.start();
63
+ }
64
+
65
+ public async shutdown(): Promise<void> {
66
+ await this.sdk.shutdown();
67
+ }
68
+ }
69
+
70
+ export default InstrumentationService;
@@ -0,0 +1,71 @@
1
+ import { LogAttributes, Logger as OtelLogger, logs } from '@opentelemetry/api-logs';
2
+ import pino from 'pino';
3
+
4
+ import { DEFAULT_LOGGER_SERVICE_NAME, ENVIRONMENT, LOG_LEVELS } from '../constants';
5
+
6
+ type PinoLogger = ReturnType<typeof pino>;
7
+
8
+ class LoggerService {
9
+ private pinoLogger: PinoLogger;
10
+ private otelLogger: OtelLogger;
11
+ private enableConsole: boolean = ENVIRONMENT.ENABLE_CONSOLE_LOGS;
12
+
13
+ constructor(serviceName = DEFAULT_LOGGER_SERVICE_NAME) {
14
+ this.pinoLogger = pino({
15
+ level: ENVIRONMENT.LOG_LEVEL,
16
+ formatters: {
17
+ level: (label) => {
18
+ return { level: label.toUpperCase() };
19
+ },
20
+ },
21
+ timestamp: () => `,"timestamp":"${new Date(Date.now()).toISOString()}"`,
22
+ transport:
23
+ ENVIRONMENT.LOG_LEVEL === LOG_LEVELS.DEBUG.name
24
+ ? {
25
+ target: 'pino-pretty',
26
+ options: { colorize: true },
27
+ }
28
+ : undefined,
29
+ });
30
+ this.otelLogger = logs.getLogger(serviceName);
31
+ }
32
+
33
+ info(message: string, meta?: LogAttributes) {
34
+ if (this.enableConsole) this.pinoLogger.info(meta || {}, message);
35
+ this.otelLogger.emit({
36
+ body: message,
37
+ severityNumber: LOG_LEVELS.INFO.severity,
38
+ attributes: meta,
39
+ });
40
+ }
41
+
42
+ error(message: string, meta?: LogAttributes) {
43
+ if (this.enableConsole) this.pinoLogger.error(meta || {}, message);
44
+ this.otelLogger.emit({
45
+ body: message,
46
+ severityNumber: LOG_LEVELS.ERROR.severity,
47
+ attributes: meta,
48
+ });
49
+ }
50
+
51
+ warn(message: string, meta?: LogAttributes) {
52
+ if (this.enableConsole) this.pinoLogger.warn(meta || {}, message);
53
+ this.otelLogger.emit({
54
+ body: message,
55
+ severityNumber: LOG_LEVELS.WARN.severity,
56
+ attributes: meta,
57
+ });
58
+ }
59
+
60
+ debug(message: string, meta?: LogAttributes) {
61
+ if (this.enableConsole) this.pinoLogger.debug(meta || {}, message);
62
+ if (ENVIRONMENT.LOG_LEVEL === LOG_LEVELS.DEBUG.name)
63
+ this.otelLogger.emit({
64
+ body: message,
65
+ severityNumber: LOG_LEVELS.DEBUG.severity,
66
+ attributes: meta,
67
+ });
68
+ }
69
+ }
70
+
71
+ export default LoggerService;
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist/cjs",
5
+ "module": "commonjs"
6
+ }
7
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "extends": "tsconfig/tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist"
5
+ },
6
+ "include": [
7
+ "src/index.ts"
8
+ ],
9
+ "exclude": [
10
+ "dist",
11
+ "build",
12
+ "node_modules",
13
+ "coverage",
14
+ "**/*.test.ts",
15
+ "**/*.test.d.ts",
16
+ "**/*.spec.ts",
17
+ ]
18
+ }
@@ -0,0 +1,11 @@
1
+ import { defineConfig } from 'tsup';
2
+
3
+ export default defineConfig({
4
+ entryPoints: ['./src/index.ts'],
5
+ bundle: true,
6
+ minify: false,
7
+ format: ['cjs'],
8
+ treeshake: true,
9
+ sourcemap: true,
10
+ outDir: 'dist/cjs'
11
+ });
@@ -0,0 +1,12 @@
1
+ import { defineConfig } from 'tsup';
2
+
3
+ export default defineConfig({
4
+ entryPoints: ['./src/index.ts'],
5
+ bundle: true,
6
+ minify: false,
7
+ format: ['esm'],
8
+ treeshake: true,
9
+ sourcemap: true,
10
+ dts: false,
11
+ outDir: 'dist/esm',
12
+ });
@@ -0,0 +1,9 @@
1
+ import { defineConfig } from 'tsup';
2
+
3
+ export default defineConfig({
4
+ entryPoints: ['./src/index.ts'],
5
+ dts: {
6
+ only: true,
7
+ },
8
+ outDir: 'dist/types',
9
+ });