Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zirain committed Jan 1, 2025
1 parent b4a377d commit 9033372
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 39 deletions.
75 changes: 40 additions & 35 deletions source/extensions/filters/http/istio_stats/istio_stats.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,41 +123,6 @@ bool peerInfoRead(Reporter reporter, const StreamInfo::FilterState& filter_state
filter_state.hasDataWithName(Istio::Common::NoPeer);
}

const Istio::Common::WorkloadMetadataObject* peerInfo(Reporter reporter,
const StreamInfo::FilterState& filter_state) {
const auto& filter_state_key =
reporter == Reporter::ServerSidecar || reporter == Reporter::ServerGateway
? Istio::Common::DownstreamPeer
: Istio::Common::UpstreamPeer;
// This's a workaround before FilterStateObject support operation like `.labels['role']`.
// The workaround is to use CelState to store the peer metadata.
// Rebuild the WorkloadMetadataObject from the CelState.
const auto* cel_state =
filter_state.getDataReadOnly<Envoy::Extensions::Filters::Common::Expr::CelState>(
filter_state_key);
if (!cel_state) {
return nullptr;
}

ProtobufWkt::Struct obj;
if (!obj.ParseFromString(cel_state->value().data())) {
return nullptr;
}

auto peer_info = std::make_unique<Istio::Common::WorkloadMetadataObject>(
extractString(obj, Istio::Common::InstanceNameToken),
extractString(obj, Istio::Common::ClusterNameToken),
extractString(obj, Istio::Common::NamespaceNameToken),
extractString(obj, Istio::Common::WorkloadNameToken),
extractString(obj, Istio::Common::ServiceNameToken),
extractString(obj, Istio::Common::ServiceVersionToken),
extractString(obj, Istio::Common::AppNameToken),
extractString(obj, Istio::Common::AppVersionToken),
Istio::Common::fromSuffix(extractString(obj, Istio::Common::WorkloadTypeToken)), "");

return peer_info.release();
}

// Process-wide context shared with all filter instances.
struct Context : public Singleton::Instance {
explicit Context(Stats::SymbolTable& symbol_table, const LocalInfo::LocalInfo& local_info)
Expand Down Expand Up @@ -801,6 +766,7 @@ struct Config : public Logger::Loggable<Logger::Id::filter> {
using ConfigSharedPtr = std::shared_ptr<Config>;

class IstioStatsFilter : public Http::PassThroughFilter,
public Logger::Loggable<Logger::Id::filter>,
public AccessLog::Instance,
public Network::ReadFilter,
public Network::ConnectionCallbacks {
Expand Down Expand Up @@ -927,6 +893,7 @@ class IstioStatsFilter : public Http::PassThroughFilter,
const auto& info = decoder_callbacks_->streamInfo();
peer_read_ = peerInfoRead(config_->reporter(), info.filterState());
if (peer_read_ || end_stream) {
ENVOY_LOG(trace, "Populating peer metadata from HTTP MX.");
populatePeerInfo(info, info.filterState());
}
if (is_grpc_ && (peer_read_ || end_stream)) {
Expand Down Expand Up @@ -965,6 +932,7 @@ class IstioStatsFilter : public Http::PassThroughFilter,
peer_read_ = peerInfoRead(config_->reporter(), filter_state);
// Report connection open once peer info is read or connection is closed.
if (peer_read_ || end_stream) {
ENVOY_LOG(trace, "Populating peer metadata from TCP MX.");
populatePeerInfo(info, filter_state);
tags_.push_back({context_.request_protocol_, context_.tcp_});
populateFlagsAndConnectionSecurity(info);
Expand Down Expand Up @@ -1259,6 +1227,43 @@ class IstioStatsFilter : public Http::PassThroughFilter,
}
}

const Istio::Common::WorkloadMetadataObject*
peerInfo(Reporter reporter, const StreamInfo::FilterState& filter_state) {
const auto& filter_state_key =
reporter == Reporter::ServerSidecar || reporter == Reporter::ServerGateway
? Istio::Common::DownstreamPeer
: Istio::Common::UpstreamPeer;
// This's a workaround before FilterStateObject support operation like `.labels['role']`.
// The workaround is to use CelState to store the peer metadata.
// Rebuild the WorkloadMetadataObject from the CelState.
const auto* cel_state =
filter_state.getDataReadOnly<Envoy::Extensions::Filters::Common::Expr::CelState>(
filter_state_key);
if (!cel_state) {
ENVOY_LOG(debug, "No peer metadata found in filter state.");
return nullptr;
}

ProtobufWkt::Struct obj;
if (!obj.ParseFromString(absl::string_view(cel_state->value()))) {
ENVOY_LOG(debug, "Failed to parse peer metadata from filter state.");
return nullptr;
}

auto peer_info = std::make_unique<Istio::Common::WorkloadMetadataObject>(
extractString(obj, Istio::Common::InstanceNameToken),
extractString(obj, Istio::Common::ClusterNameToken),
extractString(obj, Istio::Common::NamespaceNameToken),
extractString(obj, Istio::Common::WorkloadNameToken),
extractString(obj, Istio::Common::ServiceNameToken),
extractString(obj, Istio::Common::ServiceVersionToken),
extractString(obj, Istio::Common::AppNameToken),
extractString(obj, Istio::Common::AppVersionToken),
Istio::Common::fromSuffix(extractString(obj, Istio::Common::WorkloadTypeToken)), "");

return peer_info.release();
}

ConfigSharedPtr config_;
Context& context_;
Stats::StatNameDynamicPool pool_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,9 @@ void MetadataExchangeFilter::updatePeer(const Istio::Common::WorkloadMetadataObj
FilterDirection direction) {
auto filter_state_key = direction == FilterDirection::Downstream ? Istio::Common::DownstreamPeer
: Istio::Common::UpstreamPeer;
ENVOY_LOG(trace, "updatePeer for {}", filter_state_key);

auto peer_info = std::make_unique<CelState>(MetadataExchangeConfig::peerInfoPrototype());
auto pb = value.serializeAsProto();
peer_info->setValue(pb->SerializeAsString());
auto peer_info = std::make_shared<CelState>(MetadataExchangeConfig::peerInfoPrototype());
peer_info->setValue(absl::string_view(pb->SerializeAsString()));

read_callbacks_->connection().streamInfo().filterState()->setData(
filter_state_key, std::move(peer_info), StreamInfo::FilterState::StateType::Mutable,
Expand Down

0 comments on commit 9033372

Please sign in to comment.