@0xchain/telemetry 1.1.0-beta.9 → 1.1.0-beta.91
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 +10 -0
- package/dist/Instrumentations.d.ts.map +1 -1
- package/dist/Sampler.d.ts.map +1 -1
- package/dist/ignore-BvHZTNsF.js +151 -0
- package/dist/ignore.d.ts +2 -0
- package/dist/ignore.d.ts.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +609 -0
- package/dist/instrumentation.js +8 -0
- package/dist/middleware.js +43 -0
- package/dist/next.js +43 -0
- package/dist/types.d.ts +2 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +30 -10
- package/dist/ignore-B1KXtl12.js +0 -80
- package/dist/index +0 -355
- package/dist/instrumentation +0 -8
- package/dist/middleware +0 -29
- package/dist/next +0 -29
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
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
pnpm add @0xchain/telemetry
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
+
**Monorepo 内使用**:在应用包的 `package.json` 中添加 `"@0xchain/telemetry": "workspace:*"`,然后执行 `pnpm install`。使用前需先构建 telemetry 包:`pnpm nx run telemetry:build` 或 `cd packages/telemetry && pnpm exec vite build`。
|
|
14
|
+
|
|
13
15
|
### 2. 初始化追踪 (instrumentation.ts)
|
|
14
16
|
|
|
15
17
|
在项目根目录(或 `src` 目录)创建 `instrumentation.ts`,只需一行代码即可启用:
|
|
@@ -110,6 +112,14 @@ export const config = {
|
|
|
110
112
|
5. **入口拆分**:`instrumentation` 含完整 Node SDK;`middleware` / `next` 轻量,仅注入请求头,适合 Edge。
|
|
111
113
|
6. **按需加载**:仅启用 HTTP + Undici 检测,减少冷启动与内存占用。
|
|
112
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
|
+
|
|
113
123
|
## 高级配置
|
|
114
124
|
|
|
115
125
|
### 采样规则配置 (Sampling Rules)
|
|
@@ -1 +1 @@
|
|
|
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,
|
|
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.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,EAAY,UAAU,EAAoC,MAAM,oBAAoB,CAAC;AAC5F,OAAO,KAAK,
|
|
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
|
+
/** 创建 span 名称忽略匹配器 */
|
|
6
|
+
export declare const createSpanNameMatcher: (config?: IgnoreConfig) => (spanName: string) => boolean;
|
|
5
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,8 +1,8 @@
|
|
|
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, };
|
|
5
|
+
export { buildSamplingContextFromAttributes, matchSamplingRule, resolveSamplingRatio, createIgnoreMatcher, createSpanNameMatcher, defaultIgnoreConfig, };
|
|
6
6
|
/** 从环境变量加载 Telemetry 配置(异步读取配置文件,不阻塞主线程) */
|
|
7
7
|
export declare const loadTelemetryConfigFromEnv: (env?: NodeJS.ProcessEnv) => Promise<Partial<TelemetryConfig>>;
|
|
8
8
|
/** 合并环境变量与显式配置得到最终配置 */
|
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"}
|