-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathfactory.go
153 lines (128 loc) · 4.37 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
// 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"
"errors"
"fmt"
"net"
"strconv"
"go.uber.org/zap"
"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"
)
var _ = receiver.RegisterFactory(&factory{})
const (
// The value of "type" key in configuration.
typeStr = "jaeger"
// Protocol values.
protoThriftHTTP = "thrift-http"
protoThriftTChannel = "thrift-tchannel"
// Default endpoints to bind to.
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]*configmodels.ReceiverSettings{
protoThriftTChannel: {
Enabled: false,
Endpoint: defaultTChannelBindEndpoint,
},
protoThriftHTTP: {
Enabled: false,
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)
protoHTTP := rCfg.Protocols[protoThriftHTTP]
protoTChannel := rCfg.Protocols[protoThriftTChannel]
config := Configuration{}
// Set ports
if protoHTTP != nil {
var err error
config.CollectorHTTPPort, err = extractPortFromEndpoint(protoHTTP.Endpoint)
if err != nil {
return nil, err
}
}
if protoTChannel != nil {
var err error
config.CollectorThriftPort, err = extractPortFromEndpoint(protoTChannel.Endpoint)
if err != nil {
return nil, err
}
}
if (protoHTTP == nil && protoTChannel == nil) ||
(config.CollectorHTTPPort == 0 && config.CollectorThriftPort == 0) {
return nil, errors.New("either " + protoThriftHTTP + " or " + protoThriftTChannel +
" protocol endpoint with non-zero port must be defined for " + typeStr + " receiver")
}
// Jaeger receiver implementation currently does not allow specifying which interface
// to bind to so we cannot use yet the address part of endpoint.
// 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
}