-
Notifications
You must be signed in to change notification settings - Fork 7.1k
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
fix(core): Respect prefix for all Prometheus metrics #10130
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
300c3f2
fix(core): Respect prefix for all Prometheus metrics
ivov 0b2b267
Cleanup
ivov d1d192f
Cleanup
ivov b82fca2
Better readability
ivov 47a483b
Cleaner typings
ivov e9e8e8b
Use `for in` instead
ivov c7368c0
Mention breaking change
ivov 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,8 +10,7 @@ import { CacheService } from '@/services/cache/cache.service'; | |
import { MessageEventBus } from '@/eventbus/MessageEventBus/MessageEventBus'; | ||
import { EventMessageTypeNames } from 'n8n-workflow'; | ||
import type { EventMessageTypes } from '@/eventbus'; | ||
|
||
type MetricCategory = 'default' | 'api' | 'cache' | 'logs'; | ||
import type { Includes, MetricCategory, MetricLabel } from './types'; | ||
|
||
@Service() | ||
export class PrometheusMetricsService { | ||
|
@@ -24,10 +23,10 @@ export class PrometheusMetricsService { | |
|
||
private readonly prefix = config.getEnv('endpoints.metrics.prefix'); | ||
|
||
private readonly includes = { | ||
private readonly includes: Includes = { | ||
metrics: { | ||
default: config.getEnv('endpoints.metrics.includeDefaultMetrics'), | ||
api: config.getEnv('endpoints.metrics.includeApiEndpoints'), | ||
routes: config.getEnv('endpoints.metrics.includeApiEndpoints'), | ||
cache: config.getEnv('endpoints.metrics.includeCacheMetrics'), | ||
logs: config.getEnv('endpoints.metrics.includeMessageEventBusMetrics'), | ||
}, | ||
|
@@ -60,8 +59,20 @@ export class PrometheusMetricsService { | |
} | ||
|
||
disableAllMetrics() { | ||
for (const metric of Object.keys(this.includes.metrics) as MetricCategory[]) { | ||
this.includes.metrics[metric] = false; | ||
for (const metric in this.includes.metrics) { | ||
this.includes.metrics[metric as MetricCategory] = false; | ||
} | ||
} | ||
|
||
enableLabels(labels: MetricLabel[]) { | ||
for (const label of labels) { | ||
this.includes.labels[label] = true; | ||
} | ||
} | ||
|
||
disableAllLabels() { | ||
for (const label in this.includes.labels) { | ||
this.includes.labels[label as MetricLabel] = false; | ||
} | ||
} | ||
|
||
|
@@ -98,7 +109,7 @@ export class PrometheusMetricsService { | |
* Set up metrics for server routes with `express-prom-bundle` | ||
*/ | ||
private initRouteMetrics(app: express.Application) { | ||
if (!this.includes.metrics.api) return; | ||
if (!this.includes.metrics.routes) return; | ||
|
||
const metricsMiddleware = promBundle({ | ||
autoregister: false, | ||
|
@@ -126,11 +137,25 @@ export class PrometheusMetricsService { | |
private mountMetricsEndpoint(app: express.Application) { | ||
app.get('/metrics', async (_req: express.Request, res: express.Response) => { | ||
const metrics = await promClient.register.metrics(); | ||
const prefixedMetrics = this.addPrefixToMetrics(metrics); | ||
res.setHeader('Content-Type', promClient.register.contentType); | ||
res.send(metrics).end(); | ||
res.send(prefixedMetrics).end(); | ||
}); | ||
} | ||
|
||
private addPrefixToMetrics(metrics: string) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could we make this public and add unit tests just for this method? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is covered by integration tests containing the |
||
return metrics | ||
.split('\n') | ||
.map((rawLine) => { | ||
const line = rawLine.trim(); | ||
|
||
if (!line || line.startsWith('#') || line.startsWith(this.prefix)) return rawLine; | ||
|
||
return this.prefix + line; | ||
}) | ||
.join('\n'); | ||
} | ||
|
||
/** | ||
* Set up cache metrics: `n8n_cache_hits_total`, `n8n_cache_misses_total`, and | ||
* `n8n_cache_updates_total` | ||
|
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,14 @@ | ||
export type MetricCategory = 'default' | 'routes' | 'cache' | 'logs'; | ||
|
||
export type MetricLabel = | ||
| 'credentialsType' | ||
| 'nodeType' | ||
| 'workflowId' | ||
| 'apiPath' | ||
| 'apiMethod' | ||
| 'apiStatusCode'; | ||
|
||
export type Includes = { | ||
metrics: Record<MetricCategory, boolean>; | ||
labels: Record<MetricLabel, boolean>; | ||
}; |
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
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.
is this a breaking change 🤔
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.
How do you mean? This is an internal implementation detail, the schema path and the env remain the same.
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.
does this not change the metric name that gets scraped by prometheus?
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.
Oh that's what you mean. Since we advertise this as experimental I take it this is okay to change, but I can add a mention just in case.