@contrail/telemetry 1.1.0-alpha-refactor-telemetry-3 → 2.0.0-dev-telemetry-own-otel-2
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/lib/logger/index.d.ts
CHANGED
|
@@ -2,8 +2,9 @@ import pino from 'pino';
|
|
|
2
2
|
export { parseOtelResourceAttributes } from './parse-otel-resource-attributes';
|
|
3
3
|
export { PINO_LEVEL_TO_OTEL_SEVERITY, PINO_LEVEL_TO_NAME } from './logger-config';
|
|
4
4
|
export { ATTR_LOG_MESSAGE, ATTR_LOG_PAYLOAD } from './semantic-conventions';
|
|
5
|
+
export { loggerStorage, withLogContext } from './log-context';
|
|
5
6
|
export declare const baseLogger: pino.Logger<never, boolean>;
|
|
6
|
-
export declare const logger: pino.Logger
|
|
7
|
+
export declare const logger: pino.Logger;
|
|
7
8
|
/**
|
|
8
9
|
* Add a stream destination to the logger's multistream.
|
|
9
10
|
* Use this to wire additional log sinks (e.g. InMemoryLogStream in app-framework).
|
package/lib/logger/index.js
CHANGED
|
@@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
var _a, _b, _c, _d, _e, _f, _g;
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.logger = exports.baseLogger = exports.ATTR_LOG_PAYLOAD = exports.ATTR_LOG_MESSAGE = exports.PINO_LEVEL_TO_NAME = exports.PINO_LEVEL_TO_OTEL_SEVERITY = exports.parseOtelResourceAttributes = void 0;
|
|
7
|
+
exports.logger = exports.baseLogger = exports.withLogContext = exports.loggerStorage = exports.ATTR_LOG_PAYLOAD = exports.ATTR_LOG_MESSAGE = exports.PINO_LEVEL_TO_NAME = exports.PINO_LEVEL_TO_OTEL_SEVERITY = exports.parseOtelResourceAttributes = void 0;
|
|
8
8
|
exports.addStream = addStream;
|
|
9
9
|
exports.flushLogs = flushLogs;
|
|
10
10
|
const pino_1 = __importDefault(require("pino"));
|
|
@@ -16,7 +16,7 @@ const resources_1 = require("@opentelemetry/resources");
|
|
|
16
16
|
const api_1 = require("@opentelemetry/api");
|
|
17
17
|
const stream_1 = require("stream");
|
|
18
18
|
const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
|
|
19
|
-
const
|
|
19
|
+
const util_1 = require("@contrail/util");
|
|
20
20
|
const logger_config_1 = require("./logger-config");
|
|
21
21
|
const parse_otel_resource_attributes_1 = require("./parse-otel-resource-attributes");
|
|
22
22
|
const semantic_conventions_2 = require("./semantic-conventions");
|
|
@@ -29,6 +29,9 @@ Object.defineProperty(exports, "PINO_LEVEL_TO_NAME", { enumerable: true, get: fu
|
|
|
29
29
|
var semantic_conventions_3 = require("./semantic-conventions");
|
|
30
30
|
Object.defineProperty(exports, "ATTR_LOG_MESSAGE", { enumerable: true, get: function () { return semantic_conventions_3.ATTR_LOG_MESSAGE; } });
|
|
31
31
|
Object.defineProperty(exports, "ATTR_LOG_PAYLOAD", { enumerable: true, get: function () { return semantic_conventions_3.ATTR_LOG_PAYLOAD; } });
|
|
32
|
+
var log_context_1 = require("./log-context");
|
|
33
|
+
Object.defineProperty(exports, "loggerStorage", { enumerable: true, get: function () { return log_context_1.loggerStorage; } });
|
|
34
|
+
Object.defineProperty(exports, "withLogContext", { enumerable: true, get: function () { return log_context_1.withLogContext; } });
|
|
32
35
|
// --- Environment & Resource Setup ---
|
|
33
36
|
const semantic_conventions_4 = require("../semantic-conventions");
|
|
34
37
|
const LAMBDA_ENV_OTEL_ATTRIBUTES = getLambdaEnvAttributes();
|
|
@@ -115,7 +118,7 @@ function createOtelStream() {
|
|
|
115
118
|
const lambdaEnvAttributes = LAMBDA_ENV_OTEL_ATTRIBUTES;
|
|
116
119
|
let safeAttributes;
|
|
117
120
|
try {
|
|
118
|
-
safeAttributes =
|
|
121
|
+
safeAttributes = util_1.ObjectUtil.flattenObject(attributes);
|
|
119
122
|
}
|
|
120
123
|
catch {
|
|
121
124
|
// Fallback in case flattening fails somehow. This is not expected.
|
|
@@ -230,7 +233,8 @@ exports.baseLogger = (0, pino_1.default)({
|
|
|
230
233
|
[semantic_conventions_1.ATTR_SERVICE_VERSION]: serviceVersion,
|
|
231
234
|
},
|
|
232
235
|
}, pino_1.default.multistream(buildPinoStreams()));
|
|
233
|
-
|
|
236
|
+
const log_context_2 = require("./log-context");
|
|
237
|
+
exports.logger = (0, log_context_2.createContextAwareLogger)(exports.baseLogger);
|
|
234
238
|
// --- Public API ---
|
|
235
239
|
/**
|
|
236
240
|
* Add a stream destination to the logger's multistream.
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'async_hooks';
|
|
2
|
+
import type { Logger } from 'pino';
|
|
3
|
+
export declare const loggerStorage: AsyncLocalStorage<Logger>;
|
|
4
|
+
export declare function createContextAwareLogger(baseLogger: Logger): Logger;
|
|
5
|
+
export declare function withLogContext<T>(fields: Record<string, unknown>, fn: () => T): T;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loggerStorage = void 0;
|
|
4
|
+
exports.createContextAwareLogger = createContextAwareLogger;
|
|
5
|
+
exports.withLogContext = withLogContext;
|
|
6
|
+
const async_hooks_1 = require("async_hooks");
|
|
7
|
+
exports.loggerStorage = new async_hooks_1.AsyncLocalStorage();
|
|
8
|
+
let _baseLogger;
|
|
9
|
+
function createContextAwareLogger(baseLogger) {
|
|
10
|
+
_baseLogger = baseLogger;
|
|
11
|
+
return new Proxy(baseLogger, {
|
|
12
|
+
get(target, prop, receiver) {
|
|
13
|
+
var _a;
|
|
14
|
+
const currentLogger = (_a = exports.loggerStorage.getStore()) !== null && _a !== void 0 ? _a : target;
|
|
15
|
+
return Reflect.get(currentLogger, prop, receiver);
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
function withLogContext(fields, fn) {
|
|
20
|
+
var _a;
|
|
21
|
+
const parent = (_a = exports.loggerStorage.getStore()) !== null && _a !== void 0 ? _a : _baseLogger;
|
|
22
|
+
const child = parent.child(fields);
|
|
23
|
+
return exports.loggerStorage.run(child, fn);
|
|
24
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contrail/telemetry",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-dev-telemetry-own-otel-2",
|
|
4
4
|
"description": "Telemetry and monitoring utilities for contrail services",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
"license": "ISC",
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"@codegenie/serverless-express": "^4.16.0",
|
|
18
|
+
"@contrail/util": "^1.2.1",
|
|
18
19
|
"@opentelemetry/api": "^1.9.0",
|
|
19
20
|
"@opentelemetry/api-logs": "^0.211.0",
|
|
20
21
|
"@opentelemetry/core": "^2.5.0",
|
|
@@ -34,7 +35,7 @@
|
|
|
34
35
|
}
|
|
35
36
|
},
|
|
36
37
|
"devDependencies": {
|
|
37
|
-
"@types/aws-lambda": "^8.10.
|
|
38
|
+
"@types/aws-lambda": "^8.10.145",
|
|
38
39
|
"@types/jest": "^29.5.2",
|
|
39
40
|
"@types/node": "^20.0.0",
|
|
40
41
|
"jest": "^29.5.0",
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.flattenObject = flattenObject;
|
|
4
|
-
const MAX_DEPTH = 10;
|
|
5
|
-
function flattenObject(obj) {
|
|
6
|
-
const seen = new WeakSet();
|
|
7
|
-
return flattenRecursive(obj, '', seen, 0);
|
|
8
|
-
}
|
|
9
|
-
function flattenRecursive(obj, prefix, ancestors, depth) {
|
|
10
|
-
const result = {};
|
|
11
|
-
if (depth > MAX_DEPTH) {
|
|
12
|
-
result[prefix || 'value'] = '[max depth exceeded]';
|
|
13
|
-
return result;
|
|
14
|
-
}
|
|
15
|
-
ancestors.add(obj);
|
|
16
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
17
|
-
const fullKey = prefix ? `${prefix}.${key}` : key;
|
|
18
|
-
if (value === null || value === undefined) {
|
|
19
|
-
continue;
|
|
20
|
-
}
|
|
21
|
-
if (typeof value === 'object') {
|
|
22
|
-
if (ancestors.has(value)) {
|
|
23
|
-
result[fullKey] = '[circular reference]';
|
|
24
|
-
continue;
|
|
25
|
-
}
|
|
26
|
-
if (Array.isArray(value)) {
|
|
27
|
-
result[fullKey] = JSON.stringify(value);
|
|
28
|
-
}
|
|
29
|
-
else if (value instanceof Date) {
|
|
30
|
-
result[fullKey] = value.toISOString();
|
|
31
|
-
}
|
|
32
|
-
else if (value instanceof Error) {
|
|
33
|
-
result[fullKey] = value.message;
|
|
34
|
-
if (value.stack) {
|
|
35
|
-
result[`${fullKey}.stack`] = value.stack;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
Object.assign(result, flattenRecursive(value, fullKey, ancestors, depth + 1));
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
else if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
43
|
-
result[fullKey] = value;
|
|
44
|
-
}
|
|
45
|
-
else if (typeof value === 'bigint') {
|
|
46
|
-
result[fullKey] = value.toString();
|
|
47
|
-
}
|
|
48
|
-
else if (typeof value === 'symbol') {
|
|
49
|
-
result[fullKey] = value.toString();
|
|
50
|
-
}
|
|
51
|
-
else if (typeof value === 'function') {
|
|
52
|
-
result[fullKey] = '[function]';
|
|
53
|
-
}
|
|
54
|
-
else {
|
|
55
|
-
result[fullKey] = String(value);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
ancestors.delete(obj);
|
|
59
|
-
return result;
|
|
60
|
-
}
|