From dc4e6ea43c406c43a0f4ce55e5eac61346e82453 Mon Sep 17 00:00:00 2001 From: Yuri Shkuro Date: Sun, 29 May 2022 19:01:52 -0400 Subject: [PATCH] tests Signed-off-by: Yuri Shkuro --- cmd/collector/app/handler/otlp_receiver.go | 56 ++++++++++++--- .../app/handler/otlp_receiver_test.go | 68 +++++++++++++++++++ 2 files changed, 116 insertions(+), 8 deletions(-) diff --git a/cmd/collector/app/handler/otlp_receiver.go b/cmd/collector/app/handler/otlp_receiver.go index 31be44633e5b..41da6163bf48 100644 --- a/cmd/collector/app/handler/otlp_receiver.go +++ b/cmd/collector/app/handler/otlp_receiver.go @@ -21,6 +21,9 @@ import ( otlp2jaeger "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/jaeger" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config" + "go.opentelemetry.io/collector/config/configgrpc" + "go.opentelemetry.io/collector/config/confighttp" + "go.opentelemetry.io/collector/config/configtls" "go.opentelemetry.io/collector/consumer" "go.opentelemetry.io/collector/pdata/ptrace" "go.opentelemetry.io/collector/receiver/otlpreceiver" @@ -30,6 +33,7 @@ import ( "github.com/jaegertracing/jaeger/cmd/collector/app/flags" "github.com/jaegertracing/jaeger/cmd/collector/app/processor" "github.com/jaegertracing/jaeger/model" + "github.com/jaegertracing/jaeger/pkg/config/tlscfg" ) var _ component.Host = (*otelHost)(nil) // API check @@ -61,14 +65,8 @@ func startOTLPReceiver( cfg config.Receiver, nextConsumer consumer.Traces) (component.TracesReceiver, error), ) (component.TracesReceiver, error) { otlpReceiverConfig := otlpFactory.CreateDefaultConfig().(*otlpreceiver.Config) - if options.OTLP.GRPC.HostPort != "" { - otlpReceiverConfig.GRPC.NetAddr.Endpoint = options.OTLP.GRPC.HostPort - // TODO pass other options - } - if options.OTLP.HTTP.HostPort != "" { - otlpReceiverConfig.HTTP.Endpoint = options.OTLP.HTTP.HostPort - // TODO pass other options - } + applyGRPCSettings(otlpReceiverConfig.GRPC, &options.OTLP.GRPC) + applyHTTPSettings(otlpReceiverConfig.HTTP, &options.OTLP.HTTP) otlpReceiverSettings := component.ReceiverCreateSettings{ TelemetrySettings: component.TelemetrySettings{ Logger: logger, @@ -97,6 +95,48 @@ func startOTLPReceiver( return otlpReceiver, nil } +func applyGRPCSettings(cfg *configgrpc.GRPCServerSettings, opts *flags.GRPCOptions) { + if opts.HostPort != "" { + cfg.NetAddr.Endpoint = opts.HostPort + } + if opts.TLS.Enabled { + cfg.TLSSetting = applyTLSSettings(&opts.TLS) + } + if opts.MaxReceiveMessageLength > 0 { + cfg.MaxRecvMsgSizeMiB = uint64(opts.MaxReceiveMessageLength / (1024 * 1024)) + } + if opts.MaxConnectionAge != 0 || opts.MaxConnectionAgeGrace != 0 { + cfg.Keepalive = &configgrpc.KeepaliveServerConfig{ + ServerParameters: &configgrpc.KeepaliveServerParameters{ + MaxConnectionAge: opts.MaxConnectionAge, + MaxConnectionAgeGrace: opts.MaxConnectionAgeGrace, + }, + } + } +} + +func applyHTTPSettings(cfg *confighttp.HTTPServerSettings, opts *flags.HTTPOptions) { + if opts.HostPort != "" { + cfg.Endpoint = opts.HostPort + } + if opts.TLS.Enabled { + cfg.TLSSetting = applyTLSSettings(&opts.TLS) + } +} + +func applyTLSSettings(opts *tlscfg.Options) *configtls.TLSServerSetting { + return &configtls.TLSServerSetting{ + TLSSetting: configtls.TLSSetting{ + CAFile: opts.CAPath, + CertFile: opts.CertPath, + KeyFile: opts.KeyPath, + MinVersion: opts.MinVersion, + MaxVersion: opts.MaxVersion, + }, + ClientCAFile: opts.ClientCAPath, + } +} + func newConsumerDelegate(logger *zap.Logger, spanProcessor processor.SpanProcessor) *consumerDelegate { return &consumerDelegate{ batchConsumer: batchConsumer{ diff --git a/cmd/collector/app/handler/otlp_receiver_test.go b/cmd/collector/app/handler/otlp_receiver_test.go index fef7a02ac1d9..57d9d9809bf2 100644 --- a/cmd/collector/app/handler/otlp_receiver_test.go +++ b/cmd/collector/app/handler/otlp_receiver_test.go @@ -18,6 +18,7 @@ import ( "context" "errors" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -29,6 +30,7 @@ import ( "github.com/jaegertracing/jaeger/cmd/collector/app/flags" "github.com/jaegertracing/jaeger/model" + "github.com/jaegertracing/jaeger/pkg/config/tlscfg" "github.com/jaegertracing/jaeger/pkg/testutils" ) @@ -146,3 +148,69 @@ func TestOtelHost(t *testing.T) { assert.Nil(t, host.GetExtensions()) assert.Nil(t, host.GetExporters()) } + +func TestApplyOTLPGRPCServerSettings(t *testing.T) { + otlpFactory := otlpreceiver.NewFactory() + otlpReceiverConfig := otlpFactory.CreateDefaultConfig().(*otlpreceiver.Config) + + grpcOpts := &flags.GRPCOptions{ + HostPort: ":54321", + MaxReceiveMessageLength: 42 * 1024 * 1024, + MaxConnectionAge: 33 * time.Second, + MaxConnectionAgeGrace: 37 * time.Second, + TLS: tlscfg.Options{ + Enabled: true, + CAPath: "ca", + CertPath: "cert", + KeyPath: "key", + ClientCAPath: "clientca", + MinVersion: "1.1", + MaxVersion: "1.3", + }, + } + applyGRPCSettings(otlpReceiverConfig.GRPC, grpcOpts) + out := otlpReceiverConfig.GRPC + assert.Equal(t, out.NetAddr.Endpoint, ":54321") + assert.EqualValues(t, out.MaxRecvMsgSizeMiB, 42) + require.NotNil(t, out.Keepalive) + require.NotNil(t, out.Keepalive.ServerParameters) + assert.Equal(t, out.Keepalive.ServerParameters.MaxConnectionAge, 33*time.Second) + assert.Equal(t, out.Keepalive.ServerParameters.MaxConnectionAgeGrace, 37*time.Second) + require.NotNil(t, out.TLSSetting) + assert.Equal(t, out.TLSSetting.CAFile, "ca") + assert.Equal(t, out.TLSSetting.CertFile, "cert") + assert.Equal(t, out.TLSSetting.KeyFile, "key") + assert.Equal(t, out.TLSSetting.ClientCAFile, "clientca") + assert.Equal(t, out.TLSSetting.MinVersion, "1.1") + assert.Equal(t, out.TLSSetting.MaxVersion, "1.3") +} + +func TestApplyOTLPHTTPServerSettings(t *testing.T) { + otlpFactory := otlpreceiver.NewFactory() + otlpReceiverConfig := otlpFactory.CreateDefaultConfig().(*otlpreceiver.Config) + + httpOpts := &flags.HTTPOptions{ + HostPort: ":12345", + TLS: tlscfg.Options{ + Enabled: true, + CAPath: "ca", + CertPath: "cert", + KeyPath: "key", + ClientCAPath: "clientca", + MinVersion: "1.1", + MaxVersion: "1.3", + }, + } + + applyHTTPSettings(otlpReceiverConfig.HTTP, httpOpts) + + out := otlpReceiverConfig.HTTP + assert.Equal(t, out.Endpoint, ":12345") + require.NotNil(t, out.TLSSetting) + assert.Equal(t, out.TLSSetting.CAFile, "ca") + assert.Equal(t, out.TLSSetting.CertFile, "cert") + assert.Equal(t, out.TLSSetting.KeyFile, "key") + assert.Equal(t, out.TLSSetting.ClientCAFile, "clientca") + assert.Equal(t, out.TLSSetting.MinVersion, "1.1") + assert.Equal(t, out.TLSSetting.MaxVersion, "1.3") +}