From 348f970acfd3a5601a772783f84039fff6b52efd 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 | 1 + .../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, 27 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2bef76628f6..dbb93ba8ca0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Several builtin resource detectors now correctly populate the schema URL. (#1938) - Creates package `go.opentelemetry.io/otel/exporters/otlp/otlpmetric` that defines a metrics exporter that uses a `otlpmetric.Client` to send data. Creates package `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc` implementing a gRPC `otlpmetric.Client` and offers convenience functions, `New` and `NewUnstarted`, to create an `otlpmetric.Exporter`.(#1991) +- Adds `otlptracegrpc.WithGRPCConn` and `otlpmetricgrpc.WithGRPCConn` for reusing existing gRPC connection. (#2002) ### Changed 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 9860e2268a5..8bc97e5e6d1 100644 --- a/exporters/otlp/otlpmetric/internal/otlpconfig/options.go +++ b/exporters/otlp/otlpmetric/internal/otlpconfig/options.go @@ -64,6 +64,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 0c6f89fa47e..4489082b55e 100644 --- a/exporters/otlp/otlptrace/internal/connection/connection.go +++ b/exporters/otlp/otlptrace/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/otlptrace/internal/otlpconfig/options.go b/exporters/otlp/otlptrace/internal/otlpconfig/options.go index ef6a22beec2..cce4ec29706 100644 --- a/exporters/otlp/otlptrace/internal/otlpconfig/options.go +++ b/exporters/otlp/otlptrace/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/otlptrace/otlptracegrpc/options.go b/exporters/otlp/otlptrace/otlptracegrpc/options.go index 2a483f7d866..59c6cf13fe6 100644 --- a/exporters/otlp/otlptrace/otlptracegrpc/options.go +++ b/exporters/otlp/otlptrace/otlptracegrpc/options.go @@ -115,6 +115,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 {