Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run OTEL collector without configuration file #2148

Merged
merged 7 commits into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions cmd/opentelemetry-collector/app/defaults/default_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright (c) 2020 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package defaults

import (
"fmt"
"strings"

"github.com/open-telemetry/opentelemetry-collector/config"
"github.com/open-telemetry/opentelemetry-collector/config/configmodels"
"github.com/open-telemetry/opentelemetry-collector/processor/batchprocessor"
"github.com/open-telemetry/opentelemetry-collector/receiver"
"github.com/open-telemetry/opentelemetry-collector/receiver/jaegerreceiver"

"github.com/jaegertracing/jaeger/cmd/opentelemetry-collector/app/exporter/cassandra"
"github.com/jaegertracing/jaeger/cmd/opentelemetry-collector/app/exporter/elasticsearch"
"github.com/jaegertracing/jaeger/cmd/opentelemetry-collector/app/exporter/kafka"
)

// Config creates default configuration.
// It enables default Jaeger receivers, processors and exporters.
func Config(storageType string, factories config.Factories) (*configmodels.Config, error) {
exporters, err := createExporters(storageType, factories)
if err != nil {
return nil, err
}
types := []string{}
for _, v := range exporters {
types = append(types, v.Type())
}
return &configmodels.Config{
Receivers: createReceivers(factories),
Exporters: exporters,
Processors: createProcessors(factories),
Service: configmodels.Service{
Pipelines: map[string]*configmodels.Pipeline{
"traces": {
InputType: configmodels.TracesDataType,
Receivers: []string{"jaeger"},
Exporters: types,
Processors: []string{"batch"},
},
},
},
}, nil
}

func createReceivers(factories config.Factories) configmodels.Receivers {
rec := factories.Receivers["jaeger"].CreateDefaultConfig().(*jaegerreceiver.Config)
// TODO load and serve sampling strategies
// TODO bind sampling strategies file
rec.Protocols = map[string]*receiver.SecureReceiverSettings{
"grpc": {
ReceiverSettings: configmodels.ReceiverSettings{
Endpoint: "localhost:14250",
},
},
"thrift_http": {
ReceiverSettings: configmodels.ReceiverSettings{
Endpoint: "localhost:14268",
},
},
"thrift_compact": {
ReceiverSettings: configmodels.ReceiverSettings{
Endpoint: "localhost:6831",
},
},
"thrift_binary": {
ReceiverSettings: configmodels.ReceiverSettings{
Endpoint: "localhost:6832",
},
},
}
return map[string]configmodels.Receiver{
"jaeger": rec,
}
}

