config.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package statsdreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver"
  4. import (
  5. "fmt"
  6. "time"
  7. "github.com/lightstep/go-expohisto/structure"
  8. "go.opentelemetry.io/collector/config/confignet"
  9. "go.uber.org/multierr"
  10. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/internal/protocol"
  11. )
  12. // Config defines configuration for StatsD receiver.
  13. type Config struct {
  14. NetAddr confignet.NetAddr `mapstructure:",squash"`
  15. AggregationInterval time.Duration `mapstructure:"aggregation_interval"`
  16. EnableMetricType bool `mapstructure:"enable_metric_type"`
  17. IsMonotonicCounter bool `mapstructure:"is_monotonic_counter"`
  18. TimerHistogramMapping []protocol.TimerHistogramMapping `mapstructure:"timer_histogram_mapping"`
  19. }
  20. func (c *Config) Validate() error {
  21. var errs error
  22. if c.AggregationInterval <= 0 {
  23. errs = multierr.Append(errs, fmt.Errorf("aggregation_interval must be a positive duration"))
  24. }
  25. var TimerHistogramMappingMissingObjectName bool
  26. for _, eachMap := range c.TimerHistogramMapping {
  27. if eachMap.StatsdType == "" {
  28. TimerHistogramMappingMissingObjectName = true
  29. break
  30. }
  31. switch eachMap.StatsdType {
  32. case protocol.TimingTypeName, protocol.TimingAltTypeName, protocol.HistogramTypeName, protocol.DistributionTypeName:
  33. // do nothing
  34. case protocol.CounterTypeName, protocol.GaugeTypeName:
  35. fallthrough
  36. default:
  37. errs = multierr.Append(errs, fmt.Errorf("statsd_type is not a supported mapping for histogram and timing metrics: %s", eachMap.StatsdType))
  38. }
  39. if eachMap.ObserverType == "" {
  40. TimerHistogramMappingMissingObjectName = true
  41. break
  42. }
  43. switch eachMap.ObserverType {
  44. case protocol.GaugeObserver, protocol.SummaryObserver, protocol.HistogramObserver:
  45. // do nothing
  46. case protocol.DisableObserver:
  47. fallthrough
  48. default:
  49. errs = multierr.Append(errs, fmt.Errorf("observer_type is not supported for histogram and timing metrics: %s", eachMap.ObserverType))
  50. }
  51. if eachMap.ObserverType == protocol.HistogramObserver {
  52. if eachMap.Histogram.MaxSize != 0 && (eachMap.Histogram.MaxSize < structure.MinSize || eachMap.Histogram.MaxSize > structure.MaximumMaxSize) {
  53. errs = multierr.Append(errs, fmt.Errorf("histogram max_size out of range: %v", eachMap.Histogram.MaxSize))
  54. }
  55. } else {
  56. // Non-histogram observer w/ histogram config
  57. var empty protocol.HistogramConfig
  58. if eachMap.Histogram != empty {
  59. errs = multierr.Append(errs, fmt.Errorf("histogram configuration requires observer_type: histogram"))
  60. }
  61. }
  62. }
  63. if TimerHistogramMappingMissingObjectName {
  64. errs = multierr.Append(errs, fmt.Errorf("must specify object id for all TimerHistogramMappings"))
  65. }
  66. return errs
  67. }