@qrvey/telemetry 1.0.1-929 → 1.0.3-1036.beta
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 +40 -15
- package/__tests__/dataSanitizer.test.ts +32 -0
- package/__tests__/loggerService.test.ts +12 -31
- package/dist/cjs/lib/constants.js +10 -7
- package/dist/cjs/lib/constants.js.map +1 -1
- package/dist/cjs/lib/services/instrumentationService.js +24 -4
- package/dist/cjs/lib/services/instrumentationService.js.map +1 -1
- package/dist/cjs/lib/services/loggerService.js +23 -6
- package/dist/cjs/lib/services/loggerService.js.map +1 -1
- package/dist/cjs/lib/utils/dataSanitizer.js +54 -23
- package/dist/cjs/lib/utils/dataSanitizer.js.map +1 -1
- package/dist/esm/index.mjs +113 -40
- package/dist/esm/index.mjs.map +1 -1
- package/dist/types/index.d.ts +6 -0
- package/package.json +2 -2
- package/src/lib/constants.ts +11 -7
- package/src/lib/services/instrumentationService.ts +27 -8
- package/src/lib/services/loggerService.ts +26 -9
- package/src/lib/utils/dataSanitizer.ts +66 -25
|
@@ -2,15 +2,22 @@ import { LogAttributes, Logger as OtelLogger, logs } from '@opentelemetry/api-lo
|
|
|
2
2
|
import pino, { LogFn, Logger as PinoLogger } from 'pino';
|
|
3
3
|
|
|
4
4
|
import { DEFAULT_CONTEXT_SCOPE_NAME, ENVIRONMENT, LOG_LEVELS } from '../constants';
|
|
5
|
-
|
|
6
5
|
import { cleanSensitiveData } from '../utils/dataSanitizer';
|
|
7
6
|
|
|
8
7
|
type MetadataLog = LogAttributes | Record<string, unknown>;
|
|
9
8
|
|
|
9
|
+
const LOG_LEVEL_SEVERITY: Record<string, number> = {
|
|
10
|
+
[LOG_LEVELS.DEBUG.name]: LOG_LEVELS.DEBUG.severity,
|
|
11
|
+
[LOG_LEVELS.INFO.name]: LOG_LEVELS.INFO.severity,
|
|
12
|
+
[LOG_LEVELS.WARN.name]: LOG_LEVELS.WARN.severity,
|
|
13
|
+
[LOG_LEVELS.ERROR.name]: LOG_LEVELS.ERROR.severity,
|
|
14
|
+
};
|
|
15
|
+
|
|
10
16
|
export default class LoggerService {
|
|
11
17
|
private pinoLogger: PinoLogger;
|
|
12
18
|
private otelLogger: OtelLogger;
|
|
13
19
|
private enableConsole: boolean = ENVIRONMENT.ENABLE_CONSOLE_LOGS;
|
|
20
|
+
private configuredSeverity: number = LOG_LEVEL_SEVERITY[ENVIRONMENT.LOG_LEVEL] ?? LOG_LEVELS.DEBUG.severity;
|
|
14
21
|
|
|
15
22
|
constructor(scopeName = DEFAULT_CONTEXT_SCOPE_NAME) {
|
|
16
23
|
this.pinoLogger = pino({
|
|
@@ -24,9 +31,9 @@ export default class LoggerService {
|
|
|
24
31
|
transport:
|
|
25
32
|
ENVIRONMENT.LOG_LEVEL === LOG_LEVELS.DEBUG.name
|
|
26
33
|
? {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
34
|
+
target: 'pino-pretty',
|
|
35
|
+
options: { colorize: true },
|
|
36
|
+
}
|
|
30
37
|
: undefined,
|
|
31
38
|
});
|
|
32
39
|
this.otelLogger = logs.getLogger(scopeName);
|
|
@@ -38,10 +45,11 @@ export default class LoggerService {
|
|
|
38
45
|
* @param meta - Optional additional arguments to include in the log
|
|
39
46
|
*/
|
|
40
47
|
debug(message: string, meta?: MetadataLog) {
|
|
48
|
+
if (this.configuredSeverity > LOG_LEVELS.DEBUG.severity) return;
|
|
41
49
|
message = cleanSensitiveData(message);
|
|
42
50
|
meta = cleanSensitiveData(meta);
|
|
43
51
|
this.consoleLogger(this.pinoLogger.debug.bind(this.pinoLogger), message, meta);
|
|
44
|
-
|
|
52
|
+
this.emitterLogger(LOG_LEVELS.DEBUG.severity, LOG_LEVELS.DEBUG.name, message, meta);
|
|
45
53
|
}
|
|
46
54
|
|
|
47
55
|
/**
|
|
@@ -75,11 +83,24 @@ export default class LoggerService {
|
|
|
75
83
|
*/
|
|
76
84
|
error(message: string, meta?: MetadataLog) {
|
|
77
85
|
message = cleanSensitiveData(message);
|
|
86
|
+
meta = this.normalizeErrorMeta(meta);
|
|
78
87
|
meta = cleanSensitiveData(meta);
|
|
79
88
|
this.consoleLogger(this.pinoLogger.error.bind(this.pinoLogger), message, meta);
|
|
80
89
|
this.emitterLogger(LOG_LEVELS.ERROR.severity, LOG_LEVELS.ERROR.name, message, meta);
|
|
81
90
|
}
|
|
82
91
|
|
|
92
|
+
private normalizeErrorMeta(meta?: MetadataLog) {
|
|
93
|
+
if (!(meta instanceof Error)) return meta;
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
'exception.name': meta.name,
|
|
97
|
+
'exception.cause': meta.cause,
|
|
98
|
+
'exception.message': meta.message,
|
|
99
|
+
'exception.stacktrace': meta.stack,
|
|
100
|
+
'exception.code': meta.code,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
83
104
|
/**
|
|
84
105
|
* Simplify the logger applying any configuration in a single place
|
|
85
106
|
* @param logger - Log function from pino (e.g., `this.pinoLogger.info.bind(this.pinoLogger)`)
|
|
@@ -87,8 +108,6 @@ export default class LoggerService {
|
|
|
87
108
|
* @param meta - Optional additional arguments to include in the log
|
|
88
109
|
*/
|
|
89
110
|
private consoleLogger(logger: LogFn, message: string, meta?: MetadataLog) {
|
|
90
|
-
message = cleanSensitiveData(message);
|
|
91
|
-
meta = cleanSensitiveData(meta);
|
|
92
111
|
if (!this.enableConsole) return;
|
|
93
112
|
|
|
94
113
|
if (meta && typeof meta === 'string' && message && typeof message === 'string') meta = { data: meta };
|
|
@@ -103,8 +122,6 @@ export default class LoggerService {
|
|
|
103
122
|
* @param meta - Optional additional arguments to include in the log
|
|
104
123
|
*/
|
|
105
124
|
private emitterLogger(severityNumber: number, severityText: string, message: string, meta?: MetadataLog) {
|
|
106
|
-
message = cleanSensitiveData(message);
|
|
107
|
-
meta = cleanSensitiveData(meta);
|
|
108
125
|
this.otelLogger.emit({
|
|
109
126
|
body: message,
|
|
110
127
|
severityNumber,
|
|
@@ -1,41 +1,82 @@
|
|
|
1
|
-
import { ENVIRONMENT } from
|
|
1
|
+
import { ENVIRONMENT } from '../constants';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
const REDACTED_VALUE = '***REDACTED***';
|
|
4
|
+
|
|
5
|
+
function escapeRegex(value: string): string {
|
|
6
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function buildKeywordRegex(keyword: string): RegExp {
|
|
10
|
+
const escaped = escapeRegex(keyword.trim()).replace(/\\\*/g, '.*');
|
|
11
|
+
return new RegExp(`^${escaped}$`, 'i');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function matchesKeywordPattern(value: string, keywordPatterns: RegExp[]): boolean {
|
|
15
|
+
return keywordPatterns.some((pattern) => pattern.test(value));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function sanitizeString(input: string, keywords: string[]): string {
|
|
19
|
+
if (!keywords.length) return input;
|
|
20
|
+
|
|
21
|
+
const keywordPatterns = keywords.map(buildKeywordRegex);
|
|
22
|
+
|
|
23
|
+
let cleaned = input;
|
|
24
|
+
const keyValuePattern = /(["']?)([^"'\s:=,;{}\[\]]+)\1(\s*[:=]\s*)("[^"]*"|'[^']*'|[^,\s;&]+)/g;
|
|
25
|
+
cleaned = cleaned.replace(keyValuePattern, (match, quote: string, key: string, separator: string) => {
|
|
26
|
+
if (!matchesKeywordPattern(key, keywordPatterns)) return match;
|
|
27
|
+
return `${quote}${key}${quote}${separator}${REDACTED_VALUE}`;
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
return cleaned;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function cleanSensitiveDataInternal(obj: any, keywords: string[], keywordPatterns: RegExp[], seen: WeakSet<object>): any {
|
|
8
34
|
if (obj === null || obj === undefined) {
|
|
9
35
|
return obj;
|
|
10
36
|
}
|
|
11
37
|
|
|
12
38
|
if (typeof obj === 'string') {
|
|
39
|
+
return sanitizeString(obj, keywords);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (typeof obj !== 'object') {
|
|
13
43
|
return obj;
|
|
14
44
|
}
|
|
15
45
|
|
|
46
|
+
if (seen.has(obj)) {
|
|
47
|
+
return '[Circular]';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
seen.add(obj);
|
|
51
|
+
|
|
16
52
|
if (Array.isArray(obj)) {
|
|
17
|
-
|
|
53
|
+
const cleanedArray = obj.map((item) => cleanSensitiveDataInternal(item, keywords, keywordPatterns, seen));
|
|
54
|
+
seen.delete(obj);
|
|
55
|
+
return cleanedArray;
|
|
18
56
|
}
|
|
19
57
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
);
|
|
29
|
-
if (isSensitive) {
|
|
30
|
-
cleaned[key] = '***REDACTED***';
|
|
31
|
-
} else if (typeof value === 'object' && value !== null) {
|
|
32
|
-
cleaned[key] = cleanSensitiveData(value);
|
|
33
|
-
} else {
|
|
34
|
-
cleaned[key] = value;
|
|
35
|
-
}
|
|
58
|
+
const cleaned: any = {};
|
|
59
|
+
|
|
60
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
61
|
+
const isSensitive = matchesKeywordPattern(key, keywordPatterns);
|
|
62
|
+
if (isSensitive) {
|
|
63
|
+
cleaned[key] = REDACTED_VALUE;
|
|
64
|
+
} else {
|
|
65
|
+
cleaned[key] = cleanSensitiveDataInternal(value, keywords, keywordPatterns, seen);
|
|
36
66
|
}
|
|
37
|
-
return cleaned;
|
|
38
67
|
}
|
|
39
68
|
|
|
40
|
-
|
|
69
|
+
seen.delete(obj);
|
|
70
|
+
return cleaned;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Cleans sensitive data from an object by redacting values of keys that contain sensitive keywords.
|
|
75
|
+
* This utility can be used by middleware/interceptors before logging HTTP request/response data.
|
|
76
|
+
*/
|
|
77
|
+
export function cleanSensitiveData(obj: any): any {
|
|
78
|
+
const keywords = ENVIRONMENT.SENSITIVE_FIELDS.map((keyword) => keyword.trim()).filter((keyword) => keyword.length > 0);
|
|
79
|
+
const keywordPatterns = keywords.map(buildKeywordRegex);
|
|
80
|
+
|
|
81
|
+
return cleanSensitiveDataInternal(obj, keywords, keywordPatterns, new WeakSet<object>());
|
|
41
82
|
}
|