config.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package lokireceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/lokireceiver"
  4. import (
  5. "errors"
  6. "go.opentelemetry.io/collector/component"
  7. "go.opentelemetry.io/collector/config/configgrpc"
  8. "go.opentelemetry.io/collector/config/confighttp"
  9. "go.opentelemetry.io/collector/confmap"
  10. )
  11. const (
  12. // Protocol values.
  13. protoGRPC = "protocols::grpc"
  14. protoHTTP = "protocols::http"
  15. )
  16. // Protocols is the configuration for the supported protocols.
  17. type Protocols struct {
  18. GRPC *configgrpc.GRPCServerSettings `mapstructure:"grpc"`
  19. HTTP *confighttp.HTTPServerSettings `mapstructure:"http"`
  20. }
  21. // Config defines configuration for the lokireceiver receiver.
  22. type Config struct {
  23. // Protocols is the configuration for the supported protocols, currently gRPC and HTTP (Proto and JSON).
  24. Protocols `mapstructure:"protocols"`
  25. KeepTimestamp bool `mapstructure:"use_incoming_timestamp"`
  26. }
  27. var _ component.Config = (*Config)(nil)
  28. var _ confmap.Unmarshaler = (*Config)(nil)
  29. // Validate checks the receiver configuration is valid
  30. func (cfg *Config) Validate() error {
  31. if cfg.GRPC == nil && cfg.HTTP == nil {
  32. return errors.New("must specify at least one protocol when using the Loki receiver")
  33. }
  34. return nil
  35. }
  36. // Unmarshal a confmap.Conf into the config struct.
  37. func (cfg *Config) Unmarshal(conf *confmap.Conf) error {
  38. err := conf.Unmarshal(cfg, confmap.WithErrorUnused())
  39. if err != nil {
  40. return err
  41. }
  42. if !conf.IsSet(protoGRPC) {
  43. cfg.GRPC = nil
  44. }
  45. if !conf.IsSet(protoHTTP) {
  46. cfg.HTTP = nil
  47. }
  48. return nil
  49. }