Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
Signed-off-by: Yuri Shkuro <[email protected]>
  • Loading branch information
yurishkuro committed May 29, 2022
1 parent 9e18c86 commit dc4e6ea
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 8 deletions.
56 changes: 48 additions & 8 deletions cmd/collector/app/handler/otlp_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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{
Expand Down
68 changes: 68 additions & 0 deletions cmd/collector/app/handler/otlp_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"errors"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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")
}

0 comments on commit dc4e6ea

Please sign in to comment.