Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
zirain committed Jan 1, 2025
1 parent 9c56180 commit 7cbf10b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
3 changes: 3 additions & 0 deletions extensions/common/metadata_object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ Envoy::ProtobufTypes::MessagePtr WorkloadMetadataObject::serializeAsProto() cons
if (!app_version_.empty()) {
(*message->mutable_fields())[AppVersionToken].set_string_value(app_version_);
}
if (!identity_.empty()) {
(*message->mutable_fields())[IdentityToken].set_string_value(identity_);
}

if (!labels_.empty()) {
auto* labels = (*message->mutable_fields())[LabelsToken].mutable_struct_value();
Expand Down
1 change: 1 addition & 0 deletions extensions/common/metadata_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ constexpr absl::string_view WorkloadNameToken = "workload";
constexpr absl::string_view WorkloadTypeToken = "type";
constexpr absl::string_view InstanceNameToken = "name";
constexpr absl::string_view LabelsToken = "labels";
constexpr absl::string_view IdentityToken = "identity";

constexpr absl::string_view InstanceMetadataField = "NAME";
constexpr absl::string_view NamespaceMetadataField = "NAMESPACE";
Expand Down
23 changes: 12 additions & 11 deletions source/extensions/filters/http/istio_stats/istio_stats.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ 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) {
std::optional<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
Expand All @@ -136,15 +136,15 @@ const Istio::Common::WorkloadMetadataObject* peerInfo(Reporter reporter,
filter_state.getDataReadOnly<Envoy::Extensions::Filters::Common::Expr::CelState>(
filter_state_key);
if (!cel_state) {
return nullptr;
return {};
}

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

auto peer_info = std::make_unique<Istio::Common::WorkloadMetadataObject>(
Istio::Common::WorkloadMetadataObject peer_info(
extractString(obj, Istio::Common::InstanceNameToken),
extractString(obj, Istio::Common::ClusterNameToken),
extractString(obj, Istio::Common::NamespaceNameToken),
Expand All @@ -153,9 +153,10 @@ const Istio::Common::WorkloadMetadataObject* peerInfo(Reporter reporter,
extractString(obj, Istio::Common::ServiceVersionToken),
extractString(obj, Istio::Common::AppNameToken),
extractString(obj, Istio::Common::AppVersionToken),
Istio::Common::fromSuffix(extractString(obj, Istio::Common::WorkloadTypeToken)), "");
Istio::Common::fromSuffix(extractString(obj, Istio::Common::WorkloadTypeToken)),
extractString(obj, Istio::Common::IdentityToken));

return peer_info.release();
return peer_info;
}

// Process-wide context shared with all filter instances.
Expand Down Expand Up @@ -1012,9 +1013,9 @@ class IstioStatsFilter : public Http::PassThroughFilter,
const StreamInfo::FilterState& filter_state) {
// Compute peer info with client-side fallbacks.
absl::optional<Istio::Common::WorkloadMetadataObject> peer;
const auto* object = peerInfo(config_->reporter(), filter_state);
auto object = peerInfo(config_->reporter(), filter_state);
if (object) {
peer.emplace(*object);
peer.emplace(object.value());
} else if (config_->reporter() == Reporter::ClientSidecar) {
if (auto label_obj = extractEndpointMetadata(info); label_obj) {
peer.emplace(label_obj.value());
Expand Down Expand Up @@ -1156,9 +1157,9 @@ class IstioStatsFilter : public Http::PassThroughFilter,
switch (config_->reporter()) {
case Reporter::ServerGateway: {
std::optional<Istio::Common::WorkloadMetadataObject> endpoint_peer;
const auto* endpoint_object = peerInfo(Reporter::ClientSidecar, filter_state);
auto endpoint_object = peerInfo(Reporter::ClientSidecar, filter_state);
if (endpoint_object) {
endpoint_peer.emplace(*endpoint_object);
endpoint_peer.emplace(endpoint_object.value());
}
tags_.push_back(
{context_.destination_workload_,
Expand Down
2 changes: 1 addition & 1 deletion source/extensions/filters/http/peer_metadata/filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ void FilterConfig::setFilterState(StreamInfo::StreamInfo& info, bool downstream,
// Use CelState to allow operation filter_state.upstream_peer.labels['role']
auto pb = value.serializeAsProto();
auto peer_info = std::make_unique<CelState>(FilterConfig::peerInfoPrototype());
peer_info->setValue(pb->SerializeAsString());
peer_info->setValue(absl::string_view(pb->SerializeAsString()));
info.filterState()->setData(
key, std::move(peer_info), StreamInfo::FilterState::StateType::Mutable,
StreamInfo::FilterState::LifeSpan::FilterChain, sharedWithUpstream());
Expand Down

0 comments on commit 7cbf10b

Please sign in to comment.