@qrvey/telemetry 1.0.0-beta → 1.0.2-1007
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 +42 -17
- package/dist/cjs/lib/constants.js +10 -7
- package/dist/cjs/lib/constants.js.map +1 -1
- package/dist/cjs/lib/services/instrumentationService.js +25 -5
- package/dist/cjs/lib/services/instrumentationService.js.map +1 -1
- package/dist/cjs/lib/services/loggerService.js +21 -7
- package/dist/cjs/lib/services/loggerService.js.map +1 -1
- package/dist/cjs/lib/services/middlewareService.js +2 -2
- package/dist/cjs/lib/services/middlewareService.js.map +1 -1
- package/dist/cjs/lib/utils/dataSanitizer.js +32 -9
- package/dist/cjs/lib/utils/dataSanitizer.js.map +1 -1
- package/dist/esm/index.mjs +158 -67
- package/dist/esm/index.mjs.map +1 -1
- package/dist/types/index.d.ts +8 -2
- package/package.json +2 -2
- package/src/lib/constants.ts +10 -6
- package/src/lib/services/instrumentationService.ts +28 -9
- package/src/lib/services/loggerService.ts +22 -7
- package/src/lib/services/middlewareService.ts +3 -3
- package/src/lib/utils/dataSanitizer.ts +39 -10
|
@@ -3,7 +3,7 @@ import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-proto';
|
|
|
3
3
|
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-proto';
|
|
4
4
|
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
|
|
5
5
|
import { resourceFromAttributes } from '@opentelemetry/resources';
|
|
6
|
-
import { SimpleLogRecordProcessor } from '@opentelemetry/sdk-logs';
|
|
6
|
+
import { BatchLogRecordProcessor, SimpleLogRecordProcessor } from '@opentelemetry/sdk-logs';
|
|
7
7
|
import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
|
|
8
8
|
import { NodeSDK } from '@opentelemetry/sdk-node';
|
|
9
9
|
import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from '@opentelemetry/semantic-conventions';
|
|
@@ -19,9 +19,13 @@ class InstrumentationService {
|
|
|
19
19
|
private otlpTraceUrl: string = ENVIRONMENT.OTLP_TRACE_URL;
|
|
20
20
|
private otlpMetricUrl: string = ENVIRONMENT.OTLP_METRIC_URL;
|
|
21
21
|
private otlpLogUrl: string = ENVIRONMENT.OTLP_LOG_URL;
|
|
22
|
-
private serviceName: string = ENVIRONMENT.
|
|
23
|
-
private serviceVersion: string = ENVIRONMENT.
|
|
22
|
+
private serviceName: string = ENVIRONMENT.SERVICE_NAME;
|
|
23
|
+
private serviceVersion: string = ENVIRONMENT.QRVEY_VERSION;
|
|
24
24
|
private exportMetricsIntervalMillis: number = ENVIRONMENT.EXPORT_METRICS_INTERVAL_MILLIS;
|
|
25
|
+
private enableBatchLogRecordProcessor: boolean = ENVIRONMENT.ENABLE_BATCH_LOG_RECORD_PROCESSOR;
|
|
26
|
+
private logBatchMaxQueueSize: number = ENVIRONMENT.LOG_BATCH_MAX_QUEUE_SIZE;
|
|
27
|
+
private logBatchMaxExportBatchSize: number = ENVIRONMENT.LOG_BATCH_MAX_EXPORT_BATCH_SIZE;
|
|
28
|
+
private logBatchScheduledDelayMillis: number = ENVIRONMENT.LOG_BATCH_SCHEDULED_DELAY_MILLIS;
|
|
25
29
|
|
|
26
30
|
constructor() {
|
|
27
31
|
const sdkConfig: any = {
|
|
@@ -29,7 +33,16 @@ class InstrumentationService {
|
|
|
29
33
|
[ATTR_SERVICE_NAME]: this.serviceName,
|
|
30
34
|
[ATTR_SERVICE_VERSION]: this.serviceVersion,
|
|
31
35
|
}),
|
|
32
|
-
instrumentations: [
|
|
36
|
+
instrumentations: [
|
|
37
|
+
getNodeAutoInstrumentations({
|
|
38
|
+
'@opentelemetry/instrumentation-fs': {
|
|
39
|
+
enabled: false,
|
|
40
|
+
},
|
|
41
|
+
'@opentelemetry/instrumentation-dns': {
|
|
42
|
+
enabled: false,
|
|
43
|
+
},
|
|
44
|
+
}),
|
|
45
|
+
],
|
|
33
46
|
};
|
|
34
47
|
|
|
35
48
|
if (this.enableOtlpTrace) {
|
|
@@ -51,11 +64,17 @@ class InstrumentationService {
|
|
|
51
64
|
}
|
|
52
65
|
|
|
53
66
|
if (this.enableOtlpLog) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
67
|
+
const exporter = new OTLPLogExporter({
|
|
68
|
+
url: this.otlpLogUrl,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
sdkConfig.logRecordProcessor = this.enableBatchLogRecordProcessor
|
|
72
|
+
? new BatchLogRecordProcessor(exporter, {
|
|
73
|
+
maxQueueSize: this.logBatchMaxQueueSize,
|
|
74
|
+
maxExportBatchSize: this.logBatchMaxExportBatchSize,
|
|
75
|
+
scheduledDelayMillis: this.logBatchScheduledDelayMillis,
|
|
76
|
+
})
|
|
77
|
+
: new SimpleLogRecordProcessor(exporter);
|
|
59
78
|
}
|
|
60
79
|
|
|
61
80
|
this.sdk = new NodeSDK(sdkConfig);
|
|
@@ -12,7 +12,7 @@ export default class LoggerService {
|
|
|
12
12
|
private otelLogger: OtelLogger;
|
|
13
13
|
private enableConsole: boolean = ENVIRONMENT.ENABLE_CONSOLE_LOGS;
|
|
14
14
|
|
|
15
|
-
constructor(
|
|
15
|
+
constructor(scopeName = DEFAULT_CONTEXT_SCOPE_NAME) {
|
|
16
16
|
this.pinoLogger = pino({
|
|
17
17
|
level: ENVIRONMENT.LOG_LEVEL,
|
|
18
18
|
formatters: {
|
|
@@ -29,7 +29,7 @@ export default class LoggerService {
|
|
|
29
29
|
}
|
|
30
30
|
: undefined,
|
|
31
31
|
});
|
|
32
|
-
this.otelLogger = logs.getLogger(
|
|
32
|
+
this.otelLogger = logs.getLogger(scopeName);
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
/**
|
|
@@ -41,7 +41,7 @@ export default class LoggerService {
|
|
|
41
41
|
message = cleanSensitiveData(message);
|
|
42
42
|
meta = cleanSensitiveData(meta);
|
|
43
43
|
this.consoleLogger(this.pinoLogger.debug.bind(this.pinoLogger), message, meta);
|
|
44
|
-
if (ENVIRONMENT.LOG_LEVEL === LOG_LEVELS.DEBUG.name) this.emitterLogger(LOG_LEVELS.DEBUG.severity, message, meta);
|
|
44
|
+
if (ENVIRONMENT.LOG_LEVEL === LOG_LEVELS.DEBUG.name) this.emitterLogger(LOG_LEVELS.DEBUG.severity, LOG_LEVELS.DEBUG.name, message, meta);
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
/**
|
|
@@ -53,7 +53,7 @@ export default class LoggerService {
|
|
|
53
53
|
message = cleanSensitiveData(message);
|
|
54
54
|
meta = cleanSensitiveData(meta);
|
|
55
55
|
this.consoleLogger(this.pinoLogger.info.bind(this.pinoLogger), message, meta);
|
|
56
|
-
this.emitterLogger(LOG_LEVELS.INFO.severity, message, meta);
|
|
56
|
+
this.emitterLogger(LOG_LEVELS.INFO.severity, LOG_LEVELS.INFO.name, message, meta);
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
/**
|
|
@@ -65,7 +65,7 @@ export default class LoggerService {
|
|
|
65
65
|
message = cleanSensitiveData(message);
|
|
66
66
|
meta = cleanSensitiveData(meta);
|
|
67
67
|
this.consoleLogger(this.pinoLogger.warn.bind(this.pinoLogger), message, meta);
|
|
68
|
-
this.emitterLogger(LOG_LEVELS.WARN.severity, message, meta);
|
|
68
|
+
this.emitterLogger(LOG_LEVELS.WARN.severity, LOG_LEVELS.WARN.name, message, meta);
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
/**
|
|
@@ -75,9 +75,22 @@ export default class LoggerService {
|
|
|
75
75
|
*/
|
|
76
76
|
error(message: string, meta?: MetadataLog) {
|
|
77
77
|
message = cleanSensitiveData(message);
|
|
78
|
+
meta = this.normalizeErrorMeta(meta);
|
|
78
79
|
meta = cleanSensitiveData(meta);
|
|
79
80
|
this.consoleLogger(this.pinoLogger.error.bind(this.pinoLogger), message, meta);
|
|
80
|
-
this.emitterLogger(LOG_LEVELS.ERROR.severity, message, meta);
|
|
81
|
+
this.emitterLogger(LOG_LEVELS.ERROR.severity, LOG_LEVELS.ERROR.name, message, meta);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private normalizeErrorMeta(meta?: MetadataLog) {
|
|
85
|
+
if (!(meta instanceof Error)) return meta;
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
'exception.name': meta.name,
|
|
89
|
+
'exception.cause': meta.cause,
|
|
90
|
+
'exception.message': meta.message,
|
|
91
|
+
'exception.stacktrace': meta.stack,
|
|
92
|
+
'exception.code': meta.code,
|
|
93
|
+
};
|
|
81
94
|
}
|
|
82
95
|
|
|
83
96
|
/**
|
|
@@ -98,15 +111,17 @@ export default class LoggerService {
|
|
|
98
111
|
/**
|
|
99
112
|
* Emit a log message to OpenTelemetry
|
|
100
113
|
* @param severityNumber - The severity number of the log
|
|
114
|
+
* @param severityText - The severity text of the log
|
|
101
115
|
* @param message - The log message
|
|
102
116
|
* @param meta - Optional additional arguments to include in the log
|
|
103
117
|
*/
|
|
104
|
-
private emitterLogger(severityNumber: number, message: string, meta?: MetadataLog) {
|
|
118
|
+
private emitterLogger(severityNumber: number, severityText: string, message: string, meta?: MetadataLog) {
|
|
105
119
|
message = cleanSensitiveData(message);
|
|
106
120
|
meta = cleanSensitiveData(meta);
|
|
107
121
|
this.otelLogger.emit({
|
|
108
122
|
body: message,
|
|
109
123
|
severityNumber,
|
|
124
|
+
severityText,
|
|
110
125
|
attributes: meta as LogAttributes,
|
|
111
126
|
});
|
|
112
127
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import LoggerService from './loggerService';
|
|
2
2
|
|
|
3
3
|
export interface MiddlewareConfig {
|
|
4
|
-
|
|
4
|
+
scopeName?: string;
|
|
5
5
|
excludePaths?: string[];
|
|
6
6
|
logRequestBody?: boolean;
|
|
7
7
|
logResponseBody?: boolean;
|
|
@@ -15,7 +15,7 @@ export class MiddlewareService {
|
|
|
15
15
|
|
|
16
16
|
constructor(config?: MiddlewareConfig) {
|
|
17
17
|
this.config = {
|
|
18
|
-
|
|
18
|
+
scopeName: config?.scopeName || 'MiddlewareLogger',
|
|
19
19
|
excludePaths: config?.excludePaths || [],
|
|
20
20
|
logRequestBody: config?.logRequestBody || false,
|
|
21
21
|
logResponseBody: config?.logResponseBody || false,
|
|
@@ -23,7 +23,7 @@ export class MiddlewareService {
|
|
|
23
23
|
logRequestBodyFields: config?.logRequestBodyFields || [],
|
|
24
24
|
};
|
|
25
25
|
|
|
26
|
-
this.logger = new LoggerService(this.config.
|
|
26
|
+
this.logger = new LoggerService(this.config.scopeName);
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
logExpressApiCall = (req: any, res: any, next: any): void => {
|
|
@@ -1,16 +1,51 @@
|
|
|
1
1
|
import { ENVIRONMENT } from "../constants";
|
|
2
2
|
|
|
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
|
+
|
|
3
33
|
/**
|
|
4
34
|
* Cleans sensitive data from an object by redacting values of keys that contain sensitive keywords.
|
|
5
35
|
* This utility can be used by middleware/interceptors before logging HTTP request/response data.
|
|
6
36
|
*/
|
|
7
37
|
export function cleanSensitiveData(obj: any): any {
|
|
38
|
+
const keywords = ENVIRONMENT.SENSITIVE_FIELDS
|
|
39
|
+
.map((keyword) => keyword.trim())
|
|
40
|
+
.filter((keyword) => keyword.length > 0);
|
|
41
|
+
const keywordPatterns = keywords.map(buildKeywordRegex);
|
|
42
|
+
|
|
8
43
|
if (obj === null || obj === undefined) {
|
|
9
44
|
return obj;
|
|
10
45
|
}
|
|
11
46
|
|
|
12
47
|
if (typeof obj === 'string') {
|
|
13
|
-
return obj;
|
|
48
|
+
return sanitizeString(obj, keywords);
|
|
14
49
|
}
|
|
15
50
|
|
|
16
51
|
if (Array.isArray(obj)) {
|
|
@@ -18,20 +53,14 @@ export function cleanSensitiveData(obj: any): any {
|
|
|
18
53
|
}
|
|
19
54
|
|
|
20
55
|
if (typeof obj === 'object') {
|
|
21
|
-
const keywords = ENVIRONMENT.SENSITIVE_FIELDS;
|
|
22
56
|
const cleaned: any = {};
|
|
23
57
|
|
|
24
58
|
for (const [key, value] of Object.entries(obj)) {
|
|
25
|
-
const
|
|
26
|
-
const isSensitive = keywords.some(keyword =>
|
|
27
|
-
lowerKey.includes(keyword.toLowerCase())
|
|
28
|
-
);
|
|
59
|
+
const isSensitive = matchesKeywordPattern(key, keywordPatterns);
|
|
29
60
|
if (isSensitive) {
|
|
30
|
-
cleaned[key] =
|
|
31
|
-
} else if (typeof value === 'object' && value !== null) {
|
|
32
|
-
cleaned[key] = cleanSensitiveData(value);
|
|
61
|
+
cleaned[key] = REDACTED_VALUE;
|
|
33
62
|
} else {
|
|
34
|
-
cleaned[key] = value;
|
|
63
|
+
cleaned[key] = cleanSensitiveData(value);
|
|
35
64
|
}
|
|
36
65
|
}
|
|
37
66
|
return cleaned;
|