Skip to content

Commit 395d9ae

Browse files
authored
fix: filter express nextjs custom metrics (#11)
* fix: filter express nextjs custom metrics * fix: change code style
1 parent 8ec6ed5 commit 395d9ae

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

src/client.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,21 +563,45 @@ class SlsMonitor {
563563
requestHandlers = []
564564
}
565565
}
566-
if (requestHandlers.length == 0) {
567-
console.log('getCustomMetrics', JSON.stringify(responses))
566+
if (requestHandlers.length === 0) {
567+
responses = this.cleanEmptyMetric(responses, metricAttributeHash)
568568
this.aggrCustomDatas(responses, period, metricAttributeHash)
569569
return responses
570570
}
571571
if (!firstRequestFlag) {
572572
await SlsMonitor.sleep(1000)
573573
}
574574
results = await getMetricsResponse(requestHandlers)
575+
results = this.cleanEmptyMetric(results, metricAttributeHash)
575576
responses = responses.concat(results)
576-
console.log('getCustomMetrics', JSON.stringify(responses))
577577
this.aggrCustomDatas(responses, period, metricAttributeHash)
578578
return responses
579579
}
580580

581+
cleanEmptyMetric(datas, metricAttributeHash) {
582+
const metrics = []
583+
const rule = /^(GET|POST|DEL|DELETE|PUT|OPTIONS|HEAD)_(.*)$/i
584+
for (var i = 0; datas && i < datas.length; i++) {
585+
const item = datas[i]
586+
if (item.Response.Data.length === 0) {
587+
continue
588+
}
589+
const name = metricAttributeHash[item.Response.Data[0].AttributeId].AttributeName
590+
if (!name.match(rule)) {
591+
metrics.push(item)
592+
continue
593+
}
594+
for (var n = 0; n < item.Response.Data[0].Values.length; n++) {
595+
const val = item.Response.Data[0].Values[n]
596+
if (val.Value !== 0) {
597+
metrics.push(item)
598+
break
599+
}
600+
}
601+
}
602+
return metrics
603+
}
604+
581605
async getScfMetrics(region, rangeTime, period, funcName, ns, version) {
582606
const client = new TencentCloudClient(
583607
this.credentials,

0 commit comments

Comments
 (0)