Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement: Add WithDisableConnectionMetrics() to skip high-cardinality TLS metrics #342

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-342.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Add WithDisableConnectionMetrics() to skip high-cardinality TLS metrics
links:
- https://github.com/palantir/conjure-go-runtime/pull/342
3 changes: 2 additions & 1 deletion conjure-go-client/httpclient/client_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type httpClientBuilder struct {
Middlewares []Middleware

DisableMetrics refreshable.Bool
DisableConnMetrics bool
MetricsTagProviders []TagsProvider

// These middleware options are not refreshed anywhere because they are not in ClientConfig,
Expand All @@ -92,7 +93,7 @@ func (b *httpClientBuilder) Build(ctx context.Context, params ...HTTPClientParam
Dialer: refreshingclient.NewRefreshableDialer(ctx, b.DialerParams),
}
transport := refreshingclient.NewRefreshableTransport(ctx, b.TransportParams, b.TLSConfig, dialer)
transport = wrapTransport(transport, newMetricsMiddleware(b.ServiceNameTag, b.MetricsTagProviders, b.DisableMetrics))
transport = wrapTransport(transport, newMetricsMiddleware(b.ServiceNameTag, b.MetricsTagProviders, b.DisableMetrics, b.DisableConnMetrics))
transport = wrapTransport(transport, traceMiddleware{
ServiceName: b.ServiceNameTag.Value(),
CreateRequestSpan: b.CreateRequestSpan,
Expand Down
8 changes: 8 additions & 0 deletions conjure-go-client/httpclient/client_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ func WithMetrics(tagProviders ...TagsProvider) ClientOrHTTPClientParam {
})
}

// WithDisableConnectionMetrics disables detailed metrics about the TLS connection(s) created by the client.
func WithDisableConnectionMetrics() ClientOrHTTPClientParam {
return clientOrHTTPClientParamFunc(func(b *httpClientBuilder) error {
b.DisableConnMetrics = true
return nil
})
}

// WithBytesBufferPool stores a bytes buffer pool on the client for use in encoding request bodies.
// This prevents allocating a new byte buffer for every request.
func WithBytesBufferPool(pool bytesbuffers.Pool) ClientParam {
Expand Down
19 changes: 12 additions & 7 deletions conjure-go-client/httpclient/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ func MetricsMiddleware(serviceName string, tagProviders ...TagsProvider) (Middle
if err != nil {
return nil, werror.Wrap(err, "failed to construct service-name metric tag", werror.SafeParam("serviceName", serviceName))
}
return newMetricsMiddleware(serviceNameTag, tagProviders, nil), nil
return newMetricsMiddleware(serviceNameTag, tagProviders, nil, false), nil
}

func newMetricsMiddleware(serviceNameTag metrics.Tag, tagProviders []TagsProvider, disabled refreshable.Bool) Middleware {
func newMetricsMiddleware(serviceNameTag metrics.Tag, tagProviders []TagsProvider, disabled refreshable.Bool, disableConnMetrics bool) Middleware {
return &metricsMiddleware{
Disabled: disabled,
ServiceNameTag: serviceNameTag,
Expand All @@ -112,13 +112,15 @@ func newMetricsMiddleware(serviceNameTag metrics.Tag, tagProviders []TagsProvide
TagsProviderFunc(tagRequestMethodName),
StaticTagsProvider(metrics.Tags{serviceNameTag}),
),
disableConnMetrics: disableConnMetrics,
}
}

type metricsMiddleware struct {
Disabled refreshable.Bool
ServiceNameTag metrics.Tag
Tags []TagsProvider
Disabled refreshable.Bool
ServiceNameTag metrics.Tag
Tags []TagsProvider
disableConnMetrics bool
}

// RoundTrip will emit counter and timer metrics with the name 'mariner.k8sClient.request'
Expand All @@ -131,8 +133,11 @@ func (h *metricsMiddleware) RoundTrip(req *http.Request, next http.RoundTripper)

metrics.FromContext(req.Context()).Counter(MetricRequestInFlight, h.ServiceNameTag).Inc(1)
start := time.Now()
tlsMetricsContext := h.tlsTraceContext(req.Context())
resp, err := next.RoundTrip(req.WithContext(tlsMetricsContext))
ctx := req.Context()
if !h.disableConnMetrics {
ctx = h.tlsTraceContext(ctx)
}
resp, err := next.RoundTrip(req.WithContext(ctx))
duration := time.Since(start)
metrics.FromContext(req.Context()).Counter(MetricRequestInFlight, h.ServiceNameTag).Dec(1)

Expand Down