-
Notifications
You must be signed in to change notification settings - Fork 40
chore: refactor metrics endpoint #216
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
Merged
gabriel-samfira
merged 7 commits into
cloudbase:main
from
mercedes-benz:extended_metrics
Feb 20, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1d8d945
chore: refactor metrics endpoint
bavarianbidi 97f172e
fix: improve metrics collection loop
bavarianbidi 3e025dd
feat: define a default duration for metrics update
bavarianbidi 17d74df
chore: rework prometheus metrics registration
bavarianbidi 0a53b8f
fix: stop metrics collector ticker on ctx.Done
bavarianbidi 2a3e4d6
fix: pass context.TODO by getting admin context
bavarianbidi b1cbfac
fix: switch to context.Background() for adminctx
bavarianbidi 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,50 +1,21 @@ | ||
| package metrics | ||
|
|
||
| import ( | ||
| "log/slog" | ||
| "strconv" | ||
|
|
||
| "github.com/cloudbase/garm/auth" | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| ) | ||
|
|
||
| // CollectOrganizationMetric collects the metrics for the enterprise objects | ||
| func (c *GarmCollector) CollectEnterpriseMetric(ch chan<- prometheus.Metric, hostname string, controllerID string) { | ||
| ctx := auth.GetAdminContext() | ||
|
|
||
| enterprises, err := c.runner.ListEnterprises(ctx) | ||
| if err != nil { | ||
| slog.With(slog.Any("error", err)).ErrorContext(ctx, "listing providers") | ||
| return | ||
| } | ||
|
|
||
| for _, enterprise := range enterprises { | ||
| var ( | ||
| EnterpriseInfo = prometheus.NewGaugeVec(prometheus.GaugeOpts{ | ||
| Namespace: metricsNamespace, | ||
| Subsystem: metricsEnterpriseSubsystem, | ||
| Name: "info", | ||
| Help: "Info of the enterprise", | ||
| }, []string{"name", "id"}) | ||
|
|
||
| enterpriseInfo, err := prometheus.NewConstMetric( | ||
| c.enterpriseInfo, | ||
| prometheus.GaugeValue, | ||
| 1, | ||
| enterprise.Name, // label: name | ||
| enterprise.ID, // label: id | ||
| ) | ||
| if err != nil { | ||
| slog.With(slog.Any("error", err)).ErrorContext(ctx, "cannot collect enterpriseInfo metric") | ||
| continue | ||
| } | ||
| ch <- enterpriseInfo | ||
|
|
||
| enterprisePoolManagerStatus, err := prometheus.NewConstMetric( | ||
| c.enterprisePoolManagerStatus, | ||
| prometheus.GaugeValue, | ||
| bool2float64(enterprise.PoolManagerStatus.IsRunning), | ||
| enterprise.Name, // label: name | ||
| enterprise.ID, // label: id | ||
| strconv.FormatBool(enterprise.PoolManagerStatus.IsRunning), // label: running | ||
| ) | ||
| if err != nil { | ||
| slog.With(slog.Any("error", err)).ErrorContext(ctx, "cannot collect enterprisePoolManagerStatus metric") | ||
| continue | ||
| } | ||
| ch <- enterprisePoolManagerStatus | ||
| } | ||
| } | ||
| EnterprisePoolManagerStatus = prometheus.NewGaugeVec(prometheus.GaugeOpts{ | ||
| Namespace: metricsNamespace, | ||
| Subsystem: metricsEnterpriseSubsystem, | ||
| Name: "pool_manager_status", | ||
| Help: "Status of the enterprise pool manager", | ||
| }, []string{"name", "id", "running"}) | ||
| ) |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,22 +1,13 @@ | ||
| package metrics | ||
|
|
||
| import ( | ||
| "log/slog" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| ) | ||
|
|
||
| func (c *GarmCollector) CollectHealthMetric(ch chan<- prometheus.Metric, hostname string, controllerID string) { | ||
| m, err := prometheus.NewConstMetric( | ||
| c.healthMetric, | ||
| prometheus.GaugeValue, | ||
| 1, | ||
| hostname, | ||
| controllerID, | ||
| ) | ||
| if err != nil { | ||
| slog.With(slog.Any("error", err)).Error("error on creating health metric") | ||
| return | ||
| } | ||
| ch <- m | ||
| } | ||
| var ( | ||
| GarmHealth = prometheus.NewGaugeVec(prometheus.GaugeOpts{ | ||
| Namespace: metricsNamespace, | ||
| Name: "health", | ||
| Help: "Health of the garm", | ||
| }, []string{"metadata_url", "callback_url", "webhook_url", "controller_webhook_url", "controller_id"}) | ||
| ) |
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.
one downside of this implementation (register the metrics here instead of doing it in the
init):for any further metric we want to change, we have to do a e.g.
i'm either thinking of registering the metrics, no matter if
cfg.Metrics.Enableis set or directly dropcfg.Metrics.Enableand always rungarmwith/metrics.WDYT @gabriel-samfira ?
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.
Wouldn't we just add it to the
RegisterMetrics()function and it gets registered here?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.
In essence,
RegisterMetrics()would act like theinit()function, but would be capable of returning an error we can potentially handle. So instead of using theinit()function, we define our own and call it here to register the metrics.If metrics are enabled, we register them and start the collector loop.