Skip to content

Commit 4b4742d

Browse files
syzhyugasun
andauthored
feat: use metrics api from toolkit library(#32)
* clean invalid code * use metrics api from toolkit library * clean invalid code * change code style * delete library * chore(release): 0.0.12 Co-authored-by: yugasun <[email protected]>
1 parent fa215bf commit 4b4742d

File tree

7 files changed

+43
-769
lines changed

7 files changed

+43
-769
lines changed

example/src/sls.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,38 @@ app.get(`/`, (req, res) => {
77
res.sendFile(path.join(__dirname, 'index.html'))
88
})
99

10+
app.get('/user', (req, res) => {
11+
res.send([
12+
{
13+
title: 'serverless framework',
14+
link: 'https://serverless.com'
15+
}
16+
])
17+
})
18+
19+
app.get('/user/:id', (req, res) => {
20+
const id = req.params.id
21+
res.send({
22+
id: id,
23+
title: 'serverless framework',
24+
link: 'https://serverless.com'
25+
})
26+
})
27+
28+
app.get('/404', (req, res) => {
29+
res.status(404).send('Not found')
30+
})
31+
32+
app.get('/500', (req, res) => {
33+
res.status(500).send('Server Error')
34+
})
35+
1036
// Error handler
1137
app.use(function(err, req, res, next) {
1238
console.error(err)
1339
res.status(500).send('Internal Serverless Error')
1440
})
1541

42+
app.listen(8080)
43+
1644
module.exports = app

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@serverless/express",
3-
"version": "0.0.11",
3+
"version": "0.0.12",
44
"main": "src/serverless.js",
55
"publishConfig": {
66
"access": "public"

serverless.component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: express
2-
version: 0.0.11
2+
version: 0.0.12
33
author: Tencent Cloud Inc.
44
org: Tencent Cloud Inc.
55
description: Deploys a serverless Express.js application onto Tencent SCF and Tencent APIGateway.

src/_shims/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"author": "Serverless, Inc.",
1212
"license": "Apache",
1313
"dependencies": {
14-
"tencent-component-monitor": "^1.0.4",
14+
"tencent-component-monitor": "^1.0.5",
1515
"tencent-serverless-http": "^1.2.0"
1616
}
1717
}

src/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"dependencies": {
1414
"download": "^8.0.0",
1515
"fs-extra": "^8.1.0",
16-
"moment": "^2.24.0",
1716
"tencent-cloud-sdk": "^0.1.4",
1817
"tencent-component-toolkit": "^1.5.5",
1918
"type": "^2.0.0"

src/serverless.js

Lines changed: 11 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
const { Component } = require('@serverless/core')
2-
const { MultiApigw, Scf, Apigw, Cos, Cns, Cam } = require('tencent-component-toolkit')
3-
const moment = require('moment')
4-
const util = require('util')
5-
const { slsMonitor } = require('tencent-cloud-sdk')
6-
7-
const {
8-
packageCode,
9-
getDefaultProtocol,
10-
deleteRecord,
11-
prepareInputs,
12-
buildMetrics,
13-
buildCustomMetrics
14-
} = require('./utils')
2+
const { MultiApigw, Scf, Apigw, Cos, Cns, Cam, Metrics } = require('tencent-component-toolkit')
3+
const { packageCode, getDefaultProtocol, deleteRecord, prepareInputs } = require('./utils')
154
const CONFIGS = require('./config')
165

176
class ServerlessComponent extends Component {
@@ -305,102 +294,23 @@ class ServerlessComponent extends Component {
305294
if (!inputs.rangeStart || !inputs.rangeEnd) {
306295
throw new Error('rangeStart and rangeEnd are require inputs')
307296
}
308-
inputs.rangeStart = moment(inputs.rangeStart)
309-
inputs.rangeEnd = moment(inputs.rangeEnd)
310-
311-
if (inputs.rangeStart.isAfter(inputs.rangeEnd)) {
312-
throw new Error(`The rangeStart provided is after the rangeEnd`)
313-
}
314-
315-
// Validate: End is not longer than 30 days
316-
if (inputs.rangeStart.diff(inputs.rangeEnd, 'days') >= 31) {
317-
throw new Error(
318-
`The range cannot be longer than 30 days. The supplied range is: ${inputs.rangeStart.diff(
319-
inputs.rangeEnd,
320-
'days'
321-
)}`
322-
)
323-
}
324-
325-
const diffMinutes = (inputs.rangeEnd - inputs.rangeStart) / 1000 / 60
326-
let period
327-
if (diffMinutes <= 16) {
328-
// 16 mins
329-
period = 60 // 1 min
330-
} else if (diffMinutes <= 61) {
331-
// 1 hour
332-
period = 300 // 5 mins
333-
} else if (diffMinutes <= 1500) {
334-
// 24 hours
335-
period = 3600 // hour
336-
} else {
337-
period = 86400 // day
338-
}
339-
340-
const credentials = this.getCredentials()
341-
const slsClient = new slsMonitor(credentials)
342-
343-
let timeFormat = 'YYYY-MM-DDTHH:mm:ssZ'
344-
if (inputs.tz) {
345-
timeFormat = 'YYYY-MM-DDTHH:mm:ss' + inputs.tz
346-
}
347-
348-
const rangeTime = {
349-
rangeStart: inputs.rangeStart.format(timeFormat),
350-
rangeEnd: inputs.rangeEnd.format(timeFormat)
351-
}
352-
353297
let functionName, namespace, functionVersion
354298
if (this.state[this.state.region] && this.state[this.state.region].functionName) {
355299
;({ functionName, namespace, functionVersion } = this.state[this.state.region])
356300
} else {
357301
throw new Error('function name not define')
358302
}
359303

360-
console.log(
361-
'getScfMetrics params>>',
362-
this.state.region,
363-
rangeTime,
364-
period,
365-
functionName,
366-
namespace
367-
)
368-
const responses = await slsClient.getScfMetrics(
369-
this.state.region,
370-
rangeTime,
371-
period,
372-
functionName,
373-
namespace || 'default'
374-
)
375-
console.log('getScf>>>', JSON.stringify(responses))
376-
const metricResults = buildMetrics(responses, period)
377-
378-
const reqCustomTime = {
379-
rangeStart: inputs.rangeStart.format('YYYY-MM-DD HH:mm:ss'),
380-
rangeEnd: inputs.rangeEnd.format('YYYY-MM-DD HH:mm:ss')
381-
}
382-
383-
const instances = [
384-
util.format('%s|%s|%s', namespace || 'default', functionName, functionVersion || '$LATEST')
385-
]
386-
console.log('customMetrics params>>', this.state.region, instances, reqCustomTime, period)
387-
const customMetrics = await slsClient.getCustomMetrics(
388-
this.state.region,
389-
instances,
390-
reqCustomTime,
391-
period
392-
)
393-
console.log('customMetrics>>>', JSON.stringify(customMetrics))
394-
395-
const customResults = buildCustomMetrics(customMetrics)
396-
metricResults.metrics = metricResults.metrics.concat(customResults)
397-
398-
if (!metricResults.rangeStart) {
399-
metricResults.rangeStart = reqCustomTime.rangeStart
400-
}
401-
if (!metricResults.rangeEnd) {
402-
metricResults.rangeEnd = reqCustomTime.rangeEnd
304+
const options = {
305+
funcName: functionName,
306+
namespace: namespace,
307+
version: functionVersion,
308+
region: this.state.region,
309+
timezone: inputs.tz
403310
}
311+
const credentials = this.getCredentials()
312+
const mertics = new Metrics(credentials, options)
313+
const metricResults = await mertics.getDatas(inputs.rangeStart, inputs.rangeEnd)
404314
return metricResults
405315
}
406316
}

0 commit comments

Comments
 (0)