@xylabs/telemetry 4.9.6 → 4.9.7
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/neutral/index.mjs +31 -56
- package/dist/neutral/index.mjs.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/span.d.ts +4 -0
- package/dist/types/span.d.ts.map +1 -0
- package/package.json +2 -4
- package/src/index.ts +1 -1
- package/src/span.ts +37 -0
- package/dist/types/XyConsoleSpanExporter.d.ts +0 -14
- package/dist/types/XyConsoleSpanExporter.d.ts.map +0 -1
- package/src/XyConsoleSpanExporter.ts +0 -67
package/dist/neutral/index.mjs
CHANGED
|
@@ -1,63 +1,38 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
];
|
|
15
|
-
static logLevelToChalkColor = [
|
|
16
|
-
chalk.grey,
|
|
17
|
-
chalk.white,
|
|
18
|
-
chalk.green,
|
|
19
|
-
chalk.yellow,
|
|
20
|
-
chalk.red
|
|
21
|
-
];
|
|
22
|
-
_logLevel;
|
|
23
|
-
constructor(logLevel = 0) {
|
|
24
|
-
super();
|
|
25
|
-
this._logLevel = logLevel;
|
|
26
|
-
}
|
|
27
|
-
get logLevel() {
|
|
28
|
-
return this._logLevel;
|
|
29
|
-
}
|
|
30
|
-
export(spans) {
|
|
31
|
-
for (const span of spans) {
|
|
32
|
-
const spanLevel = this.spanLevel(span);
|
|
33
|
-
if (spanLevel < this.logLevel) {
|
|
34
|
-
continue;
|
|
1
|
+
// src/span.ts
|
|
2
|
+
import {
|
|
3
|
+
context,
|
|
4
|
+
trace
|
|
5
|
+
} from "@opentelemetry/api";
|
|
6
|
+
function span(name, fn, tracer) {
|
|
7
|
+
if (tracer) {
|
|
8
|
+
const span2 = tracer.startSpan(name);
|
|
9
|
+
return context.with(trace.setSpan(context.active(), span2), () => {
|
|
10
|
+
try {
|
|
11
|
+
return fn();
|
|
12
|
+
} finally {
|
|
13
|
+
span2.end();
|
|
35
14
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
this.logColor(spanLevel)(`${duration}ms`),
|
|
40
|
-
`TraceId: ${span.spanContext().traceId}`
|
|
41
|
-
].join(", ")));
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
logColor(level) {
|
|
45
|
-
return _XyConsoleSpanExporter.logLevelToChalkColor[level] ?? chalk.magenta;
|
|
15
|
+
});
|
|
16
|
+
} else {
|
|
17
|
+
return fn();
|
|
46
18
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
19
|
+
}
|
|
20
|
+
async function spanAsync(name, fn, tracer) {
|
|
21
|
+
if (tracer) {
|
|
22
|
+
const span2 = tracer.startSpan(name);
|
|
23
|
+
return await context.with(trace.setSpan(context.active(), span2), async () => {
|
|
24
|
+
try {
|
|
25
|
+
return await fn();
|
|
26
|
+
} finally {
|
|
27
|
+
span2.end();
|
|
54
28
|
}
|
|
55
|
-
}
|
|
56
|
-
|
|
29
|
+
});
|
|
30
|
+
} else {
|
|
31
|
+
return await fn();
|
|
57
32
|
}
|
|
58
|
-
}
|
|
33
|
+
}
|
|
59
34
|
export {
|
|
60
|
-
|
|
61
|
-
|
|
35
|
+
span,
|
|
36
|
+
spanAsync
|
|
62
37
|
};
|
|
63
38
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/
|
|
1
|
+
{"version":3,"sources":["../../src/span.ts"],"sourcesContent":["import {\n context, trace, type Tracer,\n} from '@opentelemetry/api'\n\nexport function span<T>(name: string, fn: () => T, tracer?: Tracer): T {\n if (tracer) {\n const span = tracer.startSpan(name)\n return context.with(trace.setSpan(context.active(), span), () => {\n try {\n return fn()\n } finally {\n span.end()\n }\n })\n } else {\n return fn()\n }\n}\n\nexport async function spanAsync<T>(\n name: string,\n fn: () => Promise<T>,\n tracer?: Tracer,\n): Promise<T> {\n if (tracer) {\n const span = tracer.startSpan(name)\n return await context.with(trace.setSpan(context.active(), span), async () => {\n try {\n return await fn()\n } finally {\n span.end()\n }\n })\n } else {\n return await fn()\n }\n}\n"],"mappings":";AAAA;AAAA,EACE;AAAA,EAAS;AAAA,OACJ;AAEA,SAAS,KAAQ,MAAc,IAAa,QAAoB;AACrE,MAAI,QAAQ;AACV,UAAMA,QAAO,OAAO,UAAU,IAAI;AAClC,WAAO,QAAQ,KAAK,MAAM,QAAQ,QAAQ,OAAO,GAAGA,KAAI,GAAG,MAAM;AAC/D,UAAI;AACF,eAAO,GAAG;AAAA,MACZ,UAAE;AACA,QAAAA,MAAK,IAAI;AAAA,MACX;AAAA,IACF,CAAC;AAAA,EACH,OAAO;AACL,WAAO,GAAG;AAAA,EACZ;AACF;AAEA,eAAsB,UACpB,MACA,IACA,QACY;AACZ,MAAI,QAAQ;AACV,UAAMA,QAAO,OAAO,UAAU,IAAI;AAClC,WAAO,MAAM,QAAQ,KAAK,MAAM,QAAQ,QAAQ,OAAO,GAAGA,KAAI,GAAG,YAAY;AAC3E,UAAI;AACF,eAAO,MAAM,GAAG;AAAA,MAClB,UAAE;AACA,QAAAA,MAAK,IAAI;AAAA,MACX;AAAA,IACF,CAAC;AAAA,EACH,OAAO;AACL,WAAO,MAAM,GAAG;AAAA,EAClB;AACF;","names":["span"]}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './
|
|
1
|
+
export * from './span.ts';
|
|
2
2
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"span.d.ts","sourceRoot":"","sources":["../../src/span.ts"],"names":[],"mappings":"AAAA,OAAO,EACW,KAAK,MAAM,EAC5B,MAAM,oBAAoB,CAAA;AAE3B,wBAAgB,IAAI,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,CAAC,CAarE;AAED,wBAAsB,SAAS,CAAC,CAAC,EAC/B,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,CAAC,CAAC,CAaZ"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xylabs/telemetry",
|
|
3
|
-
"version": "4.9.
|
|
3
|
+
"version": "4.9.7",
|
|
4
4
|
"description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"hex",
|
|
@@ -39,9 +39,7 @@
|
|
|
39
39
|
"packages/**/*"
|
|
40
40
|
],
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@opentelemetry/api": "^1.9.0"
|
|
43
|
-
"@opentelemetry/sdk-trace-base": "^2.0.0",
|
|
44
|
-
"chalk": "^5.4.1"
|
|
42
|
+
"@opentelemetry/api": "^1.9.0"
|
|
45
43
|
},
|
|
46
44
|
"devDependencies": {
|
|
47
45
|
"@xylabs/ts-scripts-yarn3": "^6.5.5",
|
package/src/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './
|
|
1
|
+
export * from './span.ts'
|
package/src/span.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import {
|
|
2
|
+
context, trace, type Tracer,
|
|
3
|
+
} from '@opentelemetry/api'
|
|
4
|
+
|
|
5
|
+
export function span<T>(name: string, fn: () => T, tracer?: Tracer): T {
|
|
6
|
+
if (tracer) {
|
|
7
|
+
const span = tracer.startSpan(name)
|
|
8
|
+
return context.with(trace.setSpan(context.active(), span), () => {
|
|
9
|
+
try {
|
|
10
|
+
return fn()
|
|
11
|
+
} finally {
|
|
12
|
+
span.end()
|
|
13
|
+
}
|
|
14
|
+
})
|
|
15
|
+
} else {
|
|
16
|
+
return fn()
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export async function spanAsync<T>(
|
|
21
|
+
name: string,
|
|
22
|
+
fn: () => Promise<T>,
|
|
23
|
+
tracer?: Tracer,
|
|
24
|
+
): Promise<T> {
|
|
25
|
+
if (tracer) {
|
|
26
|
+
const span = tracer.startSpan(name)
|
|
27
|
+
return await context.with(trace.setSpan(context.active(), span), async () => {
|
|
28
|
+
try {
|
|
29
|
+
return await fn()
|
|
30
|
+
} finally {
|
|
31
|
+
span.end()
|
|
32
|
+
}
|
|
33
|
+
})
|
|
34
|
+
} else {
|
|
35
|
+
return await fn()
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { ReadableSpan } from '@opentelemetry/sdk-trace-base';
|
|
2
|
-
import { ConsoleSpanExporter } from '@opentelemetry/sdk-trace-base';
|
|
3
|
-
export declare function spanDurationInMillis(span: ReadableSpan): number;
|
|
4
|
-
export declare class XyConsoleSpanExporter extends ConsoleSpanExporter {
|
|
5
|
-
static readonly durationToLogLevel: number[];
|
|
6
|
-
static readonly logLevelToChalkColor: import("chalk").ChalkInstance[];
|
|
7
|
-
private _logLevel;
|
|
8
|
-
constructor(logLevel?: number);
|
|
9
|
-
get logLevel(): number;
|
|
10
|
-
export(spans: ReadableSpan[]): void;
|
|
11
|
-
logColor(level: number): import("chalk").ChalkInstance;
|
|
12
|
-
spanLevel(span: ReadableSpan): number;
|
|
13
|
-
}
|
|
14
|
-
//# sourceMappingURL=XyConsoleSpanExporter.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"XyConsoleSpanExporter.d.ts","sourceRoot":"","sources":["../../src/XyConsoleSpanExporter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAA;AACjE,OAAO,EAAE,mBAAmB,EAAE,MAAM,+BAA+B,CAAA;AAGnE,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,YAAY,UAEtD;AAED,qBAAa,qBAAsB,SAAQ,mBAAmB;IAC5D,MAAM,CAAC,QAAQ,CAAC,kBAAkB,WAMjC;IAED,MAAM,CAAC,QAAQ,CAAC,oBAAoB,kCAMnC;IAED,OAAO,CAAC,SAAS,CAAQ;gBAEb,QAAQ,SAAI;IAKxB,IAAI,QAAQ,WAEX;IAEQ,MAAM,CAAC,KAAK,EAAE,YAAY,EAAE,GAAG,IAAI;IAe5C,QAAQ,CAAC,KAAK,EAAE,MAAM;IAItB,SAAS,CAAC,IAAI,EAAE,YAAY;CAW7B"}
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import type { ReadableSpan } from '@opentelemetry/sdk-trace-base'
|
|
2
|
-
import { ConsoleSpanExporter } from '@opentelemetry/sdk-trace-base'
|
|
3
|
-
import chalk from 'chalk'
|
|
4
|
-
|
|
5
|
-
export function spanDurationInMillis(span: ReadableSpan) {
|
|
6
|
-
return span.duration[0] * 1000 + span.duration[1] / 1e6
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export class XyConsoleSpanExporter extends ConsoleSpanExporter {
|
|
10
|
-
static readonly durationToLogLevel = [
|
|
11
|
-
0,
|
|
12
|
-
1,
|
|
13
|
-
10,
|
|
14
|
-
100,
|
|
15
|
-
1000,
|
|
16
|
-
]
|
|
17
|
-
|
|
18
|
-
static readonly logLevelToChalkColor = [
|
|
19
|
-
chalk.grey,
|
|
20
|
-
chalk.white,
|
|
21
|
-
chalk.green,
|
|
22
|
-
chalk.yellow,
|
|
23
|
-
chalk.red,
|
|
24
|
-
]
|
|
25
|
-
|
|
26
|
-
private _logLevel: number
|
|
27
|
-
|
|
28
|
-
constructor(logLevel = 0) {
|
|
29
|
-
super()
|
|
30
|
-
this._logLevel = logLevel
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
get logLevel() {
|
|
34
|
-
return this._logLevel
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
override export(spans: ReadableSpan[]): void {
|
|
38
|
-
for (const span of spans) {
|
|
39
|
-
const spanLevel = this.spanLevel(span)
|
|
40
|
-
if (spanLevel < this.logLevel) {
|
|
41
|
-
continue
|
|
42
|
-
}
|
|
43
|
-
const duration = spanDurationInMillis(span)
|
|
44
|
-
console.log(chalk.grey([
|
|
45
|
-
`Span [${span.name}]`,
|
|
46
|
-
this.logColor(spanLevel)(`${duration}ms`),
|
|
47
|
-
`TraceId: ${span.spanContext().traceId}`,
|
|
48
|
-
].join(', ')))
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
logColor(level: number) {
|
|
53
|
-
return XyConsoleSpanExporter.logLevelToChalkColor[level] ?? chalk.magenta
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
spanLevel(span: ReadableSpan) {
|
|
57
|
-
let logLevel = 0
|
|
58
|
-
const duration = spanDurationInMillis(span)
|
|
59
|
-
for (let x = XyConsoleSpanExporter.durationToLogLevel.length - 1; x >= 0; x--) {
|
|
60
|
-
if (duration > XyConsoleSpanExporter.durationToLogLevel[x]) {
|
|
61
|
-
logLevel = x
|
|
62
|
-
break
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return logLevel
|
|
66
|
-
}
|
|
67
|
-
}
|