From d23d4854bd269de45a6b53420d3ee9a5a5462bc5 Mon Sep 17 00:00:00 2001 From: dbolduc Date: Mon, 9 Sep 2024 15:01:46 -0400 Subject: [PATCH 1/2] feat(otel): release GCM exporter --- .../monitoring_exporter_integration_test.cc | 3 +- .../opentelemetry/monitoring_exporter.cc | 11 +++--- .../cloud/opentelemetry/monitoring_exporter.h | 4 +-- .../opentelemetry/monitoring_exporter_test.cc | 34 +++++++++---------- .../internal/grpc/metrics_exporter_impl.cc | 10 +++--- .../grpc/metrics_exporter_impl_test.cc | 27 +++++---------- .../internal/grpc/metrics_exporter_options.cc | 7 ++-- .../grpc/metrics_exporter_options_test.cc | 8 ++--- 8 files changed, 47 insertions(+), 57 deletions(-) diff --git a/google/cloud/opentelemetry/integration_tests/monitoring_exporter_integration_test.cc b/google/cloud/opentelemetry/integration_tests/monitoring_exporter_integration_test.cc index 527c123a45b0a..8c93dbd9fdc2d 100644 --- a/google/cloud/opentelemetry/integration_tests/monitoring_exporter_integration_test.cc +++ b/google/cloud/opentelemetry/integration_tests/monitoring_exporter_integration_test.cc @@ -114,8 +114,7 @@ TEST(MonitoringExporter, Basic) { auto project = Project(project_id); auto conn = monitoring_v3::MakeMetricServiceConnection(); auto client = monitoring_v3::MetricServiceClient(conn); - auto exporter = - otel_internal::MakeMonitoringExporter(project, std::move(conn)); + auto exporter = MakeMonitoringExporter(project, std::move(conn)); InstallExporter(std::move(exporter), task_id); // Perform work which creates telemetry. An export should happen. diff --git a/google/cloud/opentelemetry/monitoring_exporter.cc b/google/cloud/opentelemetry/monitoring_exporter.cc index 41f7810fde4d6..63c5da80efcff 100644 --- a/google/cloud/opentelemetry/monitoring_exporter.cc +++ b/google/cloud/opentelemetry/monitoring_exporter.cc @@ -22,7 +22,7 @@ namespace google { namespace cloud { -namespace otel_internal { +namespace otel { GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN namespace { @@ -73,14 +73,15 @@ class MonitoringExporter final opentelemetry::sdk::metrics::ResourceMetrics const& data) { auto result = opentelemetry::sdk::common::ExportResult::kSuccess; - auto tss = ToTimeSeries(data, formatter_); + auto tss = otel_internal::ToTimeSeries(data, formatter_); if (tss.empty()) { GCP_LOG(INFO) << "Cloud Monitoring Export skipped. No data."; // Return early to save the littlest bit of processing. return result; } - auto mr = ToMonitoredResource(data, mr_proto_); - auto requests = ToRequests(project_.FullName(), mr, std::move(tss)); + auto mr = otel_internal::ToMonitoredResource(data, mr_proto_); + auto requests = + otel_internal::ToRequests(project_.FullName(), mr, std::move(tss)); for (auto& request : requests) { auto status = use_service_time_series_ ? client_.CreateServiceTimeSeries(request) @@ -121,6 +122,6 @@ MakeMonitoringExporter( } GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END -} // namespace otel_internal +} // namespace otel } // namespace cloud } // namespace google diff --git a/google/cloud/opentelemetry/monitoring_exporter.h b/google/cloud/opentelemetry/monitoring_exporter.h index af7bbb893e2a4..170ad3323dcac 100644 --- a/google/cloud/opentelemetry/monitoring_exporter.h +++ b/google/cloud/opentelemetry/monitoring_exporter.h @@ -26,7 +26,7 @@ namespace google { namespace cloud { -namespace otel_internal { +namespace otel { GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN /** @@ -82,7 +82,7 @@ MakeMonitoringExporter( Options options = {}); GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END -} // namespace otel_internal +} // namespace otel } // namespace cloud } // namespace google diff --git a/google/cloud/opentelemetry/monitoring_exporter_test.cc b/google/cloud/opentelemetry/monitoring_exporter_test.cc index cf31389a3e06f..b413ddee55499 100644 --- a/google/cloud/opentelemetry/monitoring_exporter_test.cc +++ b/google/cloud/opentelemetry/monitoring_exporter_test.cc @@ -106,8 +106,8 @@ TEST(MonitoringExporter, ExportSuccess) { return Status(); }); - auto exporter = otel_internal::MakeMonitoringExporter(Project("test-project"), - std::move(mock)); + auto exporter = + MakeMonitoringExporter(Project("test-project"), std::move(mock)); auto const expected_time_series_count = kMaxTimeSeriesPerRequest + 1; auto data = MakeResourceMetrics(expected_time_series_count); auto result = exporter->Export(data); @@ -121,8 +121,8 @@ TEST(MonitoringExporter, ExportSkippedIfNoTimeSeries) { std::make_shared(); EXPECT_CALL(*mock, CreateTimeSeries).Times(0); - auto exporter = otel_internal::MakeMonitoringExporter(Project("test-project"), - std::move(mock)); + auto exporter = + MakeMonitoringExporter(Project("test-project"), std::move(mock)); auto data = MakeResourceMetrics(/*expected_time_series_count=*/0); auto result = exporter->Export(data); EXPECT_EQ(result, opentelemetry::sdk::common::ExportResult::kSuccess); @@ -161,8 +161,8 @@ TEST(MonitoringExporter, ExportFailure) { return internal::PermissionDeniedError("nope"); }); - auto exporter = otel_internal::MakeMonitoringExporter(Project("test-project"), - std::move(mock)); + auto exporter = + MakeMonitoringExporter(Project("test-project"), std::move(mock)); auto const expected_time_series_count = 2 * kMaxTimeSeriesPerRequest + 1; auto data = MakeResourceMetrics(expected_time_series_count); auto result = exporter->Export(data); @@ -203,8 +203,8 @@ TEST(MonitoringExporter, ExportFailureWithInvalidArgument) { return Status(); }); - auto exporter = otel_internal::MakeMonitoringExporter(Project("test-project"), - std::move(mock)); + auto exporter = + MakeMonitoringExporter(Project("test-project"), std::move(mock)); auto const expected_time_series_count = 2 * kMaxTimeSeriesPerRequest + 1; auto data = MakeResourceMetrics(expected_time_series_count); auto result = exporter->Export(data); @@ -230,10 +230,10 @@ TEST(MonitoringExporter, CustomFormatter) { return Status(); }); - auto options = Options{}.set( + auto options = Options{}.set( [](std::string const& s) { return "custom.googleapis.com/" + s; }); - auto exporter = otel_internal::MakeMonitoringExporter( - Project("test-project"), std::move(mock), options); + auto exporter = + MakeMonitoringExporter(Project("test-project"), std::move(mock), options); auto data = MakeResourceMetrics(/*expected_time_series_count=*/2); auto result = exporter->Export(data); EXPECT_EQ(result, opentelemetry::sdk::common::ExportResult::kSuccess); @@ -256,9 +256,9 @@ TEST(MonitoringExporter, CustomMonitoredResource) { google::api::MonitoredResource custom; custom.set_type("gcs_client"); - auto options = Options{}.set(custom); - auto exporter = otel_internal::MakeMonitoringExporter( - Project("test-project"), std::move(mock), options); + auto options = Options{}.set(custom); + auto exporter = + MakeMonitoringExporter(Project("test-project"), std::move(mock), options); auto data = MakeResourceMetrics(/*expected_time_series_count=*/2); auto result = exporter->Export(data); EXPECT_EQ(result, opentelemetry::sdk::common::ExportResult::kSuccess); @@ -275,9 +275,9 @@ TEST(MonitoringExporter, CreateServiceTimeSeries) { return Status(); }); - auto options = Options{}.set(true); - auto exporter = otel_internal::MakeMonitoringExporter( - Project("test-project"), std::move(mock), options); + auto options = Options{}.set(true); + auto exporter = + MakeMonitoringExporter(Project("test-project"), std::move(mock), options); auto data = MakeResourceMetrics(/*expected_time_series_count=*/2); auto result = exporter->Export(data); EXPECT_EQ(result, opentelemetry::sdk::common::ExportResult::kSuccess); diff --git a/google/cloud/storage/internal/grpc/metrics_exporter_impl.cc b/google/cloud/storage/internal/grpc/metrics_exporter_impl.cc index 8f52d26b2e91a..fe0038a01a518 100644 --- a/google/cloud/storage/internal/grpc/metrics_exporter_impl.cc +++ b/google/cloud/storage/internal/grpc/metrics_exporter_impl.cc @@ -101,11 +101,11 @@ absl::optional MakeMeterProviderConfig( void EnableGrpcMetricsImpl(ExporterConfig config) { if (!ExporterRegistry::Singleton().Register(config.authority)) return; - auto exporter = otel_internal::MakeMonitoringExporter( - std::move(config.project), - monitoring_v3::MakeMetricServiceConnection( - std::move(config.exporter_connection_options)), - config.exporter_options); + auto exporter = + MakeMonitoringExporter(std::move(config.project), + monitoring_v3::MakeMetricServiceConnection( + std::move(config.exporter_connection_options)), + config.exporter_options); auto provider = MakeGrpcMeterProvider(std::move(exporter), std::move(config.reader_options)); diff --git a/google/cloud/storage/internal/grpc/metrics_exporter_impl_test.cc b/google/cloud/storage/internal/grpc/metrics_exporter_impl_test.cc index 145d43efc4b07..4f9dede745339 100644 --- a/google/cloud/storage/internal/grpc/metrics_exporter_impl_test.cc +++ b/google/cloud/storage/internal/grpc/metrics_exporter_impl_test.cc @@ -70,12 +70,9 @@ TEST(GrpcMetricsExporter, EnabledResourceProject) { auto config = MakeMeterProviderConfig(FullResource(), TestOptions()); ASSERT_TRUE(config.has_value()); EXPECT_EQ(config->project, Project("project-id-resource")); - EXPECT_TRUE( - config->exporter_options.has()); - EXPECT_TRUE( - config->exporter_options.has()); - EXPECT_TRUE( - config->exporter_options.get()); + EXPECT_TRUE(config->exporter_options.has()); + EXPECT_TRUE(config->exporter_options.has()); + EXPECT_TRUE(config->exporter_options.get()); EXPECT_GT(config->reader_options.export_interval_millis, std::chrono::milliseconds(0)); EXPECT_GT(config->reader_options.export_timeout_millis, @@ -88,12 +85,9 @@ TEST(GrpcMetricsExporter, EnabledOptionsProject) { TestOptions().set("project-id-option")); ASSERT_TRUE(config.has_value()); EXPECT_EQ(config->project, Project("project-id-option")); - EXPECT_TRUE( - config->exporter_options.has()); - EXPECT_TRUE( - config->exporter_options.has()); - EXPECT_TRUE( - config->exporter_options.get()); + EXPECT_TRUE(config->exporter_options.has()); + EXPECT_TRUE(config->exporter_options.has()); + EXPECT_TRUE(config->exporter_options.get()); EXPECT_GT(config->reader_options.export_interval_millis, std::chrono::milliseconds(0)); EXPECT_GT(config->reader_options.export_timeout_millis, @@ -107,12 +101,9 @@ TEST(GrpcMetricsExporter, EnabledWithTimeout) { std::chrono::seconds(45))); ASSERT_TRUE(config.has_value()); EXPECT_EQ(config->project, Project("project-id-resource")); - EXPECT_TRUE( - config->exporter_options.has()); - EXPECT_TRUE( - config->exporter_options.has()); - EXPECT_TRUE( - config->exporter_options.get()); + EXPECT_TRUE(config->exporter_options.has()); + EXPECT_TRUE(config->exporter_options.has()); + EXPECT_TRUE(config->exporter_options.get()); EXPECT_EQ(config->reader_options.export_interval_millis, std::chrono::seconds(45)); EXPECT_GT(config->reader_options.export_timeout_millis, diff --git a/google/cloud/storage/internal/grpc/metrics_exporter_options.cc b/google/cloud/storage/internal/grpc/metrics_exporter_options.cc index 129981d7f69c5..8c483c34b161d 100644 --- a/google/cloud/storage/internal/grpc/metrics_exporter_options.cc +++ b/google/cloud/storage/internal/grpc/metrics_exporter_options.cc @@ -70,13 +70,12 @@ Options MetricsExporterOptions( labels["api"] = "GRPC"; return Options{} - .set(true) - .set([](std::string s) { + .set(true) + .set([](std::string s) { std::replace(s.begin(), s.end(), '.', '/'); return "storage.googleapis.com/client/" + std::move(s); }) - .set( - std::move(monitored_resource)); + .set(std::move(monitored_resource)); } Options MetricsExporterConnectionOptions(Options const& options) { diff --git a/google/cloud/storage/internal/grpc/metrics_exporter_options_test.cc b/google/cloud/storage/internal/grpc/metrics_exporter_options_test.cc index 5b96f2cbe5a9f..dae121c2924fa 100644 --- a/google/cloud/storage/internal/grpc/metrics_exporter_options_test.cc +++ b/google/cloud/storage/internal/grpc/metrics_exporter_options_test.cc @@ -139,8 +139,8 @@ TEST(MetricsExporterOptions, MonitoredResource) { {"host.id", "test-host-id"}, })); - EXPECT_TRUE(actual.has()); - auto mr = actual.get(); + EXPECT_TRUE(actual.has()); + auto mr = actual.get(); auto labels = mr.labels(); // The `instance_id` label has unpredictable values, EXPECT_THAT(labels, Contains(Pair("instance_id", MatchesInstanceId()))); @@ -164,8 +164,8 @@ TEST(MetricsExporterOptions, DefaultMonitoredResource) { Project("test-project"), opentelemetry::sdk::resource::Resource::Create({})); - EXPECT_TRUE(actual.has()); - auto mr = actual.get(); + EXPECT_TRUE(actual.has()); + auto mr = actual.get(); auto labels = mr.labels(); // The `instance_id` label has unpredictable values, EXPECT_THAT(labels, Contains(Pair("instance_id", MatchesInstanceId()))); From ee4b96706f0de3fefbf3832e42be0dfcc9fe46bf Mon Sep 17 00:00:00 2001 From: dbolduc Date: Mon, 9 Sep 2024 15:46:38 -0400 Subject: [PATCH 2/2] d'oh, storage != otel --- .../internal/grpc/metrics_exporter_impl.cc | 10 +++++----- .../grpc/metrics_exporter_impl_test.cc | 18 +++++++++--------- .../internal/grpc/metrics_exporter_options.cc | 6 +++--- .../grpc/metrics_exporter_options_test.cc | 8 ++++---- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/google/cloud/storage/internal/grpc/metrics_exporter_impl.cc b/google/cloud/storage/internal/grpc/metrics_exporter_impl.cc index fe0038a01a518..9e883d20852bd 100644 --- a/google/cloud/storage/internal/grpc/metrics_exporter_impl.cc +++ b/google/cloud/storage/internal/grpc/metrics_exporter_impl.cc @@ -101,11 +101,11 @@ absl::optional MakeMeterProviderConfig( void EnableGrpcMetricsImpl(ExporterConfig config) { if (!ExporterRegistry::Singleton().Register(config.authority)) return; - auto exporter = - MakeMonitoringExporter(std::move(config.project), - monitoring_v3::MakeMetricServiceConnection( - std::move(config.exporter_connection_options)), - config.exporter_options); + auto exporter = otel::MakeMonitoringExporter( + std::move(config.project), + monitoring_v3::MakeMetricServiceConnection( + std::move(config.exporter_connection_options)), + config.exporter_options); auto provider = MakeGrpcMeterProvider(std::move(exporter), std::move(config.reader_options)); diff --git a/google/cloud/storage/internal/grpc/metrics_exporter_impl_test.cc b/google/cloud/storage/internal/grpc/metrics_exporter_impl_test.cc index 4f9dede745339..59348cc42763a 100644 --- a/google/cloud/storage/internal/grpc/metrics_exporter_impl_test.cc +++ b/google/cloud/storage/internal/grpc/metrics_exporter_impl_test.cc @@ -70,9 +70,9 @@ TEST(GrpcMetricsExporter, EnabledResourceProject) { auto config = MakeMeterProviderConfig(FullResource(), TestOptions()); ASSERT_TRUE(config.has_value()); EXPECT_EQ(config->project, Project("project-id-resource")); - EXPECT_TRUE(config->exporter_options.has()); - EXPECT_TRUE(config->exporter_options.has()); - EXPECT_TRUE(config->exporter_options.get()); + EXPECT_TRUE(config->exporter_options.has()); + EXPECT_TRUE(config->exporter_options.has()); + EXPECT_TRUE(config->exporter_options.get()); EXPECT_GT(config->reader_options.export_interval_millis, std::chrono::milliseconds(0)); EXPECT_GT(config->reader_options.export_timeout_millis, @@ -85,9 +85,9 @@ TEST(GrpcMetricsExporter, EnabledOptionsProject) { TestOptions().set("project-id-option")); ASSERT_TRUE(config.has_value()); EXPECT_EQ(config->project, Project("project-id-option")); - EXPECT_TRUE(config->exporter_options.has()); - EXPECT_TRUE(config->exporter_options.has()); - EXPECT_TRUE(config->exporter_options.get()); + EXPECT_TRUE(config->exporter_options.has()); + EXPECT_TRUE(config->exporter_options.has()); + EXPECT_TRUE(config->exporter_options.get()); EXPECT_GT(config->reader_options.export_interval_millis, std::chrono::milliseconds(0)); EXPECT_GT(config->reader_options.export_timeout_millis, @@ -101,9 +101,9 @@ TEST(GrpcMetricsExporter, EnabledWithTimeout) { std::chrono::seconds(45))); ASSERT_TRUE(config.has_value()); EXPECT_EQ(config->project, Project("project-id-resource")); - EXPECT_TRUE(config->exporter_options.has()); - EXPECT_TRUE(config->exporter_options.has()); - EXPECT_TRUE(config->exporter_options.get()); + EXPECT_TRUE(config->exporter_options.has()); + EXPECT_TRUE(config->exporter_options.has()); + EXPECT_TRUE(config->exporter_options.get()); EXPECT_EQ(config->reader_options.export_interval_millis, std::chrono::seconds(45)); EXPECT_GT(config->reader_options.export_timeout_millis, diff --git a/google/cloud/storage/internal/grpc/metrics_exporter_options.cc b/google/cloud/storage/internal/grpc/metrics_exporter_options.cc index 8c483c34b161d..f573eb6cc0248 100644 --- a/google/cloud/storage/internal/grpc/metrics_exporter_options.cc +++ b/google/cloud/storage/internal/grpc/metrics_exporter_options.cc @@ -70,12 +70,12 @@ Options MetricsExporterOptions( labels["api"] = "GRPC"; return Options{} - .set(true) - .set([](std::string s) { + .set(true) + .set([](std::string s) { std::replace(s.begin(), s.end(), '.', '/'); return "storage.googleapis.com/client/" + std::move(s); }) - .set(std::move(monitored_resource)); + .set(std::move(monitored_resource)); } Options MetricsExporterConnectionOptions(Options const& options) { diff --git a/google/cloud/storage/internal/grpc/metrics_exporter_options_test.cc b/google/cloud/storage/internal/grpc/metrics_exporter_options_test.cc index dae121c2924fa..e3244780d66f9 100644 --- a/google/cloud/storage/internal/grpc/metrics_exporter_options_test.cc +++ b/google/cloud/storage/internal/grpc/metrics_exporter_options_test.cc @@ -139,8 +139,8 @@ TEST(MetricsExporterOptions, MonitoredResource) { {"host.id", "test-host-id"}, })); - EXPECT_TRUE(actual.has()); - auto mr = actual.get(); + EXPECT_TRUE(actual.has()); + auto mr = actual.get(); auto labels = mr.labels(); // The `instance_id` label has unpredictable values, EXPECT_THAT(labels, Contains(Pair("instance_id", MatchesInstanceId()))); @@ -164,8 +164,8 @@ TEST(MetricsExporterOptions, DefaultMonitoredResource) { Project("test-project"), opentelemetry::sdk::resource::Resource::Create({})); - EXPECT_TRUE(actual.has()); - auto mr = actual.get(); + EXPECT_TRUE(actual.has()); + auto mr = actual.get(); auto labels = mr.labels(); // The `instance_id` label has unpredictable values, EXPECT_THAT(labels, Contains(Pair("instance_id", MatchesInstanceId())));