Skip to content

Commit c931b98

Browse files
Refactor component and factory interface definitions (#683)
As we are preparing for Beta release we want to cleanup publicly exported types and interfaces and do all necessary refactoring and breaking changes now, before the Beta release. We will have a lot less leeway for breaking changes after the Beta release. Component related type declarations are now all in the `component` package. This makes it possible for the interfaces to reference each other. This was were very restricted earlier because component interfaces were in 5 different packages and many proposals were impossible to implement because they would result in circular dependencies between packages. (An example upcoming new capability that is enabled by this refactoring is for components to query the Host for other components and for factories). List of changes in this commit: - Move all factory interfaces and component interfaces to component package. - Rename old factories and components interfaces to use "Old" suffix for clarity. - Eliminate forced checks that components implement factories. This is already enforced by the compiler when the factory is added to the Defaults() and was unnecessary code. - Eliminated some unnecessary codes (removed overall over 200 lines). - Run `go mod tidy` on testbed. Warning: this is a breaking change to publicly exported types and function signatures. We announced that a breaking change is comming. Once we agree to merge this commit we will need to announce the exact list of changes and guide component authors to modify their components accordingly. Future changes: - Once all components are migrated to the new internal representation, delete all "Old"-suffixed definitions. This will likely be done while we are still in Beta phase, before the Stable release.
1 parent 9b61b53 commit c931b98

File tree

121 files changed

+883
-1098
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

121 files changed

+883
-1098
lines changed

component/component.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,9 @@ type Host interface {
4141
// operations.
4242
Context() context.Context
4343
}
44+
45+
// Factory interface must be implemented by all component factories.
46+
type Factory interface {
47+
// Type gets the type of the component created by this factory.
48+
Type() string
49+
}
Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,60 @@
1-
// Copyright 2019, OpenTelemetry Authors
1+
// Copyright OpenTelemetry Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
55
// You may obtain a copy of the License at
66
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
7+
// http://www.apache.org/licenses/LICENSE-2.0
88
//
99
// Unless required by applicable law or agreed to in writing, software
1010
// distributed under the License is distributed on an "AS IS" BASIS,
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package exporter
15+
package component
1616

1717
import (
1818
"context"
19-
"fmt"
2019

2120
"go.uber.org/zap"
2221

2322
"github.com/open-telemetry/opentelemetry-collector/config/configmodels"
23+
"github.com/open-telemetry/opentelemetry-collector/consumer"
2424
)
2525

26-
// BaseFactory defines the common functions for all exporter factories.
27-
type BaseFactory interface {
28-
// Type gets the type of the Exporter created by this factory.
29-
Type() string
26+
// Exporter defines functions that trace and metric exporters must implement.
27+
type Exporter interface {
28+
Component
29+
}
30+
31+
// TraceExporterOld is a TraceConsumer that is also an Exporter.
32+
type TraceExporterOld interface {
33+
consumer.TraceConsumerOld
34+
Exporter
35+
}
36+
37+
// TraceExporter is an TraceConsumer that is also an Exporter.
38+
type TraceExporter interface {
39+
consumer.TraceConsumer
40+
Exporter
41+
}
42+
43+
// MetricsExporterOld is a MetricsConsumer that is also an Exporter.
44+
type MetricsExporterOld interface {
45+
consumer.MetricsConsumerOld
46+
Exporter
47+
}
48+
49+
// MetricsExporter is a MetricsConsumer that is also an Exporter.
50+
type MetricsExporter interface {
51+
consumer.MetricsConsumer
52+
Exporter
53+
}
54+
55+
// ExporterFactoryBase defines the common functions for all exporter factories.
56+
type ExporterFactoryBase interface {
57+
Factory
3058

3159
// CreateDefaultConfig creates the default configuration for the Exporter.
3260
// This method can be called multiple times depending on the pipeline
@@ -38,50 +66,38 @@ type BaseFactory interface {
3866
CreateDefaultConfig() configmodels.Exporter
3967
}
4068

41-
// Factory can create TraceExporter and MetricsExporter.
42-
type Factory interface {
43-
BaseFactory
69+
// ExporterFactoryOld can create TraceExporterOld and MetricsExporterOld.
70+
type ExporterFactoryOld interface {
71+
ExporterFactoryBase
4472

4573
// CreateTraceExporter creates a trace exporter based on this config.
46-
CreateTraceExporter(logger *zap.Logger, cfg configmodels.Exporter) (TraceExporter, error)
74+
CreateTraceExporter(logger *zap.Logger, cfg configmodels.Exporter) (TraceExporterOld, error)
4775

4876
// CreateMetricsExporter creates a metrics exporter based on this config.
49-
CreateMetricsExporter(logger *zap.Logger, cfg configmodels.Exporter) (MetricsExporter, error)
77+
CreateMetricsExporter(logger *zap.Logger, cfg configmodels.Exporter) (MetricsExporterOld, error)
5078
}
5179

52-
// CreationParams is passed to Create* functions in FactoryV2.
53-
type CreationParams struct {
80+
// ExporterCreateParams is passed to ExporterFactory.Create* functions.
81+
type ExporterCreateParams struct {
5482
// Logger that the factory can use during creation and can pass to the created
5583
// component to be used later as well.
5684
Logger *zap.Logger
5785
}
5886

59-
// FactoryV2 can create TraceExporterV2 and MetricsExporterV2. This is the
87+
// ExporterFactory can create TraceExporter and MetricsExporter. This is the
6088
// new factory type that can create new style exporters.
61-
type FactoryV2 interface {
62-
BaseFactory
89+
type ExporterFactory interface {
90+
ExporterFactoryBase
6391

6492
// CreateTraceExporter creates a trace exporter based on this config.
6593
// If the exporter type does not support tracing or if the config is not valid
6694
// error will be returned instead.
67-
CreateTraceExporter(ctx context.Context, params CreationParams, cfg configmodels.Exporter) (TraceExporterV2, error)
95+
CreateTraceExporter(ctx context.Context, params ExporterCreateParams,
96+
cfg configmodels.Exporter) (TraceExporter, error)
6897

6998
// CreateMetricsExporter creates a metrics exporter based on this config.
7099
// If the exporter type does not support metrics or if the config is not valid
71100
// error will be returned instead.
72-
CreateMetricsExporter(ctx context.Context, params CreationParams, cfg configmodels.Exporter) (MetricsExporterV2, error)
73-
}
74-
75-
// Build takes a list of exporter factories and returns a map of type map[string]Factory
76-
// with factory type as keys. It returns a non-nil error when more than one factories
77-
// have the same type.
78-
func Build(factories ...Factory) (map[string]Factory, error) {
79-
fMap := map[string]Factory{}
80-
for _, f := range factories {
81-
if _, ok := fMap[f.Type()]; ok {
82-
return fMap, fmt.Errorf("duplicate exporter factory %q", f.Type())
83-
}
84-
fMap[f.Type()] = f
85-
}
86-
return fMap, nil
101+
CreateMetricsExporter(ctx context.Context, params ExporterCreateParams,
102+
cfg configmodels.Exporter) (MetricsExporter, error)
87103
}

exporter/factory_test.go renamed to component/exporter_test.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
// Copyright 2019, OpenTelemetry Authors
1+
// Copyright OpenTelemetry Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
55
// You may obtain a copy of the License at
66
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
7+
// http://www.apache.org/licenses/LICENSE-2.0
88
//
99
// Unless required by applicable law or agreed to in writing, software
1010
// distributed under the License is distributed on an "AS IS" BASIS,
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package exporter
15+
package component
1616

1717
import (
1818
"testing"
@@ -23,60 +23,60 @@ import (
2323
"github.com/open-telemetry/opentelemetry-collector/config/configmodels"
2424
)
2525

26-
type TestFactory struct {
26+
type TestExporterFactory struct {
2727
name string
2828
}
2929

3030
// Type gets the type of the Exporter config created by this factory.
31-
func (f *TestFactory) Type() string {
31+
func (f *TestExporterFactory) Type() string {
3232
return f.name
3333
}
3434

3535
// CreateDefaultConfig creates the default configuration for the Exporter.
36-
func (f *TestFactory) CreateDefaultConfig() configmodels.Exporter {
36+
func (f *TestExporterFactory) CreateDefaultConfig() configmodels.Exporter {
3737
return nil
3838
}
3939

4040
// CreateTraceExporter creates a trace exporter based on this config.
41-
func (f *TestFactory) CreateTraceExporter(logger *zap.Logger, cfg configmodels.Exporter) (TraceExporter, error) {
41+
func (f *TestExporterFactory) CreateTraceExporter(logger *zap.Logger, cfg configmodels.Exporter) (TraceExporterOld, error) {
4242
return nil, nil
4343
}
4444

4545
// CreateMetricsExporter creates a metrics exporter based on this config.
46-
func (f *TestFactory) CreateMetricsExporter(logger *zap.Logger, cfg configmodels.Exporter) (MetricsExporter, error) {
46+
func (f *TestExporterFactory) CreateMetricsExporter(logger *zap.Logger, cfg configmodels.Exporter) (MetricsExporterOld, error) {
4747
return nil, nil
4848
}
4949

50-
func TestFactoriesBuilder(t *testing.T) {
50+
func TestBuildExporters(t *testing.T) {
5151
type testCase struct {
52-
in []Factory
53-
out map[string]Factory
52+
in []ExporterFactoryBase
53+
out map[string]ExporterFactoryBase
5454
err bool
5555
}
5656

5757
testCases := []testCase{
5858
{
59-
in: []Factory{
60-
&TestFactory{"exp1"},
61-
&TestFactory{"exp2"},
59+
in: []ExporterFactoryBase{
60+
&TestExporterFactory{"exp1"},
61+
&TestExporterFactory{"exp2"},
6262
},
63-
out: map[string]Factory{
64-
"exp1": &TestFactory{"exp1"},
65-
"exp2": &TestFactory{"exp2"},
63+
out: map[string]ExporterFactoryBase{
64+
"exp1": &TestExporterFactory{"exp1"},
65+
"exp2": &TestExporterFactory{"exp2"},
6666
},
6767
err: false,
6868
},
6969
{
70-
in: []Factory{
71-
&TestFactory{"exp1"},
72-
&TestFactory{"exp1"},
70+
in: []ExporterFactoryBase{
71+
&TestExporterFactory{"exp1"},
72+
&TestExporterFactory{"exp1"},
7373
},
7474
err: true,
7575
},
7676
}
7777

7878
for _, c := range testCases {
79-
out, err := Build(c.in...)
79+
out, err := MakeExporterFactoryMap(c.in...)
8080
if c.err {
8181
assert.NotNil(t, err)
8282
continue

extension/extension.go renamed to component/extension.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1-
// Copyright 2019, OpenTelemetry Authors
1+
// Copyright OpenTelemetry Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
55
// You may obtain a copy of the License at
66
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
7+
// http://www.apache.org/licenses/LICENSE-2.0
88
//
99
// Unless required by applicable law or agreed to in writing, software
1010
// distributed under the License is distributed on an "AS IS" BASIS,
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
// Package extension defines service extensions that can be added to the OpenTelemetry
16-
// service but that not interact if the data pipelines, but provide some functionality
17-
// to the service, examples: health check endpoint, z-pages, etc.
18-
package extension
15+
package component
16+
17+
import (
18+
"go.uber.org/zap"
1919

20-
import "github.com/open-telemetry/opentelemetry-collector/component"
20+
"github.com/open-telemetry/opentelemetry-collector/config/configmodels"
21+
)
2122

2223
// ServiceExtension is the interface for objects hosted by the OpenTelemetry Collector that
2324
// don't participate directly on data pipelines but provide some functionality
2425
// to the service, examples: health check endpoint, z-pages, etc.
2526
type ServiceExtension interface {
26-
component.Component
27+
Component
2728
}
2829

2930
// PipelineWatcher is an extra interface for ServiceExtension hosted by the OpenTelemetry
@@ -42,3 +43,20 @@ type PipelineWatcher interface {
4243
// appropriate action before that happens.
4344
NotReady() error
4445
}
46+
47+
// ExtensionFactory is a factory interface for extensions to the service.
48+
type ExtensionFactory interface {
49+
Factory
50+
51+
// CreateDefaultConfig creates the default configuration for the Extension.
52+
// This method can be called multiple times depending on the pipeline
53+
// configuration and should not cause side-effects that prevent the creation
54+
// of multiple instances of the Extension.
55+
// The object returned by this method needs to pass the checks implemented by
56+
// 'configcheck.ValidateConfig'. It is recommended to have such check in the
57+
// tests of any implementation of the Factory interface.
58+
CreateDefaultConfig() configmodels.Extension
59+
60+
// CreateExtension creates a service extension based on the given config.
61+
CreateExtension(logger *zap.Logger, cfg configmodels.Extension) (ServiceExtension, error)
62+
}

component/factory_helpers.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright OpenTelemetry 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 component
16+
17+
import "fmt"
18+
19+
// MakeReceiverFactoryMap takes a list of receiver factories and returns a map
20+
// with factory type as keys. It returns a non-nil error when more than one factories
21+
// have the same type.
22+
func MakeReceiverFactoryMap(factories ...ReceiverFactoryBase) (map[string]ReceiverFactoryBase, error) {
23+
fMap := map[string]ReceiverFactoryBase{}
24+
for _, f := range factories {
25+
if _, ok := fMap[f.Type()]; ok {
26+
return fMap, fmt.Errorf("duplicate receiver factory %q", f.Type())
27+
}
28+
fMap[f.Type()] = f
29+
}
30+
return fMap, nil
31+
}
32+
33+
// MakeProcessorFactoryMap takes a list of processor factories and returns a map
34+
// with factory type as keys. It returns a non-nil error when more than one factories
35+
// have the same type.
36+
func MakeProcessorFactoryMap(factories ...ProcessorFactoryBase) (map[string]ProcessorFactoryBase, error) {
37+
fMap := map[string]ProcessorFactoryBase{}
38+
for _, f := range factories {
39+
if _, ok := fMap[f.Type()]; ok {
40+
return fMap, fmt.Errorf("duplicate processor factory %q", f.Type())
41+
}
42+
fMap[f.Type()] = f
43+
}
44+
return fMap, nil
45+
}
46+
47+
// MakeExporterFactoryMap takes a list of exporter factories and returns a map
48+
// with factory type as keys. It returns a non-nil error when more than one factories
49+
// have the same type.
50+
func MakeExporterFactoryMap(factories ...ExporterFactoryBase) (map[string]ExporterFactoryBase, error) {
51+
fMap := map[string]ExporterFactoryBase{}
52+
for _, f := range factories {
53+
if _, ok := fMap[f.Type()]; ok {
54+
return fMap, fmt.Errorf("duplicate exporter factory %q", f.Type())
55+
}
56+
fMap[f.Type()] = f
57+
}
58+
return fMap, nil
59+
}
60+
61+
// MakeExtensionFactoryMap takes a list of extension factories and returns a map
62+
// with factory type as keys. It returns a non-nil error when more than one factories
63+
// have the same type.
64+
func MakeExtensionFactoryMap(factories ...ExtensionFactory) (map[string]ExtensionFactory, error) {
65+
fMap := map[string]ExtensionFactory{}
66+
for _, f := range factories {
67+
if _, ok := fMap[f.Type()]; ok {
68+
return fMap, fmt.Errorf("duplicate extension factory %q", f.Type())
69+
}
70+
fMap[f.Type()] = f
71+
}
72+
return fMap, nil
73+
}

0 commit comments

Comments
 (0)