@rsdk/core 5.11.0 → 5.11.1-next.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/dist/config/parsers/index.d.ts +1 -0
- package/dist/config/parsers/index.js +1 -0
- package/dist/config/parsers/index.js.map +1 -1
- package/dist/config/parsers/string-record/exception.d.ts +4 -0
- package/dist/config/parsers/string-record/exception.js +14 -0
- package/dist/config/parsers/string-record/exception.js.map +1 -0
- package/dist/config/parsers/string-record/index.d.ts +2 -0
- package/dist/config/parsers/string-record/index.js +21 -0
- package/dist/config/parsers/string-record/index.js.map +1 -0
- package/dist/config/parsers/string-record/string-record.parser.d.ts +8 -0
- package/dist/config/parsers/string-record/string-record.parser.js +42 -0
- package/dist/config/parsers/string-record/string-record.parser.js.map +1 -0
- package/dist/logging/logger-initializing.module.d.ts +22 -1
- package/dist/logging/logger-initializing.module.js +103 -11
- package/dist/logging/logger-initializing.module.js.map +1 -1
- package/dist/logging/logging.config.d.ts +24 -1
- package/dist/logging/logging.config.js +132 -3
- package/dist/logging/logging.config.js.map +1 -1
- package/dist/logging/otel-time.transform.d.ts +9 -0
- package/dist/logging/otel-time.transform.js +35 -0
- package/dist/logging/otel-time.transform.js.map +1 -0
- package/package.json +7 -2
- package/src/config/parsers/index.ts +1 -0
- package/src/config/parsers/string-record/exception.ts +10 -0
- package/src/config/parsers/string-record/index.ts +2 -0
- package/src/config/parsers/string-record/string-record.parser.ts +55 -0
- package/src/config/parsers/string-record/string-record.spec.ts +33 -0
- package/src/logging/logger-factory.lifecycle.spec.ts +91 -0
- package/src/logging/logger-initializing.module.spec.ts +263 -0
- package/src/logging/logger-initializing.module.ts +128 -17
- package/src/logging/logging.config.ts +113 -3
- package/src/logging/otel-time.transform.spec.ts +59 -0
- package/src/logging/otel-time.transform.ts +40 -0
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Timespan } from '@rsdk/common';
|
|
2
|
+
import { DEFAULT_LEVEL, LogLevel, LogTimestampFormat } from '@rsdk/logging';
|
|
2
3
|
|
|
3
4
|
import {
|
|
4
5
|
ArrayParser,
|
|
@@ -6,11 +7,23 @@ import {
|
|
|
6
7
|
Config,
|
|
7
8
|
ConfigSection,
|
|
8
9
|
EnumParser,
|
|
10
|
+
IntParser,
|
|
9
11
|
Property,
|
|
10
12
|
StringParser,
|
|
13
|
+
StringRecordParser,
|
|
14
|
+
TimespanParser,
|
|
15
|
+
UrlParser,
|
|
11
16
|
} from '../config';
|
|
12
17
|
import { ConfigTag } from '../config/config.const';
|
|
13
18
|
|
|
19
|
+
export enum LogOtelProtocol {
|
|
20
|
+
httpProtobuf = 'http/protobuf',
|
|
21
|
+
http = 'http',
|
|
22
|
+
httpJson = 'http/json',
|
|
23
|
+
grpc = 'grpc',
|
|
24
|
+
console = 'console',
|
|
25
|
+
}
|
|
26
|
+
|
|
14
27
|
@ConfigSection({
|
|
15
28
|
tags: [ConfigTag.infrastructure, ConfigTag.logging],
|
|
16
29
|
})
|
|
@@ -32,13 +45,110 @@ export class LoggingConfig extends Config {
|
|
|
32
45
|
@Property('LOG_SYNC', new BoolParser(), {
|
|
33
46
|
defaultValue: false,
|
|
34
47
|
description:
|
|
35
|
-
'Synchronous
|
|
48
|
+
'Synchronous stdout writes. Applies only to in-process stream mode; worker transports (OTEL/HTTP/forced) are always async. https://github.com/pinojs/pino/blob/master/docs/asynchronous.md',
|
|
36
49
|
})
|
|
37
50
|
readonly sync!: boolean;
|
|
38
51
|
|
|
52
|
+
@Property('LOG_SHUTDOWN_TIMEOUT', new TimespanParser(), {
|
|
53
|
+
defaultValue: new Timespan(1, 's'),
|
|
54
|
+
description:
|
|
55
|
+
'Max time to flush buffered worker-transport logs on application shutdown. Raise it when using OTEL: its worker may take longer to flush the last batch',
|
|
56
|
+
})
|
|
57
|
+
readonly shutdownTimeout!: Timespan;
|
|
58
|
+
|
|
59
|
+
@Property('LOG_FORCE_TRANSPORT', new BoolParser(), {
|
|
60
|
+
defaultValue: false,
|
|
61
|
+
description:
|
|
62
|
+
'Use worker-based transport even when only stdout is configured (no OTEL/HTTP). Otherwise stdout-only logging stays in-process',
|
|
63
|
+
})
|
|
64
|
+
readonly forceTransport!: boolean;
|
|
65
|
+
|
|
66
|
+
@Property('LOG_HTTP_ENDPOINT', new UrlParser('http:', 'https:'), {
|
|
67
|
+
defaultValue: undefined,
|
|
68
|
+
description:
|
|
69
|
+
'URL endpoint for the HTTP logs transport. Transport is enabled when set',
|
|
70
|
+
})
|
|
71
|
+
readonly httpEndpoint?: URL;
|
|
72
|
+
|
|
73
|
+
@Property('LOG_HTTP_HEADERS', new StringRecordParser(), {
|
|
74
|
+
defaultValue: {},
|
|
75
|
+
description: 'JSON object of HTTP headers sent by the HTTP logs transport',
|
|
76
|
+
})
|
|
77
|
+
readonly httpHeaders!: Record<string, string>;
|
|
78
|
+
|
|
79
|
+
@Property('LOG_HTTP_TIMEOUT', new TimespanParser(), {
|
|
80
|
+
defaultValue: new Timespan(2500, 'ms'),
|
|
81
|
+
description: 'Request timeout for the HTTP logs transport',
|
|
82
|
+
})
|
|
83
|
+
readonly httpTimeout!: Timespan;
|
|
84
|
+
|
|
85
|
+
@Property('LOG_HTTP_BATCH_SIZE', new IntParser(), {
|
|
86
|
+
defaultValue: 100,
|
|
87
|
+
description: 'Number of logs flushed per batch by the HTTP logs transport',
|
|
88
|
+
})
|
|
89
|
+
readonly httpBatchSize!: number;
|
|
90
|
+
|
|
91
|
+
@Property('LOG_HTTP_BATCH_INTERVAL', new TimespanParser(), {
|
|
92
|
+
defaultValue: new Timespan(5, 's'),
|
|
93
|
+
description:
|
|
94
|
+
'Max interval between batch flushes of the HTTP logs transport',
|
|
95
|
+
})
|
|
96
|
+
readonly httpBatchInterval!: Timespan;
|
|
97
|
+
|
|
98
|
+
@Property('LOG_HTTP_MAX_RETRIES', new IntParser(), {
|
|
99
|
+
defaultValue: 2,
|
|
100
|
+
description: 'Max retry attempts for a failed HTTP logs batch',
|
|
101
|
+
})
|
|
102
|
+
readonly httpMaxRetries!: number;
|
|
103
|
+
|
|
104
|
+
@Property('LOG_HTTP_RETRY_DELAY', new TimespanParser(), {
|
|
105
|
+
defaultValue: new Timespan(1, 's'),
|
|
106
|
+
description: 'Base delay between HTTP logs retry attempts',
|
|
107
|
+
})
|
|
108
|
+
readonly httpRetryDelay!: Timespan;
|
|
109
|
+
|
|
110
|
+
@Property('LOG_HTTP_MAX_BUFFER_SIZE', new IntParser(), {
|
|
111
|
+
defaultValue: 100000,
|
|
112
|
+
description:
|
|
113
|
+
'Max number of buffered logs before dropping (HTTP logs transport)',
|
|
114
|
+
})
|
|
115
|
+
readonly httpMaxBufferSize!: number;
|
|
116
|
+
|
|
117
|
+
@Property('LOG_HTTP_SILENT', new BoolParser(), {
|
|
118
|
+
defaultValue: false,
|
|
119
|
+
description: 'Suppress internal warnings/errors of the HTTP logs transport',
|
|
120
|
+
})
|
|
121
|
+
readonly httpSilent!: boolean;
|
|
122
|
+
|
|
39
123
|
@Property('LOG_OTEL_ENABLED', new BoolParser(), {
|
|
40
124
|
defaultValue: false,
|
|
41
|
-
description: 'Enable OpenTelemetry exporter',
|
|
125
|
+
description: 'Enable OpenTelemetry logs exporter',
|
|
42
126
|
})
|
|
43
127
|
readonly otelEnabled!: boolean;
|
|
128
|
+
|
|
129
|
+
@Property('LOG_OTEL_ENDPOINT', new UrlParser('http:', 'https:'), {
|
|
130
|
+
defaultValue: undefined,
|
|
131
|
+
description: 'OTLP endpoint URL for the OpenTelemetry logs exporter',
|
|
132
|
+
})
|
|
133
|
+
readonly otelEndpoint?: URL;
|
|
134
|
+
|
|
135
|
+
@Property('LOG_OTEL_PROTOCOL', new EnumParser(LogOtelProtocol), {
|
|
136
|
+
defaultValue: undefined,
|
|
137
|
+
description:
|
|
138
|
+
'OTLP exporter protocol for the OpenTelemetry logs. When unset, resolved from standard OTEL_EXPORTER_OTLP_PROTOCOL env (defaults to http/protobuf)',
|
|
139
|
+
})
|
|
140
|
+
readonly otelProtocol?: LogOtelProtocol;
|
|
141
|
+
|
|
142
|
+
@Property('LOG_OTEL_SERVICE_NAME', new StringParser(), {
|
|
143
|
+
defaultValue: undefined,
|
|
144
|
+
description:
|
|
145
|
+
'service.name resource attribute for the OpenTelemetry logs. When unset, resolved from standard OTEL env',
|
|
146
|
+
})
|
|
147
|
+
readonly otelServiceName?: string;
|
|
148
|
+
|
|
149
|
+
@Property('LOG_TIMESTAMP_FORMAT', new EnumParser(LogTimestampFormat), {
|
|
150
|
+
defaultValue: LogTimestampFormat.isoTime,
|
|
151
|
+
description: 'Log timestamp format',
|
|
152
|
+
})
|
|
153
|
+
readonly logTimestampFormat!: LogTimestampFormat;
|
|
44
154
|
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { Transform } from 'node:stream';
|
|
2
|
+
|
|
3
|
+
import buildOtelTimeTransform from './otel-time.transform';
|
|
4
|
+
|
|
5
|
+
/** Прогоняет NDJSON-строки через трансформ и возвращает распарсенный вывод. */
|
|
6
|
+
async function run(lines: object[]): Promise<Record<string, unknown>[]> {
|
|
7
|
+
const stream: Transform = await buildOtelTimeTransform();
|
|
8
|
+
|
|
9
|
+
const collected = new Promise<Record<string, unknown>[]>((resolve) => {
|
|
10
|
+
let out = '';
|
|
11
|
+
|
|
12
|
+
stream.on('data', (chunk) => (out += chunk));
|
|
13
|
+
stream.on('end', () =>
|
|
14
|
+
resolve(
|
|
15
|
+
out
|
|
16
|
+
.trim()
|
|
17
|
+
.split('\n')
|
|
18
|
+
.filter(Boolean)
|
|
19
|
+
.map((line) => JSON.parse(line)),
|
|
20
|
+
),
|
|
21
|
+
);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
for (const line of lines) {
|
|
25
|
+
stream.write(`${JSON.stringify(line)}\n`);
|
|
26
|
+
}
|
|
27
|
+
stream.end();
|
|
28
|
+
|
|
29
|
+
return collected;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
describe('otel-time.transform', () => {
|
|
33
|
+
it('конвертирует строковый ISO time в epoch ms', async () => {
|
|
34
|
+
const iso = '2026-06-24T19:04:21.991Z';
|
|
35
|
+
const [record] = await run([{ level: 30, time: iso, msg: 'x' }]);
|
|
36
|
+
|
|
37
|
+
expect(record.time).toBe(Date.parse(iso));
|
|
38
|
+
expect(typeof record.time).toBe('number');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('не трогает уже числовой time', async () => {
|
|
42
|
+
const [record] = await run([{ level: 30, time: 1782327861991, msg: 'x' }]);
|
|
43
|
+
|
|
44
|
+
expect(record.time).toBe(1782327861991);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('не падает на отсутствии time', async () => {
|
|
48
|
+
const [record] = await run([{ level: 30, msg: 'x' }]);
|
|
49
|
+
|
|
50
|
+
expect('time' in record).toBe(false);
|
|
51
|
+
expect(record.msg).toBe('x');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('оставляет невалидную строку как есть', async () => {
|
|
55
|
+
const [record] = await run([{ level: 30, time: 'not-a-date', msg: 'x' }]);
|
|
56
|
+
|
|
57
|
+
expect(record.time).toBe('not-a-date');
|
|
58
|
+
});
|
|
59
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { pipeline, Transform } from 'node:stream';
|
|
2
|
+
import build from 'pino-abstract-transport';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Pipeline-трансформ перед pino-opentelemetry-transport: приводит поле `time`
|
|
6
|
+
* к числу (epoch ms), если оно пришло строкой (например, при дефолтном
|
|
7
|
+
* LOG_TIMESTAMP_FORMAT=isoTime). OTLP ожидает числовой timestamp, иначе запись
|
|
8
|
+
* молча отбрасывается. Трансформ применяется только к OTEL-ветке — stdout и
|
|
9
|
+
* прочие транспорты сохраняют свой человекочитаемый формат времени.
|
|
10
|
+
*/
|
|
11
|
+
export default function buildOtelTimeTransform(): Promise<Transform> {
|
|
12
|
+
return build(
|
|
13
|
+
(source) => {
|
|
14
|
+
const transform = new Transform({
|
|
15
|
+
objectMode: true,
|
|
16
|
+
autoDestroy: true,
|
|
17
|
+
transform(
|
|
18
|
+
chunk: Record<string, unknown>,
|
|
19
|
+
_encoding: BufferEncoding,
|
|
20
|
+
callback: (error?: Error | null, data?: string) => void,
|
|
21
|
+
): void {
|
|
22
|
+
if (typeof chunk.time === 'string') {
|
|
23
|
+
const epochMs = Date.parse(chunk.time);
|
|
24
|
+
|
|
25
|
+
if (!Number.isNaN(epochMs)) {
|
|
26
|
+
chunk.time = epochMs;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
callback(null, `${JSON.stringify(chunk)}\n`);
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
pipeline(source, transform, () => {});
|
|
35
|
+
|
|
36
|
+
return transform;
|
|
37
|
+
},
|
|
38
|
+
{ enablePipelining: true },
|
|
39
|
+
) as unknown as Promise<Transform>;
|
|
40
|
+
}
|