From c71afaf31ff960d79cd5f0f91e5f9977fb559f90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=B5nis=20Tiigi?= Date: Mon, 11 Oct 2021 12:23:12 -0700 Subject: [PATCH] allow otlp clients to use existing grpc connection (#2002) Signed-off-by: Tonis Tiigi Co-authored-by: Tyler Yahn --- 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 fb122786042..35a18a4d6bb 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) + ## [1.0.1] - 2021-10-01 ### Fixed 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 9a551217308..c4dc9973e3c 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 {