Skip to content

Commit

Permalink
configgrpc: Update ToDialOptions with settings (open-telemetry#4451)
Browse files Browse the repository at this point in the history
* `configgrpc`: Update `ToDialOptions` with settings

Adding the ability to pass TelemetrySettings to ToDialOptions to configure underlying instrumentation library.

Fixes open-telemetry#4424

* update changelog

* add telemetrysetting to exporter
  • Loading branch information
Alex Boten authored Nov 19, 2021
1 parent 02187df commit bdcb989
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
## 🛑 Breaking changes 🛑

- Remove `pdata.AttributeMap.InitFromMap` (#4429)
- Updated configgrpc `ToDialOptions` to support passing providers to instrumentation library (#4451)

## v0.39.0 Beta

Expand Down
6 changes: 3 additions & 3 deletions config/configgrpc/configgrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (gcs *GRPCClientSettings) isSchemeHTTPS() bool {
}

// ToDialOptions maps configgrpc.GRPCClientSettings to a slice of dial options for gRPC.
func (gcs *GRPCClientSettings) ToDialOptions(host component.Host) ([]grpc.DialOption, error) {
func (gcs *GRPCClientSettings) ToDialOptions(host component.Host, settings component.TelemetrySettings) ([]grpc.DialOption, error) {
var opts []grpc.DialOption
if gcs.Compression != "" {
if compressionKey := GetGRPCCompressionKey(gcs.Compression); compressionKey != CompressionUnsupported {
Expand Down Expand Up @@ -248,8 +248,8 @@ func (gcs *GRPCClientSettings) ToDialOptions(host component.Host) ([]grpc.DialOp
}

// Enable OpenTelemetry observability plugin.
opts = append(opts, grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor()))
opts = append(opts, grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor()))
opts = append(opts, grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor(otelgrpc.WithTracerProvider(settings.TracerProvider))))
opts = append(opts, grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor(otelgrpc.WithTracerProvider(settings.TracerProvider))))

return opts, nil
}
Expand Down
47 changes: 36 additions & 11 deletions config/configgrpc/configgrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,29 @@ import (
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/model/otlpgrpc"
"go.opentelemetry.io/collector/obsreport/obsreporttest"
)

func TestDefaultGrpcClientSettings(t *testing.T) {
tt, err := obsreporttest.SetupTelemetry()
require.NoError(t, err)
defer tt.Shutdown(context.Background())

gcs := &GRPCClientSettings{
TLSSetting: configtls.TLSClientSetting{
Insecure: true,
},
}
opts, err := gcs.ToDialOptions(componenttest.NewNopHost())
opts, err := gcs.ToDialOptions(componenttest.NewNopHost(), tt.TelemetrySettings)
assert.NoError(t, err)
assert.Len(t, opts, 3)
}

func TestAllGrpcClientSettings(t *testing.T) {
tt, err := obsreporttest.SetupTelemetry()
require.NoError(t, err)
defer tt.Shutdown(context.Background())

gcs := &GRPCClientSettings{
Headers: map[string]string{
"test": "test",
Expand Down Expand Up @@ -75,7 +84,7 @@ func TestAllGrpcClientSettings(t *testing.T) {
},
}

opts, err := gcs.ToDialOptions(host)
opts, err := gcs.ToDialOptions(host, tt.TelemetrySettings)
assert.NoError(t, err)
assert.Len(t, opts, 9)
}
Expand Down Expand Up @@ -149,6 +158,10 @@ func TestGrpcServerAuthSettings(t *testing.T) {
}

func TestGRPCClientSettingsError(t *testing.T) {
tt, err := obsreporttest.SetupTelemetry()
require.NoError(t, err)
defer tt.Shutdown(context.Background())

tests := []struct {
settings GRPCClientSettings
err string
Expand Down Expand Up @@ -227,7 +240,7 @@ func TestGRPCClientSettingsError(t *testing.T) {
}
for _, test := range tests {
t.Run(test.err, func(t *testing.T) {
opts, err := test.settings.ToDialOptions(test.host)
opts, err := test.settings.ToDialOptions(test.host, tt.TelemetrySettings)
assert.Nil(t, opts)
assert.Error(t, err)
assert.Regexp(t, test.err, err)
Expand All @@ -236,14 +249,18 @@ func TestGRPCClientSettingsError(t *testing.T) {
}

func TestUseSecure(t *testing.T) {
tt, err := obsreporttest.SetupTelemetry()
require.NoError(t, err)
defer tt.Shutdown(context.Background())

gcs := &GRPCClientSettings{
Headers: nil,
Endpoint: "",
Compression: "",
TLSSetting: configtls.TLSClientSetting{},
Keepalive: nil,
}
dialOpts, err := gcs.ToDialOptions(componenttest.NewNopHost())
dialOpts, err := gcs.ToDialOptions(componenttest.NewNopHost(), tt.TelemetrySettings)
assert.NoError(t, err)
assert.Len(t, dialOpts, 3)
}
Expand Down Expand Up @@ -352,6 +369,10 @@ func TestGetGRPCCompressionKey(t *testing.T) {
}

func TestHttpReception(t *testing.T) {
tt, err := obsreporttest.SetupTelemetry()
require.NoError(t, err)
defer tt.Shutdown(context.Background())

tests := []struct {
name string
tlsServerCreds *configtls.TLSServerSetting
Expand Down Expand Up @@ -456,14 +477,14 @@ func TestHttpReception(t *testing.T) {
}
// prepare

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
gss := &GRPCServerSettings{
NetAddr: confignet.NetAddr{
Endpoint: "localhost:0",
Transport: "tcp",
},
TLSSetting: tt.tlsServerCreds,
TLSSetting: test.tlsServerCreds,
}
ln, err := gss.ToListener()
assert.NoError(t, err)
Expand All @@ -478,16 +499,16 @@ func TestHttpReception(t *testing.T) {

gcs := &GRPCClientSettings{
Endpoint: ln.Addr().String(),
TLSSetting: *tt.tlsClientCreds,
TLSSetting: *test.tlsClientCreds,
}
clientOpts, errClient := gcs.ToDialOptions(componenttest.NewNopHost())
clientOpts, errClient := gcs.ToDialOptions(componenttest.NewNopHost(), tt.TelemetrySettings)
assert.NoError(t, errClient)
grpcClientConn, errDial := grpc.Dial(gcs.Endpoint, clientOpts...)
assert.NoError(t, errDial)
client := otlpgrpc.NewTracesClient(grpcClientConn)
ctx, cancelFunc := context.WithTimeout(context.Background(), 2*time.Second)
resp, errResp := client.Export(ctx, otlpgrpc.NewTracesRequest(), grpc.WaitForReady(true))
if tt.hasError {
if test.hasError {
assert.Error(t, errResp)
} else {
assert.NoError(t, errResp)
Expand All @@ -503,6 +524,10 @@ func TestReceiveOnUnixDomainSocket(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("skipping test on windows")
}
tt, err := obsreporttest.SetupTelemetry()
require.NoError(t, err)
defer tt.Shutdown(context.Background())

socketName := tempSocketName(t)
gss := &GRPCServerSettings{
NetAddr: confignet.NetAddr{
Expand All @@ -527,7 +552,7 @@ func TestReceiveOnUnixDomainSocket(t *testing.T) {
Insecure: true,
},
}
clientOpts, errClient := gcs.ToDialOptions(componenttest.NewNopHost())
clientOpts, errClient := gcs.ToDialOptions(componenttest.NewNopHost(), tt.TelemetrySettings)
assert.NoError(t, errClient)
grpcClientConn, errDial := grpc.Dial(gcs.Endpoint, clientOpts...)
assert.NoError(t, errDial)
Expand Down
6 changes: 3 additions & 3 deletions exporter/otlpexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func createTracesExporter(
set component.ExporterCreateSettings,
cfg config.Exporter,
) (component.TracesExporter, error) {
oce, err := newExporter(cfg)
oce, err := newExporter(cfg, set.TelemetrySettings)
if err != nil {
return nil, err
}
Expand All @@ -80,7 +80,7 @@ func createMetricsExporter(
set component.ExporterCreateSettings,
cfg config.Exporter,
) (component.MetricsExporter, error) {
oce, err := newExporter(cfg)
oce, err := newExporter(cfg, set.TelemetrySettings)
if err != nil {
return nil, err
}
Expand All @@ -103,7 +103,7 @@ func createLogsExporter(
set component.ExporterCreateSettings,
cfg config.Exporter,
) (component.LogsExporter, error) {
oce, err := newExporter(cfg)
oce, err := newExporter(cfg, set.TelemetrySettings)
if err != nil {
return nil, err
}
Expand Down
8 changes: 5 additions & 3 deletions exporter/otlpexporter/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,26 @@ type exporter struct {
clientConn *grpc.ClientConn
metadata metadata.MD
callOptions []grpc.CallOption

settings component.TelemetrySettings
}

// Crete new exporter and start it. The exporter will begin connecting but
// this function may return before the connection is established.
func newExporter(cfg config.Exporter) (*exporter, error) {
func newExporter(cfg config.Exporter, settings component.TelemetrySettings) (*exporter, error) {
oCfg := cfg.(*Config)

if oCfg.Endpoint == "" {
return nil, errors.New("OTLP exporter config requires an Endpoint")
}

return &exporter{config: oCfg}, nil
return &exporter{config: oCfg, settings: settings}, nil
}

// start actually creates the gRPC connection. The client construction is deferred till this point as this
// is the only place we get hold of Extensions which are required to construct auth round tripper.
func (e *exporter) start(_ context.Context, host component.Host) (err error) {
dialOpts, err := e.config.GRPCClientSettings.ToDialOptions(host)
dialOpts, err := e.config.GRPCClientSettings.ToDialOptions(host, e.settings)
if err != nil {
return err
}
Expand Down

0 comments on commit bdcb989

Please sign in to comment.