@devopsplaybook.io/otel-utils 1.0.2 → 1.0.3

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.
@@ -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
  }
@@ -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.WARN);
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
- // eslint:disable-next-line:no-console
28
- console.log(`[${level}] [${this.module}] ${message}`);
30
+ formattedMessage = message;
29
31
  }
30
32
  else if (message instanceof Error) {
31
- // eslint:disable-next-line:no-console
32
- console.log(`${level} [${this.module}] ${message}`);
33
- // eslint:disable-next-line:no-console
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
- // eslint:disable-next-line:no-console
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: message,
47
- attributes: { "log.type": "custom" },
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.2",
3
+ "version": "1.0.3",
4
4
  "description": "Utility to simplify integration with Open Telemetry",
5
5
  "keywords": [
6
6
  "Open",
@@ -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.WARN);
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
- // eslint:disable-next-line:no-console
34
- console.log(`[${level}] [${this.module}] ${message}`);
39
+ formattedMessage = message;
35
40
  } else if (message instanceof Error) {
36
- // eslint:disable-next-line:no-console
37
- console.log(`${level} [${this.module}] ${message}`);
38
- // eslint:disable-next-line:no-console
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
- // eslint:disable-next-line:no-console
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: message,
51
- attributes: { "log.type": "custom" },
65
+ body: `[${this.module}] ${formattedMessage}`,
66
+ attributes,
52
67
  });
53
68
  }
54
69
  }