@sebspark/otel 0.4.2 → 0.4.4

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/index.d.mts CHANGED
@@ -15,6 +15,8 @@ declare function getLogger(serviceOverride?: string, extraAttrs?: Attrs): {
15
15
 
16
16
  declare function getMeter(componentNameOverride?: string): Promise<_opentelemetry_api.Meter>;
17
17
 
18
+ declare function initialize(): Promise<void>;
19
+
18
20
  type OtelTracer = ReturnType<typeof trace.getTracer>;
19
21
  type Span = ReturnType<OtelTracer['startSpan']>;
20
22
  type Func<T> = (span: Span) => Promise<T> | T;
@@ -47,4 +49,4 @@ interface Tracer extends OtelTracer {
47
49
  */
48
50
  declare function getTracer(componentNameOverride?: string): Promise<Tracer>;
49
51
 
50
- export { getLogger, getMeter, getTracer };
52
+ export { getLogger, getMeter, getTracer, initialize };
package/dist/index.d.ts CHANGED
@@ -15,6 +15,8 @@ declare function getLogger(serviceOverride?: string, extraAttrs?: Attrs): {
15
15
 
16
16
  declare function getMeter(componentNameOverride?: string): Promise<_opentelemetry_api.Meter>;
17
17
 
18
+ declare function initialize(): Promise<void>;
19
+
18
20
  type OtelTracer = ReturnType<typeof trace.getTracer>;
19
21
  type Span = ReturnType<OtelTracer['startSpan']>;
20
22
  type Func<T> = (span: Span) => Promise<T> | T;
@@ -47,4 +49,4 @@ interface Tracer extends OtelTracer {
47
49
  */
48
50
  declare function getTracer(componentNameOverride?: string): Promise<Tracer>;
49
51
 
50
- export { getLogger, getMeter, getTracer };
52
+ export { getLogger, getMeter, getTracer, initialize };
package/dist/index.js CHANGED
@@ -32,26 +32,14 @@ var index_exports = {};
32
32
  __export(index_exports, {
33
33
  getLogger: () => getLogger,
34
34
  getMeter: () => getMeter,
35
- getTracer: () => getTracer
35
+ getTracer: () => getTracer,
36
+ initialize: () => initialize
36
37
  });
37
38
  module.exports = __toCommonJS(index_exports);
38
39
 
39
- // src/otel.ts
40
- var import_api3 = require("@opentelemetry/api");
40
+ // src/logger.ts
41
+ var import_api = require("@opentelemetry/api");
41
42
  var import_api_logs = require("@opentelemetry/api-logs");
42
- var import_auto_instrumentations_node = require("@opentelemetry/auto-instrumentations-node");
43
- var import_sdk_node = require("@opentelemetry/sdk-node");
44
-
45
- // src/providers.ts
46
- var import_exporter_logs_otlp_http = require("@opentelemetry/exporter-logs-otlp-http");
47
- var import_exporter_metrics_otlp_http = require("@opentelemetry/exporter-metrics-otlp-http");
48
- var import_exporter_trace_otlp_http = require("@opentelemetry/exporter-trace-otlp-http");
49
- var import_sdk_logs = require("@opentelemetry/sdk-logs");
50
- var import_sdk_metrics = require("@opentelemetry/sdk-metrics");
51
- var import_sdk_trace_node = require("@opentelemetry/sdk-trace-node");
52
-
53
- // src/loggers/console-log-pretty-exporter.ts
54
- var import_core = require("@opentelemetry/core");
55
43
 
56
44
  // src/consts.ts
57
45
  var LOG_SEVERITY_MAP = {
@@ -68,8 +56,119 @@ var LOG_SEVERITY_MAP = {
68
56
  EMERGENCY: 23
69
57
  };
70
58
 
71
- // src/loggers/formatters/shared.ts
59
+ // src/otel-context.ts
72
60
  var import_semantic_conventions = require("@opentelemetry/semantic-conventions");
61
+ function detectTelemetryContext(componentNameOverride) {
62
+ const {
63
+ OTEL_SERVICE_NAME,
64
+ // e.g. "UserSystem"
65
+ OTEL_SERVICE_VERSION,
66
+ // e.g. "1.2.3"
67
+ K_SERVICE,
68
+ K_REVISION,
69
+ K_CONFIGURATION,
70
+ KUBERNETES_SERVICE_HOST,
71
+ POD_NAME,
72
+ POD_NAMESPACE,
73
+ GCP_PROJECT,
74
+ CLOUD_PROVIDER
75
+ } = process.env;
76
+ const systemName = OTEL_SERVICE_NAME || "unknown-service";
77
+ const systemVersion = OTEL_SERVICE_VERSION || "1.0.0";
78
+ const componentName = componentNameOverride || void 0;
79
+ const resourceAttributes = {
80
+ [import_semantic_conventions.ATTR_SERVICE_NAME]: systemName,
81
+ [import_semantic_conventions.ATTR_SERVICE_VERSION]: systemVersion,
82
+ "serviceContext.service": systemName,
83
+ "serviceContext.version": systemVersion,
84
+ ...K_SERVICE && { "cloud.run.service": K_SERVICE },
85
+ ...K_REVISION && { "cloud.run.revision": K_REVISION },
86
+ ...K_CONFIGURATION && { "cloud.run.configuration": K_CONFIGURATION },
87
+ ...POD_NAME && { "k8s.pod_name": POD_NAME },
88
+ ...POD_NAMESPACE && { "k8s.namespace_name": POD_NAMESPACE },
89
+ ...KUBERNETES_SERVICE_HOST && { "cloud.orchestrator": "kubernetes" },
90
+ ...GCP_PROJECT && { "cloud.account.id": GCP_PROJECT },
91
+ ...CLOUD_PROVIDER && { "cloud.provider": CLOUD_PROVIDER },
92
+ ...componentName && { "component.name": componentName }
93
+ };
94
+ return {
95
+ systemName,
96
+ systemVersion,
97
+ componentName,
98
+ resourceAttributes
99
+ };
100
+ }
101
+
102
+ // src/logger.ts
103
+ function getLogger(serviceOverride, extraAttrs = {}) {
104
+ const { systemName, systemVersion, resourceAttributes } = detectTelemetryContext(serviceOverride);
105
+ const defaultAttrs = {
106
+ ...resourceAttributes,
107
+ ...extraAttrs
108
+ };
109
+ function emit(severityText, body, attrs = {}) {
110
+ const logger = import_api_logs.logs.getLogger(systemName, systemVersion);
111
+ const span = import_api.trace.getSpan(import_api.context.active());
112
+ const spanContext = span?.spanContext();
113
+ logger.emit({
114
+ severityText,
115
+ severityNumber: LOG_SEVERITY_MAP[severityText],
116
+ body,
117
+ attributes: {
118
+ ...defaultAttrs,
119
+ ...spanContext && {
120
+ trace_id: spanContext.traceId,
121
+ span_id: spanContext.spanId
122
+ },
123
+ ...attrs
124
+ }
125
+ });
126
+ }
127
+ return {
128
+ debug: (msg, attrs) => emit("DEBUG", msg, attrs),
129
+ info: (msg, attrs) => emit("INFO", msg, attrs),
130
+ notice: (msg, attrs) => emit("NOTICE", msg, attrs),
131
+ warn: (msg, attrs) => emit("WARNING", msg, attrs),
132
+ error: (msg, errOrAttrs, maybeAttrs = {}) => {
133
+ let body;
134
+ let attrs;
135
+ if (errOrAttrs instanceof Error) {
136
+ body = `${msg}: ${errOrAttrs.stack || errOrAttrs.message}`;
137
+ attrs = maybeAttrs;
138
+ } else {
139
+ body = msg instanceof Error ? msg.stack || msg.message : msg;
140
+ attrs = errOrAttrs || {};
141
+ }
142
+ emit("ERROR", body, attrs);
143
+ },
144
+ critical: (msg, attrs) => emit("CRITICAL", msg, attrs),
145
+ alert: (msg, attrs) => emit("ALERT", msg, attrs),
146
+ emergency: (msg, attrs) => emit("EMERGENCY", msg, attrs)
147
+ };
148
+ }
149
+
150
+ // src/metrics.ts
151
+ var import_api5 = require("@opentelemetry/api");
152
+
153
+ // src/otel.ts
154
+ var import_api4 = require("@opentelemetry/api");
155
+ var import_api_logs2 = require("@opentelemetry/api-logs");
156
+ var import_auto_instrumentations_node = require("@opentelemetry/auto-instrumentations-node");
157
+ var import_sdk_node = require("@opentelemetry/sdk-node");
158
+
159
+ // src/providers.ts
160
+ var import_exporter_logs_otlp_http = require("@opentelemetry/exporter-logs-otlp-http");
161
+ var import_exporter_metrics_otlp_http = require("@opentelemetry/exporter-metrics-otlp-http");
162
+ var import_exporter_trace_otlp_http = require("@opentelemetry/exporter-trace-otlp-http");
163
+ var import_sdk_logs = require("@opentelemetry/sdk-logs");
164
+ var import_sdk_metrics = require("@opentelemetry/sdk-metrics");
165
+ var import_sdk_trace_node = require("@opentelemetry/sdk-trace-node");
166
+
167
+ // src/loggers/console-log-pretty-exporter.ts
168
+ var import_core = require("@opentelemetry/core");
169
+
170
+ // src/loggers/formatters/shared.ts
171
+ var import_semantic_conventions2 = require("@opentelemetry/semantic-conventions");
73
172
  var import_fast_safe_stringify = __toESM(require("fast-safe-stringify"));
74
173
 
75
174
  // src/loggers/formatters/style.ts
@@ -130,8 +229,8 @@ function formatTimestamp(time) {
130
229
  return colors.dim(date.toISOString().slice(11, 23));
131
230
  }
132
231
  function formatService(resource) {
133
- const name = resource.attributes[import_semantic_conventions.ATTR_SERVICE_NAME] ?? "unknown-service";
134
- const version = resource.attributes[import_semantic_conventions.ATTR_SERVICE_VERSION] ?? "1.0.0";
232
+ const name = resource.attributes[import_semantic_conventions2.ATTR_SERVICE_NAME] ?? "unknown-service";
233
+ const version = resource.attributes[import_semantic_conventions2.ATTR_SERVICE_VERSION] ?? "1.0.0";
135
234
  return colors.gray(`[${name}@${version}]`);
136
235
  }
137
236
  function formatLevel(record) {
@@ -221,7 +320,7 @@ function isHistogramLike(val) {
221
320
  }
222
321
 
223
322
  // src/loggers/formatters/span.ts
224
- var import_api = require("@opentelemetry/api");
323
+ var import_api2 = require("@opentelemetry/api");
225
324
  var LABEL_WIDTH = 20;
226
325
  var DESCRIPTION_MAX_WIDTH = 16;
227
326
  var BAR_MIN_WIDTH = 1;
@@ -251,7 +350,7 @@ function formatSpans(spans) {
251
350
  function formatSpan(span, opts) {
252
351
  const label = formatLabel(span, opts.depth);
253
352
  const bar = buildBar(span, opts?.offsetMs, opts?.totalDurationMs);
254
- const barColor = span.status.code === import_api.SpanStatusCode.OK ? colors.green : span.status.code === import_api.SpanStatusCode.ERROR ? colors.red : colors.gray;
353
+ const barColor = span.status.code === import_api2.SpanStatusCode.OK ? colors.green : span.status.code === import_api2.SpanStatusCode.ERROR ? colors.red : colors.gray;
255
354
  const desc = formatDescription(span);
256
355
  const status = formatStatus(span);
257
356
  const duration = formatDuration(span, opts?.offsetMs);
@@ -406,7 +505,7 @@ var ConsoleLogPrettyExporter = class {
406
505
  };
407
506
 
408
507
  // src/loggers/console-span-pretty-exporter.ts
409
- var import_api2 = require("@opentelemetry/api");
508
+ var import_api3 = require("@opentelemetry/api");
410
509
  var import_core2 = require("@opentelemetry/core");
411
510
  var ConsoleSpanPrettyExporter = class {
412
511
  allowedStatuses;
@@ -414,15 +513,15 @@ var ConsoleSpanPrettyExporter = class {
414
513
  const env = process.env.SPAN_LEVEL?.toUpperCase();
415
514
  if (!env) {
416
515
  this.allowedStatuses = /* @__PURE__ */ new Set([
417
- import_api2.SpanStatusCode.UNSET,
418
- import_api2.SpanStatusCode.OK,
419
- import_api2.SpanStatusCode.ERROR
516
+ import_api3.SpanStatusCode.UNSET,
517
+ import_api3.SpanStatusCode.OK,
518
+ import_api3.SpanStatusCode.ERROR
420
519
  ]);
421
520
  } else {
422
521
  const map = {
423
- UNSET: import_api2.SpanStatusCode.UNSET,
424
- OK: import_api2.SpanStatusCode.OK,
425
- ERROR: import_api2.SpanStatusCode.ERROR
522
+ UNSET: import_api3.SpanStatusCode.UNSET,
523
+ OK: import_api3.SpanStatusCode.OK,
524
+ ERROR: import_api3.SpanStatusCode.ERROR
426
525
  };
427
526
  this.allowedStatuses = new Set(
428
527
  env.split(",").map((s) => s.trim()).map((s) => map[s]).filter((v) => typeof v === "number")
@@ -580,51 +679,6 @@ var getMetricReader = (otlpEndpoint) => {
580
679
 
581
680
  // src/resource.ts
582
681
  var import_resources = require("@opentelemetry/resources");
583
-
584
- // src/otel-context.ts
585
- var import_semantic_conventions2 = require("@opentelemetry/semantic-conventions");
586
- function detectTelemetryContext(componentNameOverride) {
587
- const {
588
- OTEL_SERVICE_NAME,
589
- // e.g. "UserSystem"
590
- OTEL_SERVICE_VERSION,
591
- // e.g. "1.2.3"
592
- K_SERVICE,
593
- K_REVISION,
594
- K_CONFIGURATION,
595
- KUBERNETES_SERVICE_HOST,
596
- POD_NAME,
597
- POD_NAMESPACE,
598
- GCP_PROJECT,
599
- CLOUD_PROVIDER
600
- } = process.env;
601
- const systemName = OTEL_SERVICE_NAME || "unknown-service";
602
- const systemVersion = OTEL_SERVICE_VERSION || "1.0.0";
603
- const componentName = componentNameOverride || void 0;
604
- const resourceAttributes = {
605
- [import_semantic_conventions2.ATTR_SERVICE_NAME]: systemName,
606
- [import_semantic_conventions2.ATTR_SERVICE_VERSION]: systemVersion,
607
- "serviceContext.service": systemName,
608
- "serviceContext.version": systemVersion,
609
- ...K_SERVICE && { "cloud.run.service": K_SERVICE },
610
- ...K_REVISION && { "cloud.run.revision": K_REVISION },
611
- ...K_CONFIGURATION && { "cloud.run.configuration": K_CONFIGURATION },
612
- ...POD_NAME && { "k8s.pod_name": POD_NAME },
613
- ...POD_NAMESPACE && { "k8s.namespace_name": POD_NAMESPACE },
614
- ...KUBERNETES_SERVICE_HOST && { "cloud.orchestrator": "kubernetes" },
615
- ...GCP_PROJECT && { "cloud.account.id": GCP_PROJECT },
616
- ...CLOUD_PROVIDER && { "cloud.provider": CLOUD_PROVIDER },
617
- ...componentName && { "component.name": componentName }
618
- };
619
- return {
620
- systemName,
621
- systemVersion,
622
- componentName,
623
- resourceAttributes
624
- };
625
- }
626
-
627
- // src/resource.ts
628
682
  var getResource = async () => {
629
683
  const baseRes = await (0, import_resources.detectResources)();
630
684
  const { resourceAttributes } = detectTelemetryContext();
@@ -637,7 +691,7 @@ var getResource = async () => {
637
691
  };
638
692
 
639
693
  // src/otel.ts
640
- import_api3.diag.setLogger(new import_api3.DiagConsoleLogger(), import_api3.DiagLogLevel.ERROR);
694
+ import_api4.diag.setLogger(new import_api4.DiagConsoleLogger(), import_api4.DiagLogLevel.ERROR);
641
695
  var initialization;
642
696
  async function initialize() {
643
697
  if (!initialization) {
@@ -651,7 +705,7 @@ async function _initialize() {
651
705
  const otlpEndpoint = process.env.OTEL_EXPORTER_OTLP_ENDPOINT;
652
706
  const resource = await getResource();
653
707
  const logProvider = getLogProvider(resource, otlpEndpoint);
654
- import_api_logs.logs.setGlobalLoggerProvider(logProvider);
708
+ import_api_logs2.logs.setGlobalLoggerProvider(logProvider);
655
709
  const spanProcessor = getSpanProcessor(otlpEndpoint);
656
710
  const metricReader = getMetricReader(otlpEndpoint);
657
711
  const sdk = new import_sdk_node.NodeSDK({
@@ -673,66 +727,7 @@ async function _initialize() {
673
727
  }
674
728
  }
675
729
 
676
- // src/logger.ts
677
- var import_api4 = require("@opentelemetry/api");
678
- var import_api_logs2 = require("@opentelemetry/api-logs");
679
- function getLogger(serviceOverride, extraAttrs = {}) {
680
- const { systemName, systemVersion, resourceAttributes } = detectTelemetryContext(serviceOverride);
681
- const defaultAttrs = {
682
- ...resourceAttributes,
683
- ...extraAttrs
684
- };
685
- let isInitialized = false;
686
- function emit(severityText, body, attrs = {}) {
687
- if (!isInitialized) {
688
- initialize().then(() => {
689
- isInitialized = true;
690
- emit(severityText, body, attrs);
691
- });
692
- return;
693
- }
694
- const logger = import_api_logs2.logs.getLogger(systemName, systemVersion);
695
- const span = import_api4.trace.getSpan(import_api4.context.active());
696
- const spanContext = span?.spanContext();
697
- logger.emit({
698
- severityText,
699
- severityNumber: LOG_SEVERITY_MAP[severityText],
700
- body,
701
- attributes: {
702
- ...defaultAttrs,
703
- ...spanContext && {
704
- trace_id: spanContext.traceId,
705
- span_id: spanContext.spanId
706
- },
707
- ...attrs
708
- }
709
- });
710
- }
711
- return {
712
- debug: (msg, attrs) => emit("DEBUG", msg, attrs),
713
- info: (msg, attrs) => emit("INFO", msg, attrs),
714
- notice: (msg, attrs) => emit("NOTICE", msg, attrs),
715
- warn: (msg, attrs) => emit("WARNING", msg, attrs),
716
- error: (msg, errOrAttrs, maybeAttrs = {}) => {
717
- let body;
718
- let attrs;
719
- if (errOrAttrs instanceof Error) {
720
- body = `${msg}: ${errOrAttrs.stack || errOrAttrs.message}`;
721
- attrs = maybeAttrs;
722
- } else {
723
- body = msg instanceof Error ? msg.stack || msg.message : msg;
724
- attrs = errOrAttrs || {};
725
- }
726
- emit("ERROR", body, attrs);
727
- },
728
- critical: (msg, attrs) => emit("CRITICAL", msg, attrs),
729
- alert: (msg, attrs) => emit("ALERT", msg, attrs),
730
- emergency: (msg, attrs) => emit("EMERGENCY", msg, attrs)
731
- };
732
- }
733
-
734
730
  // src/metrics.ts
735
- var import_api5 = require("@opentelemetry/api");
736
731
  async function getMeter(componentNameOverride) {
737
732
  await initialize();
738
733
  const { componentName, systemName, systemVersion } = detectTelemetryContext(
@@ -826,12 +821,10 @@ function extractArgs(spanOptionsSpanOrFunc, spanOrFunc, func) {
826
821
  var isFunction = (value) => typeof value === "function";
827
822
  var isSpan = (value) => value !== null && value !== void 0 && isFunction(value.spanContext) && isFunction(value.end);
828
823
  var isSpanOptions = (value) => value !== null && value !== void 0 && (!!value.startTime || !!value.attributes || !!value.kind) && !isSpan(value);
829
-
830
- // src/index.ts
831
- initialize();
832
824
  // Annotate the CommonJS export names for ESM import in node:
833
825
  0 && (module.exports = {
834
826
  getLogger,
835
827
  getMeter,
836
- getTracer
828
+ getTracer,
829
+ initialize
837
830
  });
package/dist/index.mjs CHANGED
@@ -1,6 +1,122 @@
1
+ // src/logger.ts
2
+ import { context, trace } from "@opentelemetry/api";
3
+ import { logs } from "@opentelemetry/api-logs";
4
+
5
+ // src/consts.ts
6
+ var LOG_SEVERITY_MAP = {
7
+ TRACE: 1,
8
+ DEBUG: 5,
9
+ INFO: 9,
10
+ NOTICE: 10,
11
+ WARNING: 13,
12
+ WARN: 13,
13
+ ERROR: 17,
14
+ FATAL: 21,
15
+ CRITICAL: 21,
16
+ ALERT: 22,
17
+ EMERGENCY: 23
18
+ };
19
+
20
+ // src/otel-context.ts
21
+ import {
22
+ ATTR_SERVICE_NAME,
23
+ ATTR_SERVICE_VERSION
24
+ } from "@opentelemetry/semantic-conventions";
25
+ function detectTelemetryContext(componentNameOverride) {
26
+ const {
27
+ OTEL_SERVICE_NAME,
28
+ // e.g. "UserSystem"
29
+ OTEL_SERVICE_VERSION,
30
+ // e.g. "1.2.3"
31
+ K_SERVICE,
32
+ K_REVISION,
33
+ K_CONFIGURATION,
34
+ KUBERNETES_SERVICE_HOST,
35
+ POD_NAME,
36
+ POD_NAMESPACE,
37
+ GCP_PROJECT,
38
+ CLOUD_PROVIDER
39
+ } = process.env;
40
+ const systemName = OTEL_SERVICE_NAME || "unknown-service";
41
+ const systemVersion = OTEL_SERVICE_VERSION || "1.0.0";
42
+ const componentName = componentNameOverride || void 0;
43
+ const resourceAttributes = {
44
+ [ATTR_SERVICE_NAME]: systemName,
45
+ [ATTR_SERVICE_VERSION]: systemVersion,
46
+ "serviceContext.service": systemName,
47
+ "serviceContext.version": systemVersion,
48
+ ...K_SERVICE && { "cloud.run.service": K_SERVICE },
49
+ ...K_REVISION && { "cloud.run.revision": K_REVISION },
50
+ ...K_CONFIGURATION && { "cloud.run.configuration": K_CONFIGURATION },
51
+ ...POD_NAME && { "k8s.pod_name": POD_NAME },
52
+ ...POD_NAMESPACE && { "k8s.namespace_name": POD_NAMESPACE },
53
+ ...KUBERNETES_SERVICE_HOST && { "cloud.orchestrator": "kubernetes" },
54
+ ...GCP_PROJECT && { "cloud.account.id": GCP_PROJECT },
55
+ ...CLOUD_PROVIDER && { "cloud.provider": CLOUD_PROVIDER },
56
+ ...componentName && { "component.name": componentName }
57
+ };
58
+ return {
59
+ systemName,
60
+ systemVersion,
61
+ componentName,
62
+ resourceAttributes
63
+ };
64
+ }
65
+
66
+ // src/logger.ts
67
+ function getLogger(serviceOverride, extraAttrs = {}) {
68
+ const { systemName, systemVersion, resourceAttributes } = detectTelemetryContext(serviceOverride);
69
+ const defaultAttrs = {
70
+ ...resourceAttributes,
71
+ ...extraAttrs
72
+ };
73
+ function emit(severityText, body, attrs = {}) {
74
+ const logger = logs.getLogger(systemName, systemVersion);
75
+ const span = trace.getSpan(context.active());
76
+ const spanContext = span?.spanContext();
77
+ logger.emit({
78
+ severityText,
79
+ severityNumber: LOG_SEVERITY_MAP[severityText],
80
+ body,
81
+ attributes: {
82
+ ...defaultAttrs,
83
+ ...spanContext && {
84
+ trace_id: spanContext.traceId,
85
+ span_id: spanContext.spanId
86
+ },
87
+ ...attrs
88
+ }
89
+ });
90
+ }
91
+ return {
92
+ debug: (msg, attrs) => emit("DEBUG", msg, attrs),
93
+ info: (msg, attrs) => emit("INFO", msg, attrs),
94
+ notice: (msg, attrs) => emit("NOTICE", msg, attrs),
95
+ warn: (msg, attrs) => emit("WARNING", msg, attrs),
96
+ error: (msg, errOrAttrs, maybeAttrs = {}) => {
97
+ let body;
98
+ let attrs;
99
+ if (errOrAttrs instanceof Error) {
100
+ body = `${msg}: ${errOrAttrs.stack || errOrAttrs.message}`;
101
+ attrs = maybeAttrs;
102
+ } else {
103
+ body = msg instanceof Error ? msg.stack || msg.message : msg;
104
+ attrs = errOrAttrs || {};
105
+ }
106
+ emit("ERROR", body, attrs);
107
+ },
108
+ critical: (msg, attrs) => emit("CRITICAL", msg, attrs),
109
+ alert: (msg, attrs) => emit("ALERT", msg, attrs),
110
+ emergency: (msg, attrs) => emit("EMERGENCY", msg, attrs)
111
+ };
112
+ }
113
+
114
+ // src/metrics.ts
115
+ import { metrics } from "@opentelemetry/api";
116
+
1
117
  // src/otel.ts
2
118
  import { DiagConsoleLogger, DiagLogLevel, diag } from "@opentelemetry/api";
3
- import { logs } from "@opentelemetry/api-logs";
119
+ import { logs as logs2 } from "@opentelemetry/api-logs";
4
120
  import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node";
5
121
  import { NodeSDK } from "@opentelemetry/sdk-node";
6
122
 
@@ -21,25 +137,10 @@ import {
21
137
  // src/loggers/console-log-pretty-exporter.ts
22
138
  import { ExportResultCode } from "@opentelemetry/core";
23
139
 
24
- // src/consts.ts
25
- var LOG_SEVERITY_MAP = {
26
- TRACE: 1,
27
- DEBUG: 5,
28
- INFO: 9,
29
- NOTICE: 10,
30
- WARNING: 13,
31
- WARN: 13,
32
- ERROR: 17,
33
- FATAL: 21,
34
- CRITICAL: 21,
35
- ALERT: 22,
36
- EMERGENCY: 23
37
- };
38
-
39
140
  // src/loggers/formatters/shared.ts
40
141
  import {
41
- ATTR_SERVICE_NAME,
42
- ATTR_SERVICE_VERSION
142
+ ATTR_SERVICE_NAME as ATTR_SERVICE_NAME2,
143
+ ATTR_SERVICE_VERSION as ATTR_SERVICE_VERSION2
43
144
  } from "@opentelemetry/semantic-conventions";
44
145
  import stringify from "fast-safe-stringify";
45
146
 
@@ -101,8 +202,8 @@ function formatTimestamp(time) {
101
202
  return colors.dim(date.toISOString().slice(11, 23));
102
203
  }
103
204
  function formatService(resource) {
104
- const name = resource.attributes[ATTR_SERVICE_NAME] ?? "unknown-service";
105
- const version = resource.attributes[ATTR_SERVICE_VERSION] ?? "1.0.0";
205
+ const name = resource.attributes[ATTR_SERVICE_NAME2] ?? "unknown-service";
206
+ const version = resource.attributes[ATTR_SERVICE_VERSION2] ?? "1.0.0";
106
207
  return colors.gray(`[${name}@${version}]`);
107
208
  }
108
209
  function formatLevel(record) {
@@ -554,54 +655,6 @@ import {
554
655
  detectResources,
555
656
  resourceFromAttributes
556
657
  } from "@opentelemetry/resources";
557
-
558
- // src/otel-context.ts
559
- import {
560
- ATTR_SERVICE_NAME as ATTR_SERVICE_NAME2,
561
- ATTR_SERVICE_VERSION as ATTR_SERVICE_VERSION2
562
- } from "@opentelemetry/semantic-conventions";
563
- function detectTelemetryContext(componentNameOverride) {
564
- const {
565
- OTEL_SERVICE_NAME,
566
- // e.g. "UserSystem"
567
- OTEL_SERVICE_VERSION,
568
- // e.g. "1.2.3"
569
- K_SERVICE,
570
- K_REVISION,
571
- K_CONFIGURATION,
572
- KUBERNETES_SERVICE_HOST,
573
- POD_NAME,
574
- POD_NAMESPACE,
575
- GCP_PROJECT,
576
- CLOUD_PROVIDER
577
- } = process.env;
578
- const systemName = OTEL_SERVICE_NAME || "unknown-service";
579
- const systemVersion = OTEL_SERVICE_VERSION || "1.0.0";
580
- const componentName = componentNameOverride || void 0;
581
- const resourceAttributes = {
582
- [ATTR_SERVICE_NAME2]: systemName,
583
- [ATTR_SERVICE_VERSION2]: systemVersion,
584
- "serviceContext.service": systemName,
585
- "serviceContext.version": systemVersion,
586
- ...K_SERVICE && { "cloud.run.service": K_SERVICE },
587
- ...K_REVISION && { "cloud.run.revision": K_REVISION },
588
- ...K_CONFIGURATION && { "cloud.run.configuration": K_CONFIGURATION },
589
- ...POD_NAME && { "k8s.pod_name": POD_NAME },
590
- ...POD_NAMESPACE && { "k8s.namespace_name": POD_NAMESPACE },
591
- ...KUBERNETES_SERVICE_HOST && { "cloud.orchestrator": "kubernetes" },
592
- ...GCP_PROJECT && { "cloud.account.id": GCP_PROJECT },
593
- ...CLOUD_PROVIDER && { "cloud.provider": CLOUD_PROVIDER },
594
- ...componentName && { "component.name": componentName }
595
- };
596
- return {
597
- systemName,
598
- systemVersion,
599
- componentName,
600
- resourceAttributes
601
- };
602
- }
603
-
604
- // src/resource.ts
605
658
  var getResource = async () => {
606
659
  const baseRes = await detectResources();
607
660
  const { resourceAttributes } = detectTelemetryContext();
@@ -628,7 +681,7 @@ async function _initialize() {
628
681
  const otlpEndpoint = process.env.OTEL_EXPORTER_OTLP_ENDPOINT;
629
682
  const resource = await getResource();
630
683
  const logProvider = getLogProvider(resource, otlpEndpoint);
631
- logs.setGlobalLoggerProvider(logProvider);
684
+ logs2.setGlobalLoggerProvider(logProvider);
632
685
  const spanProcessor = getSpanProcessor(otlpEndpoint);
633
686
  const metricReader = getMetricReader(otlpEndpoint);
634
687
  const sdk = new NodeSDK({
@@ -650,66 +703,7 @@ async function _initialize() {
650
703
  }
651
704
  }
652
705
 
653
- // src/logger.ts
654
- import { context, trace } from "@opentelemetry/api";
655
- import { logs as logs2 } from "@opentelemetry/api-logs";
656
- function getLogger(serviceOverride, extraAttrs = {}) {
657
- const { systemName, systemVersion, resourceAttributes } = detectTelemetryContext(serviceOverride);
658
- const defaultAttrs = {
659
- ...resourceAttributes,
660
- ...extraAttrs
661
- };
662
- let isInitialized = false;
663
- function emit(severityText, body, attrs = {}) {
664
- if (!isInitialized) {
665
- initialize().then(() => {
666
- isInitialized = true;
667
- emit(severityText, body, attrs);
668
- });
669
- return;
670
- }
671
- const logger = logs2.getLogger(systemName, systemVersion);
672
- const span = trace.getSpan(context.active());
673
- const spanContext = span?.spanContext();
674
- logger.emit({
675
- severityText,
676
- severityNumber: LOG_SEVERITY_MAP[severityText],
677
- body,
678
- attributes: {
679
- ...defaultAttrs,
680
- ...spanContext && {
681
- trace_id: spanContext.traceId,
682
- span_id: spanContext.spanId
683
- },
684
- ...attrs
685
- }
686
- });
687
- }
688
- return {
689
- debug: (msg, attrs) => emit("DEBUG", msg, attrs),
690
- info: (msg, attrs) => emit("INFO", msg, attrs),
691
- notice: (msg, attrs) => emit("NOTICE", msg, attrs),
692
- warn: (msg, attrs) => emit("WARNING", msg, attrs),
693
- error: (msg, errOrAttrs, maybeAttrs = {}) => {
694
- let body;
695
- let attrs;
696
- if (errOrAttrs instanceof Error) {
697
- body = `${msg}: ${errOrAttrs.stack || errOrAttrs.message}`;
698
- attrs = maybeAttrs;
699
- } else {
700
- body = msg instanceof Error ? msg.stack || msg.message : msg;
701
- attrs = errOrAttrs || {};
702
- }
703
- emit("ERROR", body, attrs);
704
- },
705
- critical: (msg, attrs) => emit("CRITICAL", msg, attrs),
706
- alert: (msg, attrs) => emit("ALERT", msg, attrs),
707
- emergency: (msg, attrs) => emit("EMERGENCY", msg, attrs)
708
- };
709
- }
710
-
711
706
  // src/metrics.ts
712
- import { metrics } from "@opentelemetry/api";
713
707
  async function getMeter(componentNameOverride) {
714
708
  await initialize();
715
709
  const { componentName, systemName, systemVersion } = detectTelemetryContext(
@@ -807,11 +801,9 @@ function extractArgs(spanOptionsSpanOrFunc, spanOrFunc, func) {
807
801
  var isFunction = (value) => typeof value === "function";
808
802
  var isSpan = (value) => value !== null && value !== void 0 && isFunction(value.spanContext) && isFunction(value.end);
809
803
  var isSpanOptions = (value) => value !== null && value !== void 0 && (!!value.startTime || !!value.attributes || !!value.kind) && !isSpan(value);
810
-
811
- // src/index.ts
812
- initialize();
813
804
  export {
814
805
  getLogger,
815
806
  getMeter,
816
- getTracer
807
+ getTracer,
808
+ initialize
817
809
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sebspark/otel",
3
- "version": "0.4.2",
3
+ "version": "0.4.4",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",