@0xchain/telemetry 1.1.0-beta.8 → 1.1.0-beta.9

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
@@ -4,134 +4,134 @@
4
4
 
5
5
  ## 快速开始
6
6
 
7
- 安装依赖:
7
+ ### 1. 安装依赖
8
8
 
9
9
  ```bash
10
10
  pnpm add @0xchain/telemetry
11
11
  ```
12
12
 
13
- Next.js App Router 接入:
13
+ ### 2. 初始化追踪 (instrumentation.ts)
14
+
15
+ 在项目根目录(或 `src` 目录)创建 `instrumentation.ts`,只需一行代码即可启用:
16
+
17
+ > 注意:请从 `@0xchain/telemetry/instrumentation` 导入。此入口包含完整 Node SDK,仅在 `instrumentation.ts` 中加载,不影响 Edge 的 middleware bundle。
14
18
 
15
19
  ```ts
16
- import { registerTelemetry } from "@0xchain/telemetry/next";
20
+ import { registerTelemetry } from "@0xchain/telemetry/instrumentation";
17
21
 
18
22
  export async function register() {
19
- await registerTelemetry(() => ({
20
- serviceName: "web-app",
21
- environment: "production",
22
- url: process.env.OTEL_EXPORTER_OTLP_ENDPOINT,
23
- headers: {
24
- Authorization: process.env.OTEL_EXPORTER_OTLP_HEADERS || "",
25
- },
26
- samplingRules: [
27
- { name: "inspection", ratio: 1, when: { inspection: true } },
28
- { name: "errors", ratio: 0.8, when: { minStatusCode: 500 } },
29
- { name: "default", ratio: 0.1 },
30
- ],
31
- }));
23
+ // 自动从环境变量加载配置
24
+ await registerTelemetry();
32
25
  }
33
26
  ```
34
27
 
35
- `middleware.ts` 中启用请求上下文写入:
28
+ 或者自定义配置:
36
29
 
37
30
  ```ts
38
- import { createTelemetryMiddleware, defaultTelemetryMiddlewareConfig } from "@0xchain/telemetry/next";
31
+ import { registerTelemetry } from "@0xchain/telemetry/instrumentation";
39
32
 
40
- export const config = defaultTelemetryMiddlewareConfig;
41
- export default createTelemetryMiddleware({
42
- ignore: {
43
- paths: ["/health", "/metrics"],
44
- extensions: [".png", ".css"],
45
- },
46
- });
33
+ export async function register() {
34
+ await registerTelemetry({
35
+ serviceName: "my-web-app",
36
+ environment: "production",
37
+ // 更多配置...
38
+ });
39
+ }
47
40
  ```
48
41
 
49
- ## API 说明
42
+ ### 3. 注入中间件 (middleware.ts)
50
43
 
51
- ### 核心入口
44
+ `middleware.ts` 中使用 `withTelemetryMiddleware` 包裹现有中间件,实现低侵入式集成:
52
45
 
53
- - `registerTelemetry(configOrProvider)`:用于 `instrumentation.ts` 中初始化 OpenTelemetry
54
- - `createTelemetryMiddleware(options)`:用于 `middleware.ts` 注入请求头与用户类型标记。
55
- - `startTelemetry(config)`:在 Node 环境中直接启动 SDK。
56
- - `updateTelemetryConfig(partial)`:运行时更新采样、忽略规则等配置。
57
- - `shutdownTelemetry()`:停止追踪 SDK。
46
+ > 注意:请从 `@0xchain/telemetry/middleware` 或 `@0xchain/telemetry/next` 导入。此入口轻量,无 Node SDK 依赖,适用于 Edge Runtime
58
47
 
59
- ### 配置类型
48
+ ```ts
49
+ import { NextResponse } from "next/server";
50
+ import { withTelemetryMiddleware } from "@0xchain/telemetry/middleware";
51
+
52
+ // 你的现有中间件逻辑
53
+ const userMiddleware = async (req) => {
54
+ // ... 业务逻辑
55
+ return NextResponse.next();
56
+ };
57
+
58
+ // 使用 withTelemetryMiddleware 包裹
59
+ export const middleware = withTelemetryMiddleware(userMiddleware);
60
+
61
+ export const config = {
62
+ matcher: [
63
+ /* 你的 matcher 配置 */
64
+ '/((?!_next/static|_next/image|favicon.ico).*)',
65
+ ],
66
+ };
67
+ ```
60
68
 
61
- `TelemetryConfig` 关键字段:
69
+ 如果你没有其他中间件,可以使用工厂方法:
62
70
 
