@0xchain/telemetry 1.0.0 → 1.1.0-beta.10

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 CHANGED
@@ -1,11 +1,147 @@
1
1
  # telemetry
2
2
 
3
- This library was generated with [Nx](https://nx.dev).
3
+ 面向 Next.js 16 App Router 的链路追踪方案,提供巡检识别、请求过滤与多维采样能力,基于 OpenTelemetry 输出。
4
4
 
5
- ## Building
5
+ ## 快速开始
6
6
 
7
- Run `nx build telemetry` to build the library.
7
+ ### 1. 安装依赖
8
8
 
9
- ## Running unit tests
9
+ ```bash
10
+ pnpm add @0xchain/telemetry
11
+ ```
10
12
 
11
- Run `nx test telemetry` to execute the unit tests via [Vitest](https://vitest.dev/).
13
+ **Monorepo 内使用**:在应用包的 `package.json` 中添加 `"@0xchain/telemetry": "workspace:*"`,然后执行 `pnpm install`。使用前需先构建 telemetry 包:`pnpm nx run telemetry:build` `cd packages/telemetry && pnpm exec vite build`。
14
+
15
+ ### 2. 初始化追踪 (instrumentation.ts)
16
+
17
+ 在项目根目录(或 `src` 目录)创建 `instrumentation.ts`,只需一行代码即可启用:
18
+
19
+ > 注意:请从 `@0xchain/telemetry/instrumentation` 导入。此入口包含完整 Node SDK,仅在 `instrumentation.ts` 中加载,不影响 Edge 的 middleware bundle。
20
+
21
+ ```ts
22
+ import { registerTelemetry } from "@0xchain/telemetry/instrumentation";
23
+
24
+ export async function register() {
25
+ // 自动从环境变量加载配置
26
+ await registerTelemetry();
27
+ }
28
+ ```
29
+
30
+ 或者自定义配置:
31
+
32
+ ```ts
33
+ import { registerTelemetry } from "@0xchain/telemetry/instrumentation";
34
+
35
+ export async function register() {
36
+ await registerTelemetry({
37
+ serviceName: "my-web-app",
38
+ environment: "production",
39
+ // 更多配置...
40
+ });
41
+ }
42
+ ```
43
+
44
+ ### 3. 注入中间件 (middleware.ts)
45
+
46
+ 在 `middleware.ts` 中使用 `withTelemetryMiddleware` 包裹现有中间件,实现低侵入式集成:
47
+
48
+ > 注意:请从 `@0xchain/telemetry/middleware` 或 `@0xchain/telemetry/next` 导入。此入口轻量,无 Node SDK 依赖,适用于 Edge Runtime。
49
+
50
+ ```ts
51
+ import { NextResponse } from "next/server";
52
+ import { withTelemetryMiddleware } from "@0xchain/telemetry/middleware";
53
+
54
+ // 你的现有中间件逻辑
55
+ const userMiddleware = async (req) => {
56
+ // ... 业务逻辑
57
+ return NextResponse.next();
58
+ };
59
+
60
+ // 使用 withTelemetryMiddleware 包裹
61
+ export const middleware = withTelemetryMiddleware(userMiddleware);
62
+
63
+ export const config = {
64
+ matcher: [
65
+ /* 你的 matcher 配置 */
66
+ '/((?!_next/static|_next/image|favicon.ico).*)',
67
+ ],
68
+ };
69
+ ```
70
+
71
+ 如果你没有其他中间件,可以使用工厂方法:
72
+
73
+ ```ts
74
+ import { createTelemetryMiddleware } from "@0xchain/telemetry/middleware";
75
+
76
+ export const middleware = createTelemetryMiddleware();
77
+
78
+ export const config = {
79
+ matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
80
+ };
81
+ ```
82
+
83
+ ## 环境变量配置
84
+
85
+ 支持通过环境变量实现零代码配置:
86
+
87
+ | 变量名 | 描述 | 默认值 |
88
+ |---|---|---|
89
+ | `OTEL_SERVICE_NAME` | 服务名称 | `unknown-service` |
90
+ | `OTEL_SERVICE_VERSION` | 服务版本 | `1.0.0` |
91
+ | `OTEL_EXPORTER_OTLP_ENDPOINT` | OTLP 上报地址 | - |
92
+ | `OTEL_EXPORTER_OTLP_HEADERS` | OTLP Headers (k=v,k2=v2) | - |
93
+ | `TELEMETRY_ENVIRONMENT` | 环境名称 (dev/prod) | `development`,在 Vercel 上自动使用 `VERCEL_ENV` |
94
+ | `TELEMETRY_SAMPLING_RATIO` | 默认采样率 (0.0-1.0) | `0.01` |
95
+ | `TELEMETRY_IGNORE_PATHS` | 忽略路径 (逗号分隔) | 默认忽略静态资源 |
96
+ | `TELEMETRY_INSPECTION_HEADER` | 巡检请求头 | `x-inspection` |
97
+ | `TELEMETRY_CONFIG_FILE` | 配置文件路径,支持 JSON,异步读取不阻塞启动 | - |
98
+
99
+ ## Vercel 集成
100
+
101
+ 部署到 Vercel 时,将自动检测并注入以下 resource 属性:
102
+
103
+ - `vercel.region`、`vercel.runtime`、`vercel.sha`、`vercel.host`、`vercel.deployment_id`
104
+ - `environment` 优先使用 `VERCEL_ENV`(production / preview / development)
105
+
106
+ ## 核心特性
107
+
108
+ 1. **巡检识别**:自动识别 `x-inspection` 请求头,强制采样并全量记录。
109
+ 2. **智能过滤**:内置静态资源过滤规则,支持自定义路径、扩展名和方法过滤。
110
+ 3. **多维采样**:支持按路径、用户类型、状态码配置不同的采样率。
111
+ 4. **低侵入性**:通过 HOC 方式集成中间件,不破坏原有逻辑。
112
+ 5. **入口拆分**:`instrumentation` 含完整 Node SDK;`middleware` / `next` 轻量,仅注入请求头,适合 Edge。
113
+ 6. **按需加载**:仅启用 HTTP + Undici 检测,减少冷启动与内存占用。
114
+
115
+ ## 常见问题
116
+
117
+ ### Module not found: Can't resolve '@0xchain/telemetry/instrumentation'
118
+
119
+ 1. **确认依赖**:确保应用 `package.json` 中有 `@0xchain/telemetry` 依赖。
120
+ 2. **确认构建**:telemetry 包需先构建,`dist/` 目录需存在。在 monorepo 根目录执行 `pnpm nx run telemetry:build`。
121
+ 3. **Next.js 配置**:若仍有问题,可在 `next.config.js` 中添加 `transpilePackages: ['@0xchain/telemetry']`。
122
+
123
+ ## 高级配置
124
+
125
+ ### 采样规则配置 (Sampling Rules)
126
+
127
+ 可以通过 `samplingRules` 字段或 `TELEMETRY_SAMPLING_RULES` 环境变量(JSON 字符串)配置:
128
+
129
+ ```json
130
+ [
131
+ {
132
+ "name": "critical-api",
133
+ "ratio": 1.0,
134
+ "when": { "path": ["/api/checkout", "/api/payment"] }
135
+ },
136
+ {
137
+ "name": "errors",
138
+ "ratio": 1.0,
139
+ "when": { "minStatusCode": 500 }
140
+ },
141
+ {
142
+ "name": "guest-users",
143
+ "ratio": 0.05,
144
+ "when": { "userType": ["guest"] }
145
+ }
146
+ ]
147
+ ```
@@ -1,2 +1,6 @@
1
- export default function getInstrumentations(ignoredPaths: string[]): any;
1
+ import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
2
+ import { UndiciInstrumentation } from '@opentelemetry/instrumentation-undici';
3
+ import { TelemetryConfig } from './types';
4
+ /** 按需加载 HTTP + Undici 检测,避免全量 auto-instrumentations 的启动开销 */
5
+ export default function getInstrumentations(config: TelemetryConfig): (HttpInstrumentation | UndiciInstrumentation)[];
2
6
  //# sourceMappingURL=Instrumentations.d.ts.map
@@ -1 +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"}
1
+ {"version":3,"file":"Instrumentations.d.ts","sourceRoot":"","sources":["../src/Instrumentations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,qCAAqC,CAAC;AAC1E,OAAO,EAAE,qBAAqB,EAAE,MAAM,uCAAuC,CAAC;AAE9E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AA4B/C,6DAA6D;AAC7D,MAAM,CAAC,OAAO,UAAU,mBAAmB,CAAC,MAAM,EAAE,eAAe,mDAoElE"}
package/dist/Sampler.d.ts CHANGED
@@ -1,11 +1,22 @@
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;
1
+ import { Sampler, SpanExporter } from '@opentelemetry/sdk-trace-base';
2
+ import { Attributes } from '@opentelemetry/api';
3
+ import { SamplingRule, TelemetryRuntime } from './types';
4
+ export interface SamplingContext {
5
+ path?: string;
6
+ method?: string;
7
+ userType?: string;
8
+ statusCode?: number;
9
+ inspection?: boolean;
9
10
  }
10
- export default InspectionSampler;
11
+ /** 基于 span attributes 构建采样上下文 */
12
+ export declare const buildSamplingContextFromAttributes: (attributes: Attributes) => SamplingContext;
13
+ /** 判断采样规则是否命中 */
14
+ export declare const matchSamplingRule: (rule: SamplingRule, context: SamplingContext) => boolean;
15
+ /** 根据规则解析采样比例 */
16
+ export declare const resolveSamplingRatio: (rules: SamplingRule[] | undefined, context: SamplingContext, defaultRatio: number) => number;
17
+ /** 创建基于规则的采样器 */
18
+ export declare const createRuleBasedSampler: (runtime: TelemetryRuntime) => Sampler;
19
+ /** 导出器直接透传,采样决策由 createRuleBasedSampler 在 span 创建时统一完成,避免 export 阶段重复计算 */
20
+ export declare const createFilteringTraceExporter: (exporter: SpanExporter) => SpanExporter;
21
+ export default createRuleBasedSampler;
11
22
  //# sourceMappingURL=Sampler.d.ts.map
@@ -1 +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"}
1
+ {"version":3,"file":"Sampler.d.ts","sourceRoot":"","sources":["../src/Sampler.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EAIP,YAAY,EACb,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAY,UAAU,EAAoC,MAAM,oBAAoB,CAAC;AAC5F,OAAO,KAAK,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAG9D,MAAM,WAAW,eAAe;IAC9B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAoCD,iCAAiC;AACjC,eAAO,MAAM,kCAAkC,GAAI,YAAY,UAAU,KAAG,eA4B3E,CAAC;AAcF,iBAAiB;AACjB,eAAO,MAAM,iBAAiB,GAAI,MAAM,YAAY,EAAE,SAAS,eAAe,YAiC7E,CAAC;AAKF,iBAAiB;AACjB,eAAO,MAAM,oBAAoB,GAC/B,OAAO,YAAY,EAAE,GAAG,SAAS,EACjC,SAAS,eAAe,EACxB,cAAc,MAAM,WASrB,CAAC;AAoBF,iBAAiB;AACjB,eAAO,MAAM,sBAAsB,GAAI,SAAS,gBAAgB,KAAG,OAyBlE,CAAC;AAEF,2EAA2E;AAC3E,eAAO,MAAM,4BAA4B,GAAI,UAAU,YAAY,KAAG,YAAwB,CAAC;AAE/F,eAAe,sBAAsB,CAAC"}
@@ -0,0 +1,80 @@
1
+ const d = [
2
+ "/health",
3
+ "/api/health",
4
+ "/metrics",
5
+ "/readyz",
6
+ "/livez",
7
+ "/favicon.ico",
8
+ "/robots.txt",
9
+ "/sitemap.xml",
10
+ "/_next",
11
+ "/__nextjs_",
12
+ "/.well-known"
13
+ ], m = [
14
+ ".css",
15
+ ".js",
16
+ ".mjs",
17
+ ".cjs",
18
+ ".png",
19
+ ".jpg",
20
+ ".jpeg",
21
+ ".gif",
22
+ ".svg",
23
+ ".ico",
24
+ ".webp",
25
+ ".avif",
26
+ ".woff",
27
+ ".woff2",
28
+ ".ttf",
29
+ ".eot",
30
+ ".map",
31
+ ".txt",
32
+ ".xml"
33
+ ], i = {
34
+ paths: d,
35
+ extensions: m,
36
+ methods: ["HEAD", "OPTIONS"]
37
+ }, f = (e) => e.map((s) => {
38
+ const t = s.trim().toLowerCase();
39
+ return t && (t.startsWith(".") ? t : `.${t}`);
40
+ }), w = (e) => e.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), C = (e, s) => {
41
+ if (e instanceof RegExp)
42
+ return e.test(s);
43
+ const t = e.trim();
44
+ if (!t) return !1;
45
+ if (t.includes("*")) {
46
+ const a = `^${w(t).replace(/\\\*\\\*/g, ".*").replace(/\\\*/g, "[^/]*")}$`;
47
+ return new RegExp(a).test(s);
48
+ }
49
+ return s === t ? !0 : t.endsWith("/") ? s.startsWith(t) : s.startsWith(`${t}/`) || s.startsWith(t);
50
+ }, E = (e, s) => {
51
+ const t = s.toLowerCase();
52
+ return e.some((o) => o && t.endsWith(o));
53
+ }, j = (e) => {
54
+ var a;
55
+ if (!e) return "default";
56
+ if ((a = e.paths) == null ? void 0 : a.some((n) => n instanceof RegExp)) return null;
57
+ const t = (e.paths ?? []).map(String).sort().join(","), o = (e.extensions ?? []).map((n) => n.toLowerCase()).sort().join(","), h = (e.methods ?? []).map((n) => n.toUpperCase()).sort().join(",");
58
+ return `p:${t}|e:${o}|m:${h}`;
59
+ }, c = /* @__PURE__ */ new Map(), $ = 16, I = (e) => {
60
+ var n, l, p;
61
+ const s = j(e);
62
+ if (s) {
63
+ const r = c.get(s);
64
+ if (r) return r;
65
+ }
66
+ const t = (n = e == null ? void 0 : e.paths) != null && n.length ? e.paths : i.paths, o = (l = e == null ? void 0 : e.extensions) != null && l.length ? f(e.extensions) : i.extensions, h = (p = e == null ? void 0 : e.methods) != null && p.length ? e.methods.map((r) => r.toUpperCase()) : i.methods, a = (r, u) => u && h.includes(u.toUpperCase()) ? !0 : r ? E(o, r) ? !0 : t.some((x) => C(x, r)) : !1;
67
+ if (s) {
68
+ if (c.size >= $) {
69
+ const r = c.keys().next().value;
70
+ r && c.delete(r);
71
+ }
72
+ c.set(s, a);
73
+ }
74
+ return a;
75
+ };
76
+ export {
77
+ I as c,
78
+ i as d,
79
+ C as m
80
+ };
@@ -0,0 +1,7 @@
1
+ import { IgnoreConfig, PathPattern } from './types';
2
+ export declare const defaultIgnoreConfig: Required<IgnoreConfig>;
3
+ /** 判断路径是否匹配规则 */
4
+ export declare const matchesPathPattern: (pattern: PathPattern, pathname: string) => boolean;
5
+ /** 创建忽略规则匹配器(相同配置复用,减少重复创建) */
6
+ export declare const createIgnoreMatcher: (config?: IgnoreConfig) => (pathname: string, method?: string) => boolean;
7
+ //# sourceMappingURL=ignore.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ignore.d.ts","sourceRoot":"","sources":["../src/ignore.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAsCzD,eAAO,MAAM,mBAAmB,EAAE,QAAQ,CAAC,YAAY,CAItD,CAAC;AAcF,iBAAiB;AACjB,eAAO,MAAM,kBAAkB,GAAI,SAAS,WAAW,EAAE,UAAU,MAAM,YAmBxE,CAAC;AAsBF,+BAA+B;AAC/B,eAAO,MAAM,mBAAmB,GAAI,SAAS,YAAY,gBAJT,MAAM,WAAW,MAAM,KAAK,OAsC3E,CAAC"}
package/dist/index ADDED
@@ -0,0 +1,355 @@
1
+ import { NodeSDK as b } from "@opentelemetry/sdk-node";
2
+ import { OTLPTraceExporter as v } from "@opentelemetry/exporter-trace-otlp-http";
3
+ import { resourceFromAttributes as Y } from "@opentelemetry/resources";
4
+ import { ATTR_SERVICE_VERSION as F, ATTR_SERVICE_NAME as q } from "@opentelemetry/semantic-conventions";
5
+ import { SamplingDecision as d, TraceIdRatioBasedSampler as N, AlwaysOnSampler as U, BatchSpanProcessor as z } from "@opentelemetry/sdk-trace-base";
6
+ import { trace as B, TraceFlags as M, propagation as I, context as V, SpanStatusCode as G } from "@opentelemetry/api";
7
+ import { readFile as k } from "node:fs/promises";
8
+ import { HttpInstrumentation as j } from "@opentelemetry/instrumentation-http";
9
+ import { UndiciInstrumentation as Q } from "@opentelemetry/instrumentation-undici";
10
+ import { c as X, m as W, d as J } from "./ignore-B1KXtl12.js";
11
+ const K = "deployment.environment.name", f = (t, e) => {
12
+ if (!t) return;
13
+ const r = t[e.toLowerCase()];
14
+ if (Array.isArray(r)) {
15
+ const n = r[0];
16
+ return typeof n == "string" ? n : void 0;
17
+ }
18
+ return typeof r == "string" ? r : void 0;
19
+ }, T = (t) => {
20
+ if (!t) return "";
21
+ try {
22
+ return t.startsWith("http://") || t.startsWith("https://") ? new URL(t).pathname : new URL(t, "http://localhost").pathname;
23
+ } catch {
24
+ return t.split("?")[0] || t;
25
+ }
26
+ }, w = /* @__PURE__ */ new WeakMap();
27
+ function $(t) {
28
+ const e = X(t.ignore), r = (t.inspectionHeader ?? "x-inspection").toLowerCase(), n = (t.userTypeHeader ?? "x-telemetry-user-type").toLowerCase(), o = (t.requestStartHeader ?? "x-telemetry-start").toLowerCase();
29
+ return [
30
+ new j({
31
+ startIncomingSpanHook: (s) => {
32
+ const i = "headers" in s ? s.headers : void 0, a = f(i, r), c = f(i, n), u = f(i, o), l = "method" in s ? s.method : void 0, C = T("url" in s ? s.url : void 0), L = Number(u || Date.now()), m = {
33
+ "telemetry.request.start_time_ms": Number.isFinite(L) ? L : Date.now()
34
+ };
35
+ return C && (m["url.path"] = C), l && (m["http.request.method"] = l), a && (m["telemetry.request.inspection"] = !0, m["http.request.header.x-inspection"] = a), c && (m["enduser.type"] = c, m["telemetry.request.user_type"] = c), m;
36
+ },
37
+ requestHook: (s, i) => {
38
+ const a = "headers" in i ? i.headers : void 0, c = f(a, o), u = Number(c || Date.now());
39
+ w.set(s, Number.isFinite(u) ? u : Date.now());
40
+ },
41
+ responseHook: (s, i) => {
42
+ const a = "statusCode" in i ? i.statusCode : void 0;
43
+ typeof a == "number" && (s.setAttribute("http.response.status_code", a), s.setAttribute("telemetry.response.status_code", a), a >= 500 && s.setAttribute("telemetry.request.error", !0));
44
+ const c = w.get(s);
45
+ typeof c == "number" && s.setAttribute("telemetry.request.duration_ms", Date.now() - c);
46
+ },
47
+ ignoreIncomingRequestHook: (s) => {
48
+ const i = "url" in s ? s.url : void 0, a = T(i), c = "method" in s ? s.method : void 0;
49
+ return e(a, c);
50
+ },
51
+ ignoreOutgoingRequestHook: (s) => {
52
+ const i = typeof s == "string" ? s : s.path || "";
53
+ return e(T(i));
54
+ }
55
+ }),
56
+ new Q()
57
+ ];
58
+ }
59
+ const R = (t) => {
60
+ if (typeof t == "string") return t;
61
+ if (Array.isArray(t)) {
62
+ const e = t[0];
63
+ return typeof e == "string" ? e : void 0;
64
+ }
65
+ }, x = (t) => {
66
+ if (typeof t == "number") return t;
67
+ if (typeof t == "string") {
68
+ const e = Number(t);
69
+ return Number.isFinite(e) ? e : void 0;
70
+ }
71
+ if (Array.isArray(t)) {
72
+ const e = t[0];
73
+ return x(e);
74
+ }
75
+ }, E = (t, e) => {
76
+ for (const r of e)
77
+ if (r in t)
78
+ return t[r];
79
+ }, Z = (t) => {
80
+ const e = E(t, ["url.path", "http.target", "http.route"]), r = E(t, ["http.request.method", "http.method"]), n = E(t, ["enduser.type", "telemetry.request.user_type"]), o = E(t, [
81
+ "http.response.status_code",
82
+ "http.status_code",
83
+ "telemetry.response.status_code"
84
+ ]), s = E(t, [
85
+ "telemetry.request.inspection",
86
+ "http.request.header.x-inspection",
87
+ "x-inspection"
88
+ ]), i = s === !0 || s === "true" || s === "1" || s === 1;
89
+ return {
90
+ path: R(e),
91
+ method: R(r),
92
+ userType: R(n),
93
+ statusCode: x(o),
94
+ inspection: i
95
+ };
96
+ }, O = (t, e) => !t || !(e != null && e.length) ? !1 : e.some((r) => r.toLowerCase() === t.toLowerCase()), tt = (t, e) => !t || !(e != null && e.length) ? !1 : e.some((r) => W(r, t)), et = (t, e) => {
97
+ const r = t.when;
98
+ if (!r) return !0;
99
+ if (r.inspection !== void 0 && r.inspection !== e.inspection || r.path && !tt(e.path, r.path) || r.method && !O(e.method, r.method) || r.userType && !O(e.userType, r.userType))
100
+ return !1;
101
+ if (r.statusCode && typeof e.statusCode == "number") {
102
+ if (!r.statusCode.includes(e.statusCode))
103
+ return !1;
104
+ } else if (r.statusCode && r.statusCode.length)
105
+ return !1;
106
+ return !(typeof r.minStatusCode == "number" && (typeof e.statusCode != "number" || e.statusCode < r.minStatusCode) || typeof r.maxStatusCode == "number" && (typeof e.statusCode != "number" || e.statusCode > r.maxStatusCode));
107
+ }, _ = (t) => Math.max(0, Math.min(1, t)), rt = (t, e, r) => {
108
+ if (!(t != null && t.length)) return _(r);
109
+ for (const n of t)
110
+ if (et(n, e))
111
+ return _(n.ratio);
112
+ return _(r);
113
+ }, A = /* @__PURE__ */ new Map(), nt = (t) => {
114
+ if (t <= 0)
115
+ return new N(0);
116
+ if (t >= 1)
117
+ return new U();
118
+ const e = Number(t.toFixed(6)), r = A.get(e);
119
+ if (r) return r;
120
+ const n = new N(e);
121
+ return A.set(e, n), n;
122
+ }, st = (t) => ({
123
+ shouldSample(e, r, n, o, s, i) {
124
+ const a = B.getSpanContext(e);
125
+ if (a)
126
+ return (a.traceFlags & M.SAMPLED) === M.SAMPLED ? { decision: d.RECORD_AND_SAMPLED } : { decision: d.NOT_RECORD };
127
+ const c = t.getConfig(), u = Z(s);
128
+ if (u.inspection)
129
+ return { decision: d.RECORD_AND_SAMPLED };
130
+ const l = rt(
131
+ c.samplingRules,
132
+ u,
133
+ c.defaultSamplingRatio ?? 0.01
134
+ );
135
+ return nt(l).shouldSample(e, r, n, o, s, i);
136
+ },
137
+ toString: () => "RuleBasedSampler"
138
+ }), ot = (t) => t;
139
+ let h, p;
140
+ const g = (t) => Array.from(new Set(t)), S = (t) => (t == null ? void 0 : t.split(",").map((e) => e.trim()).filter(Boolean)) ?? [], it = (t) => {
141
+ if (!t) return;
142
+ const e = {};
143
+ for (const r of t.split(",")) {
144
+ const [n, ...o] = r.split("="), s = n == null ? void 0 : n.trim();
145
+ s && (e[s] = o.join("=").trim());
146
+ }
147
+ return e;
148
+ }, y = (t) => {
149
+ if (t)
150
+ try {
151
+ return JSON.parse(t);
152
+ } catch {
153
+ return;
154
+ }
155
+ }, at = async (t) => {
156
+ if (t)
157
+ try {
158
+ const e = await k(t, "utf8");
159
+ return y(e);
160
+ } catch {
161
+ return;
162
+ }
163
+ }, H = (t, e) => {
164
+ const r = g([...(t == null ? void 0 : t.paths) ?? [], ...(e == null ? void 0 : e.paths) ?? []]), n = g([
165
+ ...(t == null ? void 0 : t.extensions) ?? [],
166
+ ...(e == null ? void 0 : e.extensions) ?? []
167
+ ]), o = g([...(t == null ? void 0 : t.methods) ?? [], ...(e == null ? void 0 : e.methods) ?? []]);
168
+ return {
169
+ paths: r.length ? r : void 0,
170
+ extensions: n.length ? n : void 0,
171
+ methods: o.length ? o : void 0
172
+ };
173
+ }, ct = (t) => {
174
+ if (t != null && t.length)
175
+ return t.map((e) => ({
176
+ ...e,
177
+ ratio: Number.isFinite(e.ratio) ? Math.max(0, Math.min(1, e.ratio)) : 0
178
+ })).filter((e) => e.ratio >= 0);
179
+ }, ut = async (t = process.env) => {
180
+ const e = await at(t.TELEMETRY_CONFIG_FILE), r = S(t.TELEMETRY_IGNORE_PATHS), n = S(t.TELEMETRY_IGNORE_EXTENSIONS), o = S(t.TELEMETRY_IGNORE_METHODS), s = y(t.TELEMETRY_SAMPLING_RULES), i = typeof (e == null ? void 0 : e.samplingRules) == "string" ? y(e.samplingRules) : e == null ? void 0 : e.samplingRules, a = {
181
+ serviceName: t.OTEL_SERVICE_NAME || t.TELEMETRY_SERVICE_NAME,
182
+ serviceVersion: t.OTEL_SERVICE_VERSION || t.TELEMETRY_SERVICE_VERSION,
183
+ environment: t.OTEL_RESOURCE_ATTRIBUTES || t.TELEMETRY_ENVIRONMENT || (t.VERCEL ? t.VERCEL_ENV : void 0),
184
+ defaultSamplingRatio: t.TELEMETRY_SAMPLING_RATIO ? Number(t.TELEMETRY_SAMPLING_RATIO) : void 0,
185
+ url: t.OTEL_EXPORTER_OTLP_ENDPOINT || t.TELEMETRY_OTLP_ENDPOINT,
186
+ headers: it(t.OTEL_EXPORTER_OTLP_HEADERS || t.TELEMETRY_OTLP_HEADERS),
187
+ inspectionHeader: t.TELEMETRY_INSPECTION_HEADER,
188
+ userTypeHeader: t.TELEMETRY_USER_TYPE_HEADER,
189
+ requestStartHeader: t.TELEMETRY_REQUEST_START_HEADER,
190
+ samplingRules: s || i,
191
+ ignore: r.length || n.length || o.length ? {
192
+ paths: r.length ? r : void 0,
193
+ extensions: n.length ? n : void 0,
194
+ methods: o.length ? o : void 0
195
+ } : void 0
196
+ };
197
+ return e && typeof e == "object" ? { ...e, ...a } : a;
198
+ }, mt = (t) => {
199
+ if (!t) return process.env.NODE_ENV || "development";
200
+ if (t.includes("deployment.environment")) {
201
+ const r = t.split(",").find((o) => o.includes("deployment.environment"));
202
+ if (!r) return t;
203
+ const [, n] = r.split("=");
204
+ return (n == null ? void 0 : n.trim()) || t;
205
+ }
206
+ return t;
207
+ }, pt = (t, e) => {
208
+ var n, o;
209
+ const r = [
210
+ { name: "inspection", ratio: 1, when: { inspection: !0 } },
211
+ { name: "error", ratio: 1, when: { minStatusCode: 500 } }
212
+ ];
213
+ if ((n = t == null ? void 0 : t.criticalPaths) != null && n.length && r.push({
214
+ name: "critical-path",
215
+ ratio: 1,
216
+ when: { path: t.criticalPaths }
217
+ }), (o = t == null ? void 0 : t.criticalUserTypes) != null && o.length && r.push({
218
+ name: "critical-user",
219
+ ratio: 1,
220
+ when: { userType: t.criticalUserTypes }
221
+ }), t != null && t.pathSampling)
222
+ for (const [s, i] of Object.entries(t.pathSampling))
223
+ r.push({
224
+ name: `path:${s}`,
225
+ ratio: i,
226
+ when: { path: [s] }
227
+ });
228
+ if (t != null && t.userTypeSampling)
229
+ for (const [s, i] of Object.entries(t.userTypeSampling))
230
+ r.push({
231
+ name: `user:${s}`,
232
+ ratio: i,
233
+ when: { userType: [s] }
234
+ });
235
+ return r.push({ name: "default", ratio: e }), r;
236
+ }, D = (t) => {
237
+ const e = mt(t.environment), r = H(J, t.ignore), n = typeof t.defaultSamplingRatio == "number" ? Math.max(0, Math.min(1, t.defaultSamplingRatio)) : 0.01, o = ct(t.samplingRules) ?? pt(t.layeredSampling, n);
238
+ return {
239
+ ...t,
240
+ serviceName: t.serviceName || "unknown-service",
241
+ serviceVersion: t.serviceVersion,
242
+ environment: e,
243
+ defaultSamplingRatio: n,
244
+ ignore: r,
245
+ samplingRules: o,
246
+ inspectionHeader: t.inspectionHeader ?? "x-inspection",
247
+ userTypeHeader: t.userTypeHeader ?? "x-telemetry-user-type",
248
+ requestStartHeader: t.requestStartHeader ?? "x-telemetry-start",
249
+ scheduledDelayMillis: t.scheduledDelayMillis ?? 1e3,
250
+ maxQueueSize: t.maxQueueSize ?? 2048,
251
+ maxExportBatchSize: t.maxExportBatchSize ?? 512,
252
+ exporterTimeoutMillis: t.exporterTimeoutMillis ?? 3e4
253
+ };
254
+ }, P = async (t = {}, e = process.env) => {
255
+ const r = await ut(e);
256
+ return D({ ...r, ...t });
257
+ }, Et = async (t) => {
258
+ let e = await P(t);
259
+ return {
260
+ getConfig: () => e,
261
+ updateConfig: (r) => {
262
+ e = D({
263
+ ...e,
264
+ ...r,
265
+ ignore: H(e.ignore, r.ignore)
266
+ });
267
+ }
268
+ };
269
+ }, lt = (t) => t[0] * 1e3 + t[1] / 1e6, ft = (t, e) => {
270
+ for (const r of e) {
271
+ const n = t.attributes[r];
272
+ if (typeof n == "number") return n;
273
+ }
274
+ }, ht = (t) => {
275
+ const e = t.events.find((n) => n.name === "exception");
276
+ if (!(e != null && e.attributes)) return;
277
+ const r = e.attributes["exception.message"];
278
+ return typeof r == "string" ? r : void 0;
279
+ }, dt = () => ({
280
+ onStart: () => {
281
+ },
282
+ onEnd: (t) => {
283
+ const e = lt(t.duration);
284
+ t.attributes["telemetry.request.duration_ms"] = Math.round(e), typeof t.attributes["telemetry.request.start_time_ms"] != "number" && (t.attributes["telemetry.request.start_time_ms"] = Date.now() - Math.round(e));
285
+ const r = ft(t, [
286
+ "http.response.status_code",
287
+ "http.status_code",
288
+ "telemetry.response.status_code"
289
+ ]);
290
+ if (typeof r == "number" && (t.attributes["telemetry.response.status_code"] = r), t.status.code === G.ERROR || typeof r == "number" && r >= 500) {
291
+ t.attributes["telemetry.request.error"] = !0;
292
+ const o = t.status.message || ht(t);
293
+ o && (t.attributes["telemetry.request.error_message"] = o);
294
+ }
295
+ },
296
+ shutdown: () => Promise.resolve(),
297
+ forceFlush: () => Promise.resolve()
298
+ }), wt = async (t) => {
299
+ const e = await P(t);
300
+ p = await Et(e);
301
+ const r = process.env, n = {
302
+ [q]: e.serviceName,
303
+ [F]: e.serviceVersion || "1.0.0",
304
+ [K]: e.environment || r.VERCEL_ENV || r.NODE_ENV || "development"
305
+ };
306
+ r.VERCEL && (r.VERCEL_REGION && (n["vercel.region"] = r.VERCEL_REGION), r.NEXT_RUNTIME && (n["vercel.runtime"] = r.NEXT_RUNTIME), r.VERCEL_GIT_COMMIT_SHA && (n["vercel.sha"] = r.VERCEL_GIT_COMMIT_SHA), r.VERCEL_URL && (n["vercel.host"] = r.VERCEL_URL), r.VERCEL_BRANCH_URL && (n["vercel.branch_host"] = r.VERCEL_BRANCH_URL), r.VERCEL_DEPLOYMENT_ID && (n["vercel.deployment_id"] = r.VERCEL_DEPLOYMENT_ID));
307
+ const o = Y(n), s = new v(e), i = ot(s), a = [
308
+ dt(),
309
+ new z(i, {
310
+ scheduledDelayMillis: e.scheduledDelayMillis,
311
+ maxQueueSize: e.maxQueueSize,
312
+ maxExportBatchSize: e.maxExportBatchSize,
313
+ exportTimeoutMillis: e.exporterTimeoutMillis
314
+ })
315
+ ];
316
+ h = new b({
317
+ serviceName: e.serviceName,
318
+ resource: o,
319
+ spanProcessors: a,
320
+ sampler: st(p),
321
+ instrumentations: $(e)
322
+ });
323
+ try {
324
+ await h.start();
325
+ } catch (c) {
326
+ console.warn("Telemetry start failed:", c);
327
+ }
328
+ return p;
329
+ }, Ot = (t) => {
330
+ p == null || p.updateConfig(t);
331
+ }, At = (t) => (I.inject(V.active(), t), t), It = (t = fetch) => async (e, r) => {
332
+ const n = (r == null ? void 0 : r.headers) ?? (e instanceof Request ? e.headers : void 0), o = new Headers(n);
333
+ return I.inject(V.active(), o, {
334
+ set: (s, i, a) => {
335
+ s.set(i, a);
336
+ }
337
+ }), t(e, { ...r, headers: o });
338
+ }, Vt = async () => {
339
+ h && await h.shutdown();
340
+ };
341
+ export {
342
+ Z as buildSamplingContextFromAttributes,
343
+ X as createIgnoreMatcher,
344
+ It as createPropagatingFetch,
345
+ Et as createTelemetryRuntime,
346
+ J as defaultIgnoreConfig,
347
+ At as injectTraceHeaders,
348
+ ut as loadTelemetryConfigFromEnv,
349
+ et as matchSamplingRule,
350
+ rt as resolveSamplingRatio,
351
+ P as resolveTelemetryConfig,
352
+ Vt as shutdownTelemetry,
353
+ wt as startTelemetry,
354
+ Ot as updateTelemetryConfig
355
+ };
package/dist/index.d.ts CHANGED
@@ -1,12 +1,22 @@
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 {};
1
+ import { buildSamplingContextFromAttributes, matchSamplingRule, resolveSamplingRatio } from './Sampler';
2
+ import { createIgnoreMatcher, defaultIgnoreConfig } from './ignore';
3
+ import { TelemetryConfig, TelemetryRuntime } from './types';
4
+ export * from './types';
5
+ export { buildSamplingContextFromAttributes, matchSamplingRule, resolveSamplingRatio, createIgnoreMatcher, defaultIgnoreConfig, };
6
+ /** 从环境变量加载 Telemetry 配置(异步读取配置文件,不阻塞主线程) */
7
+ export declare const loadTelemetryConfigFromEnv: (env?: NodeJS.ProcessEnv) => Promise<Partial<TelemetryConfig>>;
8
+ /** 合并环境变量与显式配置得到最终配置 */
9
+ export declare const resolveTelemetryConfig: (options?: Partial<TelemetryConfig>, env?: NodeJS.ProcessEnv) => Promise<TelemetryConfig>;
10
+ /** 创建可运行时更新的配置容器 */
11
+ export declare const createTelemetryRuntime: (initialConfig: Partial<TelemetryConfig>) => Promise<TelemetryRuntime>;
12
+ /** 启动 OpenTelemetry SDK */
13
+ export declare const startTelemetry: (options: Partial<TelemetryConfig> | TelemetryConfig) => Promise<TelemetryRuntime>;
14
+ /** 运行时更新 Telemetry 配置 */
15
+ export declare const updateTelemetryConfig: (partial: Partial<TelemetryConfig>) => void;
16
+ /** 注入 traceparent 等传播头 */
17
+ export declare const injectTraceHeaders: (headers: Record<string, string>) => Record<string, string>;
18
+ /** 创建带 traceparent 注入的 fetch */
19
+ export declare const createPropagatingFetch: (fetchImpl?: typeof fetch) => (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
20
+ /** 停止并释放 Telemetry SDK */
21
+ export declare const shutdownTelemetry: () => Promise<void>;
12
22
  //# sourceMappingURL=index.d.ts.map
@@ -1 +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"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAgBA,OAAO,EACL,kCAAkC,EAGlC,iBAAiB,EACjB,oBAAoB,EACrB,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC;AACpE,OAAO,KAAK,EAIV,eAAe,EACf,gBAAgB,EACjB,MAAM,SAAS,CAAC;AAEjB,cAAc,SAAS,CAAC;AACxB,OAAO,EACL,kCAAkC,EAClC,iBAAiB,EACjB,oBAAoB,EACpB,mBAAmB,EACnB,mBAAmB,GACpB,CAAC;AA4EF,4CAA4C;AAC5C,eAAO,MAAM,0BAA0B,GACrC,MAAK,MAAM,CAAC,UAAwB,KACnC,OAAO,CAAC,OAAO,CAAC,eAAe,CAAC,CAuClC,CAAC;AAiGF,wBAAwB;AACxB,eAAO,MAAM,sBAAsB,GACjC,UAAS,OAAO,CAAC,eAAe,CAAM,EACtC,MAAK,MAAM,CAAC,UAAwB,KACnC,OAAO,CAAC,eAAe,CAGzB,CAAC;AAEF,oBAAoB;AACpB,eAAO,MAAM,sBAAsB,GACjC,eAAe,OAAO,CAAC,eAAe,CAAC,KACtC,OAAO,CAAC,gBAAgB,CAY1B,CAAC;AAwDF,2BAA2B;AAC3B,eAAO,MAAM,cAAc,GAAU,SAAS,OAAO,CAAC,eAAe,CAAC,GAAG,eAAe,8BA+CvF,CAAC;AAEF,yBAAyB;AACzB,eAAO,MAAM,qBAAqB,GAAI,SAAS,OAAO,CAAC,eAAe,CAAC,SAEtE,CAAC;AAEF,0BAA0B;AAC1B,eAAO,MAAM,kBAAkB,GAAI,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,2BAGjE,CAAC;AAEF,gCAAgC;AAChC,eAAO,MAAM,sBAAsB,GAAI,YAAW,OAAO,KAAa,MACtD,OAAO,WAAW,GAAG,GAAG,EAAE,OAAO,WAAW,sBAY3D,CAAC;AAEF,0BAA0B;AAC1B,eAAO,MAAM,iBAAiB,qBAI7B,CAAC"}
@@ -0,0 +1,8 @@
1
+ import { startTelemetry as a } from "./index";
2
+ const o = async (t) => {
3
+ const e = typeof t == "function" ? await t() : t;
4
+ await a(e ?? {});
5
+ };
6
+ export {
7
+ o as registerTelemetry
8
+ };
@@ -0,0 +1,4 @@
1
+ import { TelemetryConfig } from './types';
2
+ /** 在 Next.js instrumentation.ts 中注册追踪 */
3
+ export declare const registerTelemetry: (configOrProvider?: TelemetryConfig | (() => TelemetryConfig | Promise<TelemetryConfig>)) => Promise<void>;
4
+ //# sourceMappingURL=instrumentation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"instrumentation.d.ts","sourceRoot":"","sources":["../src/instrumentation.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE/C,yCAAyC;AACzC,eAAO,MAAM,iBAAiB,GAC5B,mBAAmB,eAAe,GAAG,CAAC,MAAM,eAAe,GAAG,OAAO,CAAC,eAAe,CAAC,CAAC,kBAKxF,CAAC"}
@@ -0,0 +1,29 @@
1
+ import { NextRequest as w, NextResponse as i } from "next/server";
2
+ import { c as g } from "./ignore-B1KXtl12.js";
3
+ const h = {
4
+ matcher: [
5
+ "/((?!_next/static|_next/image|favicon.ico|robots.txt|sitemap.xml|.*\\.(?:css|js|mjs|cjs|png|jpg|jpeg|gif|svg|ico|webp|avif|woff|woff2|ttf|eot|map|txt|xml)).*)"
6
+ ]
7
+ }, l = (r, s = {}) => {
8
+ const p = g(s.ignore), d = (s.requestStartHeader ?? "x-telemetry-start").toLowerCase(), n = (s.userTypeHeader ?? "x-telemetry-user-type").toLowerCase(), a = s.resolveUserType;
9
+ return async (e, o) => {
10
+ const f = e.nextUrl.pathname;
11
+ if (p(f, e.method))
12
+ return r(e, o);
13
+ const t = new Headers(e.headers), c = (a == null ? void 0 : a(e)) || t.get(n);
14
+ c && t.set(n, c), t.set(d, String(Date.now()));
15
+ const x = new w(e, {
16
+ headers: t
17
+ }), m = await r(x, o);
18
+ return m || i.next({
19
+ request: {
20
+ headers: t
21
+ }
22
+ });
23
+ };
24
+ }, T = (r = {}) => l(() => i.next(), r);
25
+ export {
26
+ T as createTelemetryMiddleware,
27
+ h as defaultTelemetryMiddlewareConfig,
28
+ l as withTelemetryMiddleware
29
+ };
@@ -0,0 +1,24 @@
1
+ import { NextResponse, NextRequest, NextFetchEvent } from 'next/server';
2
+ import { TelemetryMiddlewareOptions } from './types';
3
+ export declare const defaultTelemetryMiddlewareConfig: {
4
+ matcher: string[];
5
+ };
6
+ export type NextMiddlewareResult = NextResponse | Response | null | undefined | void;
7
+ export type NextMiddleware = (request: NextRequest, event: NextFetchEvent) => NextMiddlewareResult | Promise<NextMiddlewareResult>;
8
+ /**
9
+ * Higher-order function to wrap existing middleware with telemetry support.
10
+ * This is less invasive than creating a separate middleware factory.
11
+ *
12
+ * Usage:
13
+ * export const middleware = withTelemetryMiddleware(async (req) => {
14
+ * // your existing middleware logic
15
+ * return NextResponse.next();
16
+ * });
17
+ */
18
+ export declare const withTelemetryMiddleware: (middleware: NextMiddleware, options?: TelemetryMiddlewareOptions) => NextMiddleware;
19
+ /**
20
+ * Simplified Telemetry Middleware Creator (Factory Pattern)
21
+ * For users who don't have existing middleware or prefer this style.
22
+ */
23
+ export declare const createTelemetryMiddleware: (options?: TelemetryMiddlewareOptions) => NextMiddleware;
24
+ //# sourceMappingURL=middleware.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAElD,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,SAAS,CAAC;AAE1D,eAAO,MAAM,gCAAgC;;CAI5C,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG,YAAY,GAAG,QAAQ,GAAG,IAAI,GAAG,SAAS,GAAG,IAAI,CAAC;AAErF,MAAM,MAAM,cAAc,GAAG,CAC3B,OAAO,EAAE,WAAW,EACpB,KAAK,EAAE,cAAc,KAClB,oBAAoB,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAAC;AAE1D;;;;;;;;;GASG;AACH,eAAO,MAAM,uBAAuB,GAClC,YAAY,cAAc,EAC1B,UAAS,0BAA+B,KACvC,cAqCF,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,yBAAyB,GAAI,UAAS,0BAA+B,mBAGjF,CAAC"}
package/dist/next ADDED
@@ -0,0 +1,29 @@
1
+ import { NextRequest as w, NextResponse as i } from "next/server";
2
+ import { c as g } from "./ignore-B1KXtl12.js";
3
+ const h = {
4
+ matcher: [
5
+ "/((?!_next/static|_next/image|favicon.ico|robots.txt|sitemap.xml|.*\\.(?:css|js|mjs|cjs|png|jpg|jpeg|gif|svg|ico|webp|avif|woff|woff2|ttf|eot|map|txt|xml)).*)"
6
+ ]
7
+ }, l = (r, s = {}) => {
8
+ const p = g(s.ignore), d = (s.requestStartHeader ?? "x-telemetry-start").toLowerCase(), n = (s.userTypeHeader ?? "x-telemetry-user-type").toLowerCase(), a = s.resolveUserType;
9
+ return async (e, o) => {
10
+ const f = e.nextUrl.pathname;
11
+ if (p(f, e.method))
12
+ return r(e, o);
13
+ const t = new Headers(e.headers), c = (a == null ? void 0 : a(e)) || t.get(n);
14
+ c && t.set(n, c), t.set(d, String(Date.now()));
15
+ const x = new w(e, {
16
+ headers: t
17
+ }), m = await r(x, o);
18
+ return m || i.next({
19
+ request: {
20
+ headers: t
21
+ }
22
+ });
23
+ };
24
+ }, T = (r = {}) => l(() => i.next(), r);
25
+ export {
26
+ T as createTelemetryMiddleware,
27
+ h as defaultTelemetryMiddlewareConfig,
28
+ l as withTelemetryMiddleware
29
+ };
package/dist/next.d.ts ADDED
@@ -0,0 +1,24 @@
1
+ import { NextResponse, NextRequest, NextFetchEvent } from 'next/server';
2
+ import { TelemetryMiddlewareOptions } from './types';
3
+ export declare const defaultTelemetryMiddlewareConfig: {
4
+ matcher: string[];
5
+ };
6
+ export type NextMiddlewareResult = NextResponse | Response | null | undefined | void;
7
+ export type NextMiddleware = (request: NextRequest, event: NextFetchEvent) => NextMiddlewareResult | Promise<NextMiddlewareResult>;
8
+ /**
9
+ * Higher-order function to wrap existing middleware with telemetry support.
10
+ * This is less invasive than creating a separate middleware factory.
11
+ *
12
+ * Usage:
13
+ * export const middleware = withTelemetryMiddleware(async (req) => {
14
+ * // your existing middleware logic
15
+ * return NextResponse.next();
16
+ * });
17
+ */
18
+ export declare const withTelemetryMiddleware: (middleware: NextMiddleware, options?: TelemetryMiddlewareOptions) => NextMiddleware;
19
+ /**
20
+ * Simplified Telemetry Middleware Creator (Factory Pattern)
21
+ * For users who don't have existing middleware or prefer this style.
22
+ */
23
+ export declare const createTelemetryMiddleware: (options?: TelemetryMiddlewareOptions) => NextMiddleware;
24
+ //# sourceMappingURL=next.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"next.d.ts","sourceRoot":"","sources":["../src/next.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAElD,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,SAAS,CAAC;AAE1D,eAAO,MAAM,gCAAgC;;CAI5C,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG,YAAY,GAAG,QAAQ,GAAG,IAAI,GAAG,SAAS,GAAG,IAAI,CAAC;AAErF,MAAM,MAAM,cAAc,GAAG,CAC3B,OAAO,EAAE,WAAW,EACpB,KAAK,EAAE,cAAc,KAClB,oBAAoB,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAAC;AAE1D;;;;;;;;;GASG;AACH,eAAO,MAAM,uBAAuB,GAClC,YAAY,cAAc,EAC1B,UAAS,0BAA+B,KACvC,cAqCF,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,yBAAyB,GAAI,UAAS,0BAA+B,mBAGjF,CAAC"}
@@ -0,0 +1,55 @@
1
+ import { OTLPExporterNodeConfigBase } from '@opentelemetry/otlp-exporter-base';
2
+ export type PathPattern = string | RegExp;
3
+ export interface IgnoreConfig {
4
+ paths?: PathPattern[];
5
+ extensions?: string[];
6
+ methods?: string[];
7
+ }
8
+ export interface SamplingRuleCondition {
9
+ path?: PathPattern[];
10
+ method?: string[];
11
+ userType?: string[];
12
+ statusCode?: number[];
13
+ minStatusCode?: number;
14
+ maxStatusCode?: number;
15
+ inspection?: boolean;
16
+ }
17
+ export interface SamplingRule {
18
+ name?: string;
19
+ ratio: number;
20
+ when?: SamplingRuleCondition;
21
+ }
22
+ export interface LayeredSamplingConfig {
23
+ criticalPaths?: PathPattern[];
24
+ criticalUserTypes?: string[];
25
+ pathSampling?: Record<string, number>;
26
+ userTypeSampling?: Record<string, number>;
27
+ }
28
+ export interface TelemetryConfig extends OTLPExporterNodeConfigBase {
29
+ serviceName: string;
30
+ serviceVersion?: string;
31
+ environment?: string;
32
+ defaultSamplingRatio?: number;
33
+ samplingRules?: SamplingRule[];
34
+ layeredSampling?: LayeredSamplingConfig;
35
+ ignore?: IgnoreConfig;
36
+ inspectionHeader?: string;
37
+ userTypeHeader?: string;
38
+ requestStartHeader?: string;
39
+ exporterTimeoutMillis?: number;
40
+ maxQueueSize?: number;
41
+ maxExportBatchSize?: number;
42
+ scheduledDelayMillis?: number;
43
+ }
44
+ export interface TelemetryRuntime {
45
+ getConfig: () => TelemetryConfig;
46
+ updateConfig: (partial: Partial<TelemetryConfig>) => void;
47
+ }
48
+ export interface TelemetryMiddlewareOptions {
49
+ ignore?: IgnoreConfig;
50
+ inspectionHeader?: string;
51
+ requestStartHeader?: string;
52
+ userTypeHeader?: string;
53
+ resolveUserType?: (request: import('next/server').NextRequest) => string | undefined;
54
+ }
55
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,mCAAmC,CAAC;AAEpF,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,MAAM,CAAC;AAE1C,MAAM,WAAW,YAAY;IAC3B,KAAK,CAAC,EAAE,WAAW,EAAE,CAAC;IACtB,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IACtB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;CACpB;AAED,MAAM,WAAW,qBAAqB;IACpC,IAAI,CAAC,EAAE,WAAW,EAAE,CAAC;IACrB,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IACtB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAED,MAAM,WAAW,YAAY;IAC3B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,qBAAqB,CAAC;CAC9B;AAED,MAAM,WAAW,qBAAqB;IACpC,aAAa,CAAC,EAAE,WAAW,EAAE,CAAC;IAC9B,iBAAiB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC7B,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACtC,gBAAgB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC3C;AAED,MAAM,WAAW,eAAgB,SAAQ,0BAA0B;IACjE,WAAW,EAAE,MAAM,CAAC;IACpB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,aAAa,CAAC,EAAE,YAAY,EAAE,CAAC;IAC/B,eAAe,CAAC,EAAE,qBAAqB,CAAC;IACxC,MAAM,CAAC,EAAE,YAAY,CAAC;IACtB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,oBAAoB,CAAC,EAAE,MAAM,CAAC;CAC/B;AAED,MAAM,WAAW,gBAAgB;IAC/B,SAAS,EAAE,MAAM,eAAe,CAAC;IACjC,YAAY,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,eAAe,CAAC,KAAK,IAAI,CAAC;CAC3D;AAED,MAAM,WAAW,0BAA0B;IACzC,MAAM,CAAC,EAAE,YAAY,CAAC;IACtB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,eAAe,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,aAAa,EAAE,WAAW,KAAK,MAAM,GAAG,SAAS,CAAC;CACtF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0xchain/telemetry",
3
- "version": "1.0.0",
3
+ "version": "1.1.0-beta.10",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -11,6 +11,24 @@
11
11
  "types": "./dist/index.d.ts",
12
12
  "import": "./dist/index.js",
13
13
  "default": "./dist/index.js"
14
+ },
15
+ "./middleware": {
16
+ "types": "./dist/middleware.d.ts",
17
+ "import": "./dist/middleware.js",
18
+ "require": "./dist/middleware.js",
19
+ "default": "./dist/middleware.js"
20
+ },
21
+ "./instrumentation": {
22
+ "types": "./dist/instrumentation.d.ts",
23
+ "import": "./dist/instrumentation.js",
24
+ "require": "./dist/instrumentation.js",
25
+ "default": "./dist/instrumentation.js"
26
+ },
27
+ "./next": {
28
+ "types": "./dist/next.d.ts",
29
+ "import": "./dist/next.js",
30
+ "require": "./dist/next.js",
31
+ "default": "./dist/next.js"
14
32
  }
15
33
  },
16
34
  "files": [
@@ -19,14 +37,15 @@
19
37
  ],
20
38
  "dependencies": {
21
39
  "@opentelemetry/api": "1.9.0",
22
- "@opentelemetry/auto-instrumentations-node": "0.62.0",
23
- "@opentelemetry/exporter-trace-otlp-http": "0.203.0",
24
- "@opentelemetry/instrumentation-undici": "0.14.0",
25
- "@opentelemetry/otlp-exporter-base": "0.203.0",
26
- "@opentelemetry/resources": "2.0.1",
27
- "@opentelemetry/sdk-node": "0.203.0",
28
- "@opentelemetry/sdk-trace-base": "2.0.1",
29
- "@opentelemetry/semantic-conventions": "1.36.0"
40
+ "@opentelemetry/instrumentation-http": "0.211.0",
41
+ "@opentelemetry/exporter-trace-otlp-http": "0.211.0",
42
+ "@opentelemetry/instrumentation-undici": "0.21.0",
43
+ "@opentelemetry/otlp-exporter-base": "0.211.0",
44
+ "@opentelemetry/resources": "2.5.0",
45
+ "@opentelemetry/sdk-node": "0.211.0",
46
+ "@opentelemetry/sdk-trace-base": "2.5.0",
47
+ "@opentelemetry/semantic-conventions": "1.39.0",
48
+ "next": "16.1.6"
30
49
  },
31
50
  "publishConfig": {
32
51
  "access": "public"
package/dist/index.js DELETED
@@ -1,98 +0,0 @@
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
- };