Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OTel Access Logger: support additional stat prefix #34091

Merged
merged 7 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ option (udpa.annotations.file_status).package_version_status = ACTIVE;
// populate `opentelemetry.proto.collector.v1.logs.ExportLogsServiceRequest.resource_logs <https://github.com/open-telemetry/opentelemetry-proto/blob/main/opentelemetry/proto/collector/logs/v1/logs_service.proto>`_.
// In addition, the request start time is set in the dedicated field.
// [#extension: envoy.access_loggers.open_telemetry]
// [#next-free-field: 6]
// [#next-free-field: 7]
message OpenTelemetryAccessLogConfig {
// [#comment:TODO(itamarkam): add 'filter_state_objects_to_log' to logs.]
grpc.v3.CommonGrpcAccessLogConfig common_config = 1 [(validate.rules).message = {required: true}];
Expand All @@ -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. Additional 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;
TAOXUY marked this conversation as resolved.
Show resolved Hide resolved
}
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 @@ -194,6 +199,40 @@ TEST_F(GrpcAccessLoggerImplTest, LogWithStats) {
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 {
public:
GrpcAccessLoggerCacheImplTest()
Expand Down