appendable.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package internal // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver/internal"
  4. import (
  5. "context"
  6. "regexp"
  7. "time"
  8. "github.com/prometheus/prometheus/model/labels"
  9. "github.com/prometheus/prometheus/storage"
  10. "go.opentelemetry.io/collector/consumer"
  11. "go.opentelemetry.io/collector/receiver"
  12. "go.opentelemetry.io/collector/receiver/receiverhelper"
  13. )
  14. // appendable translates Prometheus scraping diffs into OpenTelemetry format.
  15. type appendable struct {
  16. sink consumer.Metrics
  17. metricAdjuster MetricsAdjuster
  18. useStartTimeMetric bool
  19. trimSuffixes bool
  20. startTimeMetricRegex *regexp.Regexp
  21. externalLabels labels.Labels
  22. settings receiver.CreateSettings
  23. obsrecv *receiverhelper.ObsReport
  24. }
  25. // NewAppendable returns a storage.Appendable instance that emits metrics to the sink.
  26. func NewAppendable(
  27. sink consumer.Metrics,
  28. set receiver.CreateSettings,
  29. gcInterval time.Duration,
  30. useStartTimeMetric bool,
  31. startTimeMetricRegex *regexp.Regexp,
  32. useCreatedMetric bool,
  33. externalLabels labels.Labels,
  34. trimSuffixes bool) (storage.Appendable, error) {
  35. var metricAdjuster MetricsAdjuster
  36. if !useStartTimeMetric {
  37. metricAdjuster = NewInitialPointAdjuster(set.Logger, gcInterval, useCreatedMetric)
  38. } else {
  39. metricAdjuster = NewStartTimeMetricAdjuster(set.Logger, startTimeMetricRegex)
  40. }
  41. obsrecv, err := receiverhelper.NewObsReport(receiverhelper.ObsReportSettings{ReceiverID: set.ID, Transport: transport, ReceiverCreateSettings: set})
  42. if err != nil {
  43. return nil, err
  44. }
  45. return &appendable{
  46. sink: sink,
  47. settings: set,
  48. metricAdjuster: metricAdjuster,
  49. useStartTimeMetric: useStartTimeMetric,
  50. startTimeMetricRegex: startTimeMetricRegex,
  51. externalLabels: externalLabels,
  52. obsrecv: obsrecv,
  53. trimSuffixes: trimSuffixes,
  54. }, nil
  55. }
  56. func (o *appendable) Appender(ctx context.Context) storage.Appender {
  57. return newTransaction(ctx, o.metricAdjuster, o.sink, o.externalLabels, o.settings, o.obsrecv, o.trimSuffixes)
  58. }