@0xchain/telemetry 1.1.0-beta.18 → 1.1.0-beta.19

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 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.
@@ -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
+ };