Skip to content

Commit

Permalink
httpserver: simplify WithMetrics usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
clambin committed Apr 8, 2023
1 parent 4e9f82e commit 21a35cf
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 30 deletions.
3 changes: 2 additions & 1 deletion httpserver/doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package httpserver_test
import (
"errors"
"github.com/clambin/go-common/httpserver"
"github.com/clambin/go-common/httpserver/middleware"
"github.com/prometheus/client_golang/prometheus"
"net/http"
)
Expand All @@ -11,7 +12,7 @@ func Example() {
s, err := httpserver.New(
httpserver.WithAddr(":8080"),
httpserver.WithPrometheus(""),
httpserver.WithMetrics("", "", "example", httpserver.Summary, nil),
httpserver.WithMetrics(middleware.PrometheusMetricsOptions{Application: "example", MetricsType: middleware.Summary}),
httpserver.WithHandlers([]httpserver.Handler{{
Path: "/",
Methods: []string{http.MethodGet},
Expand Down
29 changes: 2 additions & 27 deletions httpserver/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,8 @@ func WithHandlers(handlers []Handler) Option {
}

// WithMetrics will collect the specified metrics to instrument the Server's Handlers.
func WithMetrics(namespace, subsystem, application string, metricsType MetricsType, buckets []float64) Option {
func WithMetrics(metrics middleware.PrometheusMetricsOptions) Option {
return func(s *Server) {
// TODO: this is ugly is f*ck
mwOptions := middleware.PrometheusMetricsOptions{
Namespace: namespace,
Subsystem: subsystem,
Application: application,
Buckets: buckets,
}
switch metricsType {
case Summary:
mwOptions.MetricsType = middleware.Summary
case Histogram:
mwOptions.MetricsType = middleware.Histogram
}
s.instrumentedHandler = middleware.NewPrometheusMetrics(mwOptions)
s.instrumentedHandler = middleware.NewPrometheusMetrics(metrics)
}
}

// MetricsType specifies the type of metrics to record for request duration. Use Summary if you are only interested in the average latency.
// Use Histogram if you want to use a histogram to measure a service level indicator (eg latency of 95% of all requests).
type MetricsType int

const (
// Summary measures the average duration.
Summary MetricsType = iota
// Histogram measures the latency in buckets and can be used to calculate a service level indicator. WithMetrics.Buckets
// specify the buckets to be used. If none are provided, prometheus.DefBuckets will be used.
Histogram
)
5 changes: 3 additions & 2 deletions httpserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"github.com/clambin/go-common/httpserver"
"github.com/clambin/go-common/httpserver/middleware"
"github.com/prometheus/client_golang/prometheus"
io_prometheus_client "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -120,13 +121,13 @@ func TestServer_ServerHTTP_WithMetrics(t *testing.T) {
}{
{
name: "histogram",
metrics: httpserver.WithMetrics("", "", "foobar", httpserver.Histogram, nil),
metrics: httpserver.WithMetrics(middleware.PrometheusMetricsOptions{Application: "foobar", MetricsType: middleware.Histogram}),
evalCount: evalRequestsCounter,
evalDuration: evalDurationHistogram,
},
{
name: "summary",
metrics: httpserver.WithMetrics("", "", "foobar", httpserver.Summary, nil),
metrics: httpserver.WithMetrics(middleware.PrometheusMetricsOptions{Application: "foobar", MetricsType: middleware.Summary}),
evalCount: evalRequestsCounter,
evalDuration: evalDurationSummary,
},
Expand Down

0 comments on commit 21a35cf

Please sign in to comment.