-
Notifications
You must be signed in to change notification settings - Fork 5.5k
grpc access logger: refactor common gRPC ALS out #7879
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
source/extensions/access_loggers/grpc/grpc_access_log_impl.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| #include "extensions/access_loggers/grpc/grpc_access_log_impl.h" | ||
|
|
||
| #include "envoy/upstream/upstream.h" | ||
|
|
||
| #include "common/common/assert.h" | ||
| #include "common/network/utility.h" | ||
| #include "common/stream_info/utility.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Extensions { | ||
| namespace AccessLoggers { | ||
| namespace GrpcCommon { | ||
|
|
||
| void GrpcAccessLoggerImpl::LocalStream::onRemoteClose(Grpc::Status::GrpcStatus, | ||
| const std::string&) { | ||
| ASSERT(parent_.stream_ != absl::nullopt); | ||
| if (parent_.stream_->stream_ != nullptr) { | ||
| // Only reset if we have a stream. Otherwise we had an inline failure and we will clear the | ||
| // stream data in send(). | ||
| parent_.stream_.reset(); | ||
| } | ||
| } | ||
|
|
||
| GrpcAccessLoggerImpl::GrpcAccessLoggerImpl(Grpc::RawAsyncClientPtr&& client, std::string log_name, | ||
| std::chrono::milliseconds buffer_flush_interval_msec, | ||
| uint64_t buffer_size_bytes, | ||
| Event::Dispatcher& dispatcher, | ||
| const LocalInfo::LocalInfo& local_info) | ||
| : client_(std::move(client)), log_name_(log_name), | ||
| buffer_flush_interval_msec_(buffer_flush_interval_msec), | ||
| flush_timer_(dispatcher.createTimer([this]() { | ||
| flush(); | ||
| flush_timer_->enableTimer(buffer_flush_interval_msec_); | ||
| })), | ||
| buffer_size_bytes_(buffer_size_bytes), local_info_(local_info) { | ||
| flush_timer_->enableTimer(buffer_flush_interval_msec_); | ||
| } | ||
|
|
||
| void GrpcAccessLoggerImpl::log(envoy::data::accesslog::v2::HTTPAccessLogEntry&& entry) { | ||
| approximate_message_size_bytes_ += entry.ByteSizeLong(); | ||
| message_.mutable_http_logs()->add_log_entry()->Swap(&entry); | ||
| if (approximate_message_size_bytes_ >= buffer_size_bytes_) { | ||
| flush(); | ||
| } | ||
| } | ||
|
|
||
| void GrpcAccessLoggerImpl::flush() { | ||
| if (!message_.has_http_logs()) { | ||
| // Nothing to flush. | ||
| return; | ||
| } | ||
|
|
||
| if (stream_ == absl::nullopt) { | ||
| stream_.emplace(*this); | ||
| } | ||
|
|
||
| if (stream_->stream_ == nullptr) { | ||
| stream_->stream_ = | ||
| client_->start(*Protobuf::DescriptorPool::generated_pool()->FindMethodByName( | ||
| "envoy.service.accesslog.v2.AccessLogService.StreamAccessLogs"), | ||
| *stream_); | ||
|
|
||
| auto* identifier = message_.mutable_identifier(); | ||
| *identifier->mutable_node() = local_info_.node(); | ||
| identifier->set_log_name(log_name_); | ||
| } | ||
|
|
||
| if (stream_->stream_ != nullptr) { | ||
| stream_->stream_->sendMessage(message_, false); | ||
| } else { | ||
| // Clear out the stream data due to stream creation failure. | ||
| stream_.reset(); | ||
| } | ||
|
|
||
| // Clear the message regardless of the success. | ||
| approximate_message_size_bytes_ = 0; | ||
| message_.Clear(); | ||
| } | ||
|
|
||
| GrpcAccessLoggerCacheImpl::GrpcAccessLoggerCacheImpl(Grpc::AsyncClientManager& async_client_manager, | ||
| Stats::Scope& scope, | ||
| ThreadLocal::SlotAllocator& tls, | ||
| const LocalInfo::LocalInfo& local_info) | ||
| : async_client_manager_(async_client_manager), scope_(scope), tls_slot_(tls.allocateSlot()), | ||
| local_info_(local_info) { | ||
| tls_slot_->set( | ||
| [](Event::Dispatcher& dispatcher) { return std::make_shared<ThreadLocalCache>(dispatcher); }); | ||
| } | ||
|
|
||
| GrpcAccessLoggerSharedPtr GrpcAccessLoggerCacheImpl::getOrCreateLogger( | ||
| const envoy::config::accesslog::v2::CommonGrpcAccessLogConfig& config) { | ||
| // TODO(euroelessar): Consider cleaning up loggers. | ||
| auto& cache = tls_slot_->getTyped<ThreadLocalCache>(); | ||
| const std::size_t cache_key = MessageUtil::hash(config); | ||
| const auto it = cache.access_loggers_.find(cache_key); | ||
| if (it != cache.access_loggers_.end()) { | ||
| return it->second; | ||
| } | ||
| const Grpc::AsyncClientFactoryPtr factory = | ||
| async_client_manager_.factoryForGrpcService(config.grpc_service(), scope_, false); | ||
| const GrpcAccessLoggerSharedPtr logger = std::make_shared<GrpcAccessLoggerImpl>( | ||
| factory->create(), config.log_name(), | ||
| std::chrono::milliseconds(PROTOBUF_GET_MS_OR_DEFAULT(config, buffer_flush_interval, 1000)), | ||
| PROTOBUF_GET_WRAPPED_OR_DEFAULT(config, buffer_size_bytes, 16384), cache.dispatcher_, | ||
| local_info_); | ||
| cache.access_loggers_.emplace(cache_key, logger); | ||
| return logger; | ||
| } | ||
|
|
||
| } // namespace GrpcCommon | ||
| } // namespace AccessLoggers | ||
| } // namespace Extensions | ||
| } // namespace Envoy | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...grpc/grpc_access_log_proto_descriptors.cc → ...grpc/grpc_access_log_proto_descriptors.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the separation in place key should include a notion if it's http or tcp logger. Otherwise Envoy can not ensure API promise anymore (that each logging stream is always either http or tcp).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, thanks for the reminder, added a TODO here and will add the types when I implement TCP logger.