From 8e633da5cf753df7d06f591381581b86b1849ade Mon Sep 17 00:00:00 2001 From: Tigran Najaryan <4194920+tigrannajaryan@users.noreply.github.com> Date: Fri, 21 Jun 2019 10:52:21 -0400 Subject: [PATCH] Add factory and new-style config for Zipkin receiver (#40) This is part of remaining migration to new configuration format. Github issue: https://github.com/open-telemetry/opentelemetry-service/issues/34 Testing done: make --- receiver/zipkinreceiver/config.go | 22 ++++++ receiver/zipkinreceiver/config_test.go | 54 +++++++++++++ receiver/zipkinreceiver/factory.go | 79 ++++++++++++++++++++ receiver/zipkinreceiver/factory_test.go | 49 ++++++++++++ receiver/zipkinreceiver/testdata/config.yaml | 18 +++++ 5 files changed, 222 insertions(+) create mode 100644 receiver/zipkinreceiver/config.go create mode 100644 receiver/zipkinreceiver/config_test.go create mode 100644 receiver/zipkinreceiver/factory.go create mode 100644 receiver/zipkinreceiver/factory_test.go create mode 100644 receiver/zipkinreceiver/testdata/config.yaml diff --git a/receiver/zipkinreceiver/config.go b/receiver/zipkinreceiver/config.go new file mode 100644 index 00000000000..f31305a0a2b --- /dev/null +++ b/receiver/zipkinreceiver/config.go @@ -0,0 +1,22 @@ +// Copyright 2019, OpenCensus 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 zipkinreceiver + +import "github.com/open-telemetry/opentelemetry-service/internal/configmodels" + +// ConfigV2 defines configuration for Zipkin receiver. +type ConfigV2 struct { + configmodels.ReceiverSettings `mapstructure:",squash"` +} diff --git a/receiver/zipkinreceiver/config_test.go b/receiver/zipkinreceiver/config_test.go new file mode 100644 index 00000000000..7136262a6d8 --- /dev/null +++ b/receiver/zipkinreceiver/config_test.go @@ -0,0 +1,54 @@ +// Copyright 2019, OpenCensus 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 zipkinreceiver + +import ( + "path" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/open-telemetry/opentelemetry-service/internal/configmodels" + "github.com/open-telemetry/opentelemetry-service/internal/configv2" + "github.com/open-telemetry/opentelemetry-service/internal/factories" +) + +var _ = configv2.RegisterTestFactories() + +func TestLoadConfig(t *testing.T) { + factory := factories.GetReceiverFactory(typeStr) + + config, err := configv2.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml")) + + require.NoError(t, err) + require.NotNil(t, config) + + assert.Equal(t, len(config.Receivers), 2) + + r0 := config.Receivers["zipkin"] + assert.Equal(t, r0, factory.CreateDefaultConfig()) + + r1 := config.Receivers["zipkin/customname"].(*ConfigV2) + assert.Equal(t, r1, + &ConfigV2{ + ReceiverSettings: configmodels.ReceiverSettings{ + TypeVal: typeStr, + NameVal: "zipkin/customname", + Endpoint: "127.0.0.1:8765", + Enabled: true, + }, + }) +} diff --git a/receiver/zipkinreceiver/factory.go b/receiver/zipkinreceiver/factory.go new file mode 100644 index 00000000000..36b82c974f2 --- /dev/null +++ b/receiver/zipkinreceiver/factory.go @@ -0,0 +1,79 @@ +// Copyright 2019, OpenCensus 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 zipkinreceiver + +import ( + "context" + + "github.com/open-telemetry/opentelemetry-service/consumer" + "github.com/open-telemetry/opentelemetry-service/internal/configmodels" + "github.com/open-telemetry/opentelemetry-service/internal/factories" + "github.com/open-telemetry/opentelemetry-service/receiver" +) + +// This file implements factory for Zipkin receiver. + +var _ = factories.RegisterReceiverFactory(&ReceiverFactory{}) + +const ( + // The value of "type" key in configuration. + typeStr = "zipkin" + + defaultBindEndpoint = "127.0.0.1:9411" +) + +// ReceiverFactory is the factory for Zipkin receiver. +type ReceiverFactory struct { +} + +// Type gets the type of the Receiver config created by this factory. +func (f *ReceiverFactory) Type() string { + return typeStr +} + +// CustomUnmarshaler returns nil because we don't need custom unmarshaling for this config. +func (f *ReceiverFactory) CustomUnmarshaler() factories.CustomUnmarshaler { + return nil +} + +// CreateDefaultConfig creates the default configuration for Jaeger receiver. +func (f *ReceiverFactory) CreateDefaultConfig() configmodels.Receiver { + return &ConfigV2{ + ReceiverSettings: configmodels.ReceiverSettings{ + TypeVal: typeStr, + NameVal: typeStr, + Endpoint: defaultBindEndpoint, + }, + } +} + +// CreateTraceReceiver creates a trace receiver based on provided config. +func (f *ReceiverFactory) CreateTraceReceiver( + ctx context.Context, + cfg configmodels.Receiver, + nextConsumer consumer.TraceConsumer, +) (receiver.TraceReceiver, error) { + + rCfg := cfg.(*ConfigV2) + return New(rCfg.Endpoint, nextConsumer) +} + +// CreateMetricsReceiver creates a metrics receiver based on provided config. +func (f *ReceiverFactory) CreateMetricsReceiver( + cfg configmodels.Receiver, + consumer consumer.MetricsConsumer, +) (receiver.MetricsReceiver, error) { + return nil, factories.ErrDataTypeIsNotSupported +} diff --git a/receiver/zipkinreceiver/factory_test.go b/receiver/zipkinreceiver/factory_test.go new file mode 100644 index 00000000000..37aabce9f85 --- /dev/null +++ b/receiver/zipkinreceiver/factory_test.go @@ -0,0 +1,49 @@ +// Copyright 2019, OpenCensus 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 zipkinreceiver + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/open-telemetry/opentelemetry-service/data" + "github.com/open-telemetry/opentelemetry-service/internal/factories" +) + +func TestCreateDefaultConfig(t *testing.T) { + factory := factories.GetReceiverFactory(typeStr) + cfg := factory.CreateDefaultConfig() + assert.NotNil(t, cfg, "failed to create default config") +} + +type mockTraceConsumer struct { +} + +func (m *mockTraceConsumer) ConsumeTraceData(ctx context.Context, td data.TraceData) error { return nil } + +func TestCreateReceiver(t *testing.T) { + factory := factories.GetReceiverFactory(typeStr) + cfg := factory.CreateDefaultConfig() + + tReceiver, err := factory.CreateTraceReceiver(context.Background(), cfg, &mockTraceConsumer{}) + assert.Nil(t, err, "receiver creation failed") + assert.NotNil(t, tReceiver, "receiver creation failed") + + mReceiver, err := factory.CreateMetricsReceiver(cfg, nil) + assert.Equal(t, err, factories.ErrDataTypeIsNotSupported) + assert.Nil(t, mReceiver) +} diff --git a/receiver/zipkinreceiver/testdata/config.yaml b/receiver/zipkinreceiver/testdata/config.yaml new file mode 100644 index 00000000000..bf6e7af9c88 --- /dev/null +++ b/receiver/zipkinreceiver/testdata/config.yaml @@ -0,0 +1,18 @@ +receivers: + zipkin: + zipkin/customname: + endpoint: "127.0.0.1:8765" + enabled: true + +processors: + exampleprocessor: + +exporters: + exampleexporter: + +pipelines: + traces: + receivers: [zipkin] + processors: [exampleprocessor] + exporters: [exampleexporter] +