Skip to content
This repository has been archived by the owner on Dec 8, 2020. It is now read-only.

Commit

Permalink
Update: Add noop metrics delegate for testing
Browse files Browse the repository at this point in the history
This commit adds a noop delegate to instrumentation/metrics that will just do nothing. It will not
register metrics or record them. This is used for testing packages that use Horsehead for
instrumenetation. Prometheus registers everything in a global registry at the moment, so errors can
arise in the lifecycle of tests related to metrics already having been registered. This can create
issues when inspecting the call stack and output logs of tests.
  • Loading branch information
kyleterry committed Feb 10, 2020
1 parent 0f0780e commit 28e8bb7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions instrumentation/metrics/delegates/delegates.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"

"github.com/puppetlabs/horsehead/v2/instrumentation/metrics/collectors"
"github.com/puppetlabs/horsehead/v2/instrumentation/metrics/internal/noop"
"github.com/puppetlabs/horsehead/v2/instrumentation/metrics/internal/prometheus"
)

Expand All @@ -22,13 +23,16 @@ type DelegateType string
const (
// PrometheusDelegate is a const that represents the prometheus backend
PrometheusDelegate DelegateType = "prometheus"
NoopDelegate DelegateType = "noop"
)

// New looks up t and returns a new Delegate matching that type
func New(namespace string, t DelegateType) (Delegate, error) {
switch t {
case PrometheusDelegate:
return prometheus.New(namespace), nil
case NoopDelegate:
return noop.New(), nil
}

return nil, errors.New("no delegate found")
Expand Down
29 changes: 29 additions & 0 deletions instrumentation/metrics/internal/noop/noop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package noop

import (
"net/http"

"github.com/puppetlabs/horsehead/v2/instrumentation/metrics/collectors"
)

type Noop struct{}

func (n *Noop) NewCounter(name string, opts collectors.CounterOptions) (collectors.Counter, error) {
return &Counter{}, nil
}

func (n *Noop) NewTimer(name string, opts collectors.TimerOptions) (collectors.Timer, error) {
return &Timer{}, nil
}

func (n *Noop) NewDurationMiddleware(name string, opts collectors.DurationMiddlewareOptions) (collectors.DurationMiddleware, error) {
return &DurationMiddleware{}, nil
}

func (n *Noop) NewHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
}

func New() *Noop {
return &Noop{}
}

0 comments on commit 28e8bb7

Please sign in to comment.