forked from cometbft/cometbft
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(internal/metrics): copy metrics package from go-kit (cometbft#3989)
We're only using a handful of lines, so it makes sense to copy to reduce the dependency surface. The last commit in go-kit was 6 months ago, and it seems abandoned [go-kit/kit](https://github.com/go-kit/kit). <!-- Please add a reference to the issue that this PR addresses and indicate which files are most critical to review. If it fully addresses a particular issue, please include "Closes #XXX" (where "XXX" is the issue number). If this PR is non-trivial/large/complex, please ensure that you have either created an issue that the team's had a chance to respond to, or had some discussion with the team prior to submitting substantial pull requests. The team can be reached via GitHub Discussions or the Cosmos Network Discord server in the #cometbft channel. GitHub Discussions is preferred over Discord as it allows us to keep track of conversations topically. https://github.com/cometbft/cometbft/discussions If the work in this PR is not aligned with the team's current priorities, please be advised that it may take some time before it is merged - especially if it has not yet been discussed with the team. See the project board for the team's current priorities: https://github.com/orgs/cometbft/projects/1 --> --- #### PR checklist - [x] Tests written/updated - [ ] Changelog entry added in `.changelog` (we use [unclog](https://github.com/informalsystems/unclog) to manage our changelog) - [ ] Updated relevant documentation (`docs/` or `spec/`) and code comments --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
015f455
commit 6f7d85e
Showing
38 changed files
with
1,030 additions
and
52 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,62 @@ | ||
// The MIT License (MIT) | ||
|
||
// Copyright (c) 2015 Peter Bourgon | ||
|
||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
|
||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
|
||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
// Package discard provides a no-op metrics backend. | ||
package discard | ||
|
||
import "github.com/cometbft/cometbft/libs/metrics" | ||
|
||
type counter struct{} | ||
|
||
// NewCounter returns a new no-op counter. | ||
func NewCounter() metrics.Counter { return counter{} } | ||
|
||
// With implements Counter. | ||
func (c counter) With(...string) metrics.Counter { return c } | ||
|
||
// Add implements Counter. | ||
func (counter) Add(float64) {} | ||
|
||
type gauge struct{} | ||
|
||
// NewGauge returns a new no-op gauge. | ||
func NewGauge() metrics.Gauge { return gauge{} } | ||
|
||
// With implements Gauge. | ||
func (g gauge) With(...string) metrics.Gauge { return g } | ||
|
||
// Set implements Gauge. | ||
func (gauge) Set(float64) {} | ||
|
||
// Add implements metrics.Gauge. | ||
func (gauge) Add(float64) {} | ||
|
||
type histogram struct{} | ||
|
||
// NewHistogram returns a new no-op histogram. | ||
func NewHistogram() metrics.Histogram { return histogram{} } | ||
|
||
// With implements Histogram. | ||
func (h histogram) With(...string) metrics.Histogram { return h } | ||
|
||
// Observe implements histogram. | ||
func (histogram) Observe(float64) {} |
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,118 @@ | ||
// The MIT License (MIT) | ||
|
||
// Copyright (c) 2015 Peter Bourgon | ||
|
||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
|
||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
|
||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
// Package metrics provides a framework for application instrumentation. It's | ||
// primarily designed to help you get started with good and robust | ||
// instrumentation, and to help you migrate from a less-capable system like | ||
// Graphite to a more-capable system like Prometheus. If your organization has | ||
// already standardized on an instrumentation system like Prometheus, and has no | ||
// plans to change, it may make sense to use that system's instrumentation | ||
// library directly. | ||
// | ||
// This package provides three core metric abstractions (Counter, Gauge, and | ||
// Histogram) and implementations for almost all common instrumentation | ||
// backends. Each metric has an observation method (Add, Set, or Observe, | ||
// respectively) used to record values, and a With method to "scope" the | ||
// observation by various parameters. For example, you might have a Histogram to | ||
// record request durations, parameterized by the method that's being called. | ||
// | ||
// var requestDuration metrics.Histogram | ||
// // ... | ||
// requestDuration.With("method", "MyMethod").Observe(time.Since(begin)) | ||
// | ||
// This allows a single high-level metrics object (requestDuration) to work with | ||
// many code paths somewhat dynamically. The concept of With is fully supported | ||
// in some backends like Prometheus, and not supported in other backends like | ||
// Graphite. So, With may be a no-op, depending on the concrete implementation | ||
// you choose. Please check the implementation to know for sure. For | ||
// implementations that don't provide With, it's necessary to fully parameterize | ||
// each metric in the metric name, e.g. | ||
// | ||
// // Statsd | ||
// c := statsd.NewCounter("request_duration_MyMethod_200") | ||
// c.Add(1) | ||
// | ||
// // Prometheus | ||
// c := prometheus.NewCounter(stdprometheus.CounterOpts{ | ||
// Name: "request_duration", | ||
// ... | ||
// }, []string{"method", "status_code"}) | ||
// c.With("method", "MyMethod", "status_code", strconv.Itoa(code)).Add(1) | ||
// | ||
// # Usage | ||
// | ||
// Metrics are dependencies, and should be passed to the components that need | ||
// them in the same way you'd construct and pass a database handle, or reference | ||
// to another component. Metrics should *not* be created in the global scope. | ||
// Instead, instantiate metrics in your func main, using whichever concrete | ||
// implementation is appropriate for your organization. | ||
// | ||
// latency := prometheus.NewSummaryFrom(stdprometheus.SummaryOpts{ | ||
// Namespace: "myteam", | ||
// Subsystem: "foosvc", | ||
// Name: "request_latency_seconds", | ||
// Help: "Incoming request latency in seconds.", | ||
// }, []string{"method", "status_code"}) | ||
// | ||
// Write your components to take the metrics they will use as parameters to | ||
// their constructors. Use the interface types, not the concrete types. That is, | ||
// | ||
// // NewAPI takes metrics.Histogram, not *prometheus.Summary | ||
// func NewAPI(s Store, logger log.Logger, latency metrics.Histogram) *API { | ||
// // ... | ||
// } | ||
// | ||
// func (a *API) ServeFoo(w http.ResponseWriter, r *http.Request) { | ||
// begin := time.Now() | ||
// // ... | ||
// a.latency.Observe(time.Since(begin).Seconds()) | ||
// } | ||
// | ||
// Finally, pass the metrics as dependencies when building your object graph. | ||
// This should happen in func main, not in the global scope. | ||
// | ||
// api := NewAPI(store, logger, latency) | ||
// http.ListenAndServe("/", api) | ||
// | ||
// Note that metrics are "write-only" interfaces. | ||
// | ||
// # Implementation details | ||
// | ||
// All metrics are safe for concurrent use. Considerable design influence has | ||
// been taken from https://github.com/codahale/metrics and | ||
// https://prometheus.io. | ||
// | ||
// Each telemetry system has different semantics for label values, push vs. | ||
// pull, support for histograms, etc. These properties influence the design of | ||
// their respective packages. This table attempts to summarize the key points of | ||
// distinction. | ||
// | ||
// SYSTEM DIM COUNTERS GAUGES HISTOGRAMS | ||
// dogstatsd n batch, push-aggregate batch, push-aggregate native, batch, push-each | ||
// statsd 1 batch, push-aggregate batch, push-aggregate native, batch, push-each | ||
// graphite 1 batch, push-aggregate batch, push-aggregate synthetic, batch, push-aggregate | ||
// expvar 1 atomic atomic synthetic, batch, in-place expose | ||
// influx n custom custom custom | ||
// prometheus n native native native | ||
// pcp 1 native native native | ||
// cloudwatch n batch push-aggregate batch push-aggregate synthetic, batch, push-aggregate | ||
package metrics |
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,36 @@ | ||
// The MIT License (MIT) | ||
|
||
// Copyright (c) 2015 Peter Bourgon | ||
|
||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
|
||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
|
||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
package lv | ||
|
||
// LabelValues is a type alias that provides validation on its With method. | ||
// Metrics may include it as a member to help them satisfy With semantics and | ||
// save some code duplication. | ||
type LabelValues []string | ||
|
||
// With validates the input, and returns a new aggregate labelValues. | ||
func (lvs LabelValues) With(labelValues ...string) LabelValues { | ||
if len(labelValues)%2 != 0 { | ||
labelValues = append(labelValues, "unknown") | ||
} | ||
return append(lvs, labelValues...) | ||
} |
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,44 @@ | ||
// The MIT License (MIT) | ||
|
||
// Copyright (c) 2015 Peter Bourgon | ||
|
||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
|
||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
|
||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
package lv | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestWith(t *testing.T) { | ||
var a LabelValues | ||
b := a.With("a", "1") | ||
c := a.With("b", "2", "c", "3") | ||
|
||
if want, have := "", strings.Join(a, ""); want != have { | ||
t.Errorf("With appears to mutate the original LabelValues: want %q, have %q", want, have) | ||
} | ||
if want, have := "a1", strings.Join(b, ""); want != have { | ||
t.Errorf("With does not appear to return the right thing: want %q, have %q", want, have) | ||
} | ||
if want, have := "b2c3", strings.Join(c, ""); want != have { | ||
t.Errorf("With does not appear to return the right thing: want %q, have %q", want, have) | ||
} | ||
} |
Oops, something went wrong.