|
| 1 | +// Copyright 2019, OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package sapmreceiver |
| 16 | + |
| 17 | +// This file implements factory for SAPM receiver. |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "net" |
| 23 | + "strconv" |
| 24 | + |
| 25 | + "github.com/open-telemetry/opentelemetry-collector/config/configerror" |
| 26 | + "github.com/open-telemetry/opentelemetry-collector/config/configmodels" |
| 27 | + "github.com/open-telemetry/opentelemetry-collector/consumer" |
| 28 | + "github.com/open-telemetry/opentelemetry-collector/receiver" |
| 29 | + "go.uber.org/zap" |
| 30 | +) |
| 31 | + |
| 32 | +const ( |
| 33 | + // The value of "type" key in configuration. |
| 34 | + typeStr = "sapm" |
| 35 | + |
| 36 | + // Default endpoints to bind to. |
| 37 | + defaultEndpoint = ":7276" |
| 38 | +) |
| 39 | + |
| 40 | +// Factory is the factory for SAPM receiver. |
| 41 | +type Factory struct { |
| 42 | +} |
| 43 | + |
| 44 | +// Type gets the type of the Receiver config created by this factory. |
| 45 | +func (f *Factory) Type() string { |
| 46 | + return typeStr |
| 47 | +} |
| 48 | + |
| 49 | +// CustomUnmarshaler returns nil because we don't need custom unmarshaling for this config. |
| 50 | +func (f *Factory) CustomUnmarshaler() receiver.CustomUnmarshaler { |
| 51 | + return nil |
| 52 | +} |
| 53 | + |
| 54 | +// CreateDefaultConfig creates the default configuration for SAPM receiver. |
| 55 | +func (f *Factory) CreateDefaultConfig() configmodels.Receiver { |
| 56 | + return &Config{ |
| 57 | + ReceiverSettings: configmodels.ReceiverSettings{ |
| 58 | + TypeVal: typeStr, |
| 59 | + NameVal: typeStr, |
| 60 | + Endpoint: defaultEndpoint, |
| 61 | + }, |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// extract the port number from string in "address:port" format. If the |
| 66 | +// port number cannot be extracted returns an error. |
| 67 | +// TODO make this a utility function |
| 68 | +func extractPortFromEndpoint(endpoint string) (int, error) { |
| 69 | + _, portStr, err := net.SplitHostPort(endpoint) |
| 70 | + if err != nil { |
| 71 | + return 0, fmt.Errorf("endpoint is not formatted correctly: %s", err.Error()) |
| 72 | + } |
| 73 | + port, err := strconv.ParseInt(portStr, 10, 0) |
| 74 | + if err != nil { |
| 75 | + return 0, fmt.Errorf("endpoint port is not a number: %s", err.Error()) |
| 76 | + } |
| 77 | + if port < 1 || port > 65535 { |
| 78 | + return 0, fmt.Errorf("port number must be between 1 and 65535") |
| 79 | + } |
| 80 | + return int(port), nil |
| 81 | +} |
| 82 | + |
| 83 | +// CreateTraceReceiver creates a trace receiver based on provided config. |
| 84 | +func (f *Factory) CreateTraceReceiver( |
| 85 | + ctx context.Context, |
| 86 | + logger *zap.Logger, |
| 87 | + cfg configmodels.Receiver, |
| 88 | + nextConsumer consumer.TraceConsumer, |
| 89 | +) (receiver.TraceReceiver, error) { |
| 90 | + // assert config is SAPM config |
| 91 | + rCfg := cfg.(*Config) |
| 92 | + |
| 93 | + port, err := extractPortFromEndpoint(rCfg.Endpoint) |
| 94 | + if err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + |
| 98 | + // verify that the configured port is not 0 |
| 99 | + if port == 0 { |
| 100 | + err = fmt.Errorf("endpoint with non-zero port must be enabled for %s receiver", |
| 101 | + rCfg.Name(), |
| 102 | + ) |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + |
| 106 | + // Create the receiver. |
| 107 | + return New(ctx, logger, rCfg, nextConsumer) |
| 108 | +} |
| 109 | + |
| 110 | +// CreateMetricsReceiver creates a metrics receiver based on provided config. |
| 111 | +func (f *Factory) CreateMetricsReceiver( |
| 112 | + logger *zap.Logger, |
| 113 | + cfg configmodels.Receiver, |
| 114 | + consumer consumer.MetricsConsumer, |
| 115 | +) (receiver.MetricsReceiver, error) { |
| 116 | + return nil, configerror.ErrDataTypeIsNotSupported |
| 117 | +} |
0 commit comments