-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add prometheus metrics for caches reverting telemetry metrics
- Loading branch information
egonspace
committed
May 11, 2021
1 parent
9717c49
commit 83aa427
Showing
15 changed files
with
300 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package baseapp | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/go-kit/kit/metrics/discard" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMetricsProvider(t *testing.T) { | ||
p1, p2 := MetricsProvider(true) | ||
c1 := p1() | ||
c2 := p2() | ||
require.NotEqual(t, c1.InterBlockCacheHits, discard.NewCounter()) | ||
require.NotEqual(t, c2.IAVLCacheHits, discard.NewGauge()) | ||
|
||
p1, p2 = MetricsProvider(false) | ||
c1 = p1() | ||
c2 = p2() | ||
require.Equal(t, c1.InterBlockCacheHits, discard.NewCounter()) | ||
require.Equal(t, c2.IAVLCacheHits, discard.NewGauge()) | ||
} |
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,84 @@ | ||
package cache | ||
|
||
import ( | ||
"github.com/go-kit/kit/metrics" | ||
"github.com/go-kit/kit/metrics/discard" | ||
"github.com/go-kit/kit/metrics/prometheus" | ||
stdprometheus "github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
const ( | ||
// MetricsSubsystem is a subsystem shared by all metrics exposed by this | ||
// package. | ||
MetricsSubsystem = "inter_block_cache" | ||
) | ||
|
||
// Metrics contains metrics exposed by this package. | ||
type Metrics struct { | ||
InterBlockCacheHits metrics.Counter | ||
InterBlockCacheMisses metrics.Counter | ||
InterBlockCacheEntries metrics.Gauge | ||
InterBlockCacheBytes metrics.Gauge | ||
} | ||
|
||
// PrometheusMetrics returns Metrics build using Prometheus client library. | ||
// Optionally, labels can be provided along with their values ("foo", | ||
// "fooValue"). | ||
func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics { | ||
labels := []string{} | ||
for i := 0; i < len(labelsAndValues); i += 2 { | ||
labels = append(labels, labelsAndValues[i]) | ||
} | ||
return &Metrics{ | ||
InterBlockCacheHits: prometheus.NewCounterFrom(stdprometheus.CounterOpts{ | ||
Namespace: namespace, | ||
Subsystem: MetricsSubsystem, | ||
Name: "hits", | ||
Help: "Cache hits of the inter block cache", | ||
}, labels).With(labelsAndValues...), | ||
InterBlockCacheMisses: prometheus.NewCounterFrom(stdprometheus.CounterOpts{ | ||
Namespace: namespace, | ||
Subsystem: MetricsSubsystem, | ||
Name: "misses", | ||
Help: "Cache misses of the inter block cache", | ||
}, labels).With(labelsAndValues...), | ||
InterBlockCacheEntries: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{ | ||
Namespace: namespace, | ||
Subsystem: MetricsSubsystem, | ||
Name: "entries", | ||
Help: "Cache entry count of the inter block cache", | ||
}, labels).With(labelsAndValues...), | ||
InterBlockCacheBytes: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{ | ||
Namespace: namespace, | ||
Subsystem: MetricsSubsystem, | ||
Name: "bytes_size", | ||
Help: "Cache bytes size of the inter block cache", | ||
}, labels).With(labelsAndValues...), | ||
} | ||
} | ||
|
||
// NopMetrics returns no-op Metrics. | ||
func NopMetrics() *Metrics { | ||
return &Metrics{ | ||
InterBlockCacheHits: discard.NewCounter(), | ||
InterBlockCacheMisses: discard.NewCounter(), | ||
InterBlockCacheEntries: discard.NewGauge(), | ||
InterBlockCacheBytes: discard.NewGauge(), | ||
} | ||
} | ||
|
||
type MetricsProvider func() *Metrics | ||
|
||
// PrometheusMetricsProvider returns PrometheusMetrics for each store | ||
func PrometheusMetricsProvider(namespace string, labelsAndValues ...string) func() *Metrics { | ||
return func() *Metrics { | ||
return PrometheusMetrics(namespace, labelsAndValues...) | ||
} | ||
} | ||
|
||
// NopMetricsProvider returns NopMetrics for each store | ||
func NopMetricsProvider() func() *Metrics { | ||
return func() *Metrics { | ||
return NopMetrics() | ||
} | ||
} |
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,16 @@ | ||
package cache | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/go-kit/kit/metrics/discard" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPrometheusMetrics(t *testing.T) { | ||
metrics := PrometheusMetrics("test") | ||
require.NotEqual(t, metrics.InterBlockCacheHits, discard.NewCounter()) | ||
require.NotEqual(t, metrics.InterBlockCacheMisses, discard.NewCounter()) | ||
require.NotEqual(t, metrics.InterBlockCacheEntries, discard.NewGauge()) | ||
require.NotEqual(t, metrics.InterBlockCacheBytes, discard.NewGauge()) | ||
} |
Oops, something went wrong.