receiver.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package carbonreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/carbonreceiver"
  4. import (
  5. "context"
  6. "errors"
  7. "fmt"
  8. "strings"
  9. "go.opentelemetry.io/collector/component"
  10. "go.opentelemetry.io/collector/consumer"
  11. "go.opentelemetry.io/collector/receiver"
  12. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/carbonreceiver/protocol"
  13. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/carbonreceiver/transport"
  14. )
  15. var (
  16. errEmptyEndpoint = errors.New("empty endpoint")
  17. )
  18. // carbonreceiver implements a receiver.Metrics for Carbon plaintext, aka "line", protocol.
  19. // see https://graphite.readthedocs.io/en/latest/feeding-carbon.html#the-plaintext-protocol.
  20. type carbonReceiver struct {
  21. settings receiver.CreateSettings
  22. config *Config
  23. server transport.Server
  24. reporter transport.Reporter
  25. parser protocol.Parser
  26. nextConsumer consumer.Metrics
  27. }
  28. var _ receiver.Metrics = (*carbonReceiver)(nil)
  29. // newMetricsReceiver creates the Carbon receiver with the given configuration.
  30. func newMetricsReceiver(
  31. set receiver.CreateSettings,
  32. config Config,
  33. nextConsumer consumer.Metrics,
  34. ) (receiver.Metrics, error) {
  35. if nextConsumer == nil {
  36. return nil, component.ErrNilNextConsumer
  37. }
  38. if config.Endpoint == "" {
  39. return nil, errEmptyEndpoint
  40. }
  41. if config.Parser == nil {
  42. // Set the defaults
  43. config.Parser = &protocol.Config{
  44. Type: "plaintext",
  45. Config: &protocol.PlaintextConfig{},
  46. }
  47. }
  48. parser, err := config.Parser.Config.BuildParser()
  49. if err != nil {
  50. return nil, err
  51. }
  52. rep, err := newReporter(set)
  53. if err != nil {
  54. return nil, err
  55. }
  56. r := carbonReceiver{
  57. settings: set,
  58. config: &config,
  59. nextConsumer: nextConsumer,
  60. reporter: rep,
  61. parser: parser,
  62. }
  63. return &r, nil
  64. }
  65. func buildTransportServer(config Config) (transport.Server, error) {
  66. switch strings.ToLower(config.Transport) {
  67. case "", "tcp":
  68. return transport.NewTCPServer(config.Endpoint, config.TCPIdleTimeout)
  69. case "udp":
  70. return transport.NewUDPServer(config.Endpoint)
  71. }
  72. return nil, fmt.Errorf("unsupported transport %q", config.Transport)
  73. }
  74. // Start tells the receiver to start its processing.
  75. // By convention the consumer of the received data is set when the receiver
  76. // instance is created.
  77. func (r *carbonReceiver) Start(_ context.Context, host component.Host) error {
  78. server, err := buildTransportServer(*r.config)
  79. if err != nil {
  80. return err
  81. }
  82. r.server = server
  83. go func() {
  84. if err := r.server.ListenAndServe(r.parser, r.nextConsumer, r.reporter); err != nil {
  85. host.ReportFatalError(err)
  86. }
  87. }()
  88. return nil
  89. }
  90. // Shutdown tells the receiver that should stop reception,
  91. // giving it a chance to perform any necessary clean-up.
  92. func (r *carbonReceiver) Shutdown(context.Context) error {
  93. if r.server == nil {
  94. return nil
  95. }
  96. return r.server.Close()
  97. }