From 973201275fd6063470448b15e50f68a91b374109 Mon Sep 17 00:00:00 2001 From: zirain Date: Thu, 7 Nov 2024 13:23:33 +0800 Subject: [PATCH] support ReturnDescriptorsInResponse Signed-off-by: zirain --- src/service/ratelimit.go | 32 +++++++++++++++++++++++--------- src/settings/settings.go | 5 +++++ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/service/ratelimit.go b/src/service/ratelimit.go index ea26bf0d2..26b1a11df 100644 --- a/src/service/ratelimit.go +++ b/src/service/ratelimit.go @@ -1,25 +1,21 @@ package ratelimit import ( + "encoding/json" "fmt" "math" "strconv" "strings" "sync" - "go.opentelemetry.io/otel" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/trace" - - "github.com/envoyproxy/ratelimit/src/settings" - "github.com/envoyproxy/ratelimit/src/stats" - - "github.com/envoyproxy/ratelimit/src/utils" - core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3" pb "github.com/envoyproxy/go-control-plane/envoy/service/ratelimit/v3" logger "github.com/sirupsen/logrus" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/trace" "golang.org/x/net/context" + "google.golang.org/protobuf/types/known/structpb" "github.com/envoyproxy/ratelimit/src/assert" "github.com/envoyproxy/ratelimit/src/config" @@ -27,6 +23,9 @@ import ( "github.com/envoyproxy/ratelimit/src/provider" "github.com/envoyproxy/ratelimit/src/redis" "github.com/envoyproxy/ratelimit/src/server" + "github.com/envoyproxy/ratelimit/src/settings" + "github.com/envoyproxy/ratelimit/src/stats" + "github.com/envoyproxy/ratelimit/src/utils" ) var tracer = otel.Tracer("ratelimit") @@ -50,6 +49,7 @@ type service struct { customHeaderResetHeader string customHeaderClock utils.TimeSource globalShadowMode bool + returnDescriptorsInResponse bool } func (this *service) SetConfig(updateEvent provider.ConfigUpdateEvent, healthyWithAtLeastOneConfigLoad bool) { @@ -84,6 +84,7 @@ func (this *service) SetConfig(updateEvent provider.ConfigUpdateEvent, healthyWi rlSettings := settings.NewSettings() this.globalShadowMode = rlSettings.GlobalShadowMode + this.returnDescriptorsInResponse = rlSettings.ReturnDescriptorsInResponse if rlSettings.RateLimitResponseHeadersEnabled { this.customHeadersEnabled = true @@ -237,6 +238,19 @@ func (this *service) shouldRateLimitWorker( } response.OverallCode = finalCode + if this.returnDescriptorsInResponse { + data, _ := json.Marshal(request.Descriptors) + response.DynamicMetadata = &structpb.Struct{ + Fields: map[string]*structpb.Value{ + "descriptors": { + Kind: &structpb.Value_StringValue{ + StringValue: string(data), + }, + }, + }, + } + } + return response } diff --git a/src/settings/settings.go b/src/settings/settings.go index 9febf7d9b..0a453728c 100644 --- a/src/settings/settings.go +++ b/src/settings/settings.go @@ -189,6 +189,11 @@ type Settings struct { // detailed setting of exporter should refer to https://opentelemetry.io/docs/reference/specification/protocol/exporter/, e.g. OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_CERTIFICATE, OTEL_EXPORTER_OTLP_TIMEOUT // TracingSamplingRate defaults to 1 which amounts to using the `AlwaysSample` sampler TracingSamplingRate float64 `envconfig:"TRACING_SAMPLING_RATE" default:"1"` + + // ReturnDescriptorsInResponse enables returning descriptors in the `response.DynamicMetadata`. + // By default, the descriptors encoded in json format. + // This is useful for Envoy to set dynamic metadata in `envoy.filters.http.ratelimit`. + ReturnDescriptorsInResponse bool `envconfig:"RETURN_DESCRIPTORS_IN_RESPONSE" default:"false"` } type Option func(*Settings)