-
Notifications
You must be signed in to change notification settings - Fork 151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add prometheus metrics in dedicated port #1232
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
198e284
Added prometheus metrics in dedicated server & port
Imod7 9dd6fa7
Fixed lint errors by using an IIFE & adding void operator
Imod7 dd5a15d
Update dep prom-client
Imod7 e2541df
Merged master branch
Imod7 a8e0b02
Added prometheus flags & fix for process.kill
Imod7 ead4c44
Code refactor, help page & README
Imod7 901a792
Merged master & resolved conflicts
Imod7 dccdf7f
remove jest unnecessary flags
TarikGul 4654e87
resolve merge conflict
TarikGul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,60 @@ | ||
import express from 'express'; | ||
import { Application, Request, Response } from 'express'; | ||
import client from 'prom-client'; | ||
|
||
import { Log } from '../logging/Log'; | ||
import { parseArgs } from '../parseArgs'; | ||
|
||
export const httpErrorCounter = new client.Counter({ | ||
name: 'sas_http_errors', | ||
help: 'Number of HTTP Errors', | ||
}); | ||
|
||
interface IAppConfiguration { | ||
port: number; | ||
host: string; | ||
} | ||
|
||
export default class Metrics_App { | ||
private app: Application; | ||
private readonly port: number; | ||
private readonly host: string; | ||
|
||
/** | ||
* @param appConfig configuration for app. | ||
*/ | ||
constructor({ host }: IAppConfiguration) { | ||
const args = parseArgs(); | ||
|
||
this.port = Number(args.prometheus_port); | ||
this.app = express(); | ||
this.host = host; | ||
|
||
this.metricsEndpoint(); | ||
} | ||
|
||
listen(): void { | ||
const { logger } = Log; | ||
this.app.listen(this.port, this.host, () => { | ||
logger.info( | ||
`Metrics Server started at http://${this.host}:${this.port}/` | ||
); | ||
}); | ||
} | ||
|
||
/** | ||
* Mount the metrics endpoint. | ||
*/ | ||
private metricsEndpoint() { | ||
const register = new client.Registry(); | ||
register.registerMetric(httpErrorCounter); | ||
client.collectDefaultMetrics({ register, prefix: 'sas_' }); | ||
// Set up the metrics endpoint | ||
this.app.get('/metrics', (_req: Request, res: Response) => { | ||
void (async () => { | ||
res.set('Content-Type', register.contentType); | ||
res.send(await register.metrics()); | ||
})(); | ||
}); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fine with the Logger since it's only being used in here which makes sense. But that being said as a side note, the logger can be a top level variable since it's only ever instantiated once. The logger file itself has a static
this.create
which only gets called the first time the logger is instantiated in the entry file.