GcpObservability: Use the observability config (#30932)

* GcpObservability: Use the observability config

* iwyu

* Fix build
pull/30961/head
Yash Tibrewal 2 years ago committed by GitHub
parent 6e57952589
commit b3ad894e05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      src/cpp/ext/gcp/BUILD
  2. 42
      src/cpp/ext/gcp/observability.cc
  3. 4
      src/cpp/ext/gcp/observability.h
  4. 7
      test/cpp/ext/gcp/observability_test.cc

@ -38,6 +38,8 @@ grpc_cc_library(
"observability.h", "observability.h",
], ],
external_deps = [ external_deps = [
"absl/status",
"absl/status:statusor",
"opencensus-trace", "opencensus-trace",
"opencensus-trace-stackdriver_exporter", "opencensus-trace-stackdriver_exporter",
"opencensus-stats-stackdriver_exporter", "opencensus-stats-stackdriver_exporter",
@ -46,7 +48,9 @@ grpc_cc_library(
tags = ["nofixdeps"], tags = ["nofixdeps"],
visibility = ["//test:__subpackages__"], visibility = ["//test:__subpackages__"],
deps = [ deps = [
":observability_config",
"//:gpr", "//:gpr",
"//:grpc++_public_hdrs",
"//:grpc_opencensus_plugin", "//:grpc_opencensus_plugin",
], ],
) )

@ -20,14 +20,19 @@
#include <stdint.h> #include <stdint.h>
#include <string>
#include <utility> #include <utility>
#include "absl/status/statusor.h"
#include "opencensus/exporters/stats/stackdriver/stackdriver_exporter.h" #include "opencensus/exporters/stats/stackdriver/stackdriver_exporter.h"
#include "opencensus/exporters/trace/stackdriver/stackdriver_exporter.h" #include "opencensus/exporters/trace/stackdriver/stackdriver_exporter.h"
#include "opencensus/trace/sampler.h" #include "opencensus/trace/sampler.h"
#include "opencensus/trace/trace_config.h" #include "opencensus/trace/trace_config.h"
#include <grpcpp/opencensus.h> #include <grpcpp/opencensus.h>
#include <grpcpp/support/config.h>
#include "src/cpp/ext/gcp/observability_config.h"
namespace grpc { namespace grpc {
namespace experimental { namespace experimental {
@ -42,23 +47,30 @@ constexpr uint32_t kMaxMessageEvents = 128;
constexpr uint32_t kMaxLinks = 128; constexpr uint32_t kMaxLinks = 128;
} // namespace } // namespace
void GcpObservabilityInit() { absl::Status GcpObservabilityInit() {
// TODO(yashykt): Add code for gRPC config parsing auto config = grpc::internal::GcpObservabilityConfig::ReadFromEnv();
if (!config.ok()) {
return config.status();
}
grpc::RegisterOpenCensusPlugin(); grpc::RegisterOpenCensusPlugin();
grpc::RegisterOpenCensusViewsForExport(); grpc::RegisterOpenCensusViewsForExport();
// TODO(yashykt): Setup tracing and stats exporting only if enabled in config. if (!config->cloud_trace.disabled) {
// TODO(yashykt): Get probability from config opencensus::trace::TraceConfig::SetCurrentTraceParams(
opencensus::trace::TraceConfig::SetCurrentTraceParams( {kMaxAttributes, kMaxAnnotations, kMaxMessageEvents, kMaxLinks,
{kMaxAttributes, kMaxAnnotations, kMaxMessageEvents, kMaxLinks, opencensus::trace::ProbabilitySampler(
opencensus::trace::ProbabilitySampler(1.0)}); config->cloud_trace.sampling_rate)});
opencensus::exporters::trace::StackdriverOptions trace_opts; opencensus::exporters::trace::StackdriverOptions trace_opts;
// TODO(yashykt): Set up project ID based on config trace_opts.project_id = config->project_id;
opencensus::exporters::trace::StackdriverExporter::Register( opencensus::exporters::trace::StackdriverExporter::Register(
std::move(trace_opts)); std::move(trace_opts));
opencensus::exporters::stats::StackdriverOptions stats_opts; }
// TODO(yashykt): Set up project ID based on config if (!config->cloud_monitoring.disabled) {
opencensus::exporters::stats::StackdriverExporter::Register( opencensus::exporters::stats::StackdriverOptions stats_opts;
std::move(stats_opts)); stats_opts.project_id = config->project_id;
opencensus::exporters::stats::StackdriverExporter::Register(
std::move(stats_opts));
}
return absl::Status();
} }
} // namespace experimental } // namespace experimental

@ -21,11 +21,13 @@
// instead of in src/, but I'm not yet sure about the naming, so keeping it here // instead of in src/, but I'm not yet sure about the naming, so keeping it here
// till we decide. // till we decide.
#include "absl/status/status.h"
namespace grpc { namespace grpc {
namespace experimental { namespace experimental {
// Initialize GCP Observability for gRPC. // Initialize GCP Observability for gRPC.
void GcpObservabilityInit(); absl::Status GcpObservabilityInit();
} // namespace experimental } // namespace experimental
} // namespace grpc } // namespace grpc

@ -24,7 +24,12 @@
namespace { namespace {
TEST(GcpObservabilityTest, RegistrationTest) { 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 } // namespace

Loading…
Cancel
Save