Skip to content

Commit cb97e93

Browse files
author
Tigran Najaryan
committed
Add factory and new-style config for Zipkin receiver
This is part of remaining migration to new configuration format. Github issue: open-telemetry#34 Testing done: make
1 parent 08eae6f commit cb97e93

File tree

7 files changed

+228
-0
lines changed

7 files changed

+228
-0
lines changed

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ require (
1818
github.com/grpc-ecosystem/grpc-gateway v1.8.5
1919
github.com/inconshreveable/mousetrap v1.0.0 // indirect
2020
github.com/jaegertracing/jaeger v1.9.0
21+
github.com/mitchellh/mapstructure v1.1.2 // indirect
2122
github.com/omnition/scribe-go v0.0.0-20190131012523-9e3c68f31124
2223
github.com/opentracing/opentracing-go v1.1.0 // indirect
2324
github.com/openzipkin/zipkin-go v0.1.6
@@ -34,6 +35,7 @@ require (
3435
github.com/spf13/cobra v0.0.3
3536
github.com/spf13/viper v1.2.1
3637
github.com/streadway/quantile v0.0.0-20150917103942-b0c588724d25 // indirect
38+
github.com/stretchr/objx v0.1.1 // indirect
3739
github.com/stretchr/testify v1.3.0
3840
github.com/uber-go/atomic v1.3.2 // indirect
3941
github.com/uber/jaeger-client-go v2.16.0+incompatible // indirect

go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ github.com/mitchellh/go-testing-interface v1.0.0 h1:fzU/JVNcaqHQEcVFAKeR41fkiLdI
215215
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
216216
github.com/mitchellh/mapstructure v1.0.0 h1:vVpGvMXJPqSDh2VYHF7gsfQj8Ncx+Xw5Y1KHeTRY+7I=
217217
github.com/mitchellh/mapstructure v1.0.0/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
218+
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
219+
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
218220
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
219221
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
220222
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@@ -324,6 +326,8 @@ github.com/streadway/quantile v0.0.0-20150917103942-b0c588724d25 h1:7z3LSn867ex6
324326
github.com/streadway/quantile v0.0.0-20150917103942-b0c588724d25/go.mod h1:lbP8tGiBjZ5YWIc2fzuRpTaz0b/53vT6PEs3QuAWzuU=
325327
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
326328
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
329+
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
330+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
327331
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
328332
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
329333
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

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 config V2 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)