Skip to content

Commit

Permalink
inital code
Browse files Browse the repository at this point in the history
add custom stats prefix

Signed-off-by: Xuyang Tao <taoxuy@google.com>
  • Loading branch information
TAOXUY committed May 16, 2024
1 parent 3e75c6b commit 75414e8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ message OpenTelemetryAccessLogConfig {
// See 'attributes' in the LogResource proto for more details.
// Example: ``attributes { values { key: "user_agent" value { string_value: "%REQ(USER-AGENT)%" } } }``.
opentelemetry.proto.common.v1.KeyValueList attributes = 3;

// Optional prefix to use on OpenTelemetry access logger stats. If empty, the stats will be rooted at
// ``access_logs.open_telemetry_access_log.``. If non-empty, stats will be rooted at
// ``access_logs.open_telemetry_access_log.<stat_prefix>.``.
string stat_prefix = 6;
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ GrpcAccessLoggerImpl::GrpcAccessLoggerImpl(
*Protobuf::DescriptorPool::generated_pool()->FindMethodByName(
"opentelemetry.proto.collector.logs.v1.LogsService.Export"),
GrpcCommon::optionalRetryPolicy(config.common_config()), genOTelCallbacksFactory())),
stats_({ALL_GRPC_ACCESS_LOGGER_STATS(POOL_COUNTER_PREFIX(scope, GRPC_LOG_STATS_PREFIX))}) {
stats_({ALL_GRPC_ACCESS_LOGGER_STATS(
POOL_COUNTER_PREFIX(scope, absl::StrCat(GRPC_LOG_STATS_PREFIX, config.stat_prefix())))}) {
initMessageRoot(config, local_info);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ class GrpcAccessLoggerImplTest : public testing::Test {
config_.mutable_common_config()->mutable_buffer_size_bytes()->set_value(BUFFER_SIZE_BYTES);
config_.mutable_common_config()->mutable_buffer_flush_interval()->set_nanos(
std::chrono::duration_cast<std::chrono::nanoseconds>(FlushInterval).count());
logger_ =
std::make_unique<GrpcAccessLoggerImpl>(Grpc::RawAsyncClientPtr{async_client_}, config_,
dispatcher_, local_info_, *stats_store_.rootScope());
}

Grpc::MockAsyncClient* async_client_;
Expand All @@ -103,9 +100,16 @@ class GrpcAccessLoggerImplTest : public testing::Test {
std::unique_ptr<GrpcAccessLoggerImpl> logger_;
GrpcAccessLoggerImplTestHelper grpc_access_logger_impl_test_helper_;
envoy::extensions::access_loggers::open_telemetry::v3::OpenTelemetryAccessLogConfig config_;

void setUpLogger() {
logger_ =
std::make_unique<GrpcAccessLoggerImpl>(Grpc::RawAsyncClientPtr{async_client_}, config_,
dispatcher_, local_info_, *stats_store_.rootScope());
}
};

TEST_F(GrpcAccessLoggerImplTest, Log) {
setUpLogger();
grpc_access_logger_impl_test_helper_.expectSentMessage(R"EOF(
resource_logs:
resource:
Expand Down Expand Up @@ -144,6 +148,7 @@ TEST_F(GrpcAccessLoggerImplTest, Log) {
}

TEST_F(GrpcAccessLoggerImplTest, LogWithStats) {
setUpLogger();
std::string expected_message_yaml = R"EOF(
resource_logs:
resource:
Expand Down Expand Up @@ -192,6 +197,45 @@ TEST_F(GrpcAccessLoggerImplTest, LogWithStats) {
.get()
.value(),
1);
EXPECT_EQ(stats_store_.findCounterByString("access_logs.open_telemetry_access_log.logs_written")
.value()
.get()
.value(),
1);
}

TEST_F(GrpcAccessLoggerImplTest, StatsWithCustomPrefix) {
*config_.mutable_stat_prefix() = "custom.";
setUpLogger();
grpc_access_logger_impl_test_helper_.expectSentMessage(R"EOF(
resource_logs:
resource:
attributes:
- key: "log_name"
value:
string_value: "test_log_name"
- key: "zone_name"
value:
string_value: "zone_name"
- key: "cluster_name"
value:
string_value: "cluster_name"
- key: "node_name"
value:
string_value: "node_name"
scope_logs:
- log_records:
- severity_text: "test-severity-text"
)EOF");
opentelemetry::proto::logs::v1::LogRecord entry;
entry.set_severity_text("test-severity-text");
logger_->log(opentelemetry::proto::logs::v1::LogRecord(entry));
EXPECT_EQ(
stats_store_.findCounterByString("access_logs.open_telemetry_access_log.custom.logs_written")
.value()
.get()
.value(),
1);
}

class GrpcAccessLoggerCacheImplTest : public testing::Test {
Expand Down

0 comments on commit 75414e8

Please sign in to comment.