@be-link/cls-logger 1.0.1-beta.3 → 1.0.1-beta.5
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/dist/ClsLogger.d.ts +14 -2
- package/dist/ClsLogger.d.ts.map +1 -1
- package/dist/errorMonitor.d.ts.map +1 -1
- package/dist/index.esm.js +217 -32
- package/dist/index.js +217 -32
- package/dist/index.umd.js +221 -34
- package/dist/types.d.ts +12 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -2
package/dist/index.umd.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports'
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.BeLinkClsLogger = {}
|
|
5
|
-
})(this, (function (exports
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.BeLinkClsLogger = {}));
|
|
5
|
+
})(this, (function (exports) { 'use strict';
|
|
6
6
|
|
|
7
7
|
function isPlainObject(value) {
|
|
8
8
|
return Object.prototype.toString.call(value) === '[object Object]';
|
|
@@ -540,6 +540,8 @@
|
|
|
540
540
|
}
|
|
541
541
|
|
|
542
542
|
const DEFAULT_MAX_TEXT = 4000;
|
|
543
|
+
const DEFAULT_DEDUPE_WINDOW_MS = 3000;
|
|
544
|
+
const DEFAULT_DEDUPE_MAX_KEYS = 200;
|
|
543
545
|
function truncate$2(s, maxLen) {
|
|
544
546
|
if (!s)
|
|
545
547
|
return s;
|
|
@@ -573,6 +575,54 @@
|
|
|
573
575
|
const message = truncate$2(stringifyLogValue(err), maxTextLength);
|
|
574
576
|
return { message, name: '', stack: '' };
|
|
575
577
|
}
|
|
578
|
+
function createDedupeGuard(options) {
|
|
579
|
+
const cache = new Map(); // key -> lastReportAt
|
|
580
|
+
const maxKeys = Math.max(0, options.dedupeMaxKeys);
|
|
581
|
+
const windowMs = Math.max(0, options.dedupeWindowMs);
|
|
582
|
+
function touch(key, now) {
|
|
583
|
+
// refresh insertion order
|
|
584
|
+
if (cache.has(key))
|
|
585
|
+
cache.delete(key);
|
|
586
|
+
cache.set(key, now);
|
|
587
|
+
if (maxKeys > 0) {
|
|
588
|
+
while (cache.size > maxKeys) {
|
|
589
|
+
const first = cache.keys().next().value;
|
|
590
|
+
if (!first)
|
|
591
|
+
break;
|
|
592
|
+
cache.delete(first);
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
return (key) => {
|
|
597
|
+
if (!key)
|
|
598
|
+
return true;
|
|
599
|
+
if (windowMs <= 0 || maxKeys === 0)
|
|
600
|
+
return true;
|
|
601
|
+
const now = Date.now();
|
|
602
|
+
const last = cache.get(key);
|
|
603
|
+
if (typeof last === 'number' && now - last < windowMs)
|
|
604
|
+
return false;
|
|
605
|
+
touch(key, now);
|
|
606
|
+
return true;
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
function buildErrorKey(type, payload) {
|
|
610
|
+
// 使用最核心、最稳定的字段构造签名,避免把瞬态字段(如 time)带入导致失效
|
|
611
|
+
const parts = [
|
|
612
|
+
type,
|
|
613
|
+
String(payload.source ?? ''),
|
|
614
|
+
String(payload.pagePath ?? ''),
|
|
615
|
+
String(payload.message ?? ''),
|
|
616
|
+
String(payload.errorName ?? ''),
|
|
617
|
+
String(payload.stack ?? ''),
|
|
618
|
+
String(payload.filename ?? ''),
|
|
619
|
+
String(payload.lineno ?? ''),
|
|
620
|
+
String(payload.colno ?? ''),
|
|
621
|
+
String(payload.tagName ?? ''),
|
|
622
|
+
String(payload.resourceUrl ?? ''),
|
|
623
|
+
];
|
|
624
|
+
return parts.join('|');
|
|
625
|
+
}
|
|
576
626
|
function installBrowserErrorMonitor(report, options) {
|
|
577
627
|
if (typeof window === 'undefined')
|
|
578
628
|
return;
|
|
@@ -580,6 +630,7 @@
|
|
|
580
630
|
if (w.__beLinkClsLoggerErrorInstalled__)
|
|
581
631
|
return;
|
|
582
632
|
w.__beLinkClsLoggerErrorInstalled__ = true;
|
|
633
|
+
const shouldReport = createDedupeGuard(options);
|
|
583
634
|
window.addEventListener('error', (event) => {
|
|
584
635
|
try {
|
|
585
636
|
if (!sampleHit$1(options.sampleRate))
|
|
@@ -602,6 +653,8 @@
|
|
|
602
653
|
if (e.stack)
|
|
603
654
|
payload.stack = e.stack;
|
|
604
655
|
}
|
|
656
|
+
if (!shouldReport(buildErrorKey(options.reportType, payload)))
|
|
657
|
+
return;
|
|
605
658
|
report(options.reportType, payload);
|
|
606
659
|
return;
|
|
607
660
|
}
|
|
@@ -613,6 +666,8 @@
|
|
|
613
666
|
payload.source = 'resource.error';
|
|
614
667
|
payload.tagName = tagName;
|
|
615
668
|
payload.resourceUrl = truncate$2(url, 2000);
|
|
669
|
+
if (!shouldReport(buildErrorKey(options.reportType, payload)))
|
|
670
|
+
return;
|
|
616
671
|
report(options.reportType, payload);
|
|
617
672
|
}
|
|
618
673
|
}
|
|
@@ -633,6 +688,8 @@
|
|
|
633
688
|
errorName: e.name,
|
|
634
689
|
stack: e.stack,
|
|
635
690
|
};
|
|
691
|
+
if (!shouldReport(buildErrorKey(options.reportType, payload)))
|
|
692
|
+
return;
|
|
636
693
|
report(options.reportType, payload);
|
|
637
694
|
}
|
|
638
695
|
catch {
|
|
@@ -645,6 +702,7 @@
|
|
|
645
702
|
if (g.__beLinkClsLoggerMpErrorInstalled__)
|
|
646
703
|
return;
|
|
647
704
|
g.__beLinkClsLoggerMpErrorInstalled__ = true;
|
|
705
|
+
const shouldReport = createDedupeGuard(options);
|
|
648
706
|
const wxAny = globalThis.wx;
|
|
649
707
|
// wx.* 事件(兼容在 App 已经创建后的场景)
|
|
650
708
|
try {
|
|
@@ -653,10 +711,13 @@
|
|
|
653
711
|
try {
|
|
654
712
|
if (!sampleHit$1(options.sampleRate))
|
|
655
713
|
return;
|
|
656
|
-
|
|
714
|
+
const payload = {
|
|
657
715
|
source: 'wx.onError',
|
|
658
716
|
message: truncate$2(String(msg ?? ''), options.maxTextLength),
|
|
659
|
-
}
|
|
717
|
+
};
|
|
718
|
+
if (!shouldReport(buildErrorKey(options.reportType, payload)))
|
|
719
|
+
return;
|
|
720
|
+
report(options.reportType, payload);
|
|
660
721
|
}
|
|
661
722
|
catch {
|
|
662
723
|
// ignore
|
|
@@ -674,12 +735,15 @@
|
|
|
674
735
|
if (!sampleHit$1(options.sampleRate))
|
|
675
736
|
return;
|
|
676
737
|
const e = normalizeErrorLike(res?.reason, options.maxTextLength);
|
|
677
|
-
|
|
738
|
+
const payload = {
|
|
678
739
|
source: 'wx.onUnhandledRejection',
|
|
679
740
|
message: e.message,
|
|
680
741
|
errorName: e.name,
|
|
681
742
|
stack: e.stack,
|
|
682
|
-
}
|
|
743
|
+
};
|
|
744
|
+
if (!shouldReport(buildErrorKey(options.reportType, payload)))
|
|
745
|
+
return;
|
|
746
|
+
report(options.reportType, payload);
|
|
683
747
|
}
|
|
684
748
|
catch {
|
|
685
749
|
// ignore
|
|
@@ -701,10 +765,12 @@
|
|
|
701
765
|
next.onError = function (...args) {
|
|
702
766
|
try {
|
|
703
767
|
if (sampleHit$1(options.sampleRate)) {
|
|
704
|
-
|
|
768
|
+
const payload = {
|
|
705
769
|
source: 'App.onError',
|
|
706
770
|
message: truncate$2(String(args?.[0] ?? ''), options.maxTextLength),
|
|
707
|
-
}
|
|
771
|
+
};
|
|
772
|
+
if (shouldReport(buildErrorKey(options.reportType, payload)))
|
|
773
|
+
report(options.reportType, payload);
|
|
708
774
|
}
|
|
709
775
|
}
|
|
710
776
|
catch {
|
|
@@ -720,12 +786,14 @@
|
|
|
720
786
|
if (sampleHit$1(options.sampleRate)) {
|
|
721
787
|
const reason = args?.[0]?.reason ?? args?.[0];
|
|
722
788
|
const e = normalizeErrorLike(reason, options.maxTextLength);
|
|
723
|
-
|
|
789
|
+
const payload = {
|
|
724
790
|
source: 'App.onUnhandledRejection',
|
|
725
791
|
message: e.message,
|
|
726
792
|
errorName: e.name,
|
|
727
793
|
stack: e.stack,
|
|
728
|
-
}
|
|
794
|
+
};
|
|
795
|
+
if (shouldReport(buildErrorKey(options.reportType, payload)))
|
|
796
|
+
report(options.reportType, payload);
|
|
729
797
|
}
|
|
730
798
|
}
|
|
731
799
|
catch {
|
|
@@ -754,6 +822,8 @@
|
|
|
754
822
|
sampleRate: raw.sampleRate ?? 1,
|
|
755
823
|
captureResourceError: raw.captureResourceError ?? true,
|
|
756
824
|
maxTextLength: raw.maxTextLength ?? DEFAULT_MAX_TEXT,
|
|
825
|
+
dedupeWindowMs: raw.dedupeWindowMs ?? DEFAULT_DEDUPE_WINDOW_MS,
|
|
826
|
+
dedupeMaxKeys: raw.dedupeMaxKeys ?? DEFAULT_DEDUPE_MAX_KEYS,
|
|
757
827
|
};
|
|
758
828
|
if (isMiniProgramEnv()) {
|
|
759
829
|
installMiniProgramErrorMonitor(report, options);
|
|
@@ -1830,6 +1900,27 @@
|
|
|
1830
1900
|
};
|
|
1831
1901
|
}
|
|
1832
1902
|
|
|
1903
|
+
function readGlobal(key) {
|
|
1904
|
+
try {
|
|
1905
|
+
const g = globalThis;
|
|
1906
|
+
return g[key] ?? null;
|
|
1907
|
+
}
|
|
1908
|
+
catch {
|
|
1909
|
+
return null;
|
|
1910
|
+
}
|
|
1911
|
+
}
|
|
1912
|
+
function tryRequire(moduleName) {
|
|
1913
|
+
try {
|
|
1914
|
+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
1915
|
+
const req = (typeof require === 'function' ? require : null);
|
|
1916
|
+
if (!req)
|
|
1917
|
+
return null;
|
|
1918
|
+
return req(moduleName);
|
|
1919
|
+
}
|
|
1920
|
+
catch {
|
|
1921
|
+
return null;
|
|
1922
|
+
}
|
|
1923
|
+
}
|
|
1833
1924
|
function enterClsSendingGuard() {
|
|
1834
1925
|
const g = globalThis;
|
|
1835
1926
|
const next = (g.__beLinkClsLoggerSendingCount__ ?? 0) + 1;
|
|
@@ -1841,7 +1932,10 @@
|
|
|
1841
1932
|
}
|
|
1842
1933
|
class ClsLogger {
|
|
1843
1934
|
constructor() {
|
|
1935
|
+
this.sdk = null;
|
|
1936
|
+
this.sdkPromise = null;
|
|
1844
1937
|
this.client = null;
|
|
1938
|
+
this.clientPromise = null;
|
|
1845
1939
|
this.topicId = null;
|
|
1846
1940
|
this.endpoint = 'ap-shanghai.cls.tencentcs.com';
|
|
1847
1941
|
this.retryTimes = 10;
|
|
@@ -1883,6 +1977,19 @@
|
|
|
1883
1977
|
console.warn('ClsLogger.init 没有传 topicID/topic_id');
|
|
1884
1978
|
return;
|
|
1885
1979
|
}
|
|
1980
|
+
const nextEnvType = options.envType ?? this.detectEnvType();
|
|
1981
|
+
// envType/endpoint/retryTimes 变化时:重置 client(以及可能的 sdk)
|
|
1982
|
+
const envChanged = nextEnvType !== this.envType;
|
|
1983
|
+
const endpointChanged = endpoint !== this.endpoint;
|
|
1984
|
+
const retryChanged = retryTimes !== this.retryTimes;
|
|
1985
|
+
if (envChanged || endpointChanged || retryChanged) {
|
|
1986
|
+
this.client = null;
|
|
1987
|
+
this.clientPromise = null;
|
|
1988
|
+
}
|
|
1989
|
+
if (envChanged) {
|
|
1990
|
+
this.sdk = null;
|
|
1991
|
+
this.sdkPromise = null;
|
|
1992
|
+
}
|
|
1886
1993
|
this.topicId = topicId;
|
|
1887
1994
|
this.endpoint = endpoint;
|
|
1888
1995
|
this.retryTimes = retryTimes;
|
|
@@ -1893,7 +2000,7 @@
|
|
|
1893
2000
|
this.projectName = options.projectName ?? this.projectName;
|
|
1894
2001
|
this.appId = options.appId ?? this.appId;
|
|
1895
2002
|
this.appVersion = options.appVersion ?? this.appVersion;
|
|
1896
|
-
this.envType =
|
|
2003
|
+
this.envType = nextEnvType;
|
|
1897
2004
|
this.userGenerateBaseFields = options.generateBaseFields ?? this.userGenerateBaseFields;
|
|
1898
2005
|
this.autoGenerateBaseFields = createAutoDeviceInfoBaseFields(this.envType, options.deviceInfo);
|
|
1899
2006
|
this.storageKey = options.storageKey ?? this.storageKey;
|
|
@@ -1903,8 +2010,10 @@
|
|
|
1903
2010
|
this.startupDelayMs = options.batch?.startupDelayMs ?? this.startupDelayMs;
|
|
1904
2011
|
this.failedCacheKey = options.failedCacheKey ?? this.failedCacheKey;
|
|
1905
2012
|
this.failedCacheMax = options.failedCacheMax ?? this.failedCacheMax;
|
|
1906
|
-
//
|
|
1907
|
-
this.getInstance()
|
|
2013
|
+
// 预热(避免首条日志触发 import/初始化开销)
|
|
2014
|
+
void this.getInstance().catch(() => {
|
|
2015
|
+
// ignore
|
|
2016
|
+
});
|
|
1908
2017
|
// 启动时尝试发送失败缓存
|
|
1909
2018
|
this.flushFailed();
|
|
1910
2019
|
// 初始化后立即启动请求监听
|
|
@@ -2008,14 +2117,70 @@
|
|
|
2008
2117
|
this.behaviorMonitorCleanup = null;
|
|
2009
2118
|
this.behaviorMonitorStarted = false;
|
|
2010
2119
|
}
|
|
2011
|
-
|
|
2120
|
+
/**
|
|
2121
|
+
* 获取 CLS client(按环境懒加载 SDK)
|
|
2122
|
+
* - browser: 优先走 UMD 全局变量 `tencentcloudClsSdkJsWeb`
|
|
2123
|
+
* - miniprogram: 优先走 require(webpack/taro 可解析),否则 fallback import()
|
|
2124
|
+
*/
|
|
2125
|
+
async getInstance() {
|
|
2012
2126
|
if (this.client)
|
|
2013
2127
|
return this.client;
|
|
2014
|
-
this.
|
|
2015
|
-
|
|
2016
|
-
|
|
2128
|
+
if (this.clientPromise)
|
|
2129
|
+
return this.clientPromise;
|
|
2130
|
+
this.clientPromise = this.loadSdk()
|
|
2131
|
+
.then(({ AsyncClient }) => {
|
|
2132
|
+
const client = new AsyncClient({
|
|
2133
|
+
endpoint: this.endpoint,
|
|
2134
|
+
retry_times: this.retryTimes,
|
|
2135
|
+
});
|
|
2136
|
+
this.client = client;
|
|
2137
|
+
return client;
|
|
2138
|
+
})
|
|
2139
|
+
.catch((err) => {
|
|
2140
|
+
// 失败后允许下次重试
|
|
2141
|
+
this.clientPromise = null;
|
|
2142
|
+
throw err;
|
|
2143
|
+
});
|
|
2144
|
+
return this.clientPromise;
|
|
2145
|
+
}
|
|
2146
|
+
async loadSdk() {
|
|
2147
|
+
if (this.sdk)
|
|
2148
|
+
return this.sdk;
|
|
2149
|
+
if (this.sdkPromise)
|
|
2150
|
+
return this.sdkPromise;
|
|
2151
|
+
const isMini = this.envType === 'miniprogram';
|
|
2152
|
+
const moduleName = isMini ? 'tencentcloud-cls-sdk-js-mini' : 'tencentcloud-cls-sdk-js-web';
|
|
2153
|
+
// UMD(浏览器脚本)优先读全局变量
|
|
2154
|
+
if (!isMini) {
|
|
2155
|
+
const g = readGlobal('tencentcloudClsSdkJsWeb');
|
|
2156
|
+
if (g?.AsyncClient && g?.Log && g?.LogGroup && g?.PutLogsRequest) {
|
|
2157
|
+
this.sdk = g;
|
|
2158
|
+
return this.sdk;
|
|
2159
|
+
}
|
|
2160
|
+
}
|
|
2161
|
+
// 尽量同步 require(小程序/webpack 里最稳),失败再走 import()
|
|
2162
|
+
const reqMod = tryRequire(moduleName);
|
|
2163
|
+
if (reqMod?.AsyncClient && reqMod?.Log && reqMod?.LogGroup && reqMod?.PutLogsRequest) {
|
|
2164
|
+
this.sdk = reqMod;
|
|
2165
|
+
return this.sdk;
|
|
2166
|
+
}
|
|
2167
|
+
this.sdkPromise = import(moduleName)
|
|
2168
|
+
.then((m) => {
|
|
2169
|
+
const mod = (m?.default && m.default.AsyncClient ? m.default : m);
|
|
2170
|
+
const sdk = {
|
|
2171
|
+
AsyncClient: mod.AsyncClient,
|
|
2172
|
+
Log: mod.Log,
|
|
2173
|
+
LogGroup: mod.LogGroup,
|
|
2174
|
+
PutLogsRequest: mod.PutLogsRequest,
|
|
2175
|
+
};
|
|
2176
|
+
this.sdk = sdk;
|
|
2177
|
+
return sdk;
|
|
2178
|
+
})
|
|
2179
|
+
.catch((err) => {
|
|
2180
|
+
this.sdkPromise = null;
|
|
2181
|
+
throw err;
|
|
2017
2182
|
});
|
|
2018
|
-
return this.
|
|
2183
|
+
return this.sdkPromise;
|
|
2019
2184
|
}
|
|
2020
2185
|
detectEnvType() {
|
|
2021
2186
|
const wxAny = globalThis.wx;
|
|
@@ -2047,22 +2212,33 @@
|
|
|
2047
2212
|
appVersion: this.appVersion || undefined,
|
|
2048
2213
|
...normalizedFields,
|
|
2049
2214
|
});
|
|
2050
|
-
|
|
2051
|
-
|
|
2215
|
+
// 同步 API:内部异步发送,避免把网络异常冒泡到业务(尤其小程序)
|
|
2216
|
+
void this.putAsync(finalFields).catch(() => {
|
|
2217
|
+
// ignore
|
|
2218
|
+
});
|
|
2219
|
+
}
|
|
2220
|
+
async putAsync(finalFields) {
|
|
2221
|
+
if (!this.topicId)
|
|
2222
|
+
return;
|
|
2223
|
+
const sdk = await this.loadSdk();
|
|
2224
|
+
const client = await this.getInstance();
|
|
2225
|
+
const logGroup = new sdk.LogGroup('127.0.0.1');
|
|
2052
2226
|
logGroup.setSource(this.source);
|
|
2053
|
-
const log = new
|
|
2227
|
+
const log = new sdk.Log(Date.now());
|
|
2054
2228
|
for (const key of Object.keys(finalFields)) {
|
|
2055
2229
|
log.addContent(key, stringifyLogValue(finalFields[key]));
|
|
2056
2230
|
}
|
|
2057
2231
|
logGroup.addLog(log);
|
|
2058
|
-
const request = new
|
|
2232
|
+
const request = new sdk.PutLogsRequest(this.topicId, logGroup);
|
|
2059
2233
|
const exit = enterClsSendingGuard();
|
|
2234
|
+
let p;
|
|
2060
2235
|
try {
|
|
2061
|
-
client.PutLogs(request);
|
|
2236
|
+
p = client.PutLogs(request);
|
|
2062
2237
|
}
|
|
2063
2238
|
finally {
|
|
2064
2239
|
exit();
|
|
2065
2240
|
}
|
|
2241
|
+
await p;
|
|
2066
2242
|
}
|
|
2067
2243
|
/**
|
|
2068
2244
|
* 直接上报:把 data 序列化后放入指定 key(默认 “日志内容”)
|
|
@@ -2121,11 +2297,19 @@
|
|
|
2121
2297
|
console.warn('ClsLogger.putBatch:未初始化 topic_id');
|
|
2122
2298
|
return;
|
|
2123
2299
|
}
|
|
2124
|
-
|
|
2125
|
-
|
|
2300
|
+
void this.putBatchAsync(queue).catch(() => {
|
|
2301
|
+
// ignore
|
|
2302
|
+
});
|
|
2303
|
+
}
|
|
2304
|
+
async putBatchAsync(queue) {
|
|
2305
|
+
if (!this.topicId)
|
|
2306
|
+
return;
|
|
2307
|
+
const sdk = await this.loadSdk();
|
|
2308
|
+
const client = await this.getInstance();
|
|
2309
|
+
const logGroup = new sdk.LogGroup('127.0.0.1');
|
|
2126
2310
|
logGroup.setSource(this.source);
|
|
2127
2311
|
for (const item of queue) {
|
|
2128
|
-
const log = new
|
|
2312
|
+
const log = new sdk.Log(item.time);
|
|
2129
2313
|
const data = item.data ?? {};
|
|
2130
2314
|
for (const key of Object.keys(data)) {
|
|
2131
2315
|
log.addContent(key, stringifyLogValue(data[key]));
|
|
@@ -2134,14 +2318,16 @@
|
|
|
2134
2318
|
}
|
|
2135
2319
|
if (logGroup.getLogs().length === 0)
|
|
2136
2320
|
return;
|
|
2137
|
-
const request = new
|
|
2321
|
+
const request = new sdk.PutLogsRequest(this.topicId, logGroup);
|
|
2138
2322
|
const exit = enterClsSendingGuard();
|
|
2323
|
+
let p;
|
|
2139
2324
|
try {
|
|
2140
|
-
client.PutLogs(request);
|
|
2325
|
+
p = client.PutLogs(request);
|
|
2141
2326
|
}
|
|
2142
2327
|
finally {
|
|
2143
2328
|
exit();
|
|
2144
2329
|
}
|
|
2330
|
+
await p;
|
|
2145
2331
|
}
|
|
2146
2332
|
/**
|
|
2147
2333
|
* 参考《一、概述》:统一上报入口(内存队列 + 批量发送)
|
|
@@ -2254,12 +2440,13 @@
|
|
|
2254
2440
|
async sendReportLogs(logs) {
|
|
2255
2441
|
if (!this.topicId)
|
|
2256
2442
|
return;
|
|
2257
|
-
const
|
|
2258
|
-
const
|
|
2443
|
+
const sdk = await this.loadSdk();
|
|
2444
|
+
const client = await this.getInstance();
|
|
2445
|
+
const logGroup = new sdk.LogGroup('127.0.0.1');
|
|
2259
2446
|
logGroup.setSource(this.source);
|
|
2260
2447
|
for (const item of logs) {
|
|
2261
2448
|
const fields = this.buildReportFields(item);
|
|
2262
|
-
const log = new
|
|
2449
|
+
const log = new sdk.Log(fields.timestamp);
|
|
2263
2450
|
for (const key of Object.keys(fields)) {
|
|
2264
2451
|
if (key === 'timestamp')
|
|
2265
2452
|
continue;
|
|
@@ -2267,7 +2454,7 @@
|
|
|
2267
2454
|
}
|
|
2268
2455
|
logGroup.addLog(log);
|
|
2269
2456
|
}
|
|
2270
|
-
const request = new
|
|
2457
|
+
const request = new sdk.PutLogsRequest(this.topicId, logGroup);
|
|
2271
2458
|
// 只在“发起网络请求”的同步阶段打标记,避免 requestMonitor 监控 CLS 上报请求导致递归
|
|
2272
2459
|
const exit = enterClsSendingGuard();
|
|
2273
2460
|
let p;
|
package/dist/types.d.ts
CHANGED
|
@@ -155,6 +155,18 @@ export interface ErrorMonitorOptions {
|
|
|
155
155
|
captureResourceError?: boolean;
|
|
156
156
|
/** stack/message 最大长度(超出截断),默认 4000 */
|
|
157
157
|
maxTextLength?: number;
|
|
158
|
+
/**
|
|
159
|
+
* 错误去重窗口(ms)
|
|
160
|
+
* - 目的:避免同一错误在短时间内频繁触发导致重复上报
|
|
161
|
+
* - 行为:同一“错误签名”在窗口内只会上报一次
|
|
162
|
+
* - 默认:3000
|
|
163
|
+
*/
|
|
164
|
+
dedupeWindowMs?: number;
|
|
165
|
+
/**
|
|
166
|
+
* 去重缓存最大 key 数(超过会淘汰最早的 key)
|
|
167
|
+
* - 默认:200
|
|
168
|
+
*/
|
|
169
|
+
dedupeMaxKeys?: number;
|
|
158
170
|
}
|
|
159
171
|
export interface PerformanceMonitorOptions {
|
|
160
172
|
/** 是否开启,默认 true */
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;AAC7D,MAAM,MAAM,SAAS,GAAG,aAAa,GAAG,SAAS,EAAE,GAAG;IAAE,CAAC,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAEjF;;;;;GAKG;AACH,MAAM,MAAM,cAAc,GAAG,aAAa,GAAG,SAAS,GAAG,MAAM,CAAC;AAEhE,sCAAsC;AACtC,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;AAExD,MAAM,MAAM,UAAU,GAAG,UAAU,CAAC;AAEpC,MAAM,MAAM,OAAO,GAAG,SAAS,GAAG,aAAa,CAAC;AAEhD,MAAM,WAAW,qBAAqB;IACpC,oDAAoD;IACpD,QAAQ,EAAE,MAAM,CAAC;IACjB,kBAAkB;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,eAAe;IACf,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,qBAAqB;IACrB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,YAAY;IAC3B,8BAA8B;IAC9B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,wBAAwB;IACxB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;;;OAKG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAED,MAAM,WAAW,oBAAoB;IACnC;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,gBAAgB;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,YAAY;IACZ,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW;IACX,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,sBA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G,OAAO,CAAC,MAAM,CAAC,CAAC;IAEzC,kCAAkC;IAClC,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,gDAAgD;IAChD,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,2DAA2D;IAC3D,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAEhC;;;;OAIG;IACH,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAE1B;;;;OAIG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,wCAAwC;IACxC,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,gCAAgC;IAChC,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAE5B,8DAA8D;IAC9D,WAAW,CAAC,EAAE,MAAM,MAAM,CAAC;IAE3B;;OAEG;IACH,YAAY,CAAC,EAAE;QACb;;;;WAIG;QACH,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;QAC1B,yBAAyB;QACzB,YAAY,CAAC,EAAE,MAAM,CAAC;KACvB,CAAC;CACH"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@be-link/cls-logger",
|
|
3
|
-
"version": "1.0.1-beta.
|
|
3
|
+
"version": "1.0.1-beta.5",
|
|
4
4
|
"description": "@be-link cls-logger - 腾讯云 CLS 日志上报封装",
|
|
5
5
|
"homepage": "https://github.com/snowmountain-top/be-link",
|
|
6
6
|
"author": "zhuiyi",
|
|
@@ -21,7 +21,8 @@
|
|
|
21
21
|
"dist"
|
|
22
22
|
],
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"tencentcloud-cls-sdk-js-web": "^1.0.11"
|
|
24
|
+
"tencentcloud-cls-sdk-js-web": "^1.0.11",
|
|
25
|
+
"tencentcloud-cls-sdk-js-mini": "^1.0.3"
|
|
25
26
|
},
|
|
26
27
|
"publishConfig": {
|
|
27
28
|
"access": "public"
|