@sebspark/otel 1.1.3 → 2.0.0
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.js +201 -209
- package/package.json +5 -3
- package/dist/index.d.mts +0 -76
- package/dist/index.mjs +0 -953
package/dist/index.js
CHANGED
|
@@ -1,57 +1,19 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
1
|
// src/index.ts
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
SpanStatusCode
|
|
34
|
-
|
|
35
|
-
getLogger: () => getLogger,
|
|
36
|
-
getMeter: () => getMeter,
|
|
37
|
-
getTracer: () => getTracer,
|
|
38
|
-
initialize: () => initialize,
|
|
39
|
-
instrumentations: () => instrumentations,
|
|
40
|
-
isInitialized: () => isInitialized
|
|
41
|
-
});
|
|
42
|
-
module.exports = __toCommonJS(index_exports);
|
|
43
|
-
var import_api7 = require("@opentelemetry/api");
|
|
2
|
+
import {
|
|
3
|
+
context as context4,
|
|
4
|
+
SpanStatusCode as SpanStatusCode4
|
|
5
|
+
} from "@opentelemetry/api";
|
|
44
6
|
|
|
45
7
|
// src/instrumentations.ts
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
8
|
+
import { DnsInstrumentation } from "@opentelemetry/instrumentation-dns";
|
|
9
|
+
import { ExpressInstrumentation } from "@opentelemetry/instrumentation-express";
|
|
10
|
+
import { FsInstrumentation } from "@opentelemetry/instrumentation-fs";
|
|
11
|
+
import { GrpcInstrumentation } from "@opentelemetry/instrumentation-grpc";
|
|
12
|
+
import { HttpInstrumentation } from "@opentelemetry/instrumentation-http";
|
|
13
|
+
import { NetInstrumentation } from "@opentelemetry/instrumentation-net";
|
|
14
|
+
import { RedisInstrumentation } from "@opentelemetry/instrumentation-redis";
|
|
15
|
+
import { SocketIoInstrumentation } from "@opentelemetry/instrumentation-socket.io";
|
|
16
|
+
import { UndiciInstrumentation } from "@opentelemetry/instrumentation-undici";
|
|
55
17
|
var _http;
|
|
56
18
|
var _express;
|
|
57
19
|
var _grpc;
|
|
@@ -64,63 +26,63 @@ var _socketIo;
|
|
|
64
26
|
var instrumentations = {
|
|
65
27
|
get http() {
|
|
66
28
|
if (!_http) {
|
|
67
|
-
_http = new
|
|
29
|
+
_http = new HttpInstrumentation();
|
|
68
30
|
}
|
|
69
31
|
return _http;
|
|
70
32
|
},
|
|
71
33
|
get express() {
|
|
72
34
|
if (!_express) {
|
|
73
|
-
_express = new
|
|
35
|
+
_express = new ExpressInstrumentation();
|
|
74
36
|
}
|
|
75
37
|
return _express;
|
|
76
38
|
},
|
|
77
39
|
get grpc() {
|
|
78
40
|
if (!_grpc) {
|
|
79
|
-
_grpc = new
|
|
41
|
+
_grpc = new GrpcInstrumentation();
|
|
80
42
|
}
|
|
81
43
|
return _grpc;
|
|
82
44
|
},
|
|
83
45
|
get redis() {
|
|
84
46
|
if (!_redis) {
|
|
85
|
-
_redis = new
|
|
47
|
+
_redis = new RedisInstrumentation();
|
|
86
48
|
}
|
|
87
49
|
return _redis;
|
|
88
50
|
},
|
|
89
51
|
get dns() {
|
|
90
52
|
if (!_dns) {
|
|
91
|
-
_dns = new
|
|
53
|
+
_dns = new DnsInstrumentation();
|
|
92
54
|
}
|
|
93
55
|
return _dns;
|
|
94
56
|
},
|
|
95
57
|
get net() {
|
|
96
58
|
if (!_net) {
|
|
97
|
-
_net = new
|
|
59
|
+
_net = new NetInstrumentation();
|
|
98
60
|
}
|
|
99
61
|
return _net;
|
|
100
62
|
},
|
|
101
63
|
get fs() {
|
|
102
64
|
if (!_fs) {
|
|
103
|
-
_fs = new
|
|
65
|
+
_fs = new FsInstrumentation();
|
|
104
66
|
}
|
|
105
67
|
return _fs;
|
|
106
68
|
},
|
|
107
69
|
get undici() {
|
|
108
70
|
if (!_undici) {
|
|
109
|
-
_undici = new
|
|
71
|
+
_undici = new UndiciInstrumentation();
|
|
110
72
|
}
|
|
111
73
|
return _undici;
|
|
112
74
|
},
|
|
113
75
|
get socketIo() {
|
|
114
76
|
if (!_socketIo) {
|
|
115
|
-
_socketIo = new
|
|
77
|
+
_socketIo = new SocketIoInstrumentation();
|
|
116
78
|
}
|
|
117
79
|
return _socketIo;
|
|
118
80
|
}
|
|
119
81
|
};
|
|
120
82
|
|
|
121
83
|
// src/logger.ts
|
|
122
|
-
|
|
123
|
-
|
|
84
|
+
import { context as context2, trace as trace2 } from "@opentelemetry/api";
|
|
85
|
+
import { logs as logs2 } from "@opentelemetry/api-logs";
|
|
124
86
|
|
|
125
87
|
// src/consts.ts
|
|
126
88
|
var LOG_SEVERITY_MAP = {
|
|
@@ -138,44 +100,59 @@ var LOG_SEVERITY_MAP = {
|
|
|
138
100
|
};
|
|
139
101
|
|
|
140
102
|
// src/otel.ts
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
103
|
+
import {
|
|
104
|
+
context,
|
|
105
|
+
DiagConsoleLogger,
|
|
106
|
+
DiagLogLevel,
|
|
107
|
+
diag,
|
|
108
|
+
metrics,
|
|
109
|
+
trace
|
|
110
|
+
} from "@opentelemetry/api";
|
|
111
|
+
import { logs } from "@opentelemetry/api-logs";
|
|
112
|
+
import { NodeSDK } from "@opentelemetry/sdk-node";
|
|
144
113
|
|
|
145
114
|
// src/providers.ts
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
115
|
+
import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-http";
|
|
116
|
+
import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-http";
|
|
117
|
+
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
|
|
118
|
+
import {
|
|
119
|
+
BatchLogRecordProcessor,
|
|
120
|
+
LoggerProvider,
|
|
121
|
+
SimpleLogRecordProcessor
|
|
122
|
+
} from "@opentelemetry/sdk-logs";
|
|
123
|
+
import { PeriodicExportingMetricReader } from "@opentelemetry/sdk-metrics";
|
|
124
|
+
import {
|
|
125
|
+
BatchSpanProcessor
|
|
126
|
+
} from "@opentelemetry/sdk-trace-node";
|
|
152
127
|
|
|
153
|
-
// src/loggers/console-
|
|
154
|
-
|
|
155
|
-
var import_core = require("@opentelemetry/core");
|
|
128
|
+
// src/loggers/console-metric-pretty-exporter.ts
|
|
129
|
+
import { ExportResultCode } from "@opentelemetry/core";
|
|
156
130
|
|
|
157
131
|
// src/loggers/formatters/shared.ts
|
|
158
|
-
|
|
159
|
-
|
|
132
|
+
import {
|
|
133
|
+
ATTR_SERVICE_NAME,
|
|
134
|
+
ATTR_SERVICE_VERSION
|
|
135
|
+
} from "@opentelemetry/semantic-conventions";
|
|
136
|
+
import fss from "fast-safe-stringify";
|
|
160
137
|
|
|
161
138
|
// src/loggers/formatters/style.ts
|
|
162
|
-
|
|
139
|
+
import kleur from "kleur";
|
|
163
140
|
var colors = {
|
|
164
|
-
gray:
|
|
165
|
-
dim:
|
|
166
|
-
cyan:
|
|
167
|
-
white:
|
|
168
|
-
green:
|
|
169
|
-
yellow:
|
|
170
|
-
red:
|
|
171
|
-
magenta:
|
|
141
|
+
gray: kleur.gray,
|
|
142
|
+
dim: kleur.dim,
|
|
143
|
+
cyan: kleur.cyan,
|
|
144
|
+
white: kleur.white,
|
|
145
|
+
green: kleur.green,
|
|
146
|
+
yellow: kleur.yellow,
|
|
147
|
+
red: kleur.red,
|
|
148
|
+
magenta: kleur.magenta
|
|
172
149
|
};
|
|
173
150
|
var levelColorMap = {
|
|
174
|
-
DEBUG:
|
|
175
|
-
INFO:
|
|
176
|
-
WARN:
|
|
177
|
-
ERROR:
|
|
178
|
-
FATAL:
|
|
151
|
+
DEBUG: kleur.magenta,
|
|
152
|
+
INFO: kleur.green,
|
|
153
|
+
WARN: kleur.yellow,
|
|
154
|
+
ERROR: kleur.red,
|
|
155
|
+
FATAL: kleur.red
|
|
179
156
|
};
|
|
180
157
|
var levelIconMap = {
|
|
181
158
|
DEBUG: "\u{1F41B}",
|
|
@@ -211,13 +188,14 @@ var kindColorMap = {
|
|
|
211
188
|
};
|
|
212
189
|
|
|
213
190
|
// src/loggers/formatters/shared.ts
|
|
191
|
+
var stringify = fss.default.stableStringify;
|
|
214
192
|
function formatTimestamp(time) {
|
|
215
193
|
const date = new Date(hrTimeToMillis(time));
|
|
216
194
|
return colors.dim(date.toISOString().slice(11, 23));
|
|
217
195
|
}
|
|
218
196
|
function formatService(resource) {
|
|
219
|
-
const name = resource.attributes[
|
|
220
|
-
const version = resource.attributes[
|
|
197
|
+
const name = resource.attributes[ATTR_SERVICE_NAME] ?? "unknown-service";
|
|
198
|
+
const version = resource.attributes[ATTR_SERVICE_VERSION] ?? "1.0.0";
|
|
221
199
|
return colors.gray(`[${name}@${version}]`);
|
|
222
200
|
}
|
|
223
201
|
function formatLevel(record) {
|
|
@@ -235,7 +213,7 @@ function formatScope(resource, instrumentationScope) {
|
|
|
235
213
|
return colors.cyan(`${scopeLabel}${versionLabel} `);
|
|
236
214
|
}
|
|
237
215
|
function formatMessage(record) {
|
|
238
|
-
return typeof record.body === "string" ? record.body : (
|
|
216
|
+
return typeof record.body === "string" ? record.body : stringify(record.body);
|
|
239
217
|
}
|
|
240
218
|
function formatAttributes(attrs) {
|
|
241
219
|
const keys = Object.keys(attrs).filter(
|
|
@@ -244,7 +222,7 @@ function formatAttributes(attrs) {
|
|
|
244
222
|
if (keys.length === 0) return "";
|
|
245
223
|
const formatted = keys.map((k) => {
|
|
246
224
|
const val = attrs[k];
|
|
247
|
-
return `${k}=${typeof val === "object" ? (
|
|
225
|
+
return `${k}=${typeof val === "object" ? stringify(val) : val}`;
|
|
248
226
|
});
|
|
249
227
|
return ` ${colors.gray(formatted.join(" "))}`;
|
|
250
228
|
}
|
|
@@ -307,7 +285,7 @@ function isHistogramLike(val) {
|
|
|
307
285
|
}
|
|
308
286
|
|
|
309
287
|
// src/loggers/formatters/span.ts
|
|
310
|
-
|
|
288
|
+
import { SpanStatusCode } from "@opentelemetry/api";
|
|
311
289
|
var LABEL_WIDTH = 20;
|
|
312
290
|
var DESCRIPTION_MAX_WIDTH = 16;
|
|
313
291
|
var BAR_MIN_WIDTH = 1;
|
|
@@ -337,7 +315,7 @@ function formatSpans(spans) {
|
|
|
337
315
|
function formatSpan(span, opts) {
|
|
338
316
|
const label = formatLabel(span, opts.depth);
|
|
339
317
|
const bar = buildBar(span, opts?.offsetMs, opts?.totalDurationMs);
|
|
340
|
-
const barColor = span.status.code ===
|
|
318
|
+
const barColor = span.status.code === SpanStatusCode.OK ? colors.green : span.status.code === SpanStatusCode.ERROR ? colors.red : colors.gray;
|
|
341
319
|
const desc = formatDescription(span);
|
|
342
320
|
const status = formatStatus(span);
|
|
343
321
|
const duration = formatDuration(span, opts?.offsetMs);
|
|
@@ -464,7 +442,55 @@ function computeDepth(span, allSpans) {
|
|
|
464
442
|
return depth;
|
|
465
443
|
}
|
|
466
444
|
|
|
445
|
+
// src/loggers/console-metric-pretty-exporter.ts
|
|
446
|
+
var ConsoleMetricPrettyExporter = class {
|
|
447
|
+
patterns;
|
|
448
|
+
constructor() {
|
|
449
|
+
const raw = process.env.METRIC_FILTER ?? "";
|
|
450
|
+
const entries = raw.split(",").map((e) => e.trim()).filter(Boolean);
|
|
451
|
+
this.patterns = entries.map(globToRegex);
|
|
452
|
+
}
|
|
453
|
+
filterMetrics(resourceMetrics) {
|
|
454
|
+
if (this.patterns.length === 0) return void 0;
|
|
455
|
+
const filteredScopes = resourceMetrics.scopeMetrics.map((scopeMetric) => {
|
|
456
|
+
const filteredMetrics = scopeMetric.metrics.filter(
|
|
457
|
+
(metric) => this.patterns.some((pattern) => pattern.test(metric.descriptor.name))
|
|
458
|
+
);
|
|
459
|
+
if (filteredMetrics.length === 0) return void 0;
|
|
460
|
+
return {
|
|
461
|
+
...scopeMetric,
|
|
462
|
+
metrics: filteredMetrics
|
|
463
|
+
};
|
|
464
|
+
}).filter((s) => s !== void 0);
|
|
465
|
+
if (filteredScopes.length === 0) return void 0;
|
|
466
|
+
return {
|
|
467
|
+
...resourceMetrics,
|
|
468
|
+
scopeMetrics: filteredScopes
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
export(metrics3, resultCallback) {
|
|
472
|
+
const filtered = this.filterMetrics(metrics3);
|
|
473
|
+
if (filtered) {
|
|
474
|
+
console.log(formatMetrics(filtered));
|
|
475
|
+
}
|
|
476
|
+
resultCallback({ code: ExportResultCode.SUCCESS });
|
|
477
|
+
}
|
|
478
|
+
shutdown() {
|
|
479
|
+
return Promise.resolve();
|
|
480
|
+
}
|
|
481
|
+
forceFlush() {
|
|
482
|
+
return Promise.resolve();
|
|
483
|
+
}
|
|
484
|
+
};
|
|
485
|
+
function globToRegex(glob) {
|
|
486
|
+
const escaped = glob.replace(/[.+^${}()|[\]\\]/g, "\\$&");
|
|
487
|
+
const regex = `^${escaped.replace(/\*/g, ".*")}$`;
|
|
488
|
+
return new RegExp(regex);
|
|
489
|
+
}
|
|
490
|
+
|
|
467
491
|
// src/loggers/console-log-pretty-exporter.ts
|
|
492
|
+
import { SeverityNumber } from "@opentelemetry/api-logs";
|
|
493
|
+
import { ExportResultCode as ExportResultCode2 } from "@opentelemetry/core";
|
|
468
494
|
var ConsoleLogPrettyExporter = class {
|
|
469
495
|
logThreshold;
|
|
470
496
|
constructor() {
|
|
@@ -485,42 +511,42 @@ var ConsoleLogPrettyExporter = class {
|
|
|
485
511
|
for (const record of logRecords) {
|
|
486
512
|
if ((record.severityNumber ?? 0) >= this.logThreshold) {
|
|
487
513
|
const formatted = formatLogRecord(record);
|
|
488
|
-
const severity = record.severityNumber ||
|
|
489
|
-
if (severity >=
|
|
514
|
+
const severity = record.severityNumber || SeverityNumber.UNSPECIFIED;
|
|
515
|
+
if (severity >= SeverityNumber.ERROR) {
|
|
490
516
|
console.error(formatted);
|
|
491
|
-
} else if (severity >=
|
|
517
|
+
} else if (severity >= SeverityNumber.WARN) {
|
|
492
518
|
console.warn(formatted);
|
|
493
|
-
} else if (severity >=
|
|
519
|
+
} else if (severity >= SeverityNumber.INFO) {
|
|
494
520
|
console.info(formatted);
|
|
495
|
-
} else if (severity >=
|
|
521
|
+
} else if (severity >= SeverityNumber.DEBUG) {
|
|
496
522
|
console.debug(formatted);
|
|
497
523
|
} else {
|
|
498
524
|
console.trace(formatted);
|
|
499
525
|
}
|
|
500
526
|
}
|
|
501
527
|
}
|
|
502
|
-
done?.({ code:
|
|
528
|
+
done?.({ code: ExportResultCode2.SUCCESS });
|
|
503
529
|
}
|
|
504
530
|
};
|
|
505
531
|
|
|
506
532
|
// src/loggers/console-span-pretty-exporter.ts
|
|
507
|
-
|
|
508
|
-
|
|
533
|
+
import { SpanStatusCode as SpanStatusCode2 } from "@opentelemetry/api";
|
|
534
|
+
import { ExportResultCode as ExportResultCode3 } from "@opentelemetry/core";
|
|
509
535
|
var ConsoleSpanPrettyExporter = class {
|
|
510
536
|
allowedStatuses;
|
|
511
537
|
constructor() {
|
|
512
538
|
const env = process.env.SPAN_LEVEL?.toUpperCase();
|
|
513
539
|
if (!env) {
|
|
514
540
|
this.allowedStatuses = /* @__PURE__ */ new Set([
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
541
|
+
SpanStatusCode2.UNSET,
|
|
542
|
+
SpanStatusCode2.OK,
|
|
543
|
+
SpanStatusCode2.ERROR
|
|
518
544
|
]);
|
|
519
545
|
} else {
|
|
520
546
|
const map = {
|
|
521
|
-
UNSET:
|
|
522
|
-
OK:
|
|
523
|
-
ERROR:
|
|
547
|
+
UNSET: SpanStatusCode2.UNSET,
|
|
548
|
+
OK: SpanStatusCode2.OK,
|
|
549
|
+
ERROR: SpanStatusCode2.ERROR
|
|
524
550
|
};
|
|
525
551
|
this.allowedStatuses = new Set(
|
|
526
552
|
env.split(",").map((s) => s.trim()).map((s) => map[s]).filter((v) => typeof v === "number")
|
|
@@ -537,60 +563,13 @@ var ConsoleSpanPrettyExporter = class {
|
|
|
537
563
|
if (this.shouldExport(spans)) {
|
|
538
564
|
console.log(formatSpans(spans));
|
|
539
565
|
}
|
|
540
|
-
resultCallback({ code:
|
|
566
|
+
resultCallback({ code: ExportResultCode3.SUCCESS });
|
|
541
567
|
}
|
|
542
568
|
shutdown() {
|
|
543
569
|
return Promise.resolve();
|
|
544
570
|
}
|
|
545
571
|
};
|
|
546
572
|
|
|
547
|
-
// src/loggers/console-metric-pretty-exporter.ts
|
|
548
|
-
var import_core3 = require("@opentelemetry/core");
|
|
549
|
-
var ConsoleMetricPrettyExporter = class {
|
|
550
|
-
patterns;
|
|
551
|
-
constructor() {
|
|
552
|
-
const raw = process.env.METRIC_FILTER ?? "";
|
|
553
|
-
const entries = raw.split(",").map((e) => e.trim()).filter(Boolean);
|
|
554
|
-
this.patterns = entries.map(globToRegex);
|
|
555
|
-
}
|
|
556
|
-
filterMetrics(resourceMetrics) {
|
|
557
|
-
if (this.patterns.length === 0) return void 0;
|
|
558
|
-
const filteredScopes = resourceMetrics.scopeMetrics.map((scopeMetric) => {
|
|
559
|
-
const filteredMetrics = scopeMetric.metrics.filter(
|
|
560
|
-
(metric) => this.patterns.some((pattern) => pattern.test(metric.descriptor.name))
|
|
561
|
-
);
|
|
562
|
-
if (filteredMetrics.length === 0) return void 0;
|
|
563
|
-
return {
|
|
564
|
-
...scopeMetric,
|
|
565
|
-
metrics: filteredMetrics
|
|
566
|
-
};
|
|
567
|
-
}).filter((s) => s !== void 0);
|
|
568
|
-
if (filteredScopes.length === 0) return void 0;
|
|
569
|
-
return {
|
|
570
|
-
...resourceMetrics,
|
|
571
|
-
scopeMetrics: filteredScopes
|
|
572
|
-
};
|
|
573
|
-
}
|
|
574
|
-
export(metrics3, resultCallback) {
|
|
575
|
-
const filtered = this.filterMetrics(metrics3);
|
|
576
|
-
if (filtered) {
|
|
577
|
-
console.log(formatMetrics(filtered));
|
|
578
|
-
}
|
|
579
|
-
resultCallback({ code: import_core3.ExportResultCode.SUCCESS });
|
|
580
|
-
}
|
|
581
|
-
shutdown() {
|
|
582
|
-
return Promise.resolve();
|
|
583
|
-
}
|
|
584
|
-
forceFlush() {
|
|
585
|
-
return Promise.resolve();
|
|
586
|
-
}
|
|
587
|
-
};
|
|
588
|
-
function globToRegex(glob) {
|
|
589
|
-
const escaped = glob.replace(/[.+^${}()|[\]\\]/g, "\\$&");
|
|
590
|
-
const regex = `^${escaped.replace(/\*/g, ".*")}$`;
|
|
591
|
-
return new RegExp(regex);
|
|
592
|
-
}
|
|
593
|
-
|
|
594
573
|
// src/loggers/tree-span-processor.ts
|
|
595
574
|
var TreeSpanProcessor = class {
|
|
596
575
|
exporter;
|
|
@@ -638,51 +617,61 @@ var TreeSpanProcessor = class {
|
|
|
638
617
|
// src/providers.ts
|
|
639
618
|
var getLogProvider = (resource, otlpEndpoint) => {
|
|
640
619
|
if (otlpEndpoint) {
|
|
641
|
-
const exporter2 = new
|
|
620
|
+
const exporter2 = new OTLPLogExporter({ url: `${otlpEndpoint}/v1/logs` });
|
|
642
621
|
const processors = [
|
|
643
|
-
new
|
|
622
|
+
new BatchLogRecordProcessor(exporter2)
|
|
644
623
|
];
|
|
645
624
|
if (process.env.LOG_LEVEL) {
|
|
646
625
|
processors.push(
|
|
647
|
-
new
|
|
626
|
+
new SimpleLogRecordProcessor(new ConsoleLogPrettyExporter())
|
|
648
627
|
);
|
|
649
628
|
}
|
|
650
|
-
return new
|
|
629
|
+
return new LoggerProvider({
|
|
651
630
|
resource,
|
|
652
631
|
processors
|
|
653
632
|
});
|
|
654
633
|
}
|
|
655
634
|
const exporter = new ConsoleLogPrettyExporter();
|
|
656
|
-
const processor = new
|
|
657
|
-
return new
|
|
635
|
+
const processor = new SimpleLogRecordProcessor(exporter);
|
|
636
|
+
return new LoggerProvider({
|
|
658
637
|
resource,
|
|
659
638
|
processors: [processor]
|
|
660
639
|
});
|
|
661
640
|
};
|
|
662
641
|
var getSpanProcessor = (otlpEndpoint) => {
|
|
663
|
-
const exporter = otlpEndpoint ? new
|
|
642
|
+
const exporter = otlpEndpoint ? new OTLPTraceExporter({
|
|
664
643
|
url: `${otlpEndpoint}/v1/traces`
|
|
665
644
|
}) : new ConsoleSpanPrettyExporter();
|
|
666
|
-
const processor = otlpEndpoint ? new
|
|
645
|
+
const processor = otlpEndpoint ? new BatchSpanProcessor(exporter) : new TreeSpanProcessor(exporter);
|
|
667
646
|
return processor;
|
|
668
647
|
};
|
|
669
648
|
var getMetricReader = (otlpEndpoint) => {
|
|
670
|
-
const metricExporter = otlpEndpoint ? new
|
|
649
|
+
const metricExporter = otlpEndpoint ? new OTLPMetricExporter({
|
|
671
650
|
url: `${otlpEndpoint}/v1/metrics`
|
|
672
651
|
}) : new ConsoleMetricPrettyExporter();
|
|
673
|
-
const metricReader = new
|
|
652
|
+
const metricReader = new PeriodicExportingMetricReader({
|
|
674
653
|
exporter: metricExporter
|
|
675
654
|
});
|
|
676
655
|
return metricReader;
|
|
677
656
|
};
|
|
678
657
|
|
|
679
658
|
// src/resource.ts
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
659
|
+
import { containerDetector } from "@opentelemetry/resource-detector-container";
|
|
660
|
+
import { gcpDetector } from "@opentelemetry/resource-detector-gcp";
|
|
661
|
+
import {
|
|
662
|
+
detectResources,
|
|
663
|
+
envDetector,
|
|
664
|
+
osDetector,
|
|
665
|
+
processDetector,
|
|
666
|
+
resourceFromAttributes,
|
|
667
|
+
serviceInstanceIdDetector
|
|
668
|
+
} from "@opentelemetry/resources";
|
|
683
669
|
|
|
684
670
|
// src/otel-context.ts
|
|
685
|
-
|
|
671
|
+
import {
|
|
672
|
+
ATTR_SERVICE_NAME as ATTR_SERVICE_NAME2,
|
|
673
|
+
ATTR_SERVICE_VERSION as ATTR_SERVICE_VERSION2
|
|
674
|
+
} from "@opentelemetry/semantic-conventions";
|
|
686
675
|
function detectTelemetryContext(componentNameOverride) {
|
|
687
676
|
const {
|
|
688
677
|
OTEL_SERVICE_NAME,
|
|
@@ -702,8 +691,8 @@ function detectTelemetryContext(componentNameOverride) {
|
|
|
702
691
|
const systemVersion = OTEL_SERVICE_VERSION || "1.0.0";
|
|
703
692
|
const componentName = componentNameOverride || void 0;
|
|
704
693
|
const resourceAttributes = {
|
|
705
|
-
[
|
|
706
|
-
[
|
|
694
|
+
[ATTR_SERVICE_NAME2]: systemName,
|
|
695
|
+
[ATTR_SERVICE_VERSION2]: systemVersion,
|
|
707
696
|
"serviceContext.service": systemName,
|
|
708
697
|
"serviceContext.version": systemVersion,
|
|
709
698
|
...K_SERVICE && { "cloud.run.service": K_SERVICE },
|
|
@@ -726,21 +715,21 @@ function detectTelemetryContext(componentNameOverride) {
|
|
|
726
715
|
|
|
727
716
|
// src/resource.ts
|
|
728
717
|
var getResource = async () => {
|
|
729
|
-
const baseRes = await
|
|
718
|
+
const baseRes = await detectResources({
|
|
730
719
|
detectors: [
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
720
|
+
containerDetector,
|
|
721
|
+
envDetector,
|
|
722
|
+
gcpDetector,
|
|
723
|
+
osDetector,
|
|
724
|
+
processDetector,
|
|
725
|
+
serviceInstanceIdDetector
|
|
737
726
|
]
|
|
738
727
|
});
|
|
739
728
|
if (baseRes.waitForAsyncAttributes) {
|
|
740
729
|
await baseRes.waitForAsyncAttributes();
|
|
741
730
|
}
|
|
742
731
|
const { resourceAttributes } = detectTelemetryContext();
|
|
743
|
-
const customRes =
|
|
732
|
+
const customRes = resourceFromAttributes(resourceAttributes);
|
|
744
733
|
const resource = baseRes.merge(customRes);
|
|
745
734
|
if (resource.waitForAsyncAttributes) {
|
|
746
735
|
await resource.waitForAsyncAttributes();
|
|
@@ -749,8 +738,8 @@ var getResource = async () => {
|
|
|
749
738
|
};
|
|
750
739
|
|
|
751
740
|
// src/otel.ts
|
|
752
|
-
|
|
753
|
-
|
|
741
|
+
diag.disable();
|
|
742
|
+
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.ERROR);
|
|
754
743
|
var initialization;
|
|
755
744
|
var _isInitialized = false;
|
|
756
745
|
async function initialize(...instrumentations2) {
|
|
@@ -770,15 +759,15 @@ async function _initialize(instrumentations2) {
|
|
|
770
759
|
const serviceName = process.env.OTEL_SERVICE_NAME ?? "unknown-service";
|
|
771
760
|
const otlpEndpoint = process.env.OTEL_EXPORTER_OTLP_ENDPOINT;
|
|
772
761
|
const resource = await getResource();
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
762
|
+
context.disable();
|
|
763
|
+
logs.disable();
|
|
764
|
+
trace.disable();
|
|
765
|
+
metrics.disable();
|
|
777
766
|
const logProvider = getLogProvider(resource, otlpEndpoint);
|
|
778
|
-
|
|
767
|
+
logs.setGlobalLoggerProvider(logProvider);
|
|
779
768
|
const spanProcessor = getSpanProcessor(otlpEndpoint);
|
|
780
769
|
const metricReader = getMetricReader(otlpEndpoint);
|
|
781
|
-
const sdk = new
|
|
770
|
+
const sdk = new NodeSDK({
|
|
782
771
|
spanProcessor,
|
|
783
772
|
metricReader,
|
|
784
773
|
instrumentations: instrumentations2,
|
|
@@ -810,8 +799,8 @@ function getLogger(serviceOverride, extraAttrs = {}) {
|
|
|
810
799
|
console.log(`[${severityText}] ${body}`);
|
|
811
800
|
return;
|
|
812
801
|
}
|
|
813
|
-
const logger =
|
|
814
|
-
const span =
|
|
802
|
+
const logger = logs2.getLogger(systemName, systemVersion);
|
|
803
|
+
const span = trace2.getSpan(context2.active());
|
|
815
804
|
const spanContext = span?.spanContext();
|
|
816
805
|
logger.emit({
|
|
817
806
|
severityText,
|
|
@@ -851,7 +840,7 @@ function getLogger(serviceOverride, extraAttrs = {}) {
|
|
|
851
840
|
}
|
|
852
841
|
|
|
853
842
|
// src/metrics.ts
|
|
854
|
-
|
|
843
|
+
import { metrics as metrics2 } from "@opentelemetry/api";
|
|
855
844
|
function getMeter(componentNameOverride) {
|
|
856
845
|
if (!isInitialized() && process.env.NODE_ENV !== "test") {
|
|
857
846
|
console.warn("OTEL must be initialized before using getMeter()");
|
|
@@ -859,11 +848,15 @@ function getMeter(componentNameOverride) {
|
|
|
859
848
|
const { componentName, systemName, systemVersion } = detectTelemetryContext(
|
|
860
849
|
componentNameOverride
|
|
861
850
|
);
|
|
862
|
-
return
|
|
851
|
+
return metrics2.getMeter(componentName ?? systemName, systemVersion);
|
|
863
852
|
}
|
|
864
853
|
|
|
865
854
|
// src/tracer.ts
|
|
866
|
-
|
|
855
|
+
import {
|
|
856
|
+
context as context3,
|
|
857
|
+
SpanStatusCode as SpanStatusCode3,
|
|
858
|
+
trace as trace3
|
|
859
|
+
} from "@opentelemetry/api";
|
|
867
860
|
function getTracer(componentNameOverride) {
|
|
868
861
|
if (!isInitialized() && process.env.NODE_ENV !== "test") {
|
|
869
862
|
console.warn("OTEL must be initialized before calling getTracer()");
|
|
@@ -871,7 +864,7 @@ function getTracer(componentNameOverride) {
|
|
|
871
864
|
const { componentName, systemName, systemVersion } = detectTelemetryContext(
|
|
872
865
|
componentNameOverride
|
|
873
866
|
);
|
|
874
|
-
const tracer =
|
|
867
|
+
const tracer = trace3.getTracer(
|
|
875
868
|
componentName ?? systemName,
|
|
876
869
|
systemVersion
|
|
877
870
|
);
|
|
@@ -881,16 +874,16 @@ function getTracer(componentNameOverride) {
|
|
|
881
874
|
spanOrFunc,
|
|
882
875
|
func
|
|
883
876
|
);
|
|
884
|
-
const parentContext = parent ?
|
|
877
|
+
const parentContext = parent ? trace3.setSpan(context3.active(), parent) : context3.active();
|
|
885
878
|
const span = tracer.startSpan(name, options, parentContext);
|
|
886
|
-
return await
|
|
879
|
+
return await context3.with(trace3.setSpan(parentContext, span), async () => {
|
|
887
880
|
try {
|
|
888
881
|
const result = await fn(span);
|
|
889
|
-
span.setStatus({ code:
|
|
882
|
+
span.setStatus({ code: SpanStatusCode3.OK });
|
|
890
883
|
return result;
|
|
891
884
|
} catch (err) {
|
|
892
885
|
const error = err;
|
|
893
|
-
span.setStatus({ code:
|
|
886
|
+
span.setStatus({ code: SpanStatusCode3.ERROR, message: error.message });
|
|
894
887
|
span.recordException?.(error);
|
|
895
888
|
throw err;
|
|
896
889
|
} finally {
|
|
@@ -904,16 +897,16 @@ function getTracer(componentNameOverride) {
|
|
|
904
897
|
spanOrFunc,
|
|
905
898
|
func
|
|
906
899
|
);
|
|
907
|
-
const parentContext = parent ?
|
|
900
|
+
const parentContext = parent ? trace3.setSpan(context3.active(), parent) : context3.active();
|
|
908
901
|
const span = tracer.startSpan(name, options, parentContext);
|
|
909
|
-
return
|
|
902
|
+
return context3.with(trace3.setSpan(parentContext, span), () => {
|
|
910
903
|
try {
|
|
911
904
|
const result = fn(span);
|
|
912
|
-
span.setStatus({ code:
|
|
905
|
+
span.setStatus({ code: SpanStatusCode3.OK });
|
|
913
906
|
return result;
|
|
914
907
|
} catch (err) {
|
|
915
908
|
const error = err;
|
|
916
|
-
span.setStatus({ code:
|
|
909
|
+
span.setStatus({ code: SpanStatusCode3.ERROR, message: error.message });
|
|
917
910
|
span.recordException?.(error);
|
|
918
911
|
throw err;
|
|
919
912
|
} finally {
|
|
@@ -949,14 +942,13 @@ function extractArgs(spanOptionsSpanOrFunc, spanOrFunc, func) {
|
|
|
949
942
|
var isFunction = (value) => typeof value === "function";
|
|
950
943
|
var isSpan = (value) => value !== null && value !== void 0 && isFunction(value.spanContext) && isFunction(value.end);
|
|
951
944
|
var isSpanOptions = (value) => value !== null && value !== void 0 && (!!value.startTime || !!value.attributes || !!value.kind) && !isSpan(value);
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
context,
|
|
945
|
+
export {
|
|
946
|
+
SpanStatusCode4 as SpanStatusCode,
|
|
947
|
+
context4 as context,
|
|
956
948
|
getLogger,
|
|
957
949
|
getMeter,
|
|
958
950
|
getTracer,
|
|
959
951
|
initialize,
|
|
960
952
|
instrumentations,
|
|
961
953
|
isInitialized
|
|
962
|
-
}
|
|
954
|
+
};
|