@ichaingo/telemetry 1.1.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/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # telemetry
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Building
6
+
7
+ Run `nx build telemetry` to build the library.
8
+
9
+ ## Running unit tests
10
+
11
+ Run `nx test telemetry` to execute the unit tests via [Vitest](https://vitest.dev/).
@@ -0,0 +1,2 @@
1
+ export default function getInstrumentations(ignoredPaths: string[]): any;
2
+ //# sourceMappingURL=Instrumentations.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Instrumentations.d.ts","sourceRoot":"","sources":["../src/Instrumentations.ts"],"names":[],"mappings":"AAgBA,MAAM,CAAC,OAAO,UAAU,mBAAmB,CAAC,YAAY,EAAE,MAAM,EAAE,GAAG,GAAG,CAiCvE"}
@@ -0,0 +1,11 @@
1
+ import { Sampler, SamplingResult } from '@opentelemetry/sdk-trace-base';
2
+ import { SpanKind, Attributes, Link, Context } from '@opentelemetry/api';
3
+ declare class InspectionSampler implements Sampler {
4
+ private defaultSampler;
5
+ private inspectionSampler;
6
+ constructor(ratio: number);
7
+ shouldSample(context: Context, traceId: string, spanName: string, spanKind: SpanKind, attributes: Attributes, links: Link[]): SamplingResult;
8
+ toString(): string;
9
+ }
10
+ export default InspectionSampler;
11
+ //# sourceMappingURL=Sampler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Sampler.d.ts","sourceRoot":"","sources":["../src/Sampler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,cAAc,EAA+D,MAAM,+BAA+B,CAAC;AACrI,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,IAAI,EAAE,OAAO,EAAqB,MAAM,oBAAoB,CAAC;AAG5F,cAAM,iBAAkB,YAAW,OAAO;IACtC,OAAO,CAAC,cAAc,CAAU;IAChC,OAAO,CAAC,iBAAiB,CAAU;gBAEvB,KAAK,EAAE,MAAM;IAKzB,YAAY,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,UAAU,EAAE,UAAU,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,cAAc;IAuB5I,QAAQ;CAGT;AAED,eAAe,iBAAiB,CAAC"}
@@ -0,0 +1,12 @@
1
+ import { OTLPExporterNodeConfigBase } from '@opentelemetry/otlp-exporter-base';
2
+ interface TelemetryOptions extends OTLPExporterNodeConfigBase {
3
+ serviceName: string;
4
+ serviceVersion?: string;
5
+ environment: string;
6
+ ignorePaths?: string[];
7
+ inspectionRatio?: number;
8
+ }
9
+ export declare function startTelemetry(otlpOptions: TelemetryOptions): void;
10
+ export declare function shutdownTelemetry(): void;
11
+ export {};
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,0BAA0B,EAAE,MAAM,mCAAmC,CAAC;AAY/E,UAAU,gBAAiB,SAAQ,0BAA0B;IAC3D,WAAW,EAAE,MAAM,CAAC;IACpB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;IACvB,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAID,wBAAgB,cAAc,CAAC,WAAW,EAAE,gBAAgB,QAiC3D;AAED,wBAAgB,iBAAiB,SAIhC"}
package/dist/index.js ADDED
@@ -0,0 +1,98 @@
1
+ var S = Object.defineProperty;
2
+ var f = (n, e, t) => e in n ? S(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
3
+ var m = (n, e, t) => f(n, typeof e != "symbol" ? e + "" : e, t);
4
+ import { NodeSDK as g } from "@opentelemetry/sdk-node";
5
+ import { OTLPTraceExporter as E } from "@opentelemetry/exporter-trace-otlp-http";
6
+ import { resourceFromAttributes as I } from "@opentelemetry/resources";
7
+ import { ATTR_SERVICE_VERSION as T, ATTR_SERVICE_NAME as R } from "@opentelemetry/semantic-conventions";
8
+ import { TraceIdRatioBasedSampler as _, AlwaysOnSampler as x, SamplingDecision as l, BatchSpanProcessor as w } from "@opentelemetry/sdk-trace-base";
9
+ import { trace as A, TraceFlags as u } from "@opentelemetry/api";
10
+ import { getNodeAutoInstrumentations as N } from "@opentelemetry/auto-instrumentations-node";
11
+ import { UndiciInstrumentation as P } from "@opentelemetry/instrumentation-undici";
12
+ const y = "deployment.environment.name";
13
+ class D {
14
+ constructor(e) {
15
+ m(this, "defaultSampler");
16
+ m(this, "inspectionSampler");
17
+ this.defaultSampler = new _(e), this.inspectionSampler = new x();
18
+ }
19
+ shouldSample(e, t, r, s, o, i) {
20
+ if (o["http.request.header.x-inspection"] === "true" || o["x-inspection"] === "true")
21
+ return this.inspectionSampler.shouldSample(e, t, r, s, o, i);
22
+ const a = A.getSpanContext(e);
23
+ return a ? (a.traceFlags & u.SAMPLED) === u.SAMPLED ? { decision: l.RECORD_AND_SAMPLED } : { decision: l.NOT_RECORD } : this.defaultSampler.shouldSample(e, t, r, s, o, i);
24
+ }
25
+ toString() {
26
+ return "InspectionSampler";
27
+ }
28
+ }
29
+ const O = [
30
+ "/health",
31
+ "/metrics",
32
+ "/_next/static",
33
+ "/favicon.ico",
34
+ "/__nextjs_",
35
+ "/.well-known"
36
+ ], d = (n, e) => n.some((t) => e.includes(t));
37
+ function M(n) {
38
+ const e = [...O, ...n];
39
+ return [
40
+ N({
41
+ // 禁用可能导致问题的检测器
42
+ "@opentelemetry/instrumentation-grpc": {
43
+ enabled: !1
44
+ },
45
+ "@opentelemetry/instrumentation-http": {
46
+ enabled: !0,
47
+ startIncomingSpanHook: (t) => "headers" in t && t.headers["x-inspection"] === "true" ? {
48
+ "http.request.header.x-inspection": "true",
49
+ "x-inspection": "true"
50
+ } : {},
51
+ ignoreIncomingRequestHook: (t) => {
52
+ const r = t.url || "";
53
+ return d(e, r);
54
+ },
55
+ ignoreOutgoingRequestHook: (t) => {
56
+ const r = typeof t == "string" ? t : t.path || "";
57
+ return d(e, r);
58
+ }
59
+ }
60
+ }),
61
+ new P()
62
+ ];
63
+ }
64
+ let c;
65
+ function j(n) {
66
+ const {
67
+ serviceName: e,
68
+ serviceVersion: t,
69
+ environment: r,
70
+ ignorePaths: s = [],
71
+ inspectionRatio: o = 0.01,
72
+ ...i
73
+ } = n, p = I({
74
+ [R]: e || "ichaingo-seo",
75
+ [T]: t || "1.0.0",
76
+ [y]: r || "development"
77
+ }), a = new E(i), h = new w(a, {
78
+ scheduledDelayMillis: 1e3,
79
+ // 1秒延迟
80
+ maxQueueSize: 2048,
81
+ maxExportBatchSize: 512
82
+ });
83
+ c = new g({
84
+ serviceName: e,
85
+ resource: p,
86
+ spanProcessors: [h],
87
+ sampler: new D(o),
88
+ // traceExporter,
89
+ instrumentations: M(s)
90
+ }), c.start(), console.log("OpenTelemetry started:", JSON.stringify(n, null, 2));
91
+ }
92
+ function q() {
93
+ c && c.shutdown();
94
+ }
95
+ export {
96
+ q as shutdownTelemetry,
97
+ j as startTelemetry
98
+ };
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@ichaingo/telemetry",
3
+ "version": "1.1.0",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ "./package.json": "./package.json",
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "default": "./dist/index.js"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "!**/*.tsbuildinfo"
19
+ ],
20
+ "dependencies": {
21
+ "@opentelemetry/api": "^1.9.0",
22
+ "@opentelemetry/auto-instrumentations-node": "^0.62.0",
23
+ "@opentelemetry/core": "^2.0.1",
24
+ "@opentelemetry/exporter-jaeger": "^2.0.1",
25
+ "@opentelemetry/exporter-trace-otlp-http": "^0.203.0",
26
+ "@opentelemetry/instrumentation-fetch": "^0.203.0",
27
+ "@opentelemetry/instrumentation-http": "^0.203.0",
28
+ "@opentelemetry/instrumentation-undici": "^0.14.0",
29
+ "@opentelemetry/otlp-exporter-base": "^0.203.0",
30
+ "@opentelemetry/resources": "^2.0.1",
31
+ "@opentelemetry/sdk-node": "^0.203.0",
32
+ "@opentelemetry/sdk-trace-base": "^2.0.1",
33
+ "@opentelemetry/semantic-conventions": "^1.36.0",
34
+ "@opentelemetry/winston-transport": "^0.14.0"
35
+ }
36
+ }