func createExporters(storageTypes string, factories config.Factories) (configmodels.Exporters, error) {
exporters := configmodels.Exporters{}
for _, s := range strings.Split(storageTypes, ",") {
switch s {
case "cassandra":
cass := factories.Exporters[cassandra.TypeStr].CreateDefaultConfig()
exporters[cassandra.TypeStr] = cass
case "elasticsearch":
es := factories.Exporters[elasticsearch.TypeStr].CreateDefaultConfig()
exporters[elasticsearch.TypeStr] = es
case "kafka":
kaf := factories.Exporters[kafka.TypeStr].CreateDefaultConfig()
exporters[kafka.TypeStr] = kaf
default:
return nil, fmt.Errorf("unknown storage type: %s", s)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use a switch case here and error out on a bad storage type here, instead of passing it down to main.go and handling it in storageFlags()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on the switch, the storageFlags from main also parses the variable but it has a different functionality

}
return exporters, nil
}

func createProcessors(factories config.Factories) configmodels.Processors {
batch := factories.Processors["batch"].CreateDefaultConfig().(*batchprocessor.Config)
return map[string]configmodels.Processor{
"batch": batch,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did we decide this list of default processors? I'd like to think queued_retry is also part of this list as it reduces data loss. Which brings about a more important question of - what are the use cases of running the otel-collector without a configuration file? In what scenario would we want to do that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point - the other reason for using queued_retry is that it will have some of the metrics we are interested in using.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have used the batch as a starting point since there has to be at least one processor. In the production-ready settings we might run batch, retry and memory limiter. Let's handle this in a separate task.

Which brings about a more important question of - what are the use cases of running the otel-collector without a configuration file? In what scenario would we want to do that?

The highest priority we had was to be a drop-in replacement for the current collector. The OTEL collector fails without the config file which is not compatible behavior with our collector.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there has to be at least one processor

IIRC, this is no longer true, but yes I agree, let's start with the batch processor and merge this soon.

}
}
121 changes: 121 additions & 0 deletions cmd/opentelemetry-collector/app/defaults/default_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright (c) 2020 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package defaults

import (
"sort"
"testing"

"github.com/open-telemetry/opentelemetry-collector/config"
"github.com/open-telemetry/opentelemetry-collector/config/configmodels"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"

"github.com/jaegertracing/jaeger/cmd/opentelemetry-collector/app/exporter/cassandra"
"github.com/jaegertracing/jaeger/cmd/opentelemetry-collector/app/exporter/elasticsearch"
"github.com/jaegertracing/jaeger/cmd/opentelemetry-collector/app/exporter/kafka"
)

func TestDefaultConfig(t *testing.T) {
factories := Components(viper.New())
tests := []struct {
storageType string
exporterTypes []string
pipeline map[string]*configmodels.Pipeline
err string
}{
{
storageType: "elasticsearch",
exporterTypes: []string{elasticsearch.TypeStr},
pipeline: map[string]*configmodels.Pipeline{
"traces": {
InputType: configmodels.TracesDataType,
Receivers: []string{"jaeger"},
Exporters: []string{elasticsearch.TypeStr},
Processors: []string{"batch"},
},
},
},
{
storageType: "cassandra",
exporterTypes: []string{cassandra.TypeStr},
pipeline: map[string]*configmodels.Pipeline{
"traces": {
InputType: configmodels.TracesDataType,
Receivers: []string{"jaeger"},
Exporters: []string{cassandra.TypeStr},
Processors: []string{"batch"},
},
},
},
{
storageType: "kafka",
exporterTypes: []string{kafka.TypeStr},
pipeline: map[string]*configmodels.Pipeline{
"traces": {
InputType: configmodels.TracesDataType,
Receivers: []string{"jaeger"},
Exporters: []string{kafka.TypeStr},
Processors: []string{"batch"},
},
},
},
{
storageType: "cassandra,elasticsearch",
exporterTypes: []string{cassandra.TypeStr, elasticsearch.TypeStr},
pipeline: map[string]*configmodels.Pipeline{
"traces": {
InputType: configmodels.TracesDataType,
Receivers: []string{"jaeger"},
Exporters: []string{cassandra.TypeStr, elasticsearch.TypeStr},
Processors: []string{"batch"},
},
},
},
{
storageType: "floppy",
err: "unknown storage type: floppy",
},
}
for _, test := range tests {
t.Run(test.storageType, func(t *testing.T) {
cfg, err := Config(test.storageType, factories)
if test.err != "" {
require.Nil(t, cfg)
assert.EqualError(t, err, test.err)
return
}
require.NoError(t, err)
require.NoError(t, config.ValidateConfig(cfg, zap.NewNop()))

assert.Equal(t, 1, len(cfg.Receivers))
assert.Equal(t, "jaeger", cfg.Receivers["jaeger"].Name())
assert.Equal(t, 1, len(cfg.Processors))
assert.Equal(t, "batch", cfg.Processors["batch"].Name())
assert.Equal(t, len(test.exporterTypes), len(cfg.Exporters))

types := []string{}
for _, v := range cfg.Exporters {
types = append(types, v.Type())
}
sort.Strings(types)
assert.Equal(t, test.exporterTypes, types)
assert.EqualValues(t, test.pipeline, cfg.Service.Pipelines)
})
}

}
20 changes: 20 additions & 0 deletions cmd/opentelemetry-collector/app/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
package defaults

import (
"flag"

"github.com/open-telemetry/opentelemetry-collector/config"
"github.com/open-telemetry/opentelemetry-collector/defaults"
"github.com/spf13/pflag"
"github.com/spf13/viper"

"github.com/jaegertracing/jaeger/cmd/opentelemetry-collector/app/exporter/cassandra"
Expand All @@ -29,6 +32,12 @@ import (

// Components creates default and Jaeger factories
func Components(v *viper.Viper) config.Factories {
// Add flags to viper to make the default values available.
// OTEL collector creates the default configuration in service.New() to validate that
// factories can create config.
// However, at this point the Jaeger storage flags are not added to viper.
// The Jaeger storage flags are added to cobra and then to viper in main after service.New().
initViper(v)
kafkaExp := kafka.Factory{OptionsFactory: func() *storageKafka.Options {
opts := kafka.DefaultOptions()
opts.InitFromViper(v)
Expand All @@ -51,3 +60,14 @@ func Components(v *viper.Viper) config.Factories {
factories.Exporters[esExp.Type()] = esExp
return factories
}

// initViper adds Jaeger storage flags to viper to make the default values available.
func initViper(v *viper.Viper) {
flagSet := &flag.FlagSet{}
kafka.DefaultOptions().AddFlags(flagSet)
elasticsearch.DefaultOptions().AddFlags(flagSet)
cassandra.DefaultOptions().AddFlags(flagSet)
pflagSet := &pflag.FlagSet{}
pflagSet.AddGoFlagSet(flagSet)
v.BindPFlags(pflagSet)
}
3 changes: 1 addition & 2 deletions cmd/opentelemetry-collector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ replace github.com/jaegertracing/jaeger => ./../../
require (
github.com/Shopify/sarama v1.22.2-0.20190604114437-cd910a683f9f
github.com/census-instrumentation/opencensus-proto v0.2.1
github.com/crossdock/crossdock-go v0.0.0-20160816171116-049aabb0122b
github.com/gogo/protobuf v1.3.0
github.com/jaegertracing/jaeger v1.17.0
github.com/magiconair/properties v1.8.1
github.com/open-telemetry/opentelemetry-collector v0.2.8-0.20200323151927-794a2b689bd9
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.6.2
github.com/stretchr/testify v1.5.0
github.com/uber/jaeger-lib v2.2.0+incompatible
Expand Down
Loading