Skip to content

Feat: Add CounterVec type. #19

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
merged 1 commit into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions ctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ var ErrImplemented = errors.New("there is implemenation already injected")

var ctorImpl InternalNew = nil

// name is dot spearated path
// must be uniqe, use system naming, and unit postfix, examples:
// New returns a Creator. Name is a dot separated path which must be unique.
// Examples:
// ipfs.blockstore.bloomcache.bloom.miss.total
// ipfs.routing.dht.notresuingstream.total
//
// ipfs.blockstore.bloomcache.bloom.miss.total
// ipfs.routing.dht.notresuingstream.total
//
// both arguemnts are obligatory
// Both arguemnts are mandatory.
func New(name, helptext string) Creator {
if ctorImpl == nil {
return &noop{}
Expand All @@ -24,6 +23,8 @@ func New(name, helptext string) Creator {
}
}

// NewCtx is like New but obtains the metric scope from the given
// context.
func NewCtx(ctx context.Context, name, helptext string) Creator {
return New(CtxGetScope(ctx)+"."+name, helptext)
}
Expand Down
17 changes: 14 additions & 3 deletions interface.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
// Package metrics provides a unified interface for different metrics
// backends (prometheus etc.).
package metrics

import (
"time"
)

// Increment only metric
// Counter is a metric that can only be incremented.
type Counter interface {
Inc()
Add(float64) // Only positive
}

// Increse and decrese metric
// CounterVec is a counter with tags.
type CounterVec interface {
IncWithLabelValues(labels ...string)
AddWithLabelValues(n float64, labels ...string)
}

// Gauge is a metric that can be increased and decreased.
type Gauge interface {
Set(float64) // Introduced discontinuity
Inc()
Expand All @@ -19,6 +27,7 @@ type Gauge interface {
Sub(float64)
}

// Histogram metric.
type Histogram interface {
Observe(float64) // Adds observation to Histogram
}
Expand All @@ -27,16 +36,18 @@ type Summary interface {
Observe(float64) // Adds observation to Summary
}

// Consult http://godoc.org/github.com/prometheus/client_golang/prometheus#SummaryOpts
// SummaryOpts allow specifying options for Summary. See: http://godoc.org/github.com/prometheus/client_golang/prometheus#SummaryOpts .
type SummaryOpts struct {
Objectives map[float64]float64
MaxAge time.Duration
AgeBuckets uint32
BufCap uint32
}

// Creator can be used to create different types of metrics.
type Creator interface {
Counter() Counter
CounterVec() CounterVec
Gauge() Gauge
Histogram(buckets []float64) Histogram

Expand Down
14 changes: 13 additions & 1 deletion noop.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package metrics

// Also implements the Counter interface
// Also implements the Counter and CounterVec interfaces
type noop struct{}

func (g *noop) Set(v float64) {
Expand All @@ -27,12 +27,24 @@ func (g *noop) Observe(v float64) {
// Noop
}

func (g *noop) IncWithLabelValues(labels ...string) {
// Noop
}

func (g *noop) AddWithLabelValues(v float64, labels ...string) {
// Noop
}

// Creator functions

func (g *noop) Counter() Counter {
return g
}

func (g *noop) CounterVec() CounterVec {
return g
}

func (g *noop) Gauge() Gauge {
return g
}
Expand Down
1 change: 1 addition & 0 deletions noop_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package metrics

var _ Counter = (*noop)(nil)
var _ CounterVec = (*noop)(nil)
var _ Gauge = (*noop)(nil)
var _ Histogram = (*noop)(nil)
var _ Summary = (*noop)(nil)
Expand Down