Skip to content

add support for response dynamic metadata#1027

Merged
collin-lee merged 2 commits intoenvoyproxy:mainfrom
zirain:dynamic-metadata
Dec 16, 2025
Merged

add support for response dynamic metadata#1027
collin-lee merged 2 commits intoenvoyproxy:mainfrom
zirain:dynamic-metadata

Conversation

@zirain
Copy link
Copy Markdown
Member

@zirain zirain commented Dec 10, 2025

xref: envoyproxy/envoy#41339

add a new environment flag RESPONSE_DYNAMIC_METADATA, which return DynamicMetadata in response if it's true.

Signed-off-by: zirain <zirain2009@gmail.com>
s, _ := structpb.NewStruct(nil)
s.Fields = make(map[string]*structpb.Value)

// Entities
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean Entries here?

}

func ratelimitToMetadata(req *pb.RateLimitRequest) *structpb.Struct {
dm, _ := structpb.NewStruct(nil)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for NewStruct, I think you can look at getClientNode in xds_grpc_sotw_provider.go as an existing pattern in this Project to avoid silently ignored errors:

suggestion
func ratelimitToMetadata(req *pb.RateLimitRequest) *structpb.Struct {
    fields := make(map[string]*structpb.Value)
    
    // Domain
    fields["domain"] = structpb.NewStringValue(req.Domain)
    
    // Descriptors
    descriptorsValues := make([]*structpb.Value, 0, len(req.Descriptors))
    for _, descriptor := range req.Descriptors {
        s := descriptorToStruct(descriptor)
        if s == nil {
            continue
        }
        descriptorsValues = append(descriptorsValues, structpb.NewStructValue(s))
    }
    fields["descriptors"] = structpb.NewListValue(&structpb.ListValue{
        Values: descriptorsValues,
    })
    
    // HitsAddend
    if hitsAddend := req.GetHitsAddend(); hitsAddend != 0 {
        fields["hitsAddend"] = structpb.NewNumberValue(float64(hitsAddend))
    }
    
    return &structpb.Struct{Fields: fields}
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

return dm
}

func descriptorToStruct(descriptor *ratelimitv3.RateLimitDescriptor) *structpb.Struct {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the review above regarding silently failing

suggestion
func descriptorToStruct(descriptor *ratelimitv3.RateLimitDescriptor) *structpb.Struct {
    if descriptor == nil {
        return nil
    }
    
    fields := make(map[string]*structpb.Value)
    
    // Entries
    entriesValues := make([]*structpb.Value, 0, len(descriptor.Entries))
    for _, entry := range descriptor.Entries {
        val := fmt.Sprintf("%s=%s", entry.GetKey(), entry.GetValue())
        entriesValues = append(entriesValues, structpb.NewStringValue(val))
    }
    fields["entries"] = structpb.NewListValue(&structpb.ListValue{
        Values: entriesValues,
    })
    
    // Limit
    if descriptor.GetLimit() != nil {
        fields["limit"] = structpb.NewStringValue(descriptor.Limit.String())
    }
    
    // HitsAddend
    if hitsAddend := descriptor.GetHitsAddend(); hitsAddend != nil {
        fields["hitsAddend"] = structpb.NewNumberValue(float64(hitsAddend.GetValue()))
    }
    
    return &structpb.Struct{Fields: fields}
}

Signed-off-by: zirain <zirain2009@gmail.com>
@zirain zirain requested a review from collin-lee December 12, 2025 00:47
@collin-lee collin-lee merged commit 167d0f8 into envoyproxy:main Dec 16, 2025
6 checks passed
@zirain zirain deleted the dynamic-metadata branch December 16, 2025 23:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants