-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
factory.go
125 lines (111 loc) · 3.97 KB
/
factory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package otlpreceiver
import (
"context"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configgrpc"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/internal/sharedcomponent"
"go.opentelemetry.io/collector/receiver/receiverhelper"
)
const (
typeStr = "otlp"
defaultGRPCEndpoint = "0.0.0.0:4317"
defaultHTTPEndpoint = "0.0.0.0:55681"
legacyGRPCEndpoint = "0.0.0.0:55680"
)
// NewFactory creates a new OTLP receiver factory.
func NewFactory() component.ReceiverFactory {
return receiverhelper.NewFactory(
typeStr,
createDefaultConfig,
receiverhelper.WithTraces(createTracesReceiver),
receiverhelper.WithMetrics(createMetricsReceiver),
receiverhelper.WithLogs(createLogReceiver))
}
// createDefaultConfig creates the default configuration for receiver.
func createDefaultConfig() config.Receiver {
return &Config{
ReceiverSettings: config.NewReceiverSettings(config.NewID(typeStr)),
Protocols: Protocols{
GRPC: &configgrpc.GRPCServerSettings{
NetAddr: confignet.NetAddr{
Endpoint: defaultGRPCEndpoint,
Transport: "tcp",
},
// We almost write 0 bytes, so no need to tune WriteBufferSize.
ReadBufferSize: 512 * 1024,
},
HTTP: &confighttp.HTTPServerSettings{
Endpoint: defaultHTTPEndpoint,
},
},
}
}
// CreateTracesReceiver creates a trace receiver based on provided config.
func createTracesReceiver(
_ context.Context,
set component.ReceiverCreateSettings,
cfg config.Receiver,
nextConsumer consumer.Traces,
) (component.TracesReceiver, error) {
r := receivers.GetOrAdd(cfg, func() component.Component {
return newOtlpReceiver(cfg.(*Config), set.Logger)
})
if err := r.Unwrap().(*otlpReceiver).registerTraceConsumer(nextConsumer); err != nil {
return nil, err
}
return r, nil
}
// CreateMetricsReceiver creates a metrics receiver based on provided config.
func createMetricsReceiver(
_ context.Context,
set component.ReceiverCreateSettings,
cfg config.Receiver,
consumer consumer.Metrics,
) (component.MetricsReceiver, error) {
r := receivers.GetOrAdd(cfg, func() component.Component {
return newOtlpReceiver(cfg.(*Config), set.Logger)
})
if err := r.Unwrap().(*otlpReceiver).registerMetricsConsumer(consumer); err != nil {
return nil, err
}
return r, nil
}
// CreateLogReceiver creates a log receiver based on provided config.
func createLogReceiver(
_ context.Context,
set component.ReceiverCreateSettings,
cfg config.Receiver,
consumer consumer.Logs,
) (component.LogsReceiver, error) {
r := receivers.GetOrAdd(cfg, func() component.Component {
return newOtlpReceiver(cfg.(*Config), set.Logger)
})
if err := r.Unwrap().(*otlpReceiver).registerLogsConsumer(consumer); err != nil {
return nil, err
}
return r, nil
}
// This is the map of already created OTLP receivers for particular configurations.
// We maintain this map because the Factory is asked trace and metric receivers separately
// when it gets CreateTracesReceiver() and CreateMetricsReceiver() but they must not
// create separate objects, they must use one otlpReceiver object per configuration.
// When the receiver is shutdown it should be removed from this map so the same configuration
// can be recreated successfully.
var receivers = sharedcomponent.NewSharedComponents()