-
Notifications
You must be signed in to change notification settings - Fork 26
Expose the Gatherer and use new registry over prometheus default registry #7
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
Open
jhorwit2
wants to merge
1
commit into
docker:main
Choose a base branch
from
jhorwit2:jah/expose-registerer-gatherer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package metrics | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| dto "github.com/prometheus/client_model/go" | ||
| ) | ||
|
|
||
| func TestCounter(t *testing.T) { | ||
| expected := struct { | ||
| Name string | ||
| Help string | ||
| Type dto.MetricType | ||
| Value float64 | ||
| }{ | ||
| "test_counter_counter_total", | ||
| "count all the things", | ||
| dto.MetricType_COUNTER, | ||
| 5, | ||
| } | ||
|
|
||
| ns := NewNamespace("test", "counter", nil) | ||
|
|
||
| c := ns.NewCounter("counter", "count all the things") | ||
| c.Inc(5) | ||
|
|
||
| Register(ns) | ||
| defer Deregister(ns) | ||
|
|
||
| mfs, err := Gather() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| if len(mfs) != 1 { | ||
| t.Fatalf("expected one metric family but got %d", len(mfs)) | ||
| } | ||
|
|
||
| family := mfs[0] | ||
| if *family.Name != expected.Name { | ||
| t.Fatalf("expected name `%s` but got `%s`", expected.Name, *family.Name) | ||
| } | ||
|
|
||
| if *family.Help != expected.Help { | ||
| t.Fatalf("expected help `%s` but got `%s`", expected.Help, *family.Help) | ||
| } | ||
|
|
||
| if *family.Type != expected.Type { | ||
| t.Fatalf("expected type `%d` but got `%d`", expected.Type, *family.Type) | ||
| } | ||
|
|
||
| if len(family.Metric) != 1 { | ||
| t.Fatalf("expected one metric but got %d", len(family.Metric)) | ||
| } | ||
|
|
||
| metric := family.Metric[0] | ||
| value := metric.GetCounter().GetValue() | ||
| if value != expected.Value { | ||
| t.Fatalf("expected counter value %f but got %f", expected.Value, value) | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package metrics | ||
|
|
||
| import ( | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| dto "github.com/prometheus/client_model/go" | ||
| ) | ||
|
|
||
| // Gatherer returns the metric gatherer | ||
| func Gatherer() prometheus.Gatherer { | ||
| return registry | ||
| } | ||
|
|
||
| // Gather calls the gatherer to return all the metric families of | ||
| // every metric in the registry. | ||
| func Gather() ([]*dto.MetricFamily, error) { | ||
| return Gatherer().Gather() | ||
| } | ||
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,15 +1,13 @@ | ||
| package metrics | ||
|
|
||
| import "github.com/prometheus/client_golang/prometheus" | ||
|
|
||
| // Register adds all the metrics in the provided namespace to the global | ||
| // metrics registry | ||
| func Register(n *Namespace) { | ||
| prometheus.MustRegister(n) | ||
| registry.MustRegister(n) | ||
| } | ||
|
|
||
| // Deregister removes all the metrics in the provided namespace from the | ||
| // global metrics registry | ||
| func Deregister(n *Namespace) { | ||
| prometheus.Unregister(n) | ||
| registry.Unregister(n) | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package metrics | ||
|
|
||
| import "github.com/prometheus/client_golang/prometheus" | ||
|
|
||
| var ( | ||
| registry = prometheus.NewRegistry() | ||
| ) |
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.
Ok, so now that these are exposed, how do you see us using them in the projects? Right now everything that is in this package is being used. There are no unused public APIs so I'm just trying to get an idea of how we are going to use this and the below function in our projects.
Uh oh!
There was an error while loading. Please reload this page.
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.
Gatherer()is useful when we want to include the default registry in debug mode to export more metrics. Prometheus has a concept ofGathererswhich is an[]Gathererand merges gatherers inGather(). This would all you to combine the two registries in debug mode to export them all in/metrics.The use case I have for
Gather()is to allow sending metrics between workers & managers for aggregating container stats. Yes, you could always have the managers hit/metrics; however, the majority of those metrics potentially aren't valuable (containers not in services and daemon metrics). This would allow the worker to filter metrics to only tasks & send those over the wire saving bytes. Then you can aggregate all these in the manager and export them or send them back to a daemon fordocker stats