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

Add factory and new-style config for Zipkin receiver #40

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
22 changes: 22 additions & 0 deletions receiver/zipkinreceiver/config.go
Original file line number Diff line number Diff line change
@@ -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"`
}
54 changes: 54 additions & 0 deletions receiver/zipkinreceiver/config_test.go
Original file line number Diff line number Diff line change
@@ -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,
},
})
}
79 changes: 79 additions & 0 deletions receiver/zipkinreceiver/factory.go
Original file line number Diff line number Diff line change
@@ -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"
pjanotti marked this conversation as resolved.
Show resolved Hide resolved
)

// 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
}
49 changes: 49 additions & 0 deletions receiver/zipkinreceiver/factory_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
18 changes: 18 additions & 0 deletions receiver/zipkinreceiver/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -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]