Debian Codegraph @

receiver/otlpreceiver/config.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otlpreceiver // import "go.opentelemetry.io/collector/receiver/otlpreceiver"
5
6 import (
7 "encoding"
8 "errors"
9 "fmt"
10 "net/url"
11 "path"
12
13 "go.opentelemetry.io/collector/component"
14 "go.opentelemetry.io/collector/config/configgrpc"
15 "go.opentelemetry.io/collector/config/confighttp"
16 "go.opentelemetry.io/collector/config/configoptional"
17 )
18
19 type SanitizedURLPath string
20
21 var _ encoding.TextUnmarshaler = (*SanitizedURLPath)(nil)
22
23 func (s *SanitizedURLPath) UnmarshalText(text []byte) error {
24 u, err := url.Parse(string(text))
25 if err != nil {
26 return fmt.Errorf("invalid HTTP URL path set for signal: %w", err)
27 }
28
29 if !path.IsAbs(u.Path) {
30 u.Path = "/" + u.Path
31 }
32
33 *s = SanitizedURLPath(u.Path)
34 return nil
35 }
36
37 type HTTPConfig struct {
38 ServerConfig confighttp.ServerConfig `mapstructure:",squash"`
39
40 // The URL path to receive traces on. If omitted "/v1/traces" will be used.
41 TracesURLPath SanitizedURLPath `mapstructure:"traces_url_path,omitempty"`
42
43 // The URL path to receive metrics on. If omitted "/v1/metrics" will be used.
44 MetricsURLPath SanitizedURLPath `mapstructure:"metrics_url_path,omitempty"`
45
46 // The URL path to receive logs on. If omitted "/v1/logs" will be used.
47 LogsURLPath SanitizedURLPath `mapstructure:"logs_url_path,omitempty"`
48
49 // prevent unkeyed literal initialization
50 _ struct{}
51 }
52
53 // Protocols is the configuration for the supported protocols.
54 type Protocols struct {
55 GRPC configoptional.Optional[configgrpc.ServerConfig] `mapstructure:"grpc"`
56 HTTP configoptional.Optional[HTTPConfig] `mapstructure:"http"`
57 // prevent unkeyed literal initialization
58 _ struct{}
59 }
60
61 // Config defines configuration for OTLP receiver.
62 type Config struct {
63 // Protocols is the configuration for the supported protocols, currently gRPC and HTTP (Proto and JSON).
64 Protocols `mapstructure:"protocols"`
65 // prevent unkeyed literal initialization
66 _ struct{}
67 }
68
69 var _ component.Config = (*Config)(nil)
70
71 // Validate checks the receiver configuration is valid
72 func (cfg *Config) Validate() error {
73 if !cfg.GRPC.HasValue() && !cfg.HTTP.HasValue() {
74 return errors.New("must specify at least one protocol when using the OTLP receiver")
75 }
76 return nil
77 }