forked from open-telemetry/opentelemetry-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.go
183 lines (158 loc) · 5.26 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2019, 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 jaegerreceiver
// This file implements factory for Jaeger receiver.
import (
"context"
"fmt"
"net"
"strconv"
"go.uber.org/zap"
"google.golang.org/grpc"
"github.com/open-telemetry/opentelemetry-service/config/configerror"
"github.com/open-telemetry/opentelemetry-service/config/configmodels"
"github.com/open-telemetry/opentelemetry-service/consumer"
"github.com/open-telemetry/opentelemetry-service/receiver"
)
const (
// The value of "type" key in configuration.
typeStr = "jaeger"
// Protocol values.
protoGRPC = "grpc"
protoThriftHTTP = "thrift-http"
// TODO https://github.com/open-telemetry/opentelemetry-service/issues/267
// Remove ThriftTChannel support.
protoThriftTChannel = "thrift-tchannel"
// Default endpoints to bind to.
defaultGRPCBindEndpoint = "127.0.0.1:14250"
defaultHTTPBindEndpoint = "127.0.0.1:14268"
defaultTChannelBindEndpoint = "127.0.0.1:14267"
)
// Factory is the factory for Jaeger receiver.
type Factory struct {
}
// Type gets the type of the Receiver config created by this factory.
func (f *Factory) Type() string {
return typeStr
}
// CustomUnmarshaler returns nil because we don't need custom unmarshaling for this config.
func (f *Factory) CustomUnmarshaler() receiver.CustomUnmarshaler {
return nil
}
// CreateDefaultConfig creates the default configuration for Jaeger receiver.
func (f *Factory) CreateDefaultConfig() configmodels.Receiver {
return &Config{
TypeVal: typeStr,
NameVal: typeStr,
Protocols: map[string]*receiver.SecureReceiverSettings{
protoGRPC: {
ReceiverSettings: configmodels.ReceiverSettings{
Endpoint: defaultGRPCBindEndpoint,
},
},
protoThriftTChannel: {
ReceiverSettings: configmodels.ReceiverSettings{
Endpoint: defaultTChannelBindEndpoint,
},
},
protoThriftHTTP: {
ReceiverSettings: configmodels.ReceiverSettings{
Endpoint: defaultHTTPBindEndpoint,
},
},
},
}
}
// CreateTraceReceiver creates a trace receiver based on provided config.
func (f *Factory) CreateTraceReceiver(
ctx context.Context,
logger *zap.Logger,
cfg configmodels.Receiver,
nextConsumer consumer.TraceConsumer,
) (receiver.TraceReceiver, error) {
// Convert settings in the source config to Configuration struct
// that Jaeger receiver understands.
rCfg := cfg.(*Config)
protoGRPC := rCfg.Protocols[protoGRPC]
protoHTTP := rCfg.Protocols[protoThriftHTTP]
protoTChannel := rCfg.Protocols[protoThriftTChannel]
config := Configuration{}
var grpcServerOptions []grpc.ServerOption
// Set ports
if protoGRPC != nil && protoGRPC.IsEnabled() {
var err error
config.CollectorGRPCPort, err = extractPortFromEndpoint(protoGRPC.Endpoint)
if err != nil {
return nil, err
}
if protoGRPC.TLSCredentials != nil {
option, err := protoGRPC.TLSCredentials.ToGrpcServerOption()
if err != nil {
return nil, fmt.Errorf("failed to configure TLS: %v", err)
}
grpcServerOptions = append(grpcServerOptions, option)
}
config.CollectorGRPCOptions = grpcServerOptions
}
if protoHTTP != nil && protoHTTP.IsEnabled() {
var err error
config.CollectorHTTPPort, err = extractPortFromEndpoint(protoHTTP.Endpoint)
if err != nil {
return nil, err
}
}
if protoTChannel != nil && protoTChannel.IsEnabled() {
var err error
config.CollectorThriftPort, err = extractPortFromEndpoint(protoTChannel.Endpoint)
if err != nil {
return nil, err
}
}
if (protoGRPC == nil && protoHTTP == nil && protoTChannel == nil) ||
(config.CollectorGRPCPort == 0 && config.CollectorHTTPPort == 0 && config.CollectorThriftPort == 0) {
err := fmt.Errorf("either %v, %v, or %v protocol endpoint with non-zero port must be enabled for %s receiver",
protoGRPC,
protoThriftHTTP,
protoThriftTChannel,
typeStr,
)
return nil, err
}
// Create the receiver.
return New(ctx, &config, nextConsumer)
}
// CreateMetricsReceiver creates a metrics receiver based on provided config.
func (f *Factory) CreateMetricsReceiver(
logger *zap.Logger,
cfg configmodels.Receiver,
consumer consumer.MetricsConsumer,
) (receiver.MetricsReceiver, error) {
return nil, configerror.ErrDataTypeIsNotSupported
}
// extract the port number from string in "address:port" format. If the
// port number cannot be extracted returns an error.
func extractPortFromEndpoint(endpoint string) (int, error) {
_, portStr, err := net.SplitHostPort(endpoint)
if err != nil {
return 0, fmt.Errorf("endpoint is not formatted correctly: %s", err.Error())
}
port, err := strconv.ParseInt(portStr, 10, 0)
if err != nil {
return 0, fmt.Errorf("endpoint port is not a number: %s", err.Error())
}
if port < 1 || port > 65535 {
return 0, fmt.Errorf("port number must be between 1 and 65535")
}
return int(port), nil
}