63
- - `serviceName`:服务名称
64
- - `environment`:环境标识
65
- - `defaultSamplingRatio`:默认采样比例
66
- - `samplingRules`:多维采样规则
67
- - `ignore`:忽略规则
68
- - `inspectionHeader`:巡检标记请求头
69
- - `userTypeHeader`:用户类型请求头
70
- - `requestStartHeader`:请求开始时间请求头
71
+ ```ts
72
+ import { createTelemetryMiddleware } from "@0xchain/telemetry/middleware";
71
73
 
72
- ### 采样规则
74
+ export const middleware = createTelemetryMiddleware();
73
75
 
74
- ```json
75
- [
76
- {
77
- "name": "inspection",
78
- "ratio": 1,
79
- "when": { "inspection": true }
80
- },
81
- {
82
- "name": "error",
83
- "ratio": 0.8,
84
- "when": { "minStatusCode": 500 }
85
- },
86
- {
87
- "name": "api",
88
- "ratio": 0.2,
89
- "when": { "path": ["/api/**"], "method": ["POST"] }
90
- }
91
- ]
76
+ export const config = {
77
+ matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
78
+ };
92
79
  ```
93
80
 
94
- ### 忽略规则
81
+ ## 环境变量配置
95
82
 
96
- ```json
97
- {
98
- "paths": ["/health", "/metrics", "/_next/**"],
99
- "extensions": [".png", ".css", ".js"],
100
- "methods": ["HEAD", "OPTIONS"]
101
- }
102
- ```
83
+ 支持通过环境变量实现零代码配置:
103
84
 
104
- ## 环境变量
85
+ | 变量名 | 描述 | 默认值 |
86
+ |---|---|---|
87
+ | `OTEL_SERVICE_NAME` | 服务名称 | `unknown-service` |
88
+ | `OTEL_SERVICE_VERSION` | 服务版本 | `1.0.0` |
89
+ | `OTEL_EXPORTER_OTLP_ENDPOINT` | OTLP 上报地址 | - |
90
+ | `OTEL_EXPORTER_OTLP_HEADERS` | OTLP Headers (k=v,k2=v2) | - |
91
+ | `TELEMETRY_ENVIRONMENT` | 环境名称 (dev/prod) | `development`,在 Vercel 上自动使用 `VERCEL_ENV` |
92
+ | `TELEMETRY_SAMPLING_RATIO` | 默认采样率 (0.0-1.0) | `0.01` |
93
+ | `TELEMETRY_IGNORE_PATHS` | 忽略路径 (逗号分隔) | 默认忽略静态资源 |
94
+ | `TELEMETRY_INSPECTION_HEADER` | 巡检请求头 | `x-inspection` |
95
+ | `TELEMETRY_CONFIG_FILE` | 配置文件路径,支持 JSON,异步读取不阻塞启动 | - |
105
96
 
106
- - `TELEMETRY_CONFIG_FILE`:JSON 配置文件路径
107
- - `TELEMETRY_SAMPLING_RATIO`:默认采样比例
108
- - `TELEMETRY_SAMPLING_RULES`:采样规则 JSON 字符串
109
- - `TELEMETRY_IGNORE_PATHS`:逗号分隔的忽略路径
110
- - `TELEMETRY_IGNORE_EXTENSIONS`:逗号分隔的忽略扩展名
111
- - `TELEMETRY_IGNORE_METHODS`:逗号分隔的忽略方法
112
- - `TELEMETRY_INSPECTION_HEADER`:巡检请求头
113
- - `TELEMETRY_USER_TYPE_HEADER`:用户类型请求头
114
- - `TELEMETRY_REQUEST_START_HEADER`:请求开始时间请求头
115
- - `OTEL_EXPORTER_OTLP_ENDPOINT`:OTLP endpoint
116
- - `OTEL_EXPORTER_OTLP_HEADERS`:OTLP headers
117
- - `OTEL_SERVICE_NAME`:服务名称
97
+ ## Vercel 集成
118
98
 
119
- ## 部署说明
99
+ 部署到 Vercel 时,将自动检测并注入以下 resource 属性:
120
100
 
121
- 1. 配置 OTLP endpoint 与 headers。
122
- 2. `instrumentation.ts` 中调用 `registerTelemetry`。
123
- 3. 在 `middleware.ts` 中加入 `createTelemetryMiddleware`。
124
- 4. 确认生产环境可访问 APM 的 OTLP 接收端。
101
+ - `vercel.region`、`vercel.runtime`、`vercel.sha`、`vercel.host`、`vercel.deployment_id`
102
+ - `environment` 优先使用 `VERCEL_ENV`(production / preview / development)
125
103
 
126
- ## 迁移指南
104
+ ## 核心特性
127
105
 
128
- - `startTelemetry` 初始化方式可直接替换为 `registerTelemetry`,配置项保持一致。
129
- - `ignorePaths` 替换为 `ignore` 配置对象以便使用扩展名与方法过滤。
130
- - 原巡检逻辑继续使用 `x-inspection` 请求头,默认保持兼容。
106
+ 1. **巡检识别**:自动识别 `x-inspection` 请求头,强制采样并全量记录。
107
+ 2. **智能过滤**:内置静态资源过滤规则,支持自定义路径、扩展名和方法过滤。
108
+ 3. **多维采样**:支持按路径、用户类型、状态码配置不同的采样率。
109
+ 4. **低侵入性**:通过 HOC 方式集成中间件,不破坏原有逻辑。
110
+ 5. **入口拆分**:`instrumentation` 含完整 Node SDK;`middleware` / `next` 轻量,仅注入请求头,适合 Edge。
111
+ 6. **按需加载**:仅启用 HTTP + Undici 检测,减少冷启动与内存占用。
131
112
 
132
- ## 构建与测试
113
+ ## 高级配置
133
114
 
134
- ```bash
135
- pnpm nx build @0xchain/telemetry
136
- pnpm nx test @0xchain/telemetry
115
+ ### 采样规则配置 (Sampling Rules)
116
+
117
+ 可以通过 `samplingRules` 字段或 `TELEMETRY_SAMPLING_RULES` 环境变量(JSON 字符串)配置:
118
+
119
+ ```json
120
+ [
121
+ {
122
+ "name": "critical-api",
123
+ "ratio": 1.0,
124
+ "when": { "path": ["/api/checkout", "/api/payment"] }
125
+ },
126
+ {
127
+ "name": "errors",
128
+ "ratio": 1.0,
129
+ "when": { "minStatusCode": 500 }
130
+ },
131
+ {
132
+ "name": "guest-users",
133
+ "ratio": 0.05,
134
+ "when": { "userType": ["guest"] }
135
+ }
136
+ ]
137
137
  ```
@@ -1,4 +1,6 @@
1
+ import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
2
+ import { UndiciInstrumentation } from '@opentelemetry/instrumentation-undici';
1
3
  import { TelemetryConfig } from './types';
