Skip to content

Commit

Permalink
Feat: Add CounterVec type.
Browse files Browse the repository at this point in the history
Also, fix comments and staticcheck warnings.
  • Loading branch information
hsanjuan committed Feb 18, 2025
1 parent 830797e commit 3d8a163
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
4 changes: 2 additions & 2 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package metrics

import "context"

type ctxScopeKey struct{}
type CtxScopeKey struct{}

Check failure on line 5 in context.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go next)

other declaration of CtxScopeKey

Check failure on line 5 in context.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go next)

other declaration of CtxScopeKey

Check failure on line 5 in context.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go this)

other declaration of CtxScopeKey

Check failure on line 5 in context.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go this)

other declaration of CtxScopeKey

Check failure on line 5 in context.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go next)

other declaration of CtxScopeKey

Check failure on line 5 in context.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go next)

other declaration of CtxScopeKey

Check failure on line 5 in context.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go this)

other declaration of CtxScopeKey

Check failure on line 5 in context.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go this)

other declaration of CtxScopeKey

Check failure on line 5 in context.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go next)

other declaration of CtxScopeKey

Check failure on line 5 in context.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go next)

other declaration of CtxScopeKey

Check failure on line 5 in context.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go this)

other declaration of CtxScopeKey

Check failure on line 5 in context.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go this)

other declaration of CtxScopeKey

var CtxScopeKey = ctxScopeKey{}
const CtxScopeKey = ctxScopeKey

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go next)

CtxScopeKey redeclared in this block

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go next)

undefined: ctxScopeKey

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go next)

CtxScopeKey redeclared in this block

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go next)

undefined: ctxScopeKey

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go this)

CtxScopeKey redeclared in this block

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go this)

undefined: ctxScopeKey

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go this)

CtxScopeKey redeclared in this block

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go this)

undefined: ctxScopeKey

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go next)

CtxScopeKey redeclared in this block

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go next)

undefined: ctxScopeKey

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go next)

CtxScopeKey redeclared in this block

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go next)

undefined: ctxScopeKey

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go this)

CtxScopeKey redeclared in this block

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go this)

undefined: ctxScopeKey

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go this)

CtxScopeKey redeclared in this block

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go this)

undefined: ctxScopeKey

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-check / All

CtxScopeKey redeclared in this block

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-check / All

undefined: ctxScopeKey

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-check / All

CtxScopeKey redeclared in this block

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-check / All

undefined: ctxScopeKey

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go next)

CtxScopeKey redeclared in this block

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go next)

undefined: ctxScopeKey

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go next)

CtxScopeKey redeclared in this block

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go next)

undefined: ctxScopeKey

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go this)

CtxScopeKey redeclared in this block

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go this)

undefined: ctxScopeKey

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go this)

CtxScopeKey redeclared in this block

Check failure on line 7 in context.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go this)

undefined: ctxScopeKey

func CtxGetScope(ctx context.Context) string {
s := ctx.Value(CtxScopeKey)

Check failure on line 10 in context.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go next)

CtxScopeKey (type) is not an expression

Check failure on line 10 in context.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go next)

CtxScopeKey (type) is not an expression

Check failure on line 10 in context.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go this)

CtxScopeKey (type) is not an expression

Check failure on line 10 in context.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go this)

CtxScopeKey (type) is not an expression

Check failure on line 10 in context.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go next)

CtxScopeKey (type) is not an expression

Check failure on line 10 in context.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go next)

CtxScopeKey (type) is not an expression

Check failure on line 10 in context.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go this)

CtxScopeKey (type) is not an expression

Check failure on line 10 in context.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go this)

CtxScopeKey (type) is not an expression

Check failure on line 10 in context.go

View workflow job for this annotation

GitHub Actions / go-check / All

CtxScopeKey (type) is not an expression

Check failure on line 10 in context.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go next)

CtxScopeKey (type) is not an expression

Check failure on line 10 in context.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go next)

CtxScopeKey (type) is not an expression

Check failure on line 10 in context.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go this)

CtxScopeKey (type) is not an expression

Check failure on line 10 in context.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go this)

CtxScopeKey (type) is not an expression
Expand Down
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

0 comments on commit 3d8a163

Please sign in to comment.