@@ -7,8 +7,11 @@ import (
7
7
"context"
8
8
9
9
"go.opentelemetry.io/otel/log"
10
+ nooplog "go.opentelemetry.io/otel/log/noop"
10
11
"go.opentelemetry.io/otel/metric"
12
+ noopmetric "go.opentelemetry.io/otel/metric/noop"
11
13
"go.opentelemetry.io/otel/trace"
14
+ nooptrace "go.opentelemetry.io/otel/trace/noop"
12
15
"go.uber.org/zap"
13
16
14
17
"go.opentelemetry.io/collector/component"
@@ -35,7 +38,7 @@ type Providers interface {
35
38
// Logger returns a zap.Logger that may be used by components to
36
39
// log their internal operations.
37
40
//
38
- // NOTE: from the perspective of the Telemetry implementation,
41
+ // NOTE: from the perspective of the Providers implementation,
39
42
// this Logger and the LoggerProvider are independent. However,
40
43
// the service package will arrange for logs written to this
41
44
// logger to be copied to the LoggerProvider. The effective
@@ -57,7 +60,7 @@ type Providers interface {
57
60
TracerProvider () trace.TracerProvider
58
61
}
59
62
60
- // Settings holds configuration for building Telemetry .
63
+ // Settings holds configuration for building Providers .
61
64
type Settings struct {
62
65
// BuildInfo contains build information about the collector.
63
66
BuildInfo component.BuildInfo
@@ -66,5 +69,94 @@ type Settings struct {
66
69
ZapOptions []zap.Option
67
70
}
68
71
69
- // TODO create abstract Factory interface that is implemented by otelconftelemetry.
72
+ // Factory is a factory interface for internal telemetry.
73
+ //
74
+ // This interface cannot be directly implemented. Implementations must
75
+ // use the NewFactory to implement it.
76
+ //
77
+ // NOTE This API is experimental and will change soon - use at your own risk.
70
78
// See https://github.com/open-telemetry/opentelemetry-collector/issues/4970
79
+ type Factory interface {
80
+ // CreateDefaultConfig creates the default configuration for the telemetry.
81
+ CreateDefaultConfig () component.Config
82
+
83
+ // CreateProviders creates the logger, meter, and tracer providers for
84
+ // the collector's internal telemetry.
85
+ CreateProviders (context.Context , Settings , component.Config ) (Providers , error )
86
+
87
+ // unexportedFactoryFunc is used to prevent external implementations of Factory.
88
+ unexportedFactoryFunc ()
89
+ }
90
+
91
+ type FactoryOption interface {
92
+ applyOption (* factory )
93
+ }
94
+
95
+ // factoryOptionFunc is an FactoryOption created through a function.
96
+ type factoryOptionFunc func (* factory )
97
+
98
+ func (f factoryOptionFunc ) applyOption (o * factory ) {
99
+ f (o )
100
+ }
101
+
102
+ type factory struct {
103
+ component.CreateDefaultConfigFunc
104
+ createProvidersFunc CreateProvidersFunc
105
+ }
106
+
107
+ // NewFactory returns a Factory.
108
+ //
109
+ // If createProviders is nil, then the returned Factory's CreateProviders
110
+ // method will return a Providers with noop telemetry providers.
111
+ func NewFactory (
112
+ createDefaultConfig component.CreateDefaultConfigFunc ,
113
+ createProviders CreateProvidersFunc ,
114
+ opts ... FactoryOption ,
115
+ ) Factory {
116
+ f := & factory {
117
+ CreateDefaultConfigFunc : createDefaultConfig ,
118
+ createProvidersFunc : createProviders ,
119
+ }
120
+ for _ , opt := range opts {
121
+ opt .applyOption (f )
122
+ }
123
+ return f
124
+ }
125
+
126
+ // CreateProvidersFunc is the equivalent of Factory.CreateProviders.
127
+ type CreateProvidersFunc func (context.Context , Settings , component.Config ) (Providers , error )
128
+
129
+ func (* factory ) unexportedFactoryFunc () {}
130
+
131
+ func (f * factory ) CreateProviders (ctx context.Context , settings Settings , cfg component.Config ) (Providers , error ) {
132
+ if f .createProvidersFunc == nil {
133
+ return nopProviders {}, nil
134
+ }
135
+ return f .createProvidersFunc (ctx , settings , cfg )
136
+ }
137
+
138
+ type nopProviders struct {}
139
+
140
+ func (nopProviders ) Shutdown (context.Context ) error {
141
+ return nil
142
+ }
143
+
144
+ func (nopProviders ) Resource () pcommon.Resource {
145
+ return pcommon .NewResource ()
146
+ }
147
+
148
+ func (nopProviders ) Logger () * zap.Logger {
149
+ return zap .NewNop ()
150
+ }
151
+
152
+ func (nopProviders ) LoggerProvider () log.LoggerProvider {
153
+ return nooplog .NewLoggerProvider ()
154
+ }
155
+
156
+ func (nopProviders ) MeterProvider () metric.MeterProvider {
157
+ return noopmetric .NewMeterProvider ()
158
+ }
159
+
160
+ func (nopProviders ) TracerProvider () trace.TracerProvider {
161
+ return nooptrace .NewTracerProvider ()
162
+ }
0 commit comments