2
- /** 生成 OpenTelemetry 自动检测配置 */
3
- export default function getInstrumentations(config: TelemetryConfig): any;
4
+ /** 按需加载 HTTP + Undici 检测,避免全量 auto-instrumentations 的启动开销 */
5
+ export default function getInstrumentations(config: TelemetryConfig): (HttpInstrumentation | UndiciInstrumentation)[];
4
6
  //# sourceMappingURL=Instrumentations.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Instrumentations.d.ts","sourceRoot":"","sources":["../src/Instrumentations.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AA4B/C,8BAA8B;AAC9B,MAAM,CAAC,OAAO,UAAU,mBAAmB,CAAC,MAAM,EAAE,eAAe,GAAG,GAAG,CA0ExE"}
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
@@ -16,7 +16,7 @@ export declare const matchSamplingRule: (rule: SamplingRule, context: SamplingCo
16
16
  export declare const resolveSamplingRatio: (rules: SamplingRule[] | undefined, context: SamplingContext, defaultRatio: number) => number;
17
17
  /** 创建基于规则的采样器 */
18
18
  export declare const createRuleBasedSampler: (runtime: TelemetryRuntime) => Sampler;
19
- /** 构建带采样过滤的导出器 */
20
- export declare const createFilteringTraceExporter: (exporter: SpanExporter, runtime: TelemetryRuntime) => SpanExporter;
19
+ /** 导出器直接透传,采样决策由 createRuleBasedSampler 在 span 创建时统一完成,避免 export 阶段重复计算 */
20
+ export declare const createFilteringTraceExporter: (exporter: SpanExporter) => SpanExporter;
21
21
  export default createRuleBasedSampler;
22
22
  //# sourceMappingURL=Sampler.d.ts.map
@@ -1 +1 @@
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,EAEL,UAAU,EAMX,MAAM,oBAAoB,CAAC;AAC5B,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;AAwBF,kBAAkB;AAClB,eAAO,MAAM,4BAA4B,GAAI,UAAU,YAAY,EAAE,SAAS,gBAAgB,KAAG,YAehG,CAAC;AAEF,eAAe,sBAAsB,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
+ };
package/dist/ignore.d.ts CHANGED
@@ -2,6 +2,6 @@ import { IgnoreConfig, PathPattern } from './types';
2
2
  export declare const defaultIgnoreConfig: Required<IgnoreConfig>;
3
3
  /** 判断路径是否匹配规则 */
4
4
  export declare const matchesPathPattern: (pattern: PathPattern, pathname: string) => boolean;
5
- /** 创建忽略规则匹配器 */
5
+ /** 创建忽略规则匹配器(相同配置复用,减少重复创建) */
6
6
  export declare const createIgnoreMatcher: (config?: IgnoreConfig) => (pathname: string, method?: string) => boolean;
7
7
  //# sourceMappingURL=ignore.d.ts.map
@@ -1 +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;AAqCzD,eAAO,MAAM,mBAAmB,EAAE,QAAQ,CAAC,YAAY,CAItD,CAAC;AAcF,iBAAiB;AACjB,eAAO,MAAM,kBAAkB,GAAI,SAAS,WAAW,EAAE,UAAU,MAAM,YAmBxE,CAAC;AAQF,gBAAgB;AAChB,eAAO,MAAM,mBAAmB,GAAI,SAAS,YAAY,MAS/C,UAAU,MAAM,EAAE,SAAS,MAAM,YAU1C,CAAC"}
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 CHANGED
@@ -1,73 +1,19 @@
1
- import { NodeSDK as q } from "@opentelemetry/sdk-node";
2
- import { OTLPTraceExporter as z } from "@opentelemetry/exporter-trace-otlp-http";
3
- import { resourceFromAttributes as j } from "@opentelemetry/resources";
4
- import { ATTR_SERVICE_VERSION as U, ATTR_SERVICE_NAME as k } from "@opentelemetry/semantic-conventions";
5
- import { SamplingDecision as f, TraceIdRatioBasedSampler as L, AlwaysOnSampler as B, BatchSpanProcessor as G } from "@opentelemetry/sdk-trace-base";
6
- import { trace as W, TraceFlags as A, context as x, SpanKind as $, propagation as D, SpanStatusCode as Q } from "@opentelemetry/api";
7
- import { readFileSync as K } from "node:fs";
8
- import { getNodeAutoInstrumentations as X } from "@opentelemetry/auto-instrumentations-node";
9
- import { UndiciInstrumentation as J } from "@opentelemetry/instrumentation-undici";
10
- const Z = "deployment.environment.name", tt = [
11
- "/health",
12
- "/metrics",
13
- "/readyz",
14
- "/livez",
15
- "/favicon.ico",
16
- "/robots.txt",
17
- "/sitemap.xml",
18
- "/_next",
19
- "/__nextjs_",
20
- "/.well-known"
21
- ], et = [
22
- ".css",
23
- ".js",
24
- ".mjs",
25
- ".cjs",
26
- ".png",
27
- ".jpg",
28
- ".jpeg",
29
- ".gif",
30
- ".svg",
31
- ".ico",
32
- ".webp",
33
- ".avif",
34
- ".woff",
35
- ".woff2",
36
- ".ttf",
37
- ".eot",
38
- ".map",
39
- ".txt",
40
- ".xml"
41
- ], E = {
42
- paths: tt,
43
- extensions: et,
44
- methods: ["HEAD", "OPTIONS"]
45
- }, rt = (t) => t.map((e) => {
46
- const r = e.trim().toLowerCase();
47
- return r && (r.startsWith(".") ? r : `.${r}`);
48
- }), st = (t) => t.replace(/[.+^${}()|[\]\\]/g, "\\$&"), b = (t, e) => {
49
- if (t instanceof RegExp)
50
- return t.test(e);
51
- const r = t.trim();
52
- if (!r) return !1;
53
- if (r.includes("*")) {
54
- const n = `^${st(r).replace(/\\\*\\\*/g, ".*").replace(/\\\*/g, "[^/]*")}$`;
55
- return new RegExp(n).test(e);
56
- }
57
- return e === r ? !0 : r.endsWith("/") ? e.startsWith(r) : e.startsWith(`${r}/`) || e.startsWith(r);
58
- }, nt = (t, e) => {
59
- const r = e.toLowerCase();
60
- return t.some((s) => s && r.endsWith(s));
61
- }, ot = (t) => {
62
- var o, n, i;
63
- const e = (o = t == null ? void 0 : t.paths) != null && o.length ? t.paths : E.paths, r = (n = t == null ? void 0 : t.extensions) != null && n.length ? rt(t.extensions) : E.extensions, s = (i = t == null ? void 0 : t.methods) != null && i.length ? t.methods.map((a) => a.toUpperCase()) : E.methods;
64
- return (a, u) => u && s.includes(u.toUpperCase()) ? !0 : a ? nt(r, a) ? !0 : e.some((c) => b(c, a)) : !1;
65
- }, h = (t, e) => {
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) => {
66
12
  if (!t) return;
67
13
  const r = t[e.toLowerCase()];
68
14
  if (Array.isArray(r)) {
69
- const s = r[0];
70
- return typeof s == "string" ? s : void 0;
15
+ const n = r[0];
16
+ return typeof n == "string" ? n : void 0;
71
17
  }
72
18
  return typeof r == "string" ? r : void 0;
73
19
  }, T = (t) => {
@@ -77,43 +23,37 @@ const Z = "deployment.environment.name", tt = [
77
23
  } catch {
78
24
  return t.split("?")[0] || t;
79
25
  }
80
- }, O = /* @__PURE__ */ new WeakMap();
81
- function it(t) {
82
- const e = ot(t.ignore), r = (t.inspectionHeader ?? "x-inspection").toLowerCase(), s = (t.userTypeHeader ?? "x-telemetry-user-type").toLowerCase(), o = (t.requestStartHeader ?? "x-telemetry-start").toLowerCase();
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();
83
29
  return [
84
- X({
85
- "@opentelemetry/instrumentation-grpc": {
86
- enabled: !1
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);
87
46
  },
88
- "@opentelemetry/instrumentation-http": {
89
- enabled: !0,
90
- startIncomingSpanHook: (n) => {
91
- const i = "headers" in n ? n.headers : void 0, a = h(i, r), u = h(i, s), c = h(i, o), d = "method" in n ? n.method : void 0, M = T("url" in n ? n.url : void 0), w = Number(c || Date.now()), m = {
92
- "telemetry.request.start_time_ms": Number.isFinite(w) ? w : Date.now()
93
- };
94
- return M && (m["url.path"] = M), d && (m["http.request.method"] = d), a && (m["telemetry.request.inspection"] = !0, m["http.request.header.x-inspection"] = a), u && (m["enduser.type"] = u, m["telemetry.request.user_type"] = u), m;
95
- },
96
- requestHook: (n, i) => {
97
- const a = "headers" in i ? i.headers : void 0, u = h(a, o), c = Number(u || Date.now());
98
- O.set(n, Number.isFinite(c) ? c : Date.now());
99
- },
100
- responseHook: (n, i) => {
101
- const a = "statusCode" in i ? i.statusCode : void 0;
102
- typeof a == "number" && (n.setAttribute("http.response.status_code", a), n.setAttribute("telemetry.response.status_code", a), a >= 500 && n.setAttribute("telemetry.request.error", !0));
103
- const u = O.get(n);
104
- typeof u == "number" && n.setAttribute("telemetry.request.duration_ms", Date.now() - u);
105
- },
106
- ignoreIncomingRequestHook: (n) => {
107
- const i = "url" in n ? n.url : void 0, a = T(i), u = "method" in n ? n.method : void 0;
108
- return e(a, u);
109
- },
110
- ignoreOutgoingRequestHook: (n) => {
111
- const i = typeof n == "string" ? n : n.path || "";
112
- return e(T(i));
113
- }
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));
114
54
  }
115
55
  }),
116
- new J()
56
+ new Q()
117
57
  ];
118
58
  }
119
59
  const R = (t) => {
@@ -122,7 +62,7 @@ const R = (t) => {
122
62
  const e = t[0];
123
63
  return typeof e == "string" ? e : void 0;
124
64
  }
125
- }, H = (t) => {
65
+ }, x = (t) => {
126
66
  if (typeof t == "number") return t;
127
67
  if (typeof t == "string") {
128
68
  const e = Number(t);
@@ -130,33 +70,33 @@ const R = (t) => {
130
70
  }
131
71
  if (Array.isArray(t)) {
132
72
  const e = t[0];
133
- return H(e);
73
+ return x(e);
134
74
  }
135
- }, l = (t, e) => {
75
+ }, E = (t, e) => {
136
76
  for (const r of e)
137
77
  if (r in t)
138
78
  return t[r];
139
- }, V = (t) => {
140
- const e = l(t, ["url.path", "http.target", "http.route"]), r = l(t, ["http.request.method", "http.method"]), s = l(t, ["enduser.type", "telemetry.request.user_type"]), o = l(t, [
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, [
141
81
  "http.response.status_code",
142
82
  "http.status_code",
143
83
  "telemetry.response.status_code"
144
- ]), n = l(t, [
84
+ ]), s = E(t, [
145
85
  "telemetry.request.inspection",
146
86
  "http.request.header.x-inspection",
147
87
  "x-inspection"
148
- ]), i = n === !0 || n === "true" || n === "1" || n === 1;
88
+ ]), i = s === !0 || s === "true" || s === "1" || s === 1;
149
89
  return {
150
90
  path: R(e),
151
91
  method: R(r),
152
- userType: R(s),
153
- statusCode: H(o),
92
+ userType: R(n),
93
+ statusCode: x(o),
154
94
  inspection: i
155
95
  };
156
- }, I = (t, e) => !t || !(e != null && e.length) ? !1 : e.some((r) => r.toLowerCase() === t.toLowerCase()), at = (t, e) => !t || !(e != null && e.length) ? !1 : e.some((r) => b(r, t)), ut = (t, e) => {
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) => {
157
97
  const r = t.when;
158
98
  if (!r) return !0;
159
- if (r.inspection !== void 0 && r.inspection !== e.inspection || r.path && !at(e.path, r.path) || r.method && !I(e.method, r.method) || r.userType && !I(e.userType, r.userType))
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))
160
100
  return !1;
161
101
  if (r.statusCode && typeof e.statusCode == "number") {
162
102
  if (!r.statusCode.includes(e.statusCode))
@@ -164,145 +104,113 @@ const R = (t) => {
164
104
  } else if (r.statusCode && r.statusCode.length)
165
105
  return !1;
166
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));
167
- }, S = (t) => Math.max(0, Math.min(1, t)), v = (t, e, r) => {
168
- if (!(t != null && t.length)) return S(r);
169
- for (const s of t)
170
- if (ut(s, e))
171
- return S(s.ratio);
172
- return S(r);
173
- }, P = /* @__PURE__ */ new Map(), F = (t) => {
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) => {
174
114
  if (t <= 0)
175
- return new L(0);
115
+ return new N(0);
176
116
  if (t >= 1)
177
- return new B();
178
- const e = Number(t.toFixed(6)), r = P.get(e);
117
+ return new U();
118
+ const e = Number(t.toFixed(6)), r = A.get(e);
179
119
  if (r) return r;
180
- const s = new L(e);
181
- return P.set(e, s), s;
182
- }, ct = (t) => ({
183
- shouldSample(e, r, s, o, n, i) {
184
- const a = W.getSpanContext(e);
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);
185
125
  if (a)
186
- return (a.traceFlags & A.SAMPLED) === A.SAMPLED ? { decision: f.RECORD_AND_SAMPLED } : { decision: f.NOT_RECORD };
187
- const u = t.getConfig(), c = V(n);
188
- if (c.inspection)
189
- return { decision: f.RECORD_AND_SAMPLED };
190
- const d = v(
191
- u.samplingRules,
192
- c,
193
- u.defaultSamplingRatio ?? 0.01
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
194
134
  );
195
- return F(d).shouldSample(e, r, s, o, n, i);
135
+ return nt(l).shouldSample(e, r, n, o, s, i);
196
136
  },
197
137
  toString: () => "RuleBasedSampler"
198
- }), mt = (t, e, r) => {
199
- const s = t.getConfig(), o = V(e);
200
- if (o.inspection) return !0;
201
- const n = v(
202
- s.samplingRules,
203
- o,
204
- s.defaultSamplingRatio ?? 0.01
205
- );
206
- return F(n).shouldSample(
207
- x.active(),
208
- r,
209
- "export",
210
- $.INTERNAL,
211
- e,
212
- []
213
- ).decision === f.RECORD_AND_SAMPLED;
214
- }, pt = (t, e) => ({
215
- export: (r, s) => {
216
- const o = r.filter(
217
- (n) => mt(e, n.attributes, n.spanContext().traceId)
218
- );
219
- if (!o.length) {
220
- s({ code: 0 });
221
- return;
222
- }
223
- t.export(o, s);
224
- },
225
- shutdown: () => t.shutdown(),
226
- forceFlush: () => {
227
- var r;
228
- return ((r = t.forceFlush) == null ? void 0 : r.call(t)) ?? Promise.resolve();
229
- }
230
- });
231
- let g, p;
232
- const _ = (t) => Array.from(new Set(t)), y = (t) => (t == null ? void 0 : t.split(",").map((e) => e.trim()).filter(Boolean)) ?? [], lt = (t) => {
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) => {
233
141
  if (!t) return;
234
142
  const e = {};
235
143
  for (const r of t.split(",")) {
236
- const [s, ...o] = r.split("="), n = s == null ? void 0 : s.trim();
237
- n && (e[n] = o.join("=").trim());
144
+ const [n, ...o] = r.split("="), s = n == null ? void 0 : n.trim();
145
+ s && (e[s] = o.join("=").trim());
238
146
  }
239
147
  return e;
240
- }, C = (t) => {
148
+ }, y = (t) => {
241
149
  if (t)
242
150
  try {
243
151
  return JSON.parse(t);
244
152
  } catch {
245
153
  return;
246
154
  }
247
- }, dt = (t) => {
155
+ }, at = async (t) => {
248
156
  if (t)
249
157
  try {
250
- const e = K(t, "utf8");
251
- return C(e);
158
+ const e = await k(t, "utf8");
159
+ return y(e);
252
160
  } catch {
253
161
  return;
254
162
  }
255
- }, Y = (t, e) => {
256
- const r = _([...(t == null ? void 0 : t.paths) ?? [], ...(e == null ? void 0 : e.paths) ?? []]), s = _([
163
+ }, H = (t, e) => {
164
+ const r = g([...(t == null ? void 0 : t.paths) ?? [], ...(e == null ? void 0 : e.paths) ?? []]), n = g([
257
165
  ...(t == null ? void 0 : t.extensions) ?? [],
258
166
  ...(e == null ? void 0 : e.extensions) ?? []
259
- ]), o = _([...(t == null ? void 0 : t.methods) ?? [], ...(e == null ? void 0 : e.methods) ?? []]);
167
+ ]), o = g([...(t == null ? void 0 : t.methods) ?? [], ...(e == null ? void 0 : e.methods) ?? []]);
260
168
  return {
261
169
  paths: r.length ? r : void 0,
262
- extensions: s.length ? s : void 0,
170
+ extensions: n.length ? n : void 0,
263
171
  methods: o.length ? o : void 0
264
172
  };
265
- }, ht = (t) => {
173
+ }, ct = (t) => {
266
174
  if (t != null && t.length)
267
175
  return t.map((e) => ({
268
176
  ...e,
269
177
  ratio: Number.isFinite(e.ratio) ? Math.max(0, Math.min(1, e.ratio)) : 0
270
178
  })).filter((e) => e.ratio >= 0);
271
- }, ft = (t = process.env) => {
272
- const e = dt(t.TELEMETRY_CONFIG_FILE), r = y(t.TELEMETRY_IGNORE_PATHS), s = y(t.TELEMETRY_IGNORE_EXTENSIONS), o = y(t.TELEMETRY_IGNORE_METHODS), n = C(t.TELEMETRY_SAMPLING_RULES), i = typeof (e == null ? void 0 : e.samplingRules) == "string" ? C(e.samplingRules) : e == null ? void 0 : e.samplingRules, a = {
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 = {
273
181
  serviceName: t.OTEL_SERVICE_NAME || t.TELEMETRY_SERVICE_NAME,
274
182
  serviceVersion: t.OTEL_SERVICE_VERSION || t.TELEMETRY_SERVICE_VERSION,
275
- environment: t.OTEL_RESOURCE_ATTRIBUTES || t.TELEMETRY_ENVIRONMENT,
183
+ environment: t.OTEL_RESOURCE_ATTRIBUTES || t.TELEMETRY_ENVIRONMENT || (t.VERCEL ? t.VERCEL_ENV : void 0),
276
184
  defaultSamplingRatio: t.TELEMETRY_SAMPLING_RATIO ? Number(t.TELEMETRY_SAMPLING_RATIO) : void 0,
277
185
  url: t.OTEL_EXPORTER_OTLP_ENDPOINT || t.TELEMETRY_OTLP_ENDPOINT,
278
- headers: lt(t.OTEL_EXPORTER_OTLP_HEADERS || t.TELEMETRY_OTLP_HEADERS),
186
+ headers: it(t.OTEL_EXPORTER_OTLP_HEADERS || t.TELEMETRY_OTLP_HEADERS),
279
187
  inspectionHeader: t.TELEMETRY_INSPECTION_HEADER,
280
188
  userTypeHeader: t.TELEMETRY_USER_TYPE_HEADER,
281
189
  requestStartHeader: t.TELEMETRY_REQUEST_START_HEADER,
282
- samplingRules: n || i,
283
- ignore: r.length || s.length || o.length ? {
190
+ samplingRules: s || i,
191
+ ignore: r.length || n.length || o.length ? {
284
192
  paths: r.length ? r : void 0,
285
- extensions: s.length ? s : void 0,
193
+ extensions: n.length ? n : void 0,
286
194
  methods: o.length ? o : void 0
287
195
  } : void 0
288
196
  };
289
197
  return e && typeof e == "object" ? { ...e, ...a } : a;
290
- }, Et = (t) => {
198
+ }, mt = (t) => {
291
199
  if (!t) return process.env.NODE_ENV || "development";
292
200
  if (t.includes("deployment.environment")) {
293
201
  const r = t.split(",").find((o) => o.includes("deployment.environment"));
294
202
  if (!r) return t;
295
- const [, s] = r.split("=");
296
- return (s == null ? void 0 : s.trim()) || t;
203
+ const [, n] = r.split("=");
204
+ return (n == null ? void 0 : n.trim()) || t;
297
205
  }
298
206
  return t;
299
- }, gt = (t, e) => {
300
- var s, o;
207
+ }, pt = (t, e) => {
208
+ var n, o;
301
209
  const r = [
302
210
  { name: "inspection", ratio: 1, when: { inspection: !0 } },
303
211
  { name: "error", ratio: 1, when: { minStatusCode: 500 } }
304
212
  ];
305
- if ((s = t == null ? void 0 : t.criticalPaths) != null && s.length && r.push({
213
+ if ((n = t == null ? void 0 : t.criticalPaths) != null && n.length && r.push({
306
214
  name: "critical-path",
307
215
  ratio: 1,
308
216
  when: { path: t.criticalPaths }
@@ -311,132 +219,137 @@ const _ = (t) => Array.from(new Set(t)), y = (t) => (t == null ? void 0 : t.spli
311
219
  ratio: 1,
312
220
  when: { userType: t.criticalUserTypes }
313
221
  }), t != null && t.pathSampling)
314
- for (const [n, i] of Object.entries(t.pathSampling))
222
+ for (const [s, i] of Object.entries(t.pathSampling))
315
223
  r.push({
316
- name: `path:${n}`,
224
+ name: `path:${s}`,
317
225
  ratio: i,
318
- when: { path: [n] }
226
+ when: { path: [s] }
319
227
  });
320
228
  if (t != null && t.userTypeSampling)
321
- for (const [n, i] of Object.entries(t.userTypeSampling))
229
+ for (const [s, i] of Object.entries(t.userTypeSampling))
322
230
  r.push({
323
- name: `user:${n}`,
231
+ name: `user:${s}`,
324
232
  ratio: i,
325
- when: { userType: [n] }
233
+ when: { userType: [s] }
326
234
  });
327
235
  return r.push({ name: "default", ratio: e }), r;
328
- }, N = (t = {}, e = process.env) => {
329
- const s = { ...ft(e), ...t }, o = Et(s.environment), n = Y(E, s.ignore), i = typeof s.defaultSamplingRatio == "number" ? Math.max(0, Math.min(1, s.defaultSamplingRatio)) : 0.01, a = ht(s.samplingRules) ?? gt(s.layeredSampling, i);
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);
330
238
  return {
331
- ...s,
332
- serviceName: s.serviceName || "unknown-service",
333
- serviceVersion: s.serviceVersion,
334
- environment: o,
335
- defaultSamplingRatio: i,
336
- ignore: n,
337
- samplingRules: a,
338
- inspectionHeader: s.inspectionHeader ?? "x-inspection",
339
- userTypeHeader: s.userTypeHeader ?? "x-telemetry-user-type",
340
- requestStartHeader: s.requestStartHeader ?? "x-telemetry-start",
341
- scheduledDelayMillis: s.scheduledDelayMillis ?? 1e3,
342
- maxQueueSize: s.maxQueueSize ?? 2048,
343
- maxExportBatchSize: s.maxExportBatchSize ?? 512,
344
- exporterTimeoutMillis: s.exporterTimeoutMillis ?? 3e4
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
345
253
  };
346
- }, Tt = (t) => {
347
- let e = N(t);
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);
348
259
  return {
349
260
  getConfig: () => e,
350
261
  updateConfig: (r) => {
351
- e = N({
262
+ e = D({
352
263
  ...e,
353
264
  ...r,
354
- ignore: Y(e.ignore, r.ignore)
265
+ ignore: H(e.ignore, r.ignore)
355
266
  });
356
267
  }
357
268
  };
358
- }, Rt = (t) => t[0] * 1e3 + t[1] / 1e6, St = (t, e) => {
269
+ }, lt = (t) => t[0] * 1e3 + t[1] / 1e6, ft = (t, e) => {
359
270
  for (const r of e) {
360
- const s = t.attributes[r];
361
- if (typeof s == "number") return s;
271
+ const n = t.attributes[r];
272
+ if (typeof n == "number") return n;
362
273
  }
363
- }, _t = (t) => {
364
- const e = t.events.find((s) => s.name === "exception");
274
+ }, ht = (t) => {
275
+ const e = t.events.find((n) => n.name === "exception");
365
276
  if (!(e != null && e.attributes)) return;
366
277
  const r = e.attributes["exception.message"];
367
278
  return typeof r == "string" ? r : void 0;
368
- }, yt = () => ({
279
+ }, dt = () => ({
369
280
  onStart: () => {
370
281
  },
371
282
  onEnd: (t) => {
372
- const e = Rt(t.duration);
283
+ const e = lt(t.duration);
373
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));
374
- const r = St(t, [
285
+ const r = ft(t, [
375
286
  "http.response.status_code",
376
287
  "http.status_code",
377
288
  "telemetry.response.status_code"
378
289
  ]);
379
- if (typeof r == "number" && (t.attributes["telemetry.response.status_code"] = r), t.status.code === Q.ERROR || typeof r == "number" && r >= 500) {
290
+ if (typeof r == "number" && (t.attributes["telemetry.response.status_code"] = r), t.status.code === G.ERROR || typeof r == "number" && r >= 500) {
380
291
  t.attributes["telemetry.request.error"] = !0;
381
- const o = t.status.message || _t(t);
292
+ const o = t.status.message || ht(t);
382
293
  o && (t.attributes["telemetry.request.error_message"] = o);
383
294
  }
384
295
  },
385
296
  shutdown: () => Promise.resolve(),
386
297
  forceFlush: () => Promise.resolve()
387
- }), Pt = async (t) => {
388
- const e = N(t);
389
- p = Tt(e);
390
- const r = j({
391
- [k]: e.serviceName,
392
- [U]: e.serviceVersion || "1.0.0",
393
- [Z]: e.environment || "development"
394
- }), s = new z(e), o = pt(s, p), n = [
395
- yt(),
396
- new G(o, {
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, {
397
310
  scheduledDelayMillis: e.scheduledDelayMillis,
398
311
  maxQueueSize: e.maxQueueSize,
399
312
  maxExportBatchSize: e.maxExportBatchSize,
400
313
  exportTimeoutMillis: e.exporterTimeoutMillis
401
314
  })
402
315
  ];
403
- g = new q({
316
+ h = new b({
404
317
  serviceName: e.serviceName,
405
- resource: r,
406
- spanProcessors: n,
407
- sampler: ct(p),
408
- instrumentations: it(e)
318
+ resource: o,
319
+ spanProcessors: a,
320
+ sampler: st(p),
321
+ instrumentations: $(e)
409
322
  });
410
323
  try {
411
- await g.start();
412
- } catch (i) {
413
- console.warn("Telemetry start failed:", i);
324
+ await h.start();
325
+ } catch (c) {
326
+ console.warn("Telemetry start failed:", c);
414
327
  }
415
328
  return p;
416
- }, Dt = (t) => {
329
+ }, Ot = (t) => {
417
330
  p == null || p.updateConfig(t);
418
- }, bt = (t) => (D.inject(x.active(), t), t), Ht = (t = fetch) => async (e, r) => {
419
- const s = (r == null ? void 0 : r.headers) ?? (e instanceof Request ? e.headers : void 0), o = new Headers(s);
420
- return D.inject(x.active(), o, {
421
- set: (n, i, a) => {
422
- n.set(i, a);
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);
423
336
  }
424
337
  }), t(e, { ...r, headers: o });
425
338
  }, Vt = async () => {
426
- g && await g.shutdown();
339
+ h && await h.shutdown();
427
340
  };
428
341
  export {
429
- V as buildSamplingContextFromAttributes,
430
- ot as createIgnoreMatcher,
431
- Ht as createPropagatingFetch,
432
- Tt as createTelemetryRuntime,
433
- E as defaultIgnoreConfig,
434
- bt as injectTraceHeaders,
435
- ft as loadTelemetryConfigFromEnv,
436
- ut as matchSamplingRule,
437
- v as resolveSamplingRatio,
438
- N as resolveTelemetryConfig,
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,
439
352
  Vt as shutdownTelemetry,
440
- Pt as startTelemetry,
441
- Dt as updateTelemetryConfig
353
+ wt as startTelemetry,
354
+ Ot as updateTelemetryConfig
442
355
  };
package/dist/index.d.ts CHANGED
@@ -3,15 +3,14 @@ import { createIgnoreMatcher, defaultIgnoreConfig } from './ignore';
3
3
  import { TelemetryConfig, TelemetryRuntime } from './types';
4
4
  export * from './types';
5
5
  export { buildSamplingContextFromAttributes, matchSamplingRule, resolveSamplingRatio, createIgnoreMatcher, defaultIgnoreConfig, };
6
- /** 从环境变量加载 Telemetry 配置 */
7
- /** 从环境变量加载 Telemetry 配置 */
8
- export declare const loadTelemetryConfigFromEnv: (env?: NodeJS.ProcessEnv) => Partial<TelemetryConfig>;
6
+ /** 从环境变量加载 Telemetry 配置(异步读取配置文件,不阻塞主线程) */
7
+ export declare const loadTelemetryConfigFromEnv: (env?: NodeJS.ProcessEnv) => Promise<Partial<TelemetryConfig>>;
9
8
  /** 合并环境变量与显式配置得到最终配置 */
10
- export declare const resolveTelemetryConfig: (options?: Partial<TelemetryConfig>, env?: NodeJS.ProcessEnv) => TelemetryConfig;
9
+ export declare const resolveTelemetryConfig: (options?: Partial<TelemetryConfig>, env?: NodeJS.ProcessEnv) => Promise<TelemetryConfig>;
11
10
  /** 创建可运行时更新的配置容器 */
12
- export declare const createTelemetryRuntime: (initialConfig: TelemetryConfig) => TelemetryRuntime;
11
+ export declare const createTelemetryRuntime: (initialConfig: Partial<TelemetryConfig>) => Promise<TelemetryRuntime>;
13
12
  /** 启动 OpenTelemetry SDK */
14
- export declare const startTelemetry: (options: TelemetryConfig) => Promise<TelemetryRuntime>;
13
+ export declare const startTelemetry: (options: Partial<TelemetryConfig> | TelemetryConfig) => Promise<TelemetryRuntime>;
15
14
  /** 运行时更新 Telemetry 配置 */
16
15
  export declare const updateTelemetryConfig: (partial: Partial<TelemetryConfig>) => void;
17
16
  /** 注入 traceparent 等传播头 */
@@ -1 +1 @@
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,2BAA2B;AAC3B,2BAA2B;AAC3B,eAAO,MAAM,0BAA0B,GACrC,MAAK,MAAM,CAAC,UAAwB,KACnC,OAAO,CAAC,eAAe,CAuCzB,CAAC;AAoEF,wBAAwB;AACxB,eAAO,MAAM,sBAAsB,GACjC,UAAS,OAAO,CAAC,eAAe,CAAM,EACtC,MAAK,MAAM,CAAC,UAAwB,KACnC,eA4BF,CAAC;AAEF,oBAAoB;AACpB,eAAO,MAAM,sBAAsB,GAAI,eAAe,eAAe,KAAG,gBAYvE,CAAC;AAwDF,2BAA2B;AAC3B,eAAO,MAAM,cAAc,GAAU,SAAS,eAAe,8BAqC5D,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"}
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 CHANGED
@@ -1,30 +1,29 @@
1
- import { NextResponse as o } from "next/server";
2
- import { createIgnoreMatcher as p, resolveTelemetryConfig as d, startTelemetry as g } from "./index";
3
- const u = {
1
+ import { NextRequest as w, NextResponse as i } from "next/server";
2
+ import { c as g } from "./ignore-B1KXtl12.js";
3
+ const h = {
4
4
  matcher: [
5
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
6
  ]
7
- }, w = (e = {}) => {
8
- const n = p(e.ignore), r = (e.inspectionHeader ?? "x-inspection").toLowerCase(), f = (e.requestStartHeader ?? "x-telemetry-start").toLowerCase(), c = (e.userTypeHeader ?? "x-telemetry-user-type").toLowerCase(), a = e.resolveUserType;
9
- return (s) => {
10
- try {
11
- const l = s.nextUrl.pathname;
12
- if (n(l, s.method))
13
- return o.next();
14
- const t = new Headers(s.headers), i = t.get(r);
15
- i && t.set(r, i);
16
- const m = (a == null ? void 0 : a(s)) || t.get(c);
17
- return m && t.set(c, m), t.set(f, String(Date.now())), o.next({ request: { headers: t } });
18
- } catch {
19
- return o.next();
20
- }
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
+ });
21
23
  };
22
- }, h = async (e) => {
23
- const n = typeof e == "function" ? await e() : e, r = d(n);
24
- await g(r);
25
- };
24
+ }, T = (r = {}) => l(() => i.next(), r);
26
25
  export {
27
- w as createTelemetryMiddleware,
28
- u as defaultTelemetryMiddlewareConfig,
29
- h as registerTelemetry
26
+ T as createTelemetryMiddleware,
27
+ h as defaultTelemetryMiddlewareConfig,
28
+ l as withTelemetryMiddleware
30
29
  };
package/dist/next.d.ts CHANGED
@@ -1,10 +1,24 @@
1
- import { NextResponse, NextRequest } from 'next/server';
2
- import { TelemetryConfig, TelemetryMiddlewareOptions } from './types';
1
+ import { NextResponse, NextRequest, NextFetchEvent } from 'next/server';
2
+ import { TelemetryMiddlewareOptions } from './types';
3
3
  export declare const defaultTelemetryMiddlewareConfig: {
4
4
  matcher: string[];
5
5
  };
6
- /** 创建 Next.js Middleware 链路追踪注入器 */
7
- export declare const createTelemetryMiddleware: (options?: TelemetryMiddlewareOptions) => (request: NextRequest) => NextResponse<unknown>;
8
- /** 在 Next.js instrumentation.ts 中注册追踪 */
9
- export declare const registerTelemetry: (configOrProvider: TelemetryConfig | (() => TelemetryConfig | Promise<TelemetryConfig>)) => Promise<void>;
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;
10
24
  //# sourceMappingURL=next.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"next.d.ts","sourceRoot":"","sources":["../src/next.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,KAAK,WAAW,EAAE,MAAM,aAAa,CAAC;AAE7D,OAAO,KAAK,EAAE,eAAe,EAAE,0BAA0B,EAAE,MAAM,SAAS,CAAC;AAG3E,eAAO,MAAM,gCAAgC;;CAI5C,CAAC;AAEF,oCAAoC;AACpC,eAAO,MAAM,yBAAyB,GAAI,UAAS,0BAA+B,MAOxE,SAAS,WAAW,0BAqB7B,CAAC;AAEF,yCAAyC;AACzC,eAAO,MAAM,iBAAiB,GAC5B,kBAAkB,eAAe,GAAG,CAAC,MAAM,eAAe,GAAG,OAAO,CAAC,eAAe,CAAC,CAAC,kBAMvF,CAAC"}
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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0xchain/telemetry",
3
- "version": "1.1.0-beta.8",
3
+ "version": "1.1.0-beta.9",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -12,6 +12,16 @@
12
12
  "import": "./dist/index.js",
13
13
  "default": "./dist/index.js"
14
14
  },
15
+ "./middleware": {
16
+ "types": "./dist/middleware.d.ts",
17
+ "import": "./dist/middleware.js",
18
+ "default": "./dist/middleware.js"
19
+ },
20
+ "./instrumentation": {
21
+ "types": "./dist/instrumentation.d.ts",
22
+ "import": "./dist/instrumentation.js",
23
+ "default": "./dist/instrumentation.js"
24
+ },
15
25
  "./next": {
16
26
  "types": "./dist/next.d.ts",
17
27
  "import": "./dist/next.js",
@@ -24,7 +34,7 @@
24
34
  ],
25
35
  "dependencies": {
26
36
  "@opentelemetry/api": "1.9.0",
27
- "@opentelemetry/auto-instrumentations-node": "0.69.0",
37
+ "@opentelemetry/instrumentation-http": "0.211.0",
28
38
  "@opentelemetry/exporter-trace-otlp-http": "0.211.0",
29
39
  "@opentelemetry/instrumentation-undici": "0.21.0",
30
40
  "@opentelemetry/otlp-exporter-base": "0.211.0",