factory.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package prometheusexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/prometheusexporter"
  4. import (
  5. "context"
  6. "time"
  7. "go.opentelemetry.io/collector/component"
  8. "go.opentelemetry.io/collector/exporter"
  9. "go.opentelemetry.io/collector/exporter/exporterhelper"
  10. "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/prometheusexporter/internal/metadata"
  11. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/resourcetotelemetry"
  12. )
  13. // NewFactory creates a new Prometheus exporter factory.
  14. func NewFactory() exporter.Factory {
  15. return exporter.NewFactory(
  16. metadata.Type,
  17. createDefaultConfig,
  18. exporter.WithMetrics(createMetricsExporter, metadata.MetricsStability))
  19. }
  20. func createDefaultConfig() component.Config {
  21. return &Config{
  22. ConstLabels: map[string]string{},
  23. SendTimestamps: false,
  24. MetricExpiration: time.Minute * 5,
  25. EnableOpenMetrics: false,
  26. AddMetricSuffixes: true,
  27. }
  28. }
  29. func createMetricsExporter(
  30. ctx context.Context,
  31. set exporter.CreateSettings,
  32. cfg component.Config,
  33. ) (exporter.Metrics, error) {
  34. pcfg := cfg.(*Config)
  35. prometheus, err := newPrometheusExporter(pcfg, set)
  36. if err != nil {
  37. return nil, err
  38. }
  39. exporter, err := exporterhelper.NewMetricsExporter(
  40. ctx,
  41. set,
  42. cfg,
  43. prometheus.ConsumeMetrics,
  44. exporterhelper.WithStart(prometheus.Start),
  45. exporterhelper.WithShutdown(prometheus.Shutdown),
  46. )
  47. if err != nil {
  48. return nil, err
  49. }
  50. return &wrapMetricsExporter{
  51. Metrics: resourcetotelemetry.WrapMetricsExporter(pcfg.ResourceToTelemetrySettings, exporter),
  52. exporter: prometheus,
  53. }, nil
  54. }
  55. type wrapMetricsExporter struct {
  56. exporter.Metrics
  57. exporter *prometheusExporter
  58. }