Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions src/service/ratelimit.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
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"
"github.com/envoyproxy/ratelimit/src/limiter"
"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")
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
5 changes: 5 additions & 0 deletions src/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down