GCP Observability: Add labels support for logging (#32130)

* GCP Observability: Add labels support for logging

* clang format

* Reviewer comments
pull/32149/head
Yash Tibrewal 2 years ago committed by GitHub
parent 60ec7b46f6
commit 21618f3aef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      src/cpp/ext/gcp/observability.cc
  2. 7
      src/cpp/ext/gcp/observability_logging_sink.cc
  3. 6
      src/cpp/ext/gcp/observability_logging_sink.h
  4. 20
      test/cpp/ext/gcp/observability_logging_sink_test.cc

@ -134,7 +134,7 @@ absl::Status GcpObservabilityInit() {
if (config->cloud_logging.has_value()) {
grpc::internal::RegisterLoggingFilter(
new grpc::internal::ObservabilityLoggingSink(
config->cloud_logging.value(), config->project_id));
config->cloud_logging.value(), config->project_id, config->labels));
}
return absl::OkStatus();
}

@ -48,8 +48,10 @@ namespace grpc {
namespace internal {
ObservabilityLoggingSink::ObservabilityLoggingSink(
GcpObservabilityConfig::CloudLogging logging_config, std::string project_id)
: project_id_(std::move(project_id)) {
GcpObservabilityConfig::CloudLogging logging_config, std::string project_id,
std::map<std::string, std::string> labels)
: project_id_(std::move(project_id)),
labels_(labels.begin(), labels.end()) {
for (auto& client_rpc_event_config : logging_config.client_rpc_events) {
client_configs_.emplace_back(client_rpc_event_config);
}
@ -250,6 +252,7 @@ void ObservabilityLoggingSink::LogEntry(Entry entry) {
absl::StrFormat("projects/{%s}/logs/"
"microservices.googleapis.com%%2Fobservability%%2fgrpc",
project_id_));
(*call->request.mutable_labels()).insert(labels_.begin(), labels_.end());
auto* proto_entry = call->request.add_entries();
// Fill the current timestamp
gpr_timespec timespec =

@ -23,8 +23,10 @@
#include <stdint.h>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <google/protobuf/struct.pb.h>
@ -43,7 +45,8 @@ namespace internal {
class ObservabilityLoggingSink : public LoggingSink {
public:
ObservabilityLoggingSink(GcpObservabilityConfig::CloudLogging logging_config,
std::string project_id);
std::string project_id,
std::map<std::string, std::string> labels);
~ObservabilityLoggingSink() override = default;
@ -71,6 +74,7 @@ class ObservabilityLoggingSink : public LoggingSink {
std::vector<Configuration> server_configs_;
std::string project_id_;
std::string authority_;
std::vector<std::pair<std::string, std::string>> labels_;
absl::once_flag once_;
std::unique_ptr<google::logging::v2::LoggingServiceV2::StubInterface> stub_;
};

@ -38,7 +38,7 @@ TEST(GcpObservabilityLoggingSinkTest, LoggingConfigEmpty) {
auto config = grpc_core::LoadFromJson<GcpObservabilityConfig>(
*json, grpc_core::JsonArgs(), &errors);
ASSERT_TRUE(errors.ok()) << errors.status("unexpected errors");
ObservabilityLoggingSink sink(config.cloud_logging.value(), "test");
ObservabilityLoggingSink sink(config.cloud_logging.value(), "test", {});
// client test
EXPECT_FALSE(sink.FindMatch(true, "foo", "bar").ShouldLog());
// server test
@ -63,7 +63,7 @@ TEST(GcpObservabilityLoggingSinkTest, LoggingConfigClientWildCardEntries) {
auto config = grpc_core::LoadFromJson<GcpObservabilityConfig>(
*json, grpc_core::JsonArgs(), &errors);
ASSERT_TRUE(errors.ok()) << errors.status("unexpected errors");
ObservabilityLoggingSink sink(config.cloud_logging.value(), "test");
ObservabilityLoggingSink sink(config.cloud_logging.value(), "test", {});
// client test
EXPECT_EQ(sink.FindMatch(true, "foo", "bar"),
LoggingSink::Config(1024, 4096));
@ -89,7 +89,7 @@ TEST(GcpObservabilityLoggingSinkTest, LoggingConfigBadPath) {
auto config = grpc_core::LoadFromJson<GcpObservabilityConfig>(
*json, grpc_core::JsonArgs(), &errors);
ASSERT_TRUE(errors.ok()) << errors.status("unexpected errors");
ObservabilityLoggingSink sink(config.cloud_logging.value(), "test");
ObservabilityLoggingSink sink(config.cloud_logging.value(), "test", {});
EXPECT_FALSE(sink.FindMatch(true, "foo", "").ShouldLog());
}
@ -112,7 +112,7 @@ TEST(GcpObservabilityLoggingSinkTest,
auto config = grpc_core::LoadFromJson<GcpObservabilityConfig>(
*json, grpc_core::JsonArgs(), &errors);
ASSERT_TRUE(errors.ok()) << errors.status("unexpected errors");
ObservabilityLoggingSink sink(config.cloud_logging.value(), "test");
ObservabilityLoggingSink sink(config.cloud_logging.value(), "test", {});
// client test
EXPECT_EQ(sink.FindMatch(true, "service", "bar"),
LoggingSink::Config(1024, 4096));
@ -141,7 +141,7 @@ TEST(GcpObservabilityLoggingSinkTest,
auto config = grpc_core::LoadFromJson<GcpObservabilityConfig>(
*json, grpc_core::JsonArgs(), &errors);
ASSERT_TRUE(errors.ok()) << errors.status("unexpected errors");
ObservabilityLoggingSink sink(config.cloud_logging.value(), "test");
ObservabilityLoggingSink sink(config.cloud_logging.value(), "test", {});
// client test
EXPECT_EQ(sink.FindMatch(true, "foo", "bar"),
LoggingSink::Config(1024, 4096));
@ -175,7 +175,7 @@ TEST(GcpObservabilityLoggingSinkTest, LoggingConfigClientMultipleEventEntries) {
auto config = grpc_core::LoadFromJson<GcpObservabilityConfig>(
*json, grpc_core::JsonArgs(), &errors);
ASSERT_TRUE(errors.ok()) << errors.status("unexpected errors");
ObservabilityLoggingSink sink(config.cloud_logging.value(), "test");
ObservabilityLoggingSink sink(config.cloud_logging.value(), "test", {});
// client test
EXPECT_EQ(sink.FindMatch(true, "foo", "bar"),
LoggingSink::Config(1024, 4096));
@ -203,7 +203,7 @@ TEST(GcpObservabilityLoggingSinkTest, LoggingConfigServerWildCardEntries) {
auto config = grpc_core::LoadFromJson<GcpObservabilityConfig>(
*json, grpc_core::JsonArgs(), &errors);
ASSERT_TRUE(errors.ok()) << errors.status("unexpected errors");
ObservabilityLoggingSink sink(config.cloud_logging.value(), "test");
ObservabilityLoggingSink sink(config.cloud_logging.value(), "test", {});
// client test
EXPECT_FALSE(sink.FindMatch(true, "foo", "bar").ShouldLog());
// server test
@ -230,7 +230,7 @@ TEST(GcpObservabilityLoggingSinkTest,
auto config = grpc_core::LoadFromJson<GcpObservabilityConfig>(
*json, grpc_core::JsonArgs(), &errors);
ASSERT_TRUE(errors.ok()) << errors.status("unexpected errors");
ObservabilityLoggingSink sink(config.cloud_logging.value(), "test");
ObservabilityLoggingSink sink(config.cloud_logging.value(), "test", {});
// client test
EXPECT_FALSE(sink.FindMatch(true, "service", "bar").ShouldLog());
EXPECT_FALSE(sink.FindMatch(true, "foo", "bar").ShouldLog());
@ -259,7 +259,7 @@ TEST(GcpObservabilityLoggingSinkTest,
auto config = grpc_core::LoadFromJson<GcpObservabilityConfig>(
*json, grpc_core::JsonArgs(), &errors);
ASSERT_TRUE(errors.ok()) << errors.status("unexpected errors");
ObservabilityLoggingSink sink(config.cloud_logging.value(), "test");
ObservabilityLoggingSink sink(config.cloud_logging.value(), "test", {});
// client test
EXPECT_FALSE(sink.FindMatch(true, "foo", "bar").ShouldLog());
EXPECT_FALSE(sink.FindMatch(true, "foo", "baz").ShouldLog());
@ -293,7 +293,7 @@ TEST(GcpObservabilityLoggingSinkTest, LoggingConfigServerMultipleEventEntries) {
auto config = grpc_core::LoadFromJson<GcpObservabilityConfig>(
*json, grpc_core::JsonArgs(), &errors);
ASSERT_TRUE(errors.ok()) << errors.status("unexpected errors");
ObservabilityLoggingSink sink(config.cloud_logging.value(), "test");
ObservabilityLoggingSink sink(config.cloud_logging.value(), "test", {});
// client test
EXPECT_FALSE(sink.FindMatch(true, "foo", "bar").ShouldLog());
EXPECT_FALSE(sink.FindMatch(true, "foo", "baz").ShouldLog());

Loading…
Cancel
Save