| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package otlpexporter |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"net" |
| 10 |
"regexp" |
| 11 |
"strconv" |
| 12 |
"strings" |
| 13 |
|
| 14 |
"go.opentelemetry.io/collector/component" |
| 15 |
"go.opentelemetry.io/collector/config/configgrpc" |
| 16 |
"go.opentelemetry.io/collector/config/configretry" |
| 17 |
"go.opentelemetry.io/collector/exporter/exporterhelper" |
| 18 |
) |
| 19 |
|
| 20 |
|
| 21 |
type Config struct { |
| 22 |
TimeoutConfig exporterhelper.TimeoutConfig `mapstructure:",squash"` |
| 23 |
QueueConfig exporterhelper.QueueBatchConfig `mapstructure:"sending_queue"` |
| 24 |
RetryConfig configretry.BackOffConfig `mapstructure:"retry_on_failure"` |
| 25 |
ClientConfig configgrpc.ClientConfig `mapstructure:",squash"` |
| 26 |
|
| 27 |
|
| 28 |
_ struct{} |
| 29 |
} |
| 30 |
|
| 31 |
func (c *Config) Validate() error { |
| 32 |
if after, ok := strings.CutPrefix(c.ClientConfig.Endpoint, "unix://"); ok { |
| 33 |
if after == "" { |
| 34 |
return errors.New("unix socket path cannot be empty") |
| 35 |
} |
| 36 |
return nil |
| 37 |
} |
| 38 |
|
| 39 |
endpoint := c.sanitizedEndpoint() |
| 40 |
if endpoint == "" { |
| 41 |
return errors.New(`requires a non-empty "endpoint"`) |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
_, port, err := net.SplitHostPort(endpoint) |
| 46 |
if err != nil { |
| 47 |
return err |
| 48 |
} |
| 49 |
if _, err := strconv.Atoi(port); err != nil { |
| 50 |
return fmt.Errorf(`invalid port "%s"`, port) |
| 51 |
} |
| 52 |
|
| 53 |
return nil |
| 54 |
} |
| 55 |
|
| 56 |
func (c *Config) sanitizedEndpoint() string { |
| 57 |
switch { |
| 58 |
case strings.HasPrefix(c.ClientConfig.Endpoint, "http://"): |
| 59 |
return strings.TrimPrefix(c.ClientConfig.Endpoint, "http://") |
| 60 |
case strings.HasPrefix(c.ClientConfig.Endpoint, "https://"): |
| 61 |
return strings.TrimPrefix(c.ClientConfig.Endpoint, "https://") |
| 62 |
case strings.HasPrefix(c.ClientConfig.Endpoint, "dns://"): |
| 63 |
r := regexp.MustCompile(`^dns:///?`) |
| 64 |
return r.ReplaceAllString(c.ClientConfig.Endpoint, "") |
| 65 |
default: |
| 66 |
return c.ClientConfig.Endpoint |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
var _ component.Config = (*Config)(nil) |