@qrvey/telemetry 0.0.1 → 0.0.3
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/README.md +2 -2
- package/__tests__/loggerService.test.ts +336 -0
- package/dist/cjs/lib/constants.js +2 -2
- package/dist/cjs/lib/constants.js.map +1 -1
- package/dist/cjs/lib/services/loggerService.js +55 -29
- package/dist/cjs/lib/services/loggerService.js.map +1 -1
- package/dist/esm/index.mjs +57 -32
- package/dist/esm/index.mjs.map +1 -1
- package/dist/types/index.d.ts +39 -4
- package/jest.config.ts +15 -0
- package/package.json +7 -2
- package/src/lib/constants.ts +1 -1
- package/src/lib/services/loggerService.ts +63 -35
package/README.md
CHANGED
|
@@ -30,8 +30,8 @@ const { InstrumentationService, LoggerService } = require("@qrvey/telemetry");
|
|
|
30
30
|
// Initialize instrumentation
|
|
31
31
|
new InstrumentationService();
|
|
32
32
|
|
|
33
|
-
// Initialize logger
|
|
34
|
-
const logger = new LoggerService();
|
|
33
|
+
// Initialize logger (optional name for context)
|
|
34
|
+
const logger = new LoggerService('your_context_name');
|
|
35
35
|
|
|
36
36
|
logger.info("this is info message");
|
|
37
37
|
logger.debug("this is a debug message");
|
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import { LogAttributes, logs } from '@opentelemetry/api-logs';
|
|
3
|
+
import * as pino from 'pino';
|
|
4
|
+
|
|
5
|
+
import { DEFAULT_CONTEXT_SCOPE_NAME, ENVIRONMENT, LOG_LEVELS } from '../src/lib/constants';
|
|
6
|
+
import LoggerService from '../src/lib/services/loggerService';
|
|
7
|
+
|
|
8
|
+
// Mock dependencies
|
|
9
|
+
jest.mock('pino');
|
|
10
|
+
jest.mock('@opentelemetry/api-logs');
|
|
11
|
+
jest.mock('../src/lib/constants', () => ({
|
|
12
|
+
DEFAULT_CONTEXT_SCOPE_NAME: 'test-service',
|
|
13
|
+
ENVIRONMENT: {
|
|
14
|
+
LOG_LEVEL: 'info',
|
|
15
|
+
ENABLE_CONSOLE_LOGS: true,
|
|
16
|
+
},
|
|
17
|
+
LOG_LEVELS: {
|
|
18
|
+
DEBUG: { name: 'debug', severity: 5 },
|
|
19
|
+
INFO: { name: 'info', severity: 9 },
|
|
20
|
+
WARN: { name: 'warn', severity: 13 },
|
|
21
|
+
ERROR: { name: 'error', severity: 17 },
|
|
22
|
+
},
|
|
23
|
+
}));
|
|
24
|
+
|
|
25
|
+
describe('LoggerService', () => {
|
|
26
|
+
let loggerService: LoggerService;
|
|
27
|
+
let mockPinoLogger: any;
|
|
28
|
+
let mockOtelLogger: any;
|
|
29
|
+
|
|
30
|
+
const mockedPino = pino.default as jest.MockedFunction<any>;
|
|
31
|
+
const mockedLogs = logs as jest.Mocked<typeof logs>;
|
|
32
|
+
|
|
33
|
+
beforeEach(() => {
|
|
34
|
+
jest.clearAllMocks();
|
|
35
|
+
|
|
36
|
+
// Mock pino logger methods
|
|
37
|
+
mockPinoLogger = {
|
|
38
|
+
debug: jest.fn(),
|
|
39
|
+
info: jest.fn(),
|
|
40
|
+
warn: jest.fn(),
|
|
41
|
+
error: jest.fn(),
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// Mock OpenTelemetry logger
|
|
45
|
+
mockOtelLogger = {
|
|
46
|
+
emit: jest.fn(),
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
mockedPino.mockReturnValue(mockPinoLogger);
|
|
50
|
+
mockedLogs.getLogger.mockReturnValue(mockOtelLogger);
|
|
51
|
+
|
|
52
|
+
loggerService = new LoggerService();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe('constructor', () => {
|
|
56
|
+
it('should create instance with default service name', () => {
|
|
57
|
+
const service = new LoggerService();
|
|
58
|
+
expect(service).toBeInstanceOf(LoggerService);
|
|
59
|
+
expect(mockedLogs.getLogger).toHaveBeenCalledWith(DEFAULT_CONTEXT_SCOPE_NAME);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('should create instance with custom service name', () => {
|
|
63
|
+
const customServiceName = 'custom-service';
|
|
64
|
+
const service = new LoggerService(customServiceName);
|
|
65
|
+
expect(service).toBeInstanceOf(LoggerService);
|
|
66
|
+
expect(mockedLogs.getLogger).toHaveBeenCalledWith(customServiceName);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('should configure pino logger correctly', () => {
|
|
70
|
+
new LoggerService();
|
|
71
|
+
expect(mockedPino).toHaveBeenCalledWith({
|
|
72
|
+
level: ENVIRONMENT.LOG_LEVEL,
|
|
73
|
+
formatters: {
|
|
74
|
+
level: expect.any(Function),
|
|
75
|
+
},
|
|
76
|
+
timestamp: expect.any(Function),
|
|
77
|
+
transport: undefined,
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
describe('console logging when enabled', () => {
|
|
83
|
+
beforeEach(() => {
|
|
84
|
+
// Ensure console logging is enabled
|
|
85
|
+
(loggerService as any).enableConsole = true;
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
describe('debug method', () => {
|
|
89
|
+
it('should call pino debug with message and meta', () => {
|
|
90
|
+
const message = 'Debug message';
|
|
91
|
+
const meta = { userId: '123', action: 'debug' };
|
|
92
|
+
|
|
93
|
+
loggerService.debug(message, meta);
|
|
94
|
+
|
|
95
|
+
expect(mockPinoLogger.debug).toHaveBeenCalledWith(meta, message);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('should call pino debug with only message when no meta provided', () => {
|
|
99
|
+
const message = 'Debug message';
|
|
100
|
+
|
|
101
|
+
loggerService.debug(message);
|
|
102
|
+
|
|
103
|
+
expect(mockPinoLogger.debug).toHaveBeenCalledWith(undefined, message);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('should convert string meta to object with data property', () => {
|
|
107
|
+
const message = 'Debug message';
|
|
108
|
+
const meta = 'string metadata' as any;
|
|
109
|
+
|
|
110
|
+
loggerService.debug(message, meta);
|
|
111
|
+
|
|
112
|
+
expect(mockPinoLogger.debug).toHaveBeenCalledWith({ data: meta }, message);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('should not emit to OpenTelemetry when LOG_LEVEL is not debug', () => {
|
|
116
|
+
const message = 'Debug message';
|
|
117
|
+
const meta = { test: true };
|
|
118
|
+
|
|
119
|
+
loggerService.debug(message, meta);
|
|
120
|
+
|
|
121
|
+
// Debug should not emit to OpenTelemetry when LOG_LEVEL is 'info'
|
|
122
|
+
expect(mockOtelLogger.emit).not.toHaveBeenCalled();
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('should test debug method calls console logger correctly', () => {
|
|
126
|
+
const message = 'Debug message';
|
|
127
|
+
const meta = { test: true };
|
|
128
|
+
|
|
129
|
+
loggerService.debug(message, meta);
|
|
130
|
+
|
|
131
|
+
// Verify that console logger was called
|
|
132
|
+
expect(mockPinoLogger.debug).toHaveBeenCalledWith(meta, message);
|
|
133
|
+
// Debug should not emit to OpenTelemetry when LOG_LEVEL is 'info'
|
|
134
|
+
expect(mockOtelLogger.emit).not.toHaveBeenCalled();
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
describe('info method', () => {
|
|
139
|
+
it('should call pino info with message and meta', () => {
|
|
140
|
+
const message = 'Info message';
|
|
141
|
+
const meta = { userId: '123', action: 'info' };
|
|
142
|
+
|
|
143
|
+
loggerService.info(message, meta);
|
|
144
|
+
|
|
145
|
+
expect(mockPinoLogger.info).toHaveBeenCalledWith(meta, message);
|
|
146
|
+
expect(mockOtelLogger.emit).toHaveBeenCalledWith({
|
|
147
|
+
body: message,
|
|
148
|
+
severityNumber: LOG_LEVELS.INFO.severity,
|
|
149
|
+
attributes: meta as LogAttributes,
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('should handle null meta', () => {
|
|
154
|
+
const message = 'Info message';
|
|
155
|
+
const meta = null as any;
|
|
156
|
+
|
|
157
|
+
loggerService.info(message, meta);
|
|
158
|
+
|
|
159
|
+
expect(mockPinoLogger.info).toHaveBeenCalledWith(meta, message);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('should handle undefined meta', () => {
|
|
163
|
+
const message = 'Info message';
|
|
164
|
+
|
|
165
|
+
loggerService.info(message, undefined);
|
|
166
|
+
|
|
167
|
+
expect(mockPinoLogger.info).toHaveBeenCalledWith(undefined, message);
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
describe('warn method', () => {
|
|
172
|
+
it('should call pino warn with message and meta', () => {
|
|
173
|
+
const message = 'Warning message';
|
|
174
|
+
const meta = { warning: true };
|
|
175
|
+
|
|
176
|
+
loggerService.warn(message, meta);
|
|
177
|
+
|
|
178
|
+
expect(mockPinoLogger.warn).toHaveBeenCalledWith(meta, message);
|
|
179
|
+
expect(mockOtelLogger.emit).toHaveBeenCalledWith({
|
|
180
|
+
body: message,
|
|
181
|
+
severityNumber: LOG_LEVELS.WARN.severity,
|
|
182
|
+
attributes: meta as LogAttributes,
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it('should convert string meta to object', () => {
|
|
187
|
+
const message = 'Warning message';
|
|
188
|
+
const meta = 'warning data' as any;
|
|
189
|
+
|
|
190
|
+
loggerService.warn(message, meta);
|
|
191
|
+
|
|
192
|
+
expect(mockPinoLogger.warn).toHaveBeenCalledWith({ data: meta }, message);
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
describe('error method', () => {
|
|
197
|
+
it('should call pino error with message and meta', () => {
|
|
198
|
+
const message = 'Error message';
|
|
199
|
+
const meta = { error: true, stack: 'error stack' };
|
|
200
|
+
|
|
201
|
+
loggerService.error(message, meta);
|
|
202
|
+
|
|
203
|
+
expect(mockPinoLogger.error).toHaveBeenCalledWith(meta, message);
|
|
204
|
+
expect(mockOtelLogger.emit).toHaveBeenCalledWith({
|
|
205
|
+
body: message,
|
|
206
|
+
severityNumber: LOG_LEVELS.ERROR.severity,
|
|
207
|
+
attributes: meta as LogAttributes,
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
describe('console logging when disabled', () => {
|
|
214
|
+
beforeEach(() => {
|
|
215
|
+
// Disable console logging
|
|
216
|
+
(loggerService as any).enableConsole = false;
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('should not call pino logger when console logging is disabled for info', () => {
|
|
220
|
+
const message = 'Test info message';
|
|
221
|
+
const meta = { userId: '123' };
|
|
222
|
+
|
|
223
|
+
loggerService.info(message, meta);
|
|
224
|
+
|
|
225
|
+
expect(mockPinoLogger.info).not.toHaveBeenCalled();
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it('should not call pino logger when console logging is disabled for debug', () => {
|
|
229
|
+
const message = 'Debug message';
|
|
230
|
+
|
|
231
|
+
loggerService.debug(message);
|
|
232
|
+
|
|
233
|
+
expect(mockPinoLogger.debug).not.toHaveBeenCalled();
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('should not call pino logger when console logging is disabled for warn', () => {
|
|
237
|
+
const message = 'Warning message';
|
|
238
|
+
|
|
239
|
+
loggerService.warn(message);
|
|
240
|
+
|
|
241
|
+
expect(mockPinoLogger.warn).not.toHaveBeenCalled();
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('should not call pino logger when console logging is disabled for error', () => {
|
|
245
|
+
const message = 'Error message';
|
|
246
|
+
|
|
247
|
+
loggerService.error(message);
|
|
248
|
+
|
|
249
|
+
expect(mockPinoLogger.error).not.toHaveBeenCalled();
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it('should still call OpenTelemetry logger even when console logging is disabled', () => {
|
|
253
|
+
const message = 'Test info message';
|
|
254
|
+
const meta = { userId: '123' };
|
|
255
|
+
|
|
256
|
+
loggerService.info(message, meta);
|
|
257
|
+
|
|
258
|
+
expect(mockOtelLogger.emit).toHaveBeenCalledWith({
|
|
259
|
+
body: message,
|
|
260
|
+
severityNumber: LOG_LEVELS.INFO.severity,
|
|
261
|
+
attributes: meta,
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
describe('metadata handling edge cases', () => {
|
|
267
|
+
beforeEach(() => {
|
|
268
|
+
(loggerService as any).enableConsole = true;
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it('should handle empty string meta without conversion', () => {
|
|
272
|
+
const message = 'Test message';
|
|
273
|
+
const meta = '' as any;
|
|
274
|
+
|
|
275
|
+
loggerService.info(message, meta);
|
|
276
|
+
|
|
277
|
+
// Empty string is falsy, so it won't be converted to { data: meta }
|
|
278
|
+
expect(mockPinoLogger.info).toHaveBeenCalledWith(meta, message);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it('should handle object meta without conversion', () => {
|
|
282
|
+
const message = 'Test message';
|
|
283
|
+
const meta = { key: 'value', nested: { prop: 'test' } };
|
|
284
|
+
|
|
285
|
+
loggerService.info(message, meta);
|
|
286
|
+
|
|
287
|
+
expect(mockPinoLogger.info).toHaveBeenCalledWith(meta, message);
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it('should handle array meta', () => {
|
|
291
|
+
const message = 'Test message';
|
|
292
|
+
const meta = ['item1', 'item2'] as any;
|
|
293
|
+
|
|
294
|
+
loggerService.info(message, meta);
|
|
295
|
+
|
|
296
|
+
expect(mockPinoLogger.info).toHaveBeenCalledWith(meta, message);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it('should handle number meta as data object', () => {
|
|
300
|
+
const message = 'Test message';
|
|
301
|
+
const meta = 123 as any;
|
|
302
|
+
|
|
303
|
+
loggerService.info(message, meta);
|
|
304
|
+
|
|
305
|
+
expect(mockPinoLogger.info).toHaveBeenCalledWith(meta, message);
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
describe('OpenTelemetry integration', () => {
|
|
310
|
+
it('should emit logs to OpenTelemetry with correct severity levels', () => {
|
|
311
|
+
const message = 'Test message';
|
|
312
|
+
const meta = { test: true };
|
|
313
|
+
|
|
314
|
+
loggerService.info(message, meta);
|
|
315
|
+
loggerService.warn(message, meta);
|
|
316
|
+
loggerService.error(message, meta);
|
|
317
|
+
|
|
318
|
+
expect(mockOtelLogger.emit).toHaveBeenCalledTimes(3);
|
|
319
|
+
expect(mockOtelLogger.emit).toHaveBeenNthCalledWith(1, {
|
|
320
|
+
body: message,
|
|
321
|
+
severityNumber: LOG_LEVELS.INFO.severity,
|
|
322
|
+
attributes: meta,
|
|
323
|
+
});
|
|
324
|
+
expect(mockOtelLogger.emit).toHaveBeenNthCalledWith(2, {
|
|
325
|
+
body: message,
|
|
326
|
+
severityNumber: LOG_LEVELS.WARN.severity,
|
|
327
|
+
attributes: meta,
|
|
328
|
+
});
|
|
329
|
+
expect(mockOtelLogger.emit).toHaveBeenNthCalledWith(3, {
|
|
330
|
+
body: message,
|
|
331
|
+
severityNumber: LOG_LEVELS.ERROR.severity,
|
|
332
|
+
attributes: meta,
|
|
333
|
+
});
|
|
334
|
+
});
|
|
335
|
+
});
|
|
336
|
+
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ENVIRONMENT = exports.LOG_LEVELS = exports.
|
|
4
|
-
exports.
|
|
3
|
+
exports.ENVIRONMENT = exports.LOG_LEVELS = exports.DEFAULT_CONTEXT_SCOPE_NAME = void 0;
|
|
4
|
+
exports.DEFAULT_CONTEXT_SCOPE_NAME = 'context_scope_name';
|
|
5
5
|
exports.LOG_LEVELS = {
|
|
6
6
|
DEBUG: { name: 'debug', severity: 5 },
|
|
7
7
|
INFO: { name: 'info', severity: 9 },
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/lib/constants.ts"],"names":[],"mappings":";;;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AAO,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"}
|
|
@@ -7,7 +7,7 @@ const api_logs_1 = require("@opentelemetry/api-logs");
|
|
|
7
7
|
const pino_1 = __importDefault(require("pino"));
|
|
8
8
|
const constants_1 = require("../constants");
|
|
9
9
|
class LoggerService {
|
|
10
|
-
constructor(serviceName = constants_1.
|
|
10
|
+
constructor(serviceName = constants_1.DEFAULT_CONTEXT_SCOPE_NAME) {
|
|
11
11
|
this.enableConsole = constants_1.ENVIRONMENT.ENABLE_CONSOLE_LOGS;
|
|
12
12
|
this.pinoLogger = (0, pino_1.default)({
|
|
13
13
|
level: constants_1.ENVIRONMENT.LOG_LEVEL,
|
|
@@ -26,43 +26,69 @@ class LoggerService {
|
|
|
26
26
|
});
|
|
27
27
|
this.otelLogger = api_logs_1.logs.getLogger(serviceName);
|
|
28
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* Log a debug message, use this as a `console.log` replacement.
|
|
31
|
+
* @param message - The log message
|
|
32
|
+
* @param meta - Optional additional arguments to include in the log
|
|
33
|
+
*/
|
|
34
|
+
debug(message, meta) {
|
|
35
|
+
this.consoleLogger(this.pinoLogger.debug.bind(this.pinoLogger), message, meta);
|
|
36
|
+
if (constants_1.ENVIRONMENT.LOG_LEVEL === constants_1.LOG_LEVELS.DEBUG.name)
|
|
37
|
+
this.emitterLogger(constants_1.LOG_LEVELS.DEBUG.severity, message, meta);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Log an informational message
|
|
41
|
+
* @param message - The log message
|
|
42
|
+
* @param meta - Optional additional arguments to include in the log
|
|
43
|
+
*/
|
|
29
44
|
info(message, meta) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
45
|
+
this.consoleLogger(this.pinoLogger.info.bind(this.pinoLogger), message, meta);
|
|
46
|
+
this.emitterLogger(constants_1.LOG_LEVELS.INFO.severity, message, meta);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Log a warning message
|
|
50
|
+
* @param message - The log message
|
|
51
|
+
* @param meta - Optional additional arguments to include in the log
|
|
52
|
+
*/
|
|
53
|
+
warn(message, meta) {
|
|
54
|
+
this.consoleLogger(this.pinoLogger.warn.bind(this.pinoLogger), message, meta);
|
|
55
|
+
this.emitterLogger(constants_1.LOG_LEVELS.WARN.severity, message, meta);
|
|
37
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Log an error message
|
|
59
|
+
* @param message - The log message
|
|
60
|
+
* @param meta - Optional additional arguments to include in the log
|
|
61
|
+
*/
|
|
38
62
|
error(message, meta) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
this.otelLogger.emit({
|
|
42
|
-
body: message,
|
|
43
|
-
severityNumber: constants_1.LOG_LEVELS.ERROR.severity,
|
|
44
|
-
attributes: meta,
|
|
45
|
-
});
|
|
63
|
+
this.consoleLogger(this.pinoLogger.error.bind(this.pinoLogger), message, meta);
|
|
64
|
+
this.emitterLogger(constants_1.LOG_LEVELS.ERROR.severity, message, meta);
|
|
46
65
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
66
|
+
/**
|
|
67
|
+
* Simplify the logger applying any configuration in a single place
|
|
68
|
+
* @param logger - Log function from pino (e.g., `this.pinoLogger.info.bind(this.pinoLogger)`)
|
|
69
|
+
* @param message - The log message
|
|
70
|
+
* @param meta - Optional additional arguments to include in the log
|
|
71
|
+
*/
|
|
72
|
+
consoleLogger(logger, message, meta) {
|
|
73
|
+
if (!this.enableConsole)
|
|
74
|
+
return;
|
|
75
|
+
if (meta && typeof meta === 'string' && message && typeof message === 'string')
|
|
76
|
+
meta = { data: meta };
|
|
77
|
+
logger(meta, message);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Emit a log message to OpenTelemetry
|
|
81
|
+
* @param severityNumber - The severity number of the log
|
|
82
|
+
* @param message - The log message
|
|
83
|
+
* @param meta - Optional additional arguments to include in the log
|
|
84
|
+
*/
|
|
85
|
+
emitterLogger(severityNumber, message, meta) {
|
|
50
86
|
this.otelLogger.emit({
|
|
51
87
|
body: message,
|
|
52
|
-
severityNumber
|
|
88
|
+
severityNumber,
|
|
53
89
|
attributes: meta,
|
|
54
90
|
});
|
|
55
91
|
}
|
|
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
92
|
}
|
|
67
93
|
exports.default = LoggerService;
|
|
68
94
|
//# sourceMappingURL=loggerService.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loggerService.js","sourceRoot":"","sources":["../../../../src/lib/services/loggerService.ts"],"names":[],"mappings":";;;;;AAAA,sDAAoF;AACpF,
|
|
1
|
+
{"version":3,"file":"loggerService.js","sourceRoot":"","sources":["../../../../src/lib/services/loggerService.ts"],"names":[],"mappings":";;;;;AAAA,sDAAoF;AACpF,gDAAyD;AAEzD,4CAAmF;AAInF,MAAqB,aAAa;I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AAc;YACd,UAAU,EAAE,IAAqB;SACpC,CAAC,CAAC;IACP,CAAC;CACJ;AA3FD,gCA2FC"}
|
package/dist/esm/index.mjs
CHANGED
|
@@ -13,7 +13,7 @@ import { ATTR_SERVICE_VERSION, ATTR_SERVICE_NAME } from '@opentelemetry/semantic
|
|
|
13
13
|
// src/lib/services/loggerService.ts
|
|
14
14
|
|
|
15
15
|
// src/lib/constants.ts
|
|
16
|
-
var
|
|
16
|
+
var DEFAULT_CONTEXT_SCOPE_NAME = "context_scope_name";
|
|
17
17
|
var LOG_LEVELS = {
|
|
18
18
|
DEBUG: { name: "debug", severity: 5 },
|
|
19
19
|
INFO: { severity: 9 },
|
|
@@ -36,7 +36,7 @@ var ENVIRONMENT = {
|
|
|
36
36
|
|
|
37
37
|
// src/lib/services/loggerService.ts
|
|
38
38
|
var LoggerService = class {
|
|
39
|
-
constructor(serviceName =
|
|
39
|
+
constructor(serviceName = DEFAULT_CONTEXT_SCOPE_NAME) {
|
|
40
40
|
this.enableConsole = ENVIRONMENT.ENABLE_CONSOLE_LOGS;
|
|
41
41
|
this.pinoLogger = pino({
|
|
42
42
|
level: ENVIRONMENT.LOG_LEVEL,
|
|
@@ -53,45 +53,70 @@ var LoggerService = class {
|
|
|
53
53
|
});
|
|
54
54
|
this.otelLogger = logs.getLogger(serviceName);
|
|
55
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* Log a debug message, use this as a `console.log` replacement.
|
|
58
|
+
* @param message - The log message
|
|
59
|
+
* @param meta - Optional additional arguments to include in the log
|
|
60
|
+
*/
|
|
61
|
+
debug(message, meta) {
|
|
62
|
+
this.consoleLogger(this.pinoLogger.debug.bind(this.pinoLogger), message, meta);
|
|
63
|
+
if (ENVIRONMENT.LOG_LEVEL === LOG_LEVELS.DEBUG.name)
|
|
64
|
+
this.emitterLogger(LOG_LEVELS.DEBUG.severity, message, meta);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Log an informational message
|
|
68
|
+
* @param message - The log message
|
|
69
|
+
* @param meta - Optional additional arguments to include in the log
|
|
70
|
+
*/
|
|
56
71
|
info(message, meta) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
this.otelLogger.emit({
|
|
60
|
-
body: message,
|
|
61
|
-
severityNumber: LOG_LEVELS.INFO.severity,
|
|
62
|
-
attributes: meta
|
|
63
|
-
});
|
|
72
|
+
this.consoleLogger(this.pinoLogger.info.bind(this.pinoLogger), message, meta);
|
|
73
|
+
this.emitterLogger(LOG_LEVELS.INFO.severity, message, meta);
|
|
64
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* Log a warning message
|
|
77
|
+
* @param message - The log message
|
|
78
|
+
* @param meta - Optional additional arguments to include in the log
|
|
79
|
+
*/
|
|
80
|
+
warn(message, meta) {
|
|
81
|
+
this.consoleLogger(this.pinoLogger.warn.bind(this.pinoLogger), message, meta);
|
|
82
|
+
this.emitterLogger(LOG_LEVELS.WARN.severity, message, meta);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Log an error message
|
|
86
|
+
* @param message - The log message
|
|
87
|
+
* @param meta - Optional additional arguments to include in the log
|
|
88
|
+
*/
|
|
65
89
|
error(message, meta) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
this.otelLogger.emit({
|
|
69
|
-
body: message,
|
|
70
|
-
severityNumber: LOG_LEVELS.ERROR.severity,
|
|
71
|
-
attributes: meta
|
|
72
|
-
});
|
|
90
|
+
this.consoleLogger(this.pinoLogger.error.bind(this.pinoLogger), message, meta);
|
|
91
|
+
this.emitterLogger(LOG_LEVELS.ERROR.severity, message, meta);
|
|
73
92
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
93
|
+
/**
|
|
94
|
+
* Simplify the logger applying any configuration in a single place
|
|
95
|
+
* @param logger - Log function from pino (e.g., `this.pinoLogger.info.bind(this.pinoLogger)`)
|
|
96
|
+
* @param message - The log message
|
|
97
|
+
* @param meta - Optional additional arguments to include in the log
|
|
98
|
+
*/
|
|
99
|
+
consoleLogger(logger, message, meta) {
|
|
100
|
+
if (!this.enableConsole)
|
|
101
|
+
return;
|
|
102
|
+
if (meta && typeof meta === "string" && message && typeof message === "string")
|
|
103
|
+
meta = { data: meta };
|
|
104
|
+
logger(meta, message);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Emit a log message to OpenTelemetry
|
|
108
|
+
* @param severityNumber - The severity number of the log
|
|
109
|
+
* @param message - The log message
|
|
110
|
+
* @param meta - Optional additional arguments to include in the log
|
|
111
|
+
*/
|
|
112
|
+
emitterLogger(severityNumber, message, meta) {
|
|
77
113
|
this.otelLogger.emit({
|
|
78
114
|
body: message,
|
|
79
|
-
severityNumber
|
|
115
|
+
severityNumber,
|
|
80
116
|
attributes: meta
|
|
81
117
|
});
|
|
82
118
|
}
|
|
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
119
|
};
|
|
94
|
-
var loggerService_default = LoggerService;
|
|
95
120
|
var InstrumentationService = class {
|
|
96
121
|
constructor() {
|
|
97
122
|
this.enableOtlpTrace = ENVIRONMENT.ENABLE_OTLP_TRACE;
|
|
@@ -143,6 +168,6 @@ var InstrumentationService = class {
|
|
|
143
168
|
};
|
|
144
169
|
var instrumentationService_default = InstrumentationService;
|
|
145
170
|
|
|
146
|
-
export { instrumentationService_default as InstrumentationService,
|
|
171
|
+
export { instrumentationService_default as InstrumentationService, LoggerService };
|
|
147
172
|
//# sourceMappingURL=out.js.map
|
|
148
173
|
//# sourceMappingURL=index.mjs.map
|
package/dist/esm/index.mjs.map
CHANGED
|
@@ -1 +1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AAA,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"]}
|
|
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,UAA2C;;;ACD3C,IAAM,6BAA6B;AAEnC,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,EAC1C,oBAAoB,QAAQ,IAAI,sBAAsB;AAAA,EACtD,uBAAuB,QAAQ,IAAI,yBAAyB;AAAA,EAC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,CAAC;AAAA,MACD,kBAAkB,CAAC,4BAA4B,CAAC;AAAA,IACpD;AAEA,QAAI,KAAK,iBAAiB;AACtB,gBAAU,gBAAgB,IAAI,kBAAkB;AAAA,QAC5C,KAAK,KAAK;AAAA,QACV,SAAS;AAAA,UACL,gBAAgB;AAAA,QACpB;AAAA,MACJ,CAAC;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, { LogFn, Logger as PinoLogger } from 'pino';\n\nimport { DEFAULT_CONTEXT_SCOPE_NAME, ENVIRONMENT, LOG_LEVELS } from '../constants';\n\ntype MetadataLog = LogAttributes | Record<string, unknown>;\n\nexport default class LoggerService {\n private pinoLogger: PinoLogger;\n private otelLogger: OtelLogger;\n private enableConsole: boolean = ENVIRONMENT.ENABLE_CONSOLE_LOGS;\n\n constructor(serviceName = DEFAULT_CONTEXT_SCOPE_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 /**\n * Log a debug message, use this as a `console.log` replacement.\n * @param message - The log message\n * @param meta - Optional additional arguments to include in the log\n */\n debug(message: string, meta?: MetadataLog) {\n this.consoleLogger(this.pinoLogger.debug.bind(this.pinoLogger), message, meta);\n if (ENVIRONMENT.LOG_LEVEL === LOG_LEVELS.DEBUG.name) this.emitterLogger(LOG_LEVELS.DEBUG.severity, message, meta);\n }\n\n /**\n * Log an informational message\n * @param message - The log message\n * @param meta - Optional additional arguments to include in the log\n */\n info(message: string, meta?: MetadataLog) {\n this.consoleLogger(this.pinoLogger.info.bind(this.pinoLogger), message, meta);\n this.emitterLogger(LOG_LEVELS.INFO.severity, message, meta);\n }\n\n /**\n * Log a warning message\n * @param message - The log message\n * @param meta - Optional additional arguments to include in the log\n */\n warn(message: string, meta?: MetadataLog) {\n this.consoleLogger(this.pinoLogger.warn.bind(this.pinoLogger), message, meta);\n this.emitterLogger(LOG_LEVELS.WARN.severity, message, meta);\n }\n\n /**\n * Log an error message\n * @param message - The log message\n * @param meta - Optional additional arguments to include in the log\n */\n error(message: string, meta?: MetadataLog) {\n this.consoleLogger(this.pinoLogger.error.bind(this.pinoLogger), message, meta);\n this.emitterLogger(LOG_LEVELS.ERROR.severity, message, meta);\n }\n\n /**\n * Simplify the logger applying any configuration in a single place\n * @param logger - Log function from pino (e.g., `this.pinoLogger.info.bind(this.pinoLogger)`)\n * @param message - The log message\n * @param meta - Optional additional arguments to include in the log\n */\n private consoleLogger(logger: LogFn, message: string, meta?: MetadataLog) {\n if (!this.enableConsole) return;\n\n if (meta && typeof meta === 'string' && message && typeof message === 'string') meta = { data: meta };\n logger(meta, message);\n }\n\n /**\n * Emit a log message to OpenTelemetry\n * @param severityNumber - The severity number of the log\n * @param message - The log message\n * @param meta - Optional additional arguments to include in the log\n */\n private emitterLogger(severityNumber: number, message: string, meta?: MetadataLog) {\n this.otelLogger.emit({\n body: message,\n severityNumber,\n attributes: meta as LogAttributes,\n });\n }\n}\n","export const DEFAULT_CONTEXT_SCOPE_NAME = 'context_scope_name';\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"]}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,14 +1,49 @@
|
|
|
1
1
|
import { LogAttributes } from '@opentelemetry/api-logs';
|
|
2
2
|
|
|
3
|
+
type MetadataLog = LogAttributes | Record<string, unknown>;
|
|
3
4
|
declare class LoggerService {
|
|
4
5
|
private pinoLogger;
|
|
5
6
|
private otelLogger;
|
|
6
7
|
private enableConsole;
|
|
7
8
|
constructor(serviceName?: string);
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Log a debug message, use this as a `console.log` replacement.
|
|
11
|
+
* @param message - The log message
|
|
12
|
+
* @param meta - Optional additional arguments to include in the log
|
|
13
|
+
*/
|
|
14
|
+
debug(message: string, meta?: MetadataLog): void;
|
|
15
|
+
/**
|
|
16
|
+
* Log an informational message
|
|
17
|
+
* @param message - The log message
|
|
18
|
+
* @param meta - Optional additional arguments to include in the log
|
|
19
|
+
*/
|
|
20
|
+
info(message: string, meta?: MetadataLog): void;
|
|
21
|
+
/**
|
|
22
|
+
* Log a warning message
|
|
23
|
+
* @param message - The log message
|
|
24
|
+
* @param meta - Optional additional arguments to include in the log
|
|
25
|
+
*/
|
|
26
|
+
warn(message: string, meta?: MetadataLog): void;
|
|
27
|
+
/**
|
|
28
|
+
* Log an error message
|
|
29
|
+
* @param message - The log message
|
|
30
|
+
* @param meta - Optional additional arguments to include in the log
|
|
31
|
+
*/
|
|
32
|
+
error(message: string, meta?: MetadataLog): void;
|
|
33
|
+
/**
|
|
34
|
+
* Simplify the logger applying any configuration in a single place
|
|
35
|
+
* @param logger - Log function from pino (e.g., `this.pinoLogger.info.bind(this.pinoLogger)`)
|
|
36
|
+
* @param message - The log message
|
|
37
|
+
* @param meta - Optional additional arguments to include in the log
|
|
38
|
+
*/
|
|
39
|
+
private consoleLogger;
|
|
40
|
+
/**
|
|
41
|
+
* Emit a log message to OpenTelemetry
|
|
42
|
+
* @param severityNumber - The severity number of the log
|
|
43
|
+
* @param message - The log message
|
|
44
|
+
* @param meta - Optional additional arguments to include in the log
|
|
45
|
+
*/
|
|
46
|
+
private emitterLogger;
|
|
12
47
|
}
|
|
13
48
|
|
|
14
49
|
declare class InstrumentationService {
|
package/jest.config.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
preset: 'ts-jest',
|
|
3
|
+
testEnvironment: 'node',
|
|
4
|
+
collectCoverageFrom: ['src/**/*.ts', '!src/**/*.test.ts', '!src/**/*.spec.ts', '!src/types/**/*', '!src/interfaces/**/*', '!src/index.ts'],
|
|
5
|
+
coverageDirectory: 'coverage',
|
|
6
|
+
coverageReporters: ['text', 'lcov', 'html'],
|
|
7
|
+
testMatch: ['**/__tests__/**/*.ts', '**/*.test.ts', '**/*.spec.ts'],
|
|
8
|
+
moduleFileExtensions: ['ts', 'js', 'json'],
|
|
9
|
+
transform: {
|
|
10
|
+
'^.+\\.ts$': 'ts-jest',
|
|
11
|
+
},
|
|
12
|
+
clearMocks: true,
|
|
13
|
+
resetMocks: true,
|
|
14
|
+
restoreMocks: true,
|
|
15
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@qrvey/telemetry",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"types": "dist/types/index.d.ts",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"exports": {
|
|
@@ -11,7 +11,9 @@
|
|
|
11
11
|
}
|
|
12
12
|
},
|
|
13
13
|
"scripts": {
|
|
14
|
-
"test": "
|
|
14
|
+
"test": "jest",
|
|
15
|
+
"test:watch": "jest --watch",
|
|
16
|
+
"test:coverage": "jest --coverage",
|
|
15
17
|
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\"",
|
|
16
18
|
"lint:fix": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
|
|
17
19
|
"type-check": "tsc",
|
|
@@ -41,9 +43,12 @@
|
|
|
41
43
|
"pino-pretty": "13.1.1"
|
|
42
44
|
},
|
|
43
45
|
"devDependencies": {
|
|
46
|
+
"@types/jest": "29.5.12",
|
|
44
47
|
"eslint": "8.48.0",
|
|
45
48
|
"eslint-import-resolver-typescript": "3.8.0",
|
|
46
49
|
"eslint-plugin-import": "2.31.0",
|
|
50
|
+
"jest": "29.7.0",
|
|
51
|
+
"ts-jest": "29.1.4",
|
|
47
52
|
"tsup": "8.0.2",
|
|
48
53
|
"typescript": "5.9.2"
|
|
49
54
|
},
|
package/src/lib/constants.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { LogAttributes, Logger as OtelLogger, logs } from '@opentelemetry/api-logs';
|
|
2
|
-
import pino from 'pino';
|
|
2
|
+
import pino, { LogFn, Logger as PinoLogger } from 'pino';
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import { DEFAULT_CONTEXT_SCOPE_NAME, ENVIRONMENT, LOG_LEVELS } from '../constants';
|
|
5
5
|
|
|
6
|
-
type
|
|
6
|
+
type MetadataLog = LogAttributes | Record<string, unknown>;
|
|
7
7
|
|
|
8
|
-
class LoggerService {
|
|
8
|
+
export default class LoggerService {
|
|
9
9
|
private pinoLogger: PinoLogger;
|
|
10
10
|
private otelLogger: OtelLogger;
|
|
11
11
|
private enableConsole: boolean = ENVIRONMENT.ENABLE_CONSOLE_LOGS;
|
|
12
12
|
|
|
13
|
-
constructor(serviceName =
|
|
13
|
+
constructor(serviceName = DEFAULT_CONTEXT_SCOPE_NAME) {
|
|
14
14
|
this.pinoLogger = pino({
|
|
15
15
|
level: ENVIRONMENT.LOG_LEVEL,
|
|
16
16
|
formatters: {
|
|
@@ -30,42 +30,70 @@ class LoggerService {
|
|
|
30
30
|
this.otelLogger = logs.getLogger(serviceName);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
33
|
+
/**
|
|
34
|
+
* Log a debug message, use this as a `console.log` replacement.
|
|
35
|
+
* @param message - The log message
|
|
36
|
+
* @param meta - Optional additional arguments to include in the log
|
|
37
|
+
*/
|
|
38
|
+
debug(message: string, meta?: MetadataLog) {
|
|
39
|
+
this.consoleLogger(this.pinoLogger.debug.bind(this.pinoLogger), message, meta);
|
|
40
|
+
if (ENVIRONMENT.LOG_LEVEL === LOG_LEVELS.DEBUG.name) this.emitterLogger(LOG_LEVELS.DEBUG.severity, message, meta);
|
|
40
41
|
}
|
|
41
42
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Log an informational message
|
|
45
|
+
* @param message - The log message
|
|
46
|
+
* @param meta - Optional additional arguments to include in the log
|
|
47
|
+
*/
|
|
48
|
+
info(message: string, meta?: MetadataLog) {
|
|
49
|
+
this.consoleLogger(this.pinoLogger.info.bind(this.pinoLogger), message, meta);
|
|
50
|
+
this.emitterLogger(LOG_LEVELS.INFO.severity, message, meta);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Log a warning message
|
|
55
|
+
* @param message - The log message
|
|
56
|
+
* @param meta - Optional additional arguments to include in the log
|
|
57
|
+
*/
|
|
58
|
+
warn(message: string, meta?: MetadataLog) {
|
|
59
|
+
this.consoleLogger(this.pinoLogger.warn.bind(this.pinoLogger), message, meta);
|
|
60
|
+
this.emitterLogger(LOG_LEVELS.WARN.severity, message, meta);
|
|
49
61
|
}
|
|
50
62
|
|
|
51
|
-
|
|
52
|
-
|
|
63
|
+
/**
|
|
64
|
+
* Log an error message
|
|
65
|
+
* @param message - The log message
|
|
66
|
+
* @param meta - Optional additional arguments to include in the log
|
|
67
|
+
*/
|
|
68
|
+
error(message: string, meta?: MetadataLog) {
|
|
69
|
+
this.consoleLogger(this.pinoLogger.error.bind(this.pinoLogger), message, meta);
|
|
70
|
+
this.emitterLogger(LOG_LEVELS.ERROR.severity, message, meta);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Simplify the logger applying any configuration in a single place
|
|
75
|
+
* @param logger - Log function from pino (e.g., `this.pinoLogger.info.bind(this.pinoLogger)`)
|
|
76
|
+
* @param message - The log message
|
|
77
|
+
* @param meta - Optional additional arguments to include in the log
|
|
78
|
+
*/
|
|
79
|
+
private consoleLogger(logger: LogFn, message: string, meta?: MetadataLog) {
|
|
80
|
+
if (!this.enableConsole) return;
|
|
81
|
+
|
|
82
|
+
if (meta && typeof meta === 'string' && message && typeof message === 'string') meta = { data: meta };
|
|
83
|
+
logger(meta, message);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Emit a log message to OpenTelemetry
|
|
88
|
+
* @param severityNumber - The severity number of the log
|
|
89
|
+
* @param message - The log message
|
|
90
|
+
* @param meta - Optional additional arguments to include in the log
|
|
91
|
+
*/
|
|
92
|
+
private emitterLogger(severityNumber: number, message: string, meta?: MetadataLog) {
|
|
53
93
|
this.otelLogger.emit({
|
|
54
94
|
body: message,
|
|
55
|
-
severityNumber
|
|
56
|
-
attributes: meta,
|
|
95
|
+
severityNumber,
|
|
96
|
+
attributes: meta as LogAttributes,
|
|
57
97
|
});
|
|
58
98
|
}
|
|
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
99
|
}
|
|
70
|
-
|
|
71
|
-
export default LoggerService;
|