@0xchain/telemetry 1.1.0-beta.8 → 1.1.0-beta.80
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/LICENSE +21 -0
- package/README.md +105 -95
- package/dist/Instrumentations.d.ts +4 -2
- package/dist/Instrumentations.d.ts.map +1 -1
- package/dist/Sampler.d.ts +2 -2
- package/dist/Sampler.d.ts.map +1 -1
- package/dist/ignore-BvHZTNsF.js +151 -0
- package/dist/ignore.d.ts +3 -1
- package/dist/ignore.d.ts.map +1 -1
- package/dist/index.d.ts +7 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +609 -0
- package/dist/instrumentation.d.ts +4 -0
- package/dist/instrumentation.d.ts.map +1 -0
- package/dist/instrumentation.js +8 -0
- package/dist/middleware.d.ts +24 -0
- package/dist/middleware.d.ts.map +1 -0
- package/dist/middleware.js +43 -0
- package/dist/next.d.ts +20 -6
- package/dist/next.d.ts.map +1 -1
- package/dist/next.js +43 -0
- package/dist/types.d.ts +2 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +40 -10
- package/dist/index +0 -442
- package/dist/next +0 -30
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-present 0xchain
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -4,134 +4,144 @@
|
|
|
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
|
-
|
|
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。
|
|
14
20
|
|
|
15
21
|
```ts
|
|
16
|
-
import { registerTelemetry } from "@0xchain/telemetry/
|
|
22
|
+
import { registerTelemetry } from "@0xchain/telemetry/instrumentation";
|
|
17
23
|
|
|
18
24
|
export async function register() {
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
}));
|
|
25
|
+
// 自动从环境变量加载配置
|
|
26
|
+
await registerTelemetry();
|
|
32
27
|
}
|
|
33
28
|
```
|
|
34
29
|
|
|
35
|
-
|
|
30
|
+
或者自定义配置:
|
|
36
31
|
|
|
37
32
|
```ts
|
|
38
|
-
import {
|
|
33
|
+
import { registerTelemetry } from "@0xchain/telemetry/instrumentation";
|
|
39
34
|
|
|
40
|
-
export
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
}
|
|
35
|
+
export async function register() {
|
|
36
|
+
await registerTelemetry({
|
|
37
|
+
serviceName: "my-web-app",
|
|
38
|
+
environment: "production",
|
|
39
|
+
// 更多配置...
|
|
40
|
+
});
|
|
41
|
+
}
|
|
47
42
|
```
|
|
48
43
|
|
|
49
|
-
|
|
44
|
+
### 3. 注入中间件 (middleware.ts)
|
|
50
45
|
|
|
51
|
-
|
|
46
|
+
在 `middleware.ts` 中使用 `withTelemetryMiddleware` 包裹现有中间件,实现低侵入式集成:
|
|
52
47
|
|
|
53
|
-
|
|
54
|
-
- `createTelemetryMiddleware(options)`:用于 `middleware.ts` 注入请求头与用户类型标记。
|
|
55
|
-
- `startTelemetry(config)`:在 Node 环境中直接启动 SDK。
|
|
56
|
-
- `updateTelemetryConfig(partial)`:运行时更新采样、忽略规则等配置。
|
|
57
|
-
- `shutdownTelemetry()`:停止追踪 SDK。
|
|
48
|
+
> 注意:请从 `@0xchain/telemetry/middleware` 或 `@0xchain/telemetry/next` 导入。此入口轻量,无 Node SDK 依赖,适用于 Edge Runtime。
|
|
58
49
|
|
|
59
|
-
|
|
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
|
+
```
|
|
60
70
|
|
|
61
|
-
|
|
71
|
+
如果你没有其他中间件,可以使用工厂方法:
|
|
62
72
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
- `defaultSamplingRatio`:默认采样比例
|
|
66
|
-
- `samplingRules`:多维采样规则
|
|
67
|
-
- `ignore`:忽略规则
|
|
68
|
-
- `inspectionHeader`:巡检标记请求头
|
|
69
|
-
- `userTypeHeader`:用户类型请求头
|
|
70
|
-
- `requestStartHeader`:请求开始时间请求头
|
|
73
|
+
```ts
|
|
74
|
+
import { createTelemetryMiddleware } from "@0xchain/telemetry/middleware";
|
|
71
75
|
|
|
72
|
-
|
|
76
|
+
export const middleware = createTelemetryMiddleware();
|
|
73
77
|
|
|
74
|
-
|
|
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
|
-
]
|
|
78
|
+
export const config = {
|
|
79
|
+
matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
|
|
80
|
+
};
|
|
92
81
|
```
|
|
93
82
|
|
|
94
|
-
|
|
83
|
+
## 环境变量配置
|
|
95
84
|
|
|
96
|
-
|
|
97
|
-
{
|
|
98
|
-
"paths": ["/health", "/metrics", "/_next/**"],
|
|
99
|
-
"extensions": [".png", ".css", ".js"],
|
|
100
|
-
"methods": ["HEAD", "OPTIONS"]
|
|
101
|
-
}
|
|
102
|
-
```
|
|
85
|
+
支持通过环境变量实现零代码配置:
|
|
103
86
|
|
|
104
|
-
|
|
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,异步读取不阻塞启动 | - |
|
|
105
98
|
|
|
106
|
-
|
|
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`:服务名称
|
|
99
|
+
## Vercel 集成
|
|
118
100
|
|
|
119
|
-
|
|
101
|
+
部署到 Vercel 时,将自动检测并注入以下 resource 属性:
|
|
120
102
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
3. 在 `middleware.ts` 中加入 `createTelemetryMiddleware`。
|
|
124
|
-
4. 确认生产环境可访问 APM 的 OTLP 接收端。
|
|
103
|
+
- `vercel.region`、`vercel.runtime`、`vercel.sha`、`vercel.host`、`vercel.deployment_id`
|
|
104
|
+
- `environment` 优先使用 `VERCEL_ENV`(production / preview / development)
|
|
125
105
|
|
|
126
|
-
##
|
|
106
|
+
## 核心特性
|
|
127
107
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
108
|
+
1. **巡检识别**:自动识别 `x-inspection` 请求头,强制采样并全量记录。
|
|
109
|
+
2. **智能过滤**:内置静态资源过滤规则,支持自定义路径、扩展名和方法过滤。
|
|
110
|
+
3. **多维采样**:支持按路径、用户类型、状态码配置不同的采样率。
|
|
111
|
+
4. **低侵入性**:通过 HOC 方式集成中间件,不破坏原有逻辑。
|
|
112
|
+
5. **入口拆分**:`instrumentation` 含完整 Node SDK;`middleware` / `next` 轻量,仅注入请求头,适合 Edge。
|
|
113
|
+
6. **按需加载**:仅启用 HTTP + Undici 检测,减少冷启动与内存占用。
|
|
131
114
|
|
|
132
|
-
##
|
|
115
|
+
## 常见问题
|
|
133
116
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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
|
+
]
|
|
137
147
|
```
|
|
@@ -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
|
-
/**
|
|
3
|
-
export default function getInstrumentations(config: TelemetryConfig):
|
|
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":"
|
|
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,mDA2ElE"}
|
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
|
|
19
|
+
/** 导出器直接透传,采样决策由 createRuleBasedSampler 在 span 创建时统一完成,避免 export 阶段重复计算 */
|
|
20
|
+
export declare const createFilteringTraceExporter: (exporter: SpanExporter) => SpanExporter;
|
|
21
21
|
export default createRuleBasedSampler;
|
|
22
22
|
//# sourceMappingURL=Sampler.d.ts.map
|
package/dist/Sampler.d.ts.map
CHANGED
|
@@ -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,
|
|
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,EAAgB,YAAY,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAG5E,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;AAgCF,iBAAiB;AACjB,eAAO,MAAM,sBAAsB,GAAI,SAAS,gBAAgB,KAAG,OA6ClE,CAAC;AAEF,2EAA2E;AAC3E,eAAO,MAAM,4BAA4B,GAAI,UAAU,YAAY,KAAG,YAAwB,CAAC;AAE/F,eAAe,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
const defaultIgnorePaths = [
|
|
2
|
+
// 健康检查 & 监控
|
|
3
|
+
"/health",
|
|
4
|
+
"/api/health",
|
|
5
|
+
"/metrics",
|
|
6
|
+
"/api/metrics",
|
|
7
|
+
"/readyz",
|
|
8
|
+
"/livez",
|
|
9
|
+
"/monitoring",
|
|
10
|
+
// Next.js 内部路由
|
|
11
|
+
"/_next",
|
|
12
|
+
"/__nextjs_",
|
|
13
|
+
// Next.js App Router 元数据路由
|
|
14
|
+
"/opengraph-image",
|
|
15
|
+
"/twitter-image",
|
|
16
|
+
"/icon",
|
|
17
|
+
"/apple-icon",
|
|
18
|
+
// Vercel 内部路由
|
|
19
|
+
"/_vercel",
|
|
20
|
+
// 静态资源 & SEO
|
|
21
|
+
"/favicon.ico",
|
|
22
|
+
"/robots.txt",
|
|
23
|
+
"/sitemap.xml",
|
|
24
|
+
"/manifest.json",
|
|
25
|
+
"/manifest.webmanifest",
|
|
26
|
+
"/.well-known",
|
|
27
|
+
// 认证回调(NextAuth.js)
|
|
28
|
+
"/api/auth"
|
|
29
|
+
];
|
|
30
|
+
const defaultIgnoreSpanNames = [
|
|
31
|
+
// Next.js 内部服务端操作(非 HTTP 请求,由 Next.js OTel 自动产生)
|
|
32
|
+
/^NextNodeServer\./,
|
|
33
|
+
/^NextServer\./,
|
|
34
|
+
/^BaseServer\./,
|
|
35
|
+
/^AppRender\./,
|
|
36
|
+
/^AppRouteRouteHandlers\./,
|
|
37
|
+
// Next.js 中间件执行 span(如 "middleware GET /path")
|
|
38
|
+
/^middleware /i,
|
|
39
|
+
// Next.js 元数据解析
|
|
40
|
+
/^resolveMetadata$/,
|
|
41
|
+
/^generateMetadata$/
|
|
42
|
+
];
|
|
43
|
+
const defaultIgnoreExtensions = [
|
|
44
|
+
".css",
|
|
45
|
+
".js",
|
|
46
|
+
".mjs",
|
|
47
|
+
".cjs",
|
|
48
|
+
".png",
|
|
49
|
+
".jpg",
|
|
50
|
+
".jpeg",
|
|
51
|
+
".gif",
|
|
52
|
+
".svg",
|
|
53
|
+
".ico",
|
|
54
|
+
".webp",
|
|
55
|
+
".avif",
|
|
56
|
+
".woff",
|
|
57
|
+
".woff2",
|
|
58
|
+
".ttf",
|
|
59
|
+
".eot",
|
|
60
|
+
".map",
|
|
61
|
+
".txt",
|
|
62
|
+
".xml"
|
|
63
|
+
];
|
|
64
|
+
const defaultIgnoreConfig = {
|
|
65
|
+
paths: defaultIgnorePaths,
|
|
66
|
+
extensions: defaultIgnoreExtensions,
|
|
67
|
+
methods: ["HEAD", "OPTIONS"],
|
|
68
|
+
spanNames: defaultIgnoreSpanNames
|
|
69
|
+
};
|
|
70
|
+
const normalizeExtensions = (extensions) => {
|
|
71
|
+
return extensions.map((ext) => {
|
|
72
|
+
const normalized = ext.trim().toLowerCase();
|
|
73
|
+
if (!normalized) return normalized;
|
|
74
|
+
return normalized.startsWith(".") ? normalized : `.${normalized}`;
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
const escapeRegex = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
78
|
+
const matchesPathPattern = (pattern, pathname) => {
|
|
79
|
+
if (pattern instanceof RegExp) {
|
|
80
|
+
return pattern.test(pathname);
|
|
81
|
+
}
|
|
82
|
+
const normalized = pattern.trim();
|
|
83
|
+
if (!normalized) return false;
|
|
84
|
+
const hasGlob = normalized.includes("*");
|
|
85
|
+
if (hasGlob) {
|
|
86
|
+
const escaped = escapeRegex(normalized);
|
|
87
|
+
const regexSource = `^${escaped.replace(/\\\*\\\*/g, ".*").replace(/\\\*/g, "[^/]*")}$`;
|
|
88
|
+
return new RegExp(regexSource).test(pathname);
|
|
89
|
+
}
|
|
90
|
+
if (pathname === normalized) return true;
|
|
91
|
+
if (normalized.endsWith("/")) {
|
|
92
|
+
return pathname.startsWith(normalized);
|
|
93
|
+
}
|
|
94
|
+
return pathname.startsWith(`${normalized}/`) || pathname.startsWith(normalized);
|
|
95
|
+
};
|
|
96
|
+
const hasIgnoredExtension = (extensions, pathname) => {
|
|
97
|
+
const lower = pathname.toLowerCase();
|
|
98
|
+
return extensions.some((ext) => ext && lower.endsWith(ext));
|
|
99
|
+
};
|
|
100
|
+
const getCacheKey = (config) => {
|
|
101
|
+
if (!config) return "default";
|
|
102
|
+
const hasRegExp = config.paths?.some((p) => p instanceof RegExp);
|
|
103
|
+
if (hasRegExp) return null;
|
|
104
|
+
const paths = (config.paths ?? []).map(String).sort().join(",");
|
|
105
|
+
const extensions = (config.extensions ?? []).map((e) => e.toLowerCase()).sort().join(",");
|
|
106
|
+
const methods = (config.methods ?? []).map((m) => m.toUpperCase()).sort().join(",");
|
|
107
|
+
return `p:${paths}|e:${extensions}|m:${methods}`;
|
|
108
|
+
};
|
|
109
|
+
const matcherCache = /* @__PURE__ */ new Map();
|
|
110
|
+
const CACHE_MAX_SIZE = 16;
|
|
111
|
+
const createSpanNameMatcher = (config) => {
|
|
112
|
+
const patterns = config?.spanNames?.length ? config.spanNames : defaultIgnoreConfig.spanNames;
|
|
113
|
+
return (spanName) => {
|
|
114
|
+
if (!spanName || !patterns.length) return false;
|
|
115
|
+
return patterns.some((pattern) => matchesPathPattern(pattern, spanName));
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
const createIgnoreMatcher = (config) => {
|
|
119
|
+
const cacheKey = getCacheKey(config);
|
|
120
|
+
if (cacheKey) {
|
|
121
|
+
const cached = matcherCache.get(cacheKey);
|
|
122
|
+
if (cached) return cached;
|
|
123
|
+
}
|
|
124
|
+
const paths = config?.paths?.length ? config.paths : defaultIgnoreConfig.paths;
|
|
125
|
+
const extensions = config?.extensions?.length ? normalizeExtensions(config.extensions) : defaultIgnoreConfig.extensions;
|
|
126
|
+
const methods = config?.methods?.length ? config.methods.map((method) => method.toUpperCase()) : defaultIgnoreConfig.methods;
|
|
127
|
+
const matcher = (pathname, method) => {
|
|
128
|
+
if (method && methods.includes(method.toUpperCase())) {
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
if (!pathname) return false;
|
|
132
|
+
if (hasIgnoredExtension(extensions, pathname)) {
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
return paths.some((pattern) => matchesPathPattern(pattern, pathname));
|
|
136
|
+
};
|
|
137
|
+
if (cacheKey) {
|
|
138
|
+
if (matcherCache.size >= CACHE_MAX_SIZE) {
|
|
139
|
+
const firstKey = matcherCache.keys().next().value;
|
|
140
|
+
if (firstKey) matcherCache.delete(firstKey);
|
|
141
|
+
}
|
|
142
|
+
matcherCache.set(cacheKey, matcher);
|
|
143
|
+
}
|
|
144
|
+
return matcher;
|
|
145
|
+
};
|
|
146
|
+
export {
|
|
147
|
+
createSpanNameMatcher as a,
|
|
148
|
+
createIgnoreMatcher as c,
|
|
149
|
+
defaultIgnoreConfig as d,
|
|
150
|
+
matchesPathPattern as m
|
|
151
|
+
};
|
package/dist/ignore.d.ts
CHANGED
|
@@ -2,6 +2,8 @@ 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
|
+
/** 创建 span 名称忽略匹配器 */
|
|
6
|
+
export declare const createSpanNameMatcher: (config?: IgnoreConfig) => (spanName: string) => boolean;
|
|
7
|
+
/** 创建忽略规则匹配器(相同配置复用,减少重复创建) */
|
|
6
8
|
export declare const createIgnoreMatcher: (config?: IgnoreConfig) => (pathname: string, method?: string) => boolean;
|
|
7
9
|
//# sourceMappingURL=ignore.d.ts.map
|
package/dist/ignore.d.ts.map
CHANGED
|
@@ -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;
|
|
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;AAyEzD,eAAO,MAAM,mBAAmB,EAAE,QAAQ,CAAC,YAAY,CAKtD,CAAC;AAcF,iBAAiB;AACjB,eAAO,MAAM,kBAAkB,GAAI,SAAS,WAAW,EAAE,UAAU,MAAM,YAmBxE,CAAC;AAsBF,sBAAsB;AACtB,eAAO,MAAM,qBAAqB,GAAI,SAAS,YAAY,MAKjD,UAAU,MAAM,YAIzB,CAAC;AAEF,+BAA+B;AAC/B,eAAO,MAAM,mBAAmB,GAAI,SAAS,YAAY,gBAhBT,MAAM,WAAW,MAAM,KAAK,OAkD3E,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
import { buildSamplingContextFromAttributes, matchSamplingRule, resolveSamplingRatio } from './Sampler';
|
|
2
|
-
import { createIgnoreMatcher, defaultIgnoreConfig } from './ignore';
|
|
2
|
+
import { createIgnoreMatcher, createSpanNameMatcher, defaultIgnoreConfig } from './ignore';
|
|
3
3
|
import { TelemetryConfig, TelemetryRuntime } from './types';
|
|
4
4
|
export * from './types';
|
|
5
|
-
export { buildSamplingContextFromAttributes, matchSamplingRule, resolveSamplingRatio, createIgnoreMatcher, defaultIgnoreConfig, };
|
|
6
|
-
/** 从环境变量加载 Telemetry
|
|
7
|
-
|
|
8
|
-
export declare const loadTelemetryConfigFromEnv: (env?: NodeJS.ProcessEnv) => Partial<TelemetryConfig>;
|
|
5
|
+
export { buildSamplingContextFromAttributes, matchSamplingRule, resolveSamplingRatio, createIgnoreMatcher, createSpanNameMatcher, defaultIgnoreConfig, };
|
|
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 等传播头 */
|
package/dist/index.d.ts.map
CHANGED
|
@@ -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;
|
|
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,qBAAqB,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC;AAC3F,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,qBAAqB,EACrB,mBAAmB,GACpB,CAAC;AA8EF,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;AAuHF,2BAA2B;AAC3B,eAAO,MAAM,cAAc,GAAU,SAAS,OAAO,CAAC,eAAe,CAAC,GAAG,eAAe,8BAgDvF,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"}
|