Skip to content
This repository was archived by the owner on Nov 7, 2022. It is now read-only.

Commit 00fa8ee

Browse files
Add implementation of Attributes processor config
- Added factory and config loading for Attributes processor. - Added ability to create processors in ProcessorFactory interface. Testing done: automated tests
1 parent 5129823 commit 00fa8ee

File tree

8 files changed

+275
-24
lines changed

8 files changed

+275
-24
lines changed

internal/configv2/example_factories.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/census-instrumentation/opencensus-service/consumer"
2121
"github.com/census-instrumentation/opencensus-service/internal/configmodels"
2222
"github.com/census-instrumentation/opencensus-service/internal/factories"
23+
"github.com/census-instrumentation/opencensus-service/processor"
2324
"github.com/census-instrumentation/opencensus-service/receiver"
2425
)
2526

@@ -192,6 +193,22 @@ func (f *ExampleProcessorFactory) CreateDefaultConfig() configmodels.Processor {
192193
}
193194
}
194195

196+
// CreateTraceProcessor creates a trace processor based on this config.
197+
func (f *ExampleProcessorFactory) CreateTraceProcessor(
198+
nextConsumer consumer.TraceConsumer,
199+
cfg configmodels.Processor,
200+
) (processor.TraceProcessor, error) {
201+
return nil, factories.ErrDataTypeIsNotSupported
202+
}
203+
204+
// CreateMetricsProcessor creates a metrics processor based on this config.
205+
func (f *ExampleProcessorFactory) CreateMetricsProcessor(
206+
nextConsumer consumer.MetricsConsumer,
207+
cfg configmodels.Processor,
208+
) (processor.MetricsProcessor, error) {
209+
return nil, factories.ErrDataTypeIsNotSupported
210+
}
211+
195212
// RegisterTestFactories registers example factories. This is only used by tests.
196213
func RegisterTestFactories() error {
197214
_ = factories.RegisterReceiverFactory(&ExampleReceiverFactory{})

internal/factories/factories.go

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ import (
2323

2424
"github.com/census-instrumentation/opencensus-service/consumer"
2525
"github.com/census-instrumentation/opencensus-service/internal/configmodels"
26+
"github.com/census-instrumentation/opencensus-service/processor"
2627
"github.com/census-instrumentation/opencensus-service/receiver"
2728
)
2829

2930
///////////////////////////////////////////////////////////////////////////////
3031
// Receiver factory and its registry.
3132

32-
// ReceiverFactory is factory interface for receivers. Note: only configuration-related
33-
// functionality exists for now. We will add more factory functionality in the future.
33+
// ReceiverFactory is factory interface for receivers.
3434
type ReceiverFactory interface {
3535
// Type gets the type of the Receiver created by this factory.
3636
Type() string
@@ -115,32 +115,43 @@ func GetExporterFactory(typeStr string) ExporterFactory {
115115
}
116116

117117
///////////////////////////////////////////////////////////////////////////////
118-
// Option factory and its registry.
118+
// Processor factory and its registry.
119119

120-
// OptionFactory is factory interface for options. Note: only configuration-related
121-
// functionality exists for now. We will add more factory functionality in the future.
122-
type OptionFactory interface {
123-
// Type gets the type of the Option created by this factory.
120+
// ProcessorFactory is factory interface for processors.
121+
type ProcessorFactory interface {
122+
// Type gets the type of the Processor created by this factory.
124123
Type() string
125124

126-
// CreateDefaultConfig creates the default configuration for the Option.
125+
// CreateDefaultConfig creates the default configuration for the Processor.
127126
CreateDefaultConfig() configmodels.Processor
127+
128+
// CreateTraceProcessor creates a trace processor based on this config.
129+
// If the processor type does not support tracing or if the config is not valid
130+
// error will be returned instead.
131+
CreateTraceProcessor(nextConsumer consumer.TraceConsumer,
132+
cfg configmodels.Processor) (processor.TraceProcessor, error)
133+
134+
// CreateMetricsProcessor creates a metrics processor based on this config.
135+
// If the processor type does not support metrics or if the config is not valid
136+
// error will be returned instead.
137+
CreateMetricsProcessor(nextConsumer consumer.MetricsConsumer,
138+
cfg configmodels.Processor) (processor.MetricsProcessor, error)
128139
}
129140

130-
// List of registered option factories.
131-
var optionFactories = make(map[string]OptionFactory)
141+
// List of registered processor factories.
142+
var processorFactories = make(map[string]ProcessorFactory)
132143

133-
// RegisterProcessorFactory registers a option factory.
134-
func RegisterProcessorFactory(factory OptionFactory) error {
135-
if optionFactories[factory.Type()] != nil {
136-
panic(fmt.Sprintf("duplicate option factory %q", factory.Type()))
144+
// RegisterProcessorFactory registers a processor factory.
145+
func RegisterProcessorFactory(factory ProcessorFactory) error {
146+
if processorFactories[factory.Type()] != nil {
147+
panic(fmt.Sprintf("duplicate processor factory %q", factory.Type()))
137148
}
138149

139-
optionFactories[factory.Type()] = factory
150+
processorFactories[factory.Type()] = factory
140151
return nil
141152
}
142153

143-
// GetProcessorFactory gets a option factory by type string.
144-
func GetProcessorFactory(typeStr string) OptionFactory {
145-
return optionFactories[typeStr]
154+
// GetProcessorFactory gets a processor factory by type string.
155+
func GetProcessorFactory(typeStr string) ProcessorFactory {
156+
return processorFactories[typeStr]
146157
}

internal/factories/factories_test.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"context"
1919
"testing"
2020

21+
"github.com/census-instrumentation/opencensus-service/processor"
22+
2123
"github.com/census-instrumentation/opencensus-service/consumer"
2224
"github.com/census-instrumentation/opencensus-service/receiver"
2325

@@ -130,21 +132,37 @@ func TestRegisterExporterFactory(t *testing.T) {
130132
}
131133
}
132134

133-
type ExampleOptionFactory struct {
135+
type ExampleProcessorFactory struct {
134136
}
135137

136-
// Type gets the type of the Option config created by this factory.
137-
func (f *ExampleOptionFactory) Type() string {
138+
// Type gets the type of the Processor config created by this factory.
139+
func (f *ExampleProcessorFactory) Type() string {
138140
return "exampleoption"
139141
}
140142

141143
// CreateDefaultConfig creates the default configuration for the Processor.
142-
func (f *ExampleOptionFactory) CreateDefaultConfig() configmodels.Processor {
144+
func (f *ExampleProcessorFactory) CreateDefaultConfig() configmodels.Processor {
143145
return nil
144146
}
145147

146-
func TestRegisterOptionFactory(t *testing.T) {
147-
f := ExampleOptionFactory{}
148+
// CreateTraceProcessor creates a trace processor based on this config.
149+
func (f *ExampleProcessorFactory) CreateTraceProcessor(
150+
nextConsumer consumer.TraceConsumer,
151+
cfg configmodels.Processor,
152+
) (processor.TraceProcessor, error) {
153+
return nil, ErrDataTypeIsNotSupported
154+
}
155+
156+
// CreateMetricsProcessor creates a metrics processor based on this config.
157+
func (f *ExampleProcessorFactory) CreateMetricsProcessor(
158+
nextConsumer consumer.MetricsConsumer,
159+
cfg configmodels.Processor,
160+
) (processor.MetricsProcessor, error) {
161+
return nil, ErrDataTypeIsNotSupported
162+
}
163+
164+
func TestRegisterProcessorFactory(t *testing.T) {
165+
f := ExampleProcessorFactory{}
148166
err := RegisterProcessorFactory(&f)
149167
if err != nil {
150168
t.Fatalf("cannot register factory")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 addattributesprocessor
16+
17+
import (
18+
"github.com/census-instrumentation/opencensus-service/internal/configmodels"
19+
)
20+
21+
// ConfigV2 defines configuration for Attributes processor.
22+
type ConfigV2 struct {
23+
configmodels.ProcessorSettings `mapstructure:",squash"`
24+
Overwrite bool `mapstructure:"overwrite"`
25+
Values map[string]interface{} `mapstructure:"values"`
26+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 addattributesprocessor
16+
17+
import (
18+
"path"
19+
"testing"
20+
21+
"github.com/stretchr/testify/assert"
22+
"github.com/stretchr/testify/require"
23+
24+
"github.com/census-instrumentation/opencensus-service/internal/configv2"
25+
"github.com/census-instrumentation/opencensus-service/internal/factories"
26+
)
27+
28+
var _ = configv2.RegisterTestFactories()
29+
30+
func TestLoadConfig(t *testing.T) {
31+
32+
factory := factories.GetProcessorFactory(typeStr)
33+
34+
config, err := configv2.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"))
35+
36+
require.Nil(t, err)
37+
require.NotNil(t, config)
38+
39+
p0 := config.Processors["attributes"]
40+
assert.Equal(t, p0, factory.CreateDefaultConfig())
41+
42+
p1 := config.Processors["attributes/2"]
43+
assert.Equal(t, p1,
44+
&ConfigV2{
45+
Values: map[string]interface{}{
46+
"attribute1": 123,
47+
"string attribute": "string value",
48+
"attribute.with.dot": "another value",
49+
},
50+
})
51+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 addattributesprocessor
16+
17+
import (
18+
"github.com/census-instrumentation/opencensus-service/consumer"
19+
"github.com/census-instrumentation/opencensus-service/internal/configmodels"
20+
"github.com/census-instrumentation/opencensus-service/internal/factories"
21+
"github.com/census-instrumentation/opencensus-service/processor"
22+
)
23+
24+
var _ = factories.RegisterProcessorFactory(&processorFactory{})
25+
26+
const (
27+
// The value of "type" key in configuration.
28+
typeStr = "attributes"
29+
)
30+
31+
// processorFactory is the factory for OpenCensus exporter.
32+
type processorFactory struct {
33+
}
34+
35+
// Type gets the type of the Option config created by this factory.
36+
func (f *processorFactory) Type() string {
37+
return typeStr
38+
}
39+
40+
// CreateDefaultConfig creates the default configuration for exporter.
41+
func (f *processorFactory) CreateDefaultConfig() configmodels.Processor {
42+
return &ConfigV2{
43+
Values: map[string]interface{}{},
44+
}
45+
}
46+
47+
// CreateTraceProcessor creates a trace processor based on this config.
48+
func (f *processorFactory) CreateTraceProcessor(
49+
nextConsumer consumer.TraceConsumer,
50+
cfg configmodels.Processor,
51+
) (processor.TraceProcessor, error) {
52+
oCfg := cfg.(*ConfigV2)
53+
return NewTraceProcessor(nextConsumer, WithAttributes(oCfg.Values), WithOverwrite(oCfg.Overwrite))
54+
}
55+
56+
// CreateMetricsProcessor creates a metrics processor based on this config.
57+
func (f *processorFactory) CreateMetricsProcessor(
58+
nextConsumer consumer.MetricsConsumer,
59+
cfg configmodels.Processor,
60+
) (processor.MetricsProcessor, error) {
61+
return nil, factories.ErrDataTypeIsNotSupported
62+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 addattributesprocessor
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/assert"
21+
"github.com/stretchr/testify/require"
22+
23+
"github.com/census-instrumentation/opencensus-service/internal/factories"
24+
)
25+
26+
func TestCreateDefaultConfig(t *testing.T) {
27+
factory := factories.GetProcessorFactory(typeStr)
28+
require.NotNil(t, factory)
29+
30+
cfg := factory.CreateDefaultConfig()
31+
assert.NotNil(t, cfg, "failed to create default config")
32+
}
33+
34+
func TestCreateProcessor(t *testing.T) {
35+
factory := factories.GetProcessorFactory(typeStr)
36+
require.NotNil(t, factory)
37+
38+
cfg := factory.CreateDefaultConfig()
39+
40+
tp, err := factory.CreateTraceProcessor(nil, cfg)
41+
assert.NotNil(t, tp)
42+
assert.NoError(t, err, "cannot create trace processor")
43+
44+
mp, err := factory.CreateMetricsProcessor(nil, cfg)
45+
assert.Nil(t, mp)
46+
assert.Error(t, err, "should not be able to create metric processor")
47+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
receivers:
2+
examplereceiver:
3+
4+
processors:
5+
attributes:
6+
attributes/2:
7+
values:
8+
attribute1: 123
9+
"string attribute": "string value"
10+
"attribute.with.dot": "another value"
11+
12+
exporters:
13+
exampleexporter:
14+
15+
pipelines:
16+
traces:
17+
receivers: [examplereceiver]
18+
processors: [attributes]
19+
exporters: [exampleexporter]

0 commit comments

Comments
 (0)