Skip to content

Commit

Permalink
refactor: adjust naming
Browse files Browse the repository at this point in the history
Co-authored-by: Alexander Knipping <[email protected]>
  • Loading branch information
costela and obitech committed Nov 9, 2023
1 parent ae3723c commit 2f24eed
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Simple low-overhead circuit breaker library.
## Usage

```go
h, _ := hoglet.NewCircuit(
h, err := hoglet.NewCircuit(
func(ctx context.Context, bar int) (Foo, error) {
if bar == 42 {
return Foo{Bar: bar}, nil
Expand All @@ -19,6 +19,8 @@ h, _ := hoglet.NewCircuit(
hoglet.NewSlidingWindowBreaker(10, 0.1),
hoglet.WithFailureCondition(hoglet.IgnoreContextCancelation),
)
/* if err != nil ... */

f, _ := h.Call(context.Background(), 42)
fmt.Println(f.Bar) // 42

Expand Down
15 changes: 8 additions & 7 deletions extensions/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ const (
namespace = "hoglet"
)

// WithPrometheusMetrics returns a [hoglet.BreakerMiddleware] that registers prometheus metrics for the circuit breaker.
// WithPrometheusMetrics returns a [hoglet.BreakerMiddleware] that registers prometheus metrics for the circuit.
//
// ⚠️ Note: the provided name must be unique across all hoglet instances using the same registerer.
func WithPrometheusMetrics(name string, reg prometheus.Registerer) hoglet.BreakerMiddleware {
func WithPrometheusMetrics(circuitName string, reg prometheus.Registerer) hoglet.BreakerMiddleware {
return func(next hoglet.ObserverFactory) (hoglet.ObserverFactory, error) {
callDurations := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Expand All @@ -27,10 +27,10 @@ func WithPrometheusMetrics(name string, reg prometheus.Registerer) hoglet.Breake
Name: "call_durations_seconds",
Help: "Call durations in seconds",
ConstLabels: prometheus.Labels{
"circuit": name,
"circuit": circuitName,
},
},
[]string{"error"},
[]string{"success"},
)

droppedCalls := prometheus.NewCounterVec(
Expand All @@ -40,7 +40,7 @@ func WithPrometheusMetrics(name string, reg prometheus.Registerer) hoglet.Breake
Name: "dropped_calls_total",
Help: "Total number of calls with an open circuit (i.e.: calls that did not reach the wrapped function)",
ConstLabels: prometheus.Labels{
"circuit": name,
"circuit": circuitName,
},
},
[]string{"cause"},
Expand All @@ -53,7 +53,7 @@ func WithPrometheusMetrics(name string, reg prometheus.Registerer) hoglet.Breake
Name: "inflight_calls_current",
Help: "Current number of calls in-flight",
ConstLabels: prometheus.Labels{
"circuit": name,
"circuit": circuitName,
},
},
)
Expand Down Expand Up @@ -99,7 +99,8 @@ func (pos *prometheusObserverFactory) ObserverForCall(ctx context.Context, state
start := pos.timesource.Now()
pos.inflightCalls.Inc()
return hoglet.ObserverFunc(func(b bool) {
pos.callDurations.WithLabelValues(strconv.FormatBool(b)).Observe(pos.timesource.Since(start).Seconds())
// invert failure → success to make the metric more intuitive
pos.callDurations.WithLabelValues(strconv.FormatBool(!b)).Observe(pos.timesource.Since(start).Seconds())
pos.inflightCalls.Dec()
o.Observe(b)
}), nil
Expand Down

0 comments on commit 2f24eed

Please sign in to comment.