Skip to content

Commit 8e633da

Browse files
Add factory and new-style config for Zipkin receiver (#40)
This is part of remaining migration to new configuration format. Github issue: #34 Testing done: make
1 parent 08eae6f commit 8e633da

File tree

5 files changed

+222
-0
lines changed

5 files changed

+222
-0
lines changed

receiver/zipkinreceiver/config.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2019, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package zipkinreceiver
16+
17+
import "github.com/open-telemetry/opentelemetry-service/internal/configmodels"
18+
19+
// ConfigV2 defines configuration for Zipkin receiver.
20+
type ConfigV2 struct {
21+
configmodels.ReceiverSettings `mapstructure:",squash"`
22+
}
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2019, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package zipkinreceiver
16+
17+
import (
18+
"path"
19+
"testing"
20+
21+
"github.com/stretchr/testify/assert"
22+
"github.com/stretchr/testify/require"
23+
24+
"github.com/open-telemetry/opentelemetry-service/internal/configmodels"
25+
"github.com/open-telemetry/opentelemetry-service/internal/configv2"
26+
"github.com/open-telemetry/opentelemetry-service/internal/factories"
27+
)
28+
29+
var _ = configv2.RegisterTestFactories()
30+
31+
func TestLoadConfig(t *testing.T) {
32+
factory := factories.GetReceiverFactory(typeStr)
33+
34+
config, err := configv2.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"))
35+
36+
require.NoError(t, err)
37+
require.NotNil(t, config)
38+
39+
assert.Equal(t, len(config.Receivers), 2)
40+
41+
r0 := config.Receivers["zipkin"]
42+
assert.Equal(t, r0, factory.CreateDefaultConfig())
43+
44+
r1 := config.Receivers["zipkin/customname"].(*ConfigV2)
45+
assert.Equal(t, r1,
46+
&ConfigV2{
47+
ReceiverSettings: configmodels.ReceiverSettings{
48+
TypeVal: typeStr,
49+
NameVal: "zipkin/customname",
50+
Endpoint: "127.0.0.1:8765",
51+
Enabled: true,
52+
},
53+
})
54+
}

receiver/zipkinreceiver/factory.go

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2019, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package zipkinreceiver
16+
17+
import (
18+
"context"
19+
20+
"github.com/open-telemetry/opentelemetry-service/consumer"
21+
"github.com/open-telemetry/opentelemetry-service/internal/configmodels"
22+
"github.com/open-telemetry/opentelemetry-service/internal/factories"
23+
"github.com/open-telemetry/opentelemetry-service/receiver"
24+
)
25+
26+
// This file implements factory for Zipkin receiver.
27+
28+
var _ = factories.RegisterReceiverFactory(&ReceiverFactory{})
29+
30+
const (
31+
// The value of "type" key in configuration.
32+
typeStr = "zipkin"
33+
34+
defaultBindEndpoint = "127.0.0.1:9411"
35+
)
36+
37+
// ReceiverFactory is the factory for Zipkin receiver.
38+
type ReceiverFactory struct {
39+
}
40+
41+
// Type gets the type of the Receiver config created by this factory.
42+
func (f *ReceiverFactory) Type() string {
43+
return typeStr
44+
}
45+
46+
// CustomUnmarshaler returns nil because we don't need custom unmarshaling for this config.
47+
func (f *ReceiverFactory) CustomUnmarshaler() factories.CustomUnmarshaler {
48+
return nil
49+
}
50+
51+
// CreateDefaultConfig creates the default configuration for Jaeger receiver.
52+
func (f *ReceiverFactory) CreateDefaultConfig() configmodels.Receiver {
53+
return &ConfigV2{
54+
ReceiverSettings: configmodels.ReceiverSettings{
55+
TypeVal: typeStr,
56+
NameVal: typeStr,
57+
Endpoint: defaultBindEndpoint,
58+
},
59+
}
60+
}
61+
62+
// CreateTraceReceiver creates a trace receiver based on provided config.
63+
func (f *ReceiverFactory) CreateTraceReceiver(
64+
ctx context.Context,
65+
cfg configmodels.Receiver,
66+
nextConsumer consumer.TraceConsumer,
67+
) (receiver.TraceReceiver, error) {
68+
69+
rCfg := cfg.(*ConfigV2)
70+
return New(rCfg.Endpoint, nextConsumer)
71+
}
72+
73+
// CreateMetricsReceiver creates a metrics receiver based on provided config.
74+
func (f *ReceiverFactory) CreateMetricsReceiver(
75+
cfg configmodels.Receiver,
76+
consumer consumer.MetricsConsumer,
77+
) (receiver.MetricsReceiver, error) {
78+
return nil, factories.ErrDataTypeIsNotSupported
79+
}
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2019, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package zipkinreceiver
16+
17+
import (
18+
"context"
19+
"testing"
20+
21+
"github.com/stretchr/testify/assert"
22+
23+
"github.com/open-telemetry/opentelemetry-service/data"
24+
"github.com/open-telemetry/opentelemetry-service/internal/factories"
25+
)
26+
27+
func TestCreateDefaultConfig(t *testing.T) {
28+
factory := factories.GetReceiverFactory(typeStr)
29+
cfg := factory.CreateDefaultConfig()
30+
assert.NotNil(t, cfg, "failed to create default config")
31+
}
32+
33+
type mockTraceConsumer struct {
34+
}
35+
36+
func (m *mockTraceConsumer) ConsumeTraceData(ctx context.Context, td data.TraceData) error { return nil }
37+
38+
func TestCreateReceiver(t *testing.T) {
39+
factory := factories.GetReceiverFactory(typeStr)
40+
cfg := factory.CreateDefaultConfig()
41+
42+
tReceiver, err := factory.CreateTraceReceiver(context.Background(), cfg, &mockTraceConsumer{})
43+
assert.Nil(t, err, "receiver creation failed")
44+
assert.NotNil(t, tReceiver, "receiver creation failed")
45+
46+
mReceiver, err := factory.CreateMetricsReceiver(cfg, nil)
47+
assert.Equal(t, err, factories.ErrDataTypeIsNotSupported)
48+
assert.Nil(t, mReceiver)
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
receivers:
2+
zipkin:
3+
zipkin/customname:
4+
endpoint: "127.0.0.1:8765"
5+
enabled: true
6+
7+
processors:
8+
exampleprocessor:
9+
10+
exporters:
11+
exampleexporter:
12+
13+
pipelines:
14+
traces:
15+
receivers: [zipkin]
16+
processors: [exampleprocessor]
17+
exporters: [exampleexporter]
18+

0 commit comments

Comments
 (0)