carbon.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package datasenders // import "github.com/open-telemetry/opentelemetry-collector-contrib/testbed/datasenders"
  4. import (
  5. "context"
  6. "fmt"
  7. "time"
  8. "go.opentelemetry.io/collector/consumer"
  9. "go.opentelemetry.io/collector/exporter/exportertest"
  10. "go.uber.org/zap"
  11. "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/carbonexporter"
  12. "github.com/open-telemetry/opentelemetry-collector-contrib/testbed/testbed"
  13. )
  14. // CarbonDataSender implements MetricDataSender for Carbon metrics protocol.
  15. type CarbonDataSender struct {
  16. testbed.DataSenderBase
  17. consumer.Metrics
  18. }
  19. // Ensure CarbonDataSender implements MetricDataSenderOld.
  20. var _ testbed.MetricDataSender = (*CarbonDataSender)(nil)
  21. // NewCarbonDataSender creates a new Carbon metric protocol sender that will send
  22. // to the specified port after Start is called.
  23. func NewCarbonDataSender(port int) *CarbonDataSender {
  24. return &CarbonDataSender{
  25. DataSenderBase: testbed.DataSenderBase{
  26. Port: port,
  27. Host: testbed.DefaultHost,
  28. },
  29. }
  30. }
  31. // Start the sender.
  32. func (cs *CarbonDataSender) Start() error {
  33. factory := carbonexporter.NewFactory()
  34. cfg := &carbonexporter.Config{
  35. Endpoint: cs.GetEndpoint().String(),
  36. Timeout: 5 * time.Second,
  37. }
  38. params := exportertest.NewNopCreateSettings()
  39. params.Logger = zap.L()
  40. exporter, err := factory.CreateMetricsExporter(context.Background(), params, cfg)
  41. if err != nil {
  42. return err
  43. }
  44. cs.Metrics = exporter
  45. return nil
  46. }
  47. // GenConfigYAMLStr returns receiver config for the agent.
  48. func (cs *CarbonDataSender) GenConfigYAMLStr() string {
  49. // Note that this generates a receiver config for agent.
  50. return fmt.Sprintf(`
  51. carbon:
  52. endpoint: %s`, cs.GetEndpoint())
  53. }
  54. // ProtocolName returns protocol name as it is specified in Collector config.
  55. func (cs *CarbonDataSender) ProtocolName() string {
  56. return "carbon"
  57. }