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",
],
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",
],
)

@ -20,14 +20,19 @@
#include <stdint.h>
#include <string>
#include <utility>
#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 <grpcpp/opencensus.h>
#include <grpcpp/support/config.h>
#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

@ -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

@ -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

Loading…
Cancel
Save