Skip to content

Commit abb04af

Browse files
authored
Merge pull request #1 from serverless-tencent/develop
add get metrics api
2 parents 378c0c4 + 2a31ea7 commit abb04af

File tree

4 files changed

+126
-6
lines changed

4 files changed

+126
-6
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 string rfc3339 format', // 2018-09-22T19:51:23+08:00
53+
rangeEnd: 'end Time string rfc3339 format' // 2018-09-22T19:51:23+08:00
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

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@
1818
"author": "Dfounderliu",
1919
"license": "Apache",
2020
"dependencies": {
21-
"configstore": "3.1.2",
22-
"dot-qs": "0.2.0",
23-
"mime-types": "2.1.24",
24-
"object-assign": "3.0.0",
25-
"xml2js": "0.4.19",
2621
"caseless": "0.12.0",
2722
"combined-stream": "1.0.6",
23+
"configstore": "3.1.2",
24+
"dot-qs": "0.2.0",
2825
"extend": "3.0.2",
2926
"forever-agent": "0.6.1",
3027
"form-data": "2.3.2",
@@ -33,13 +30,17 @@
3330
"is-typedarray": "1.0.0",
3431
"isstream": "0.1.2",
3532
"json-stringify-safe": "5.0.1",
33+
"mime-types": "2.1.24",
34+
"moment": "^2.24.0",
3635
"oauth-sign": "0.9.0",
36+
"object-assign": "3.0.0",
3737
"performance-now": "2.1.0",
3838
"qs": "6.5.2",
3939
"safe-buffer": "5.1.2",
4040
"tough-cookie": "2.5.0",
4141
"tunnel-agent": "0.6.0",
42-
"uuid": "3.3.2"
42+
"uuid": "3.3.2",
43+
"xml2js": "0.4.19"
4344
},
4445
"devDependencies": {
4546
"babel-eslint": "9.0.0",

src/client.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const dotQs = require('dot-qs')
44
const request = require('../lib/request/index')
55
const crypto = require('crypto')
66
const cos = require('../lib/cos/cos')
7+
const util = require('util')
78

89
var defaults = {
910
signatureMethod: 'HmacSHA1',
@@ -200,6 +201,104 @@ class DomainClient {
200201

201202
class CosClient extends cos {}
202203

204+
class SlsMonitor {
205+
constructor(credentials = {}) {
206+
this.credentials = credentials
207+
}
208+
209+
async request(data) {
210+
return await new TencentCloudClient(this.credentials, {
211+
host: 'monitor.tencentcloudapi.com',
212+
path: '/'
213+
}).doCloudApiRequest(data)
214+
}
215+
216+
aggregationByDay(responses) {
217+
const len = responses.length
218+
for (var i = 0; i < len; i++) {
219+
const result = responses[i]
220+
const tlen = result.Response.DataPoints[0].Timestamps.length
221+
const values = result.Response.DataPoints[0].Values
222+
let total = values[0]
223+
224+
const newTimes = []
225+
const newValues = []
226+
for (var n = 0; n < tlen; n++) {
227+
if (n > 0 && !(n % 24)) {
228+
newTimes.push(result.Response.DataPoints[0].Timestamps[n])
229+
const v = total / 24
230+
newValues.push(parseFloat((v.toFixed(2)), 10))
231+
total = values[n]
232+
} else {
233+
total += values[n]
234+
}
235+
}
236+
result.Response.DataPoints[0].Timestamps = newTimes
237+
result.Response.DataPoints[0].Values = newValues
238+
}
239+
}
240+
241+
async getScfMetrics(region, rangeTime, period, funcName, ns, version) {
242+
const client = new TencentCloudClient(this.credentials, {
243+
host: 'monitor.tencentcloudapi.com',
244+
path: '/'
245+
})
246+
const req = {
247+
Action: 'GetMonitorData',
248+
Version: '2018-07-24',
249+
}
250+
251+
const metrics = ['Duration', 'Invocation', 'Error', 'ConcurrentExecutions', 'ConfigMem', 'FunctionErrorPercentage', 'Http2xx', 'Http432', 'Http433', 'Http434', 'Http4xx', 'Mem', 'MemDuration', 'Syserr'];
252+
253+
let aggrFlag = false
254+
if (period == 86400) {
255+
period = 3600 // day
256+
aggrFlag = true
257+
}
258+
const result = {
259+
rangeStart: rangeTime.rangeStart,
260+
rangeEnd: rangeTime.rangeEnd,
261+
metrics: []
262+
}
263+
264+
const requestHandlers = []
265+
for (var i = 0; i < metrics.length; i++) {
266+
req.Namespace = 'qce/scf_v2';
267+
req.MetricName = metrics[i];
268+
req.Period = period;
269+
req.StartTime = rangeTime.rangeStart;
270+
req.EndTime = rangeTime.rangeEnd;
271+
req.Instances = [{
272+
Dimensions: [
273+
{
274+
Name: 'functionName',
275+
Value: funcName,
276+
},
277+
{
278+
Name: 'version',
279+
Value: version || '$latest',
280+
},
281+
{
282+
Name: 'namespace',
283+
Value: ns,
284+
}
285+
]
286+
}];
287+
requestHandlers.push(client.doCloudApiRequest(req))
288+
}
289+
return new Promise((resolve, reject)=> {
290+
Promise.all(requestHandlers).then((results) => {
291+
if (aggrFlag)
292+
this.aggregationByDay(results)
293+
294+
resolve(results)
295+
}).catch((error) => {
296+
reject(error)
297+
})
298+
})
299+
}
300+
}
301+
203302
module.exports = {
204303
ScfClient,
205304
TagClient,
@@ -209,5 +308,6 @@ module.exports = {
209308
ApigwClient,
210309
DomainClient,
211310
CosClient,
311+
SlsMonitor,
212312
TcbClient
213313
}

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ module.exports = {
77
apigw: require('./client').ApigwClient,
88
domain: require('./client').DomainClient,
99
cos: require('./client').CosClient,
10+
slsMonitor: require('./client').SlsMonitor,
1011
tcb: require('./client').TcbClient
1112
}

0 commit comments

Comments
 (0)