Skip to content

Commit f72682d

Browse files
committed
add sls monitor api
1 parent a79bcca commit f72682d

File tree

3 files changed

+115
-2
lines changed

3 files changed

+115
-2
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,21 @@ cosClient.getService(function(err, data) {
4040
```
4141

4242
参考地址:https://cloud.tencent.com/document/product/436/8629
43+
44+
## Scf 监控接口
45+
46+
```
47+
const { slsMonitor } = require('./src/index')
48+
49+
const slsClient = new slsMonitor(secret)
50+
51+
const rangeTime = {
52+
rangeStart: 'begin Time Object',
53+
rangeEnd: 'end Time Object'
54+
}
55+
const period = 3600
56+
const ret = await slsClient.getScfMetrics('ap-guangzhou', rangeTime, period, 'funcName', 'default', '$latest')
57+
console.log(ret)
58+
```
59+
60+
参考地址: https://cloud.tencent.com/document/product/248/31649

src/client.js

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,106 @@ class DomainClient {
176176

177177
class CosClient extends cos {}
178178

179+
class SlsMonitor {
180+
constructor(credentials = {}) {
181+
this.credentials = credentials
182+
}
183+
async request(data) {
184+
return await new TencentCloudClient(this.credentials, {
185+
host: 'monitor.tencentcloudapi.com',
186+
path: '/'
187+
}).doCloudApiRequest(data)
188+
}
189+
190+
async getScfMetrics(region, rangeTime, period, funcName, ns, version) {
191+
// const cred = new Credential(credentials.secretId, credentials.secretKey);
192+
// const client = new MonitorClient(cred, region || 'ap-guangzhou');
193+
const client = new TencentCloudClient(this.credentials, {
194+
host: 'monitor.tencentcloudapi.com',
195+
path: '/'
196+
})
197+
const req = {
198+
Action: 'GetMonitorData',
199+
Version: '2018-07-24',
200+
}
201+
202+
const metrics = ['Duration', 'Invocation', 'Error', 'ConcurrentExecutions', 'ConfigMem', 'FunctionErrorPercentage', 'Http2xx', 'Http432', 'Http433', 'Http434', 'Http4xx', 'Mem', 'MemDuration'];
203+
204+
const result = {
205+
rangeStart: rangeTime.rangeStart,
206+
rangeEnd: rangeTime.rangeEnd,
207+
metrics: []
208+
}
209+
210+
const requestHandlers = []
211+
for (var i = 0; i < metrics.length; i++) {
212+
req.Namespace = 'qce/scf_v2';
213+
req.MetricName = metrics[i];
214+
req.Period = period;
215+
req.StartTime = rangeTime.rangeStart;
216+
req.EndTime = rangeTime.rangeEnd;
217+
req.Instances = [{
218+
Dimensions: [
219+
{
220+
Name: 'functionName',
221+
Value: funcName,
222+
},
223+
{
224+
Name: 'version',
225+
Value: version || '$latest',
226+
},
227+
{
228+
Name: 'namespace',
229+
Value: ns,
230+
}
231+
]
232+
}];
233+
requestHandlers.push(client.doCloudApiRequest(req))
234+
}
235+
return new Promise((resolve, reject)=> {
236+
Promise.all(requestHandlers).then((results) => {
237+
for (var i = 0; i < results.length; i++) {
238+
const response = results[i].Response;
239+
const metric = {
240+
type: response.MetricName,
241+
title: response.MetricName,
242+
values: [],
243+
total: 0
244+
}
245+
246+
response.DataPoints[0].Timestamps.forEach((val, i) => {
247+
if (!metric.values[i]) {
248+
metric.values[i] = {
249+
timestamp: val
250+
}
251+
} else {
252+
metric.values[i].timestamp = val
253+
}
254+
255+
if (response.DataPoints[0].Values[i] != undefined) {
256+
metric.values[i].value = response.DataPoints[0].Values[i]
257+
metric.total = Math.round(metric.total + metric.values[i].value)
258+
}
259+
260+
})
261+
result.metrics.push(metric)
262+
}
263+
resolve(result)
264+
265+
}).catch((error) => {
266+
reject(error)
267+
})
268+
})
269+
}
270+
}
271+
179272
module.exports = {
180273
ScfClient,
181274
TagClient,
182275
CamClient,
183276
CnsClient,
184277
ApigwClient,
185278
DomainClient,
186-
CosClient
279+
CosClient,
280+
SlsMonitor
187281
}

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ module.exports = {
55
tag: require('./client').TagClient,
66
apigw: require('./client').ApigwClient,
77
domain: require('./client').DomainClient,
8-
cos: require('./client').CosClient
8+
cos: require('./client').CosClient,
9+
slsMonitor: require('./client').SlsMonitor
910
}

0 commit comments

Comments
 (0)