diff --git a/src/cpp/ext/gcp/BUILD b/src/cpp/ext/gcp/BUILD index e728e2d216e..edb4f385dc5 100644 --- a/src/cpp/ext/gcp/BUILD +++ b/src/cpp/ext/gcp/BUILD @@ -38,6 +38,8 @@ grpc_cc_library( "observability.h", ], external_deps = [ + "absl/status", + "absl/status:statusor", "opencensus-trace", "opencensus-trace-stackdriver_exporter", "opencensus-stats-stackdriver_exporter", @@ -46,7 +48,9 @@ grpc_cc_library( tags = ["nofixdeps"], visibility = ["//test:__subpackages__"], deps = [ + ":observability_config", "//:gpr", + "//:grpc++_public_hdrs", "//:grpc_opencensus_plugin", ], ) diff --git a/src/cpp/ext/gcp/observability.cc b/src/cpp/ext/gcp/observability.cc index 2ccb8f64e97..e5a2132b8d0 100644 --- a/src/cpp/ext/gcp/observability.cc +++ b/src/cpp/ext/gcp/observability.cc @@ -20,14 +20,19 @@ #include +#include #include +#include "absl/status/statusor.h" #include "opencensus/exporters/stats/stackdriver/stackdriver_exporter.h" #include "opencensus/exporters/trace/stackdriver/stackdriver_exporter.h" #include "opencensus/trace/sampler.h" #include "opencensus/trace/trace_config.h" #include +#include + +#include "src/cpp/ext/gcp/observability_config.h" namespace grpc { namespace experimental { @@ -42,23 +47,30 @@ constexpr uint32_t kMaxMessageEvents = 128; constexpr uint32_t kMaxLinks = 128; } // namespace -void GcpObservabilityInit() { - // TODO(yashykt): Add code for gRPC config parsing +absl::Status GcpObservabilityInit() { + auto config = grpc::internal::GcpObservabilityConfig::ReadFromEnv(); + if (!config.ok()) { + return config.status(); + } grpc::RegisterOpenCensusPlugin(); grpc::RegisterOpenCensusViewsForExport(); - // TODO(yashykt): Setup tracing and stats exporting only if enabled in config. - // TODO(yashykt): Get probability from config - opencensus::trace::TraceConfig::SetCurrentTraceParams( - {kMaxAttributes, kMaxAnnotations, kMaxMessageEvents, kMaxLinks, - opencensus::trace::ProbabilitySampler(1.0)}); - opencensus::exporters::trace::StackdriverOptions trace_opts; - // TODO(yashykt): Set up project ID based on config - opencensus::exporters::trace::StackdriverExporter::Register( - std::move(trace_opts)); - opencensus::exporters::stats::StackdriverOptions stats_opts; - // TODO(yashykt): Set up project ID based on config - opencensus::exporters::stats::StackdriverExporter::Register( - std::move(stats_opts)); + if (!config->cloud_trace.disabled) { + opencensus::trace::TraceConfig::SetCurrentTraceParams( + {kMaxAttributes, kMaxAnnotations, kMaxMessageEvents, kMaxLinks, + opencensus::trace::ProbabilitySampler( + config->cloud_trace.sampling_rate)}); + opencensus::exporters::trace::StackdriverOptions trace_opts; + trace_opts.project_id = config->project_id; + opencensus::exporters::trace::StackdriverExporter::Register( + std::move(trace_opts)); + } + if (!config->cloud_monitoring.disabled) { + opencensus::exporters::stats::StackdriverOptions stats_opts; + stats_opts.project_id = config->project_id; + opencensus::exporters::stats::StackdriverExporter::Register( + std::move(stats_opts)); + } + return absl::Status(); } } // namespace experimental diff --git a/src/cpp/ext/gcp/observability.h b/src/cpp/ext/gcp/observability.h index f5e401b875a..b562e1cabf5 100644 --- a/src/cpp/ext/gcp/observability.h +++ b/src/cpp/ext/gcp/observability.h @@ -21,11 +21,13 @@ // instead of in src/, but I'm not yet sure about the naming, so keeping it here // till we decide. +#include "absl/status/status.h" + namespace grpc { namespace experimental { // Initialize GCP Observability for gRPC. -void GcpObservabilityInit(); +absl::Status GcpObservabilityInit(); } // namespace experimental } // namespace grpc diff --git a/test/cpp/ext/gcp/observability_test.cc b/test/cpp/ext/gcp/observability_test.cc index 5c2cc387de0..47fb6706150 100644 --- a/test/cpp/ext/gcp/observability_test.cc +++ b/test/cpp/ext/gcp/observability_test.cc @@ -24,7 +24,12 @@ namespace { TEST(GcpObservabilityTest, RegistrationTest) { - grpc::experimental::GcpObservabilityInit(); + auto status = grpc::experimental::GcpObservabilityInit(); + EXPECT_EQ(status, + absl::FailedPreconditionError( + "Environment variables GRPC_OBSERVABILITY_CONFIG_FILE or " + "GRPC_OBSERVABILITY_CONFIG " + "not defined")); } } // namespace