@devopsplaybook.io/otel-utils 1.0.1 → 1.0.3-beta1
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/src/ModuleLogger.d.ts +4 -3
- package/dist/src/ModuleLogger.js +26 -16
- package/package.json +14 -14
- package/src/ModuleLogger.ts +32 -17
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { StandardLoggerInterface } from "./models/StandardLoggerInterface";
|
|
2
|
+
import { Span } from "@opentelemetry/sdk-trace-base";
|
|
2
3
|
export declare class ModuleLogger {
|
|
3
4
|
private module;
|
|
4
5
|
private standardLogger?;
|
|
5
6
|
constructor(module: string, standardLogger: StandardLoggerInterface);
|
|
6
|
-
info(message: Error | string | any): void;
|
|
7
|
-
warn(message: Error | string | any): void;
|
|
8
|
-
error(message: Error | string | any): void;
|
|
7
|
+
info(message: Error | string | any, context?: Span): void;
|
|
8
|
+
warn(message: Error | string | any, context?: Span): void;
|
|
9
|
+
error(message: Error | string | any, context?: Span): void;
|
|
9
10
|
private display;
|
|
10
11
|
}
|
package/dist/src/ModuleLogger.js
CHANGED
|
@@ -8,43 +8,53 @@ class ModuleLogger {
|
|
|
8
8
|
this.standardLogger = standardLogger;
|
|
9
9
|
}
|
|
10
10
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
11
|
-
info(message) {
|
|
12
|
-
this.display("info", message, api_logs_1.SeverityNumber.
|
|
11
|
+
info(message, context) {
|
|
12
|
+
this.display("info", message, api_logs_1.SeverityNumber.INFO, context);
|
|
13
13
|
}
|
|
14
14
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
15
|
-
warn(message) {
|
|
16
|
-
this.display("warn", message, api_logs_1.SeverityNumber.WARN);
|
|
15
|
+
warn(message, context) {
|
|
16
|
+
this.display("warn", message, api_logs_1.SeverityNumber.WARN, context);
|
|
17
17
|
}
|
|
18
18
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
19
|
-
error(message) {
|
|
20
|
-
this.display("error", message, api_logs_1.SeverityNumber.ERROR);
|
|
19
|
+
error(message, context) {
|
|
20
|
+
this.display("error", message, api_logs_1.SeverityNumber.ERROR, context);
|
|
21
21
|
}
|
|
22
22
|
display(level,
|
|
23
23
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
24
|
-
message, severityNumber = api_logs_1.SeverityNumber.INFO) {
|
|
24
|
+
message, severityNumber = api_logs_1.SeverityNumber.INFO, context) {
|
|
25
25
|
var _a, _b;
|
|
26
|
+
let formattedMessage = "";
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
28
|
+
const attributes = { "log.type": "custom" };
|
|
26
29
|
if (typeof message === "string") {
|
|
27
|
-
|
|
28
|
-
console.log(`[${level}] [${this.module}] ${message}`);
|
|
30
|
+
formattedMessage = message;
|
|
29
31
|
}
|
|
30
32
|
else if (message instanceof Error) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
formattedMessage = message.message;
|
|
34
|
+
attributes["exception.type"] = message.name;
|
|
35
|
+
attributes["exception.message"] = message.message;
|
|
36
|
+
attributes["exception.stacktrace"] = message.stack;
|
|
34
37
|
console.log(message.stack);
|
|
35
38
|
}
|
|
36
39
|
else if (typeof message === "object") {
|
|
37
|
-
|
|
38
|
-
console.log(`${level} [${this.module}] ${JSON.stringify(message)}`);
|
|
40
|
+
formattedMessage = JSON.stringify(message);
|
|
39
41
|
}
|
|
42
|
+
if (context) {
|
|
43
|
+
const spanCtx = context.spanContext();
|
|
44
|
+
if (spanCtx) {
|
|
45
|
+
attributes["span.id"] = spanCtx.spanId;
|
|
46
|
+
attributes["trace.id"] = spanCtx.traceId;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
console.log(`[${level}] [${this.module}] ${formattedMessage}`);
|
|
40
50
|
if (!((_a = this.standardLogger) === null || _a === void 0 ? void 0 : _a.getLogger())) {
|
|
41
51
|
return;
|
|
42
52
|
}
|
|
43
53
|
(_b = this.standardLogger.getLogger()) === null || _b === void 0 ? void 0 : _b.emit({
|
|
44
54
|
severityNumber,
|
|
45
55
|
severityText: level,
|
|
46
|
-
body:
|
|
47
|
-
attributes
|
|
56
|
+
body: `[${this.module}] ${formattedMessage}`,
|
|
57
|
+
attributes,
|
|
48
58
|
});
|
|
49
59
|
}
|
|
50
60
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devopsplaybook.io/otel-utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3-beta1",
|
|
4
4
|
"description": "Utility to simplify integration with Open Telemetry",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Open",
|
|
@@ -20,24 +20,24 @@
|
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"@opentelemetry/api": "^1.9.0",
|
|
23
|
-
"@opentelemetry/api-logs": "^0.
|
|
24
|
-
"@opentelemetry/auto-instrumentations-node": "^0.62.
|
|
25
|
-
"@opentelemetry/exporter-logs-otlp-http": "^0.
|
|
26
|
-
"@opentelemetry/exporter-trace-otlp-http": "^0.
|
|
27
|
-
"@opentelemetry/id-generator-aws-xray": "^2.0.
|
|
28
|
-
"@opentelemetry/resources": "^2.0
|
|
29
|
-
"@opentelemetry/sdk-logs": "^0.
|
|
30
|
-
"@opentelemetry/sdk-node": "^0.
|
|
31
|
-
"@opentelemetry/sdk-trace-base": "^2.0
|
|
32
|
-
"@opentelemetry/sdk-trace-web": "^2.0
|
|
33
|
-
"@opentelemetry/semantic-conventions": "^1.
|
|
23
|
+
"@opentelemetry/api-logs": "^0.204.0",
|
|
24
|
+
"@opentelemetry/auto-instrumentations-node": "^0.62.2",
|
|
25
|
+
"@opentelemetry/exporter-logs-otlp-http": "^0.204.0",
|
|
26
|
+
"@opentelemetry/exporter-trace-otlp-http": "^0.204.0",
|
|
27
|
+
"@opentelemetry/id-generator-aws-xray": "^2.0.1",
|
|
28
|
+
"@opentelemetry/resources": "^2.1.0",
|
|
29
|
+
"@opentelemetry/sdk-logs": "^0.204.0",
|
|
30
|
+
"@opentelemetry/sdk-node": "^0.204.0",
|
|
31
|
+
"@opentelemetry/sdk-trace-base": "^2.1.0",
|
|
32
|
+
"@opentelemetry/sdk-trace-web": "^2.1.0",
|
|
33
|
+
"@opentelemetry/semantic-conventions": "^1.37.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@eslint/js": "^9.34.0",
|
|
37
|
-
"@types/node": "^24.3.
|
|
37
|
+
"@types/node": "^24.3.1",
|
|
38
38
|
"@types/sqlite3": "^5.1.0",
|
|
39
39
|
"ts-node": "^10.9.2",
|
|
40
|
-
"typescript-eslint": "^8.
|
|
40
|
+
"typescript-eslint": "^8.42.0",
|
|
41
41
|
"typescript": "^5.9.2"
|
|
42
42
|
},
|
|
43
43
|
"publishConfig": {
|
package/src/ModuleLogger.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { SeverityNumber } from "@opentelemetry/api-logs";
|
|
2
2
|
import { StandardLoggerInterface } from "./models/StandardLoggerInterface";
|
|
3
|
+
import { Span } from "@opentelemetry/sdk-trace-base";
|
|
3
4
|
|
|
4
5
|
export class ModuleLogger {
|
|
5
6
|
private module: string;
|
|
@@ -11,44 +12,58 @@ export class ModuleLogger {
|
|
|
11
12
|
}
|
|
12
13
|
|
|
13
14
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
14
|
-
public info(message: Error | string | any): void {
|
|
15
|
-
this.display("info", message, SeverityNumber.
|
|
15
|
+
public info(message: Error | string | any, context?: Span): void {
|
|
16
|
+
this.display("info", message, SeverityNumber.INFO, context);
|
|
16
17
|
}
|
|
17
18
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
|
-
public warn(message: Error | string | any): void {
|
|
19
|
-
this.display("warn", message, SeverityNumber.WARN);
|
|
19
|
+
public warn(message: Error | string | any, context?: Span): void {
|
|
20
|
+
this.display("warn", message, SeverityNumber.WARN, context);
|
|
20
21
|
}
|
|
21
22
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
22
|
-
public error(message: Error | string | any): void {
|
|
23
|
-
this.display("error", message, SeverityNumber.ERROR);
|
|
23
|
+
public error(message: Error | string | any, context?: Span): void {
|
|
24
|
+
this.display("error", message, SeverityNumber.ERROR, context);
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
private display(
|
|
27
28
|
level: string,
|
|
28
29
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
29
|
-
message: any,
|
|
30
|
-
severityNumber = SeverityNumber.INFO
|
|
30
|
+
message: string | Error | any,
|
|
31
|
+
severityNumber = SeverityNumber.INFO,
|
|
32
|
+
context?: Span
|
|
31
33
|
): void {
|
|
34
|
+
let formattedMessage = "";
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
36
|
+
const attributes: Record<string, any> = { "log.type": "custom" };
|
|
37
|
+
|
|
32
38
|
if (typeof message === "string") {
|
|
33
|
-
|
|
34
|
-
console.log(`[${level}] [${this.module}] ${message}`);
|
|
39
|
+
formattedMessage = message;
|
|
35
40
|
} else if (message instanceof Error) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
41
|
+
formattedMessage = message.message;
|
|
42
|
+
attributes["exception.type"] = message.name;
|
|
43
|
+
attributes["exception.message"] = message.message;
|
|
44
|
+
attributes["exception.stacktrace"] = message.stack;
|
|
39
45
|
console.log((message as Error).stack);
|
|
40
46
|
} else if (typeof message === "object") {
|
|
41
|
-
|
|
42
|
-
console.log(`${level} [${this.module}] ${JSON.stringify(message)}`);
|
|
47
|
+
formattedMessage = JSON.stringify(message);
|
|
43
48
|
}
|
|
49
|
+
|
|
50
|
+
if (context) {
|
|
51
|
+
const spanCtx = context.spanContext();
|
|
52
|
+
if (spanCtx) {
|
|
53
|
+
attributes["span.id"] = spanCtx.spanId;
|
|
54
|
+
attributes["trace.id"] = spanCtx.traceId;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
console.log(`[${level}] [${this.module}] ${formattedMessage}`);
|
|
44
59
|
if (!this.standardLogger?.getLogger()) {
|
|
45
60
|
return;
|
|
46
61
|
}
|
|
47
62
|
this.standardLogger.getLogger()?.emit({
|
|
48
63
|
severityNumber,
|
|
49
64
|
severityText: level,
|
|
50
|
-
body:
|
|
51
|
-
attributes
|
|
65
|
+
body: `[${this.module}] ${formattedMessage}`,
|
|
66
|
+
attributes,
|
|
52
67
|
});
|
|
53
68
|
}
|
|
54
69
|
}
|