-
-
Notifications
You must be signed in to change notification settings - Fork 654
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(services): add database metrics interface (#1739)
* feat(services): add database metrics interface * remove some metrics
- Loading branch information
Showing
6 changed files
with
125 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { RequestHandler } from 'express' | ||
import { ClusterService, Metric } from '../helper/cluster.service' | ||
import Config from '../config' | ||
import * as prom from 'prom-client' | ||
|
||
const register = new prom.Registry() | ||
|
||
const DATABASE_CPU = new prom.Gauge({ | ||
name: 'laf_mongo_cpu', | ||
help: 'the cpu of the mongo', | ||
registers: [register], | ||
labelNames: ['container', 'pod', 'appid'], | ||
}) | ||
|
||
const DATABASE_MEMORY = new prom.Gauge({ | ||
name: 'laf_mongo_memory', | ||
help: 'the memory of the mongo', | ||
registers: [register], | ||
labelNames: ['container', 'pod', 'appid'], | ||
}) | ||
|
||
function updateMetrics(metric: Metric) { | ||
DATABASE_CPU.labels(metric.containerName, metric.podName, metric.appid).set( | ||
metric.cpu, | ||
) | ||
DATABASE_MEMORY.labels( | ||
metric.containerName, | ||
metric.podName, | ||
metric.appid, | ||
).set(metric.memory) | ||
} | ||
|
||
const getDatabaseMetrics: RequestHandler = async (req, res) => { | ||
const token = req.params.token | ||
|
||
if (!token || Config.API_SECRET !== token) { | ||
return res.status(403).send('forbidden') | ||
} | ||
|
||
// Clear the metrics data generated by the last request | ||
DATABASE_CPU.reset() | ||
DATABASE_MEMORY.reset() | ||
|
||
const databaseMetrics = await ClusterService.getPodMetrics( | ||
ClusterService.DB_NAMESPACE, | ||
ClusterService.LABEL_DATABASE, | ||
'DATABASE', | ||
) | ||
|
||
for (const metric of databaseMetrics) { | ||
updateMetrics(metric) | ||
} | ||
|
||
res.set('Content-Type', 'text/plain') | ||
res.send(await register.metrics()) | ||
} | ||
|
||
export default getDatabaseMetrics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters