Skip to content
Merged
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
34 changes: 32 additions & 2 deletions api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,12 @@ func (c *Client) dialGRPC(ctx context.Context, addr string) error {
dialOpts = append(dialOpts, grpc.WithContextDialer(c.grpcDialer()))
dialOpts = append(dialOpts,
grpc.WithChainUnaryInterceptor(
otelgrpc.UnaryClientInterceptor(),
otelUnaryClientInterceptor(),
metadata.UnaryClientInterceptor,
breaker.UnaryClientInterceptor(cb),
),
grpc.WithChainStreamInterceptor(
otelgrpc.StreamClientInterceptor(),
otelStreamClientInterceptor(),
metadata.StreamClientInterceptor,
breaker.StreamClientInterceptor(cb),
),
Expand All @@ -422,6 +422,36 @@ func (c *Client) dialGRPC(ctx context.Context, addr string) error {
return nil
}

// TODO(noah): Once we upgrade to go 1.21, change invocations of this to
// sync.OnceValue
func onceValue[T any](f func() T) func() T {
var (
value T
once sync.Once
)
return func() T {
once.Do(func() {
value = f()
})
return value
}
}

// We wrap the creation of the otelgrpc interceptors in a sync.Once - this is
// because each time this is called, they create a new underlying metric. If
// something (e.g tbot) is repeatedly creating new clients and closing them,
// then this leads to a memory leak since the underlying metric is not cleaned
// up.
// See https://github.com/gravitational/teleport/issues/30759
// See https://github.com/open-telemetry/opentelemetry-go-contrib/issues/4226
var otelStreamClientInterceptor = onceValue(func() grpc.StreamClientInterceptor {
return otelgrpc.StreamClientInterceptor()
})

var otelUnaryClientInterceptor = onceValue(func() grpc.UnaryClientInterceptor {
return otelgrpc.UnaryClientInterceptor()
})

// ConfigureALPN configures ALPN SNI cluster routing information in TLS settings allowing for
// allowing to dial auth service through Teleport Proxy directly without using SSH Tunnels.
func ConfigureALPN(tlsConfig *tls.Config, clusterName string) *tls.Config {
Expand Down