Skip to content

Commit

Permalink
HTTP client metrics classify request option
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewesteves committed Jan 20, 2025
1 parent 9aa101a commit 4bfa4e4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
6 changes: 5 additions & 1 deletion httpclient/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ type Opts struct {

// MetricsCollector is a metrics collector.
MetricsCollector MetricsCollector

// ClassifyRequest does request classification, producing non-parameterized summary for given request.
ClassifyRequest func(r *http.Request, clientType string) string
}

// NewWithOpts wraps delegate transports with options
Expand Down Expand Up @@ -96,7 +99,8 @@ func NewWithOpts(cfg *Config, opts Opts) (*http.Client, error) {

if cfg.Metrics.Enabled {
delegate = NewMetricsRoundTripperWithOpts(delegate, opts.MetricsCollector, MetricsRoundTripperOpts{
ClientType: opts.ClientType,
ClientType: opts.ClientType,
ClassifyRequest: opts.ClassifyRequest,
})
}

Expand Down
43 changes: 43 additions & 0 deletions httpclient/httpclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"

"github.com/acronis/go-appkit/httpserver/middleware"
"github.com/acronis/go-appkit/log/logtest"
"github.com/acronis/go-appkit/testutil"
)

func TestNewHTTPClientLoggingRoundTripper(t *testing.T) {
Expand Down Expand Up @@ -147,3 +150,43 @@ func TestMustHTTPClientWithOptsRoundTripperPolicy(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 2, retriesCount)
}

func TestMustHTTPClientWithOptsRoundTripperMetrics(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusOK)
}))
defer server.Close()

collector := NewPrometheusMetricsCollector("")
defer collector.Unregister()

clientType := "test-client-type"
classifyRequest := "test-classify-request"
cfg := NewConfig()
cfg.Metrics.Enabled = true
client := MustNewWithOpts(cfg, Opts{
UserAgent: "test-agent",
ClientType: clientType,
MetricsCollector: collector,
ClassifyRequest: func(r *http.Request, clientType string) string {
return classifyRequest
},
})

req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, server.URL, nil)
require.NoError(t, err)

r, err := client.Do(req)
defer func() { _ = r.Body.Close() }()
require.NoError(t, err)

labels := prometheus.Labels{
"client_type": clientType,
"remote_address": strings.ReplaceAll(server.URL, "http://", ""),
"summary": classifyRequest,
"request_type": "",
"status": "200",
}
hist := collector.Durations.With(labels).(prometheus.Histogram)
testutil.AssertSamplesCountInHistogram(t, hist, 1)
}

0 comments on commit 4bfa4e4

Please sign in to comment.