exporter_test.cc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include <gtest/gtest.h>
  4. #include <string>
  5. #include "opentelemetry/exporters/prometheus/exporter.h"
  6. #include "opentelemetry/exporters/prometheus/exporter_options.h"
  7. #include "opentelemetry/sdk/metrics/instruments.h"
  8. /**
  9. * PrometheusExporterTest is a friend class of PrometheusExporter.
  10. * It has access to a private constructor that does not take in
  11. * an exposer as an argument, and instead takes no arguments; this
  12. * private constructor is only to be used here for testing
  13. */
  14. using opentelemetry::exporter::metrics::PrometheusExporter;
  15. using opentelemetry::exporter::metrics::PrometheusExporterOptions;
  16. using opentelemetry::sdk::metrics::AggregationTemporality;
  17. using opentelemetry::sdk::metrics::InstrumentType;
  18. /**
  19. * When a PrometheusExporter is initialized,
  20. * isShutdown should be false.
  21. */
  22. TEST(PrometheusExporter, InitializeConstructorIsNotShutdown)
  23. {
  24. PrometheusExporterOptions options;
  25. options.url = "localhost:8081";
  26. PrometheusExporter exporter(options);
  27. // // Asserts that the exporter is not shutdown.
  28. // ASSERT_TRUE(!exporter.IsShutdown());
  29. exporter.Shutdown();
  30. }
  31. /**
  32. * The shutdown() function should set the isShutdown field to true.
  33. */
  34. TEST(PrometheusExporter, ShutdownSetsIsShutdownToTrue)
  35. {
  36. PrometheusExporterOptions options;
  37. PrometheusExporter exporter(options);
  38. // exporter shuold not be shutdown by default
  39. ASSERT_TRUE(!exporter.IsShutdown());
  40. exporter.Shutdown();
  41. // the exporter shuold be shutdown
  42. ASSERT_TRUE(exporter.IsShutdown());
  43. // shutdown function should be idempotent
  44. exporter.Shutdown();
  45. ASSERT_TRUE(exporter.IsShutdown());
  46. }
  47. /**
  48. * The Aggregation temporality should be correctly set
  49. */
  50. TEST(PrometheusExporter, CheckAggregationTemporality)
  51. {
  52. PrometheusExporterOptions options;
  53. PrometheusExporter exporter(options);
  54. ASSERT_EQ(exporter.GetAggregationTemporality(InstrumentType::kCounter),
  55. AggregationTemporality::kCumulative);
  56. ASSERT_EQ(exporter.GetAggregationTemporality(InstrumentType::kHistogram),
  57. AggregationTemporality::kCumulative);
  58. ASSERT_EQ(exporter.GetAggregationTemporality(InstrumentType::kObservableCounter),
  59. AggregationTemporality::kCumulative);
  60. ASSERT_EQ(exporter.GetAggregationTemporality(InstrumentType::kObservableGauge),
  61. AggregationTemporality::kCumulative);
  62. ASSERT_EQ(exporter.GetAggregationTemporality(InstrumentType::kObservableUpDownCounter),
  63. AggregationTemporality::kCumulative);
  64. ASSERT_EQ(exporter.GetAggregationTemporality(InstrumentType::kUpDownCounter),
  65. AggregationTemporality::kCumulative);
  66. }