From cb0bc0cbb4e8b3ff36f639a0fc8c8c59dc3e4960 Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Mon, 14 Jun 2021 11:03:15 -0700 Subject: [PATCH] allow otlp clients to use existing grpc connection Signed-off-by: Tonis Tiigi --- CHANGELOG.md | 4 ++++ .../otlp/otlpmetric/internal/connection/connection.go | 4 ++++ exporters/otlp/otlpmetric/internal/otlpconfig/options.go | 1 + exporters/otlp/otlpmetric/otlpmetricgrpc/options.go | 8 ++++++++ .../otlp/otlptrace/internal/connection/connection.go | 4 ++++ exporters/otlp/otlptrace/internal/otlpconfig/options.go | 1 + exporters/otlp/otlptrace/otlptracegrpc/options.go | 8 ++++++++ 7 files changed, 30 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2cb46459cc5..81cc34e5d9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] +### Added + +- Adds `otlptracegrpc.WithGRPCConn` and `otlpmetricgrpc.WithGRPCConn` for reusing existing gRPC connection. (#2002) + ### Changed - NoopMeterProvider is now private and NewNoopMeterProvider must be used to obtain a noopMeterProvider. (#2237) diff --git a/exporters/otlp/otlpmetric/internal/connection/connection.go b/exporters/otlp/otlpmetric/internal/connection/connection.go index edfd07d34a1..4ace51f2201 100644 --- a/exporters/otlp/otlpmetric/internal/connection/connection.go +++ b/exporters/otlp/otlpmetric/internal/connection/connection.go @@ -220,6 +220,10 @@ func (c *Connection) setConnection(cc *grpc.ClientConn) bool { } func (c *Connection) dialToCollector(ctx context.Context) (*grpc.ClientConn, error) { + if c.cfg.GRPCConn != nil { + return c.cfg.GRPCConn, nil + } + dialOpts := []grpc.DialOption{} if c.cfg.ServiceConfig != "" { dialOpts = append(dialOpts, grpc.WithDefaultServiceConfig(c.cfg.ServiceConfig)) diff --git a/exporters/otlp/otlpmetric/internal/otlpconfig/options.go b/exporters/otlp/otlpmetric/internal/otlpconfig/options.go index cc46fc775ca..b5fb9ce2e67 100644 --- a/exporters/otlp/otlpmetric/internal/otlpconfig/options.go +++ b/exporters/otlp/otlpmetric/internal/otlpconfig/options.go @@ -75,6 +75,7 @@ type ( ReconnectionPeriod time.Duration ServiceConfig string DialOptions []grpc.DialOption + GRPCConn *grpc.ClientConn RetrySettings RetrySettings } ) diff --git a/exporters/otlp/otlpmetric/otlpmetricgrpc/options.go b/exporters/otlp/otlpmetric/otlpmetricgrpc/options.go index 57b1a2ec08b..6b64145dd5e 100644 --- a/exporters/otlp/otlpmetric/otlpmetricgrpc/options.go +++ b/exporters/otlp/otlpmetric/otlpmetricgrpc/options.go @@ -116,6 +116,14 @@ func WithDialOption(opts ...grpc.DialOption) Option { })} } +// WithGRPCConn allows reusing existing gRPC connection when it has already been +// established for other services. When set, other dial options will be ignored. +func WithGRPCConn(conn *grpc.ClientConn) Option { + return wrappedOption{otlpconfig.NewGRPCOption(func(cfg *otlpconfig.Config) { + cfg.GRPCConn = conn + })} +} + // WithTimeout tells the client the max waiting time for the backend to process // each metrics batch. If unset, the default will be 10 seconds. func WithTimeout(duration time.Duration) Option { diff --git a/exporters/otlp/otlptrace/internal/connection/connection.go b/exporters/otlp/otlptrace/internal/connection/connection.go index b2b94731fd6..67e06888c89 100644 --- a/exporters/otlp/otlptrace/internal/connection/connection.go +++ b/exporters/otlp/otlptrace/internal/connection/connection.go @@ -221,6 +221,10 @@ func (c *Connection) setConnection(cc *grpc.ClientConn) bool { } func (c *Connection) dialToCollector(ctx context.Context) (*grpc.ClientConn, error) { + if c.cfg.GRPCConn != nil { + return c.cfg.GRPCConn, nil + } + dialOpts := []grpc.DialOption{} if c.cfg.ServiceConfig != "" { dialOpts = append(dialOpts, grpc.WithDefaultServiceConfig(c.cfg.ServiceConfig)) diff --git a/exporters/otlp/otlptrace/internal/otlpconfig/options.go b/exporters/otlp/otlptrace/internal/otlpconfig/options.go index 0ec2c879eae..8e21250c674 100644 --- a/exporters/otlp/otlptrace/internal/otlpconfig/options.go +++ b/exporters/otlp/otlptrace/internal/otlpconfig/options.go @@ -58,6 +58,7 @@ type ( ReconnectionPeriod time.Duration ServiceConfig string DialOptions []grpc.DialOption + GRPCConn *grpc.ClientConn } ) diff --git a/exporters/otlp/otlptrace/otlptracegrpc/options.go b/exporters/otlp/otlptrace/otlptracegrpc/options.go index d2cf76461cc..86bfc1dc8a5 100644 --- a/exporters/otlp/otlptrace/otlptracegrpc/options.go +++ b/exporters/otlp/otlptrace/otlptracegrpc/options.go @@ -116,6 +116,14 @@ func WithDialOption(opts ...grpc.DialOption) Option { })} } +// WithGRPCConn allows reusing existing gRPC connection when it has already been +// established for other services. When set, other dial options will be ignored. +func WithGRPCConn(conn *grpc.ClientConn) Option { + return wrappedOption{otlpconfig.NewGRPCOption(func(cfg *otlpconfig.Config) { + cfg.GRPCConn = conn + })} +} + // WithTimeout tells the driver the max waiting time for the backend to process // each spans batch. If unset, the default will be 10 seconds. func WithTimeout(duration time.Duration) Option {