-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
factory.go
87 lines (74 loc) · 2.21 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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package sapmreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sapmreceiver"
// This file implements factory for SAPM receiver.
import (
"context"
"fmt"
"net"
"strconv"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/receiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sapmreceiver/internal/metadata"
)
const (
// Default endpoints to bind to.
defaultEndpoint = ":7276"
)
// NewFactory creates a factory for SAPM receiver.
func NewFactory() receiver.Factory {
return receiver.NewFactory(
metadata.Type,
createDefaultConfig,
receiver.WithTraces(createTracesReceiver, metadata.TracesStability))
}
func createDefaultConfig() component.Config {
return &Config{
HTTPServerSettings: confighttp.HTTPServerSettings{
Endpoint: defaultEndpoint,
},
}
}
// extract the port number from string in "address:port" format. If the
// port number cannot be extracted returns an error.
// TODO make this a utility function
func extractPortFromEndpoint(endpoint string) (int, error) {
_, portStr, err := net.SplitHostPort(endpoint)
if err != nil {
return 0, fmt.Errorf("endpoint is not formatted correctly: %w", err)
}
port, err := strconv.ParseInt(portStr, 10, 0)
if err != nil {
return 0, fmt.Errorf("endpoint port is not a number: %w", err)
}
if port < 1 || port > 65535 {
return 0, fmt.Errorf("port number must be between 1 and 65535")
}
return int(port), nil
}
// verify that the configured port is not 0
func (rCfg *Config) validate() error {
_, err := extractPortFromEndpoint(rCfg.Endpoint)
if err != nil {
return err
}
return nil
}
// CreateTracesReceiver creates a trace receiver based on provided config.
func createTracesReceiver(
_ context.Context,
params receiver.CreateSettings,
cfg component.Config,
nextConsumer consumer.Traces,
) (receiver.Traces, error) {
// assert config is SAPM config
rCfg := cfg.(*Config)
err := rCfg.validate()
if err != nil {
return nil, err
}
// Create the receiver.
return newReceiver(params, rCfg, nextConsumer)
}