Skip to content

Commit

Permalink
(#6814)internal: add grpc timing bucket config
Browse files Browse the repository at this point in the history
Signed-off-by: Amirsalar Safaei <amirsalar.safaei@divar.ir>
  • Loading branch information
amirsalarsafaei committed Jun 12, 2024
1 parent 094e1cc commit c351554
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 34 deletions.
72 changes: 38 additions & 34 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,33 @@ const (
PluginName = "envoy_ext_authz_grpc"
)

var defaultGRPCRequestDurationSecondsBuckets = []float64{
1e-6,
5e-6,
1e-5,
5e-5,
1e-4,
5e-4,
1e-3,
3e-3,
5e-3,
0.1,
1,
}

// Validate receives a slice of bytes representing the plugin's
// configuration and returns a configuration value that can be used to
// instantiate the plugin.
func Validate(m *plugins.Manager, bs []byte) (*Config, error) {
cfg := Config{
Addr: defaultAddr,
DryRun: defaultDryRun,
EnableReflection: defaultEnableReflection,
GRPCMaxRecvMsgSize: defaultGRPCServerMaxReceiveMessageSize,
GRPCMaxSendMsgSize: defaultGRPCServerMaxSendMessageSize,
SkipRequestBodyParse: defaultSkipRequestBodyParse,
EnablePerformanceMetrics: defaultEnablePerformanceMetrics,
Addr: defaultAddr,
DryRun: defaultDryRun,
EnableReflection: defaultEnableReflection,
GRPCMaxRecvMsgSize: defaultGRPCServerMaxReceiveMessageSize,
GRPCMaxSendMsgSize: defaultGRPCServerMaxSendMessageSize,
SkipRequestBodyParse: defaultSkipRequestBodyParse,
EnablePerformanceMetrics: defaultEnablePerformanceMetrics,
GRPCRequestDurationSecondsBuckets: defaultGRPCRequestDurationSecondsBuckets,
}

if err := util.Unmarshal(bs, &cfg); err != nil {
Expand Down Expand Up @@ -168,21 +183,9 @@ func New(m *plugins.Manager, cfg *Config) plugins.Plugin {
}
if cfg.EnablePerformanceMetrics {
histogramAuthzDuration := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "grpc_request_duration_seconds",
Help: "A histogram of duration for grpc authz requests.",
Buckets: []float64{
1e-6,
5e-6,
1e-5,
5e-5,
1e-4,
5e-4,
1e-3,
3e-3,
5e-3,
0.1,
1,
},
Name: "grpc_request_duration_seconds",
Help: "A histogram of duration for grpc authz requests.",
Buckets: cfg.GRPCRequestDurationSecondsBuckets,
}, []string{"handler"})
plugin.metricAuthzDuration = *histogramAuthzDuration
errorCounter := prometheus.NewCounterVec(prometheus.CounterOpts{
Expand All @@ -201,18 +204,19 @@ func New(m *plugins.Manager, cfg *Config) plugins.Plugin {

// Config represents the plugin configuration.
type Config struct {
Addr string `json:"addr"`
Query string `json:"query"` // Deprecated: Use Path instead
Path string `json:"path"`
DryRun bool `json:"dry-run"`
EnableReflection bool `json:"enable-reflection"`
parsedQuery ast.Body
ProtoDescriptor string `json:"proto-descriptor"`
protoSet *protoregistry.Files
GRPCMaxRecvMsgSize int `json:"grpc-max-recv-msg-size"`
GRPCMaxSendMsgSize int `json:"grpc-max-send-msg-size"`
SkipRequestBodyParse bool `json:"skip-request-body-parse"`
EnablePerformanceMetrics bool `json:"enable-performance-metrics"`
Addr string `json:"addr"`
Query string `json:"query"` // Deprecated: Use Path instead
Path string `json:"path"`
DryRun bool `json:"dry-run"`
EnableReflection bool `json:"enable-reflection"`
parsedQuery ast.Body
ProtoDescriptor string `json:"proto-descriptor"`
protoSet *protoregistry.Files
GRPCMaxRecvMsgSize int `json:"grpc-max-recv-msg-size"`
GRPCMaxSendMsgSize int `json:"grpc-max-send-msg-size"`
SkipRequestBodyParse bool `json:"skip-request-body-parse"`
EnablePerformanceMetrics bool `json:"enable-performance-metrics"`
GRPCRequestDurationSecondsBuckets []float64 `json:"grpc-request-duration-seconds-buckets"`
}

type envoyExtAuthzGrpcServer struct {
Expand Down
21 changes: 21 additions & 0 deletions internal/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,23 @@ func TestConfigValidWithGRPCMaxMessageSizes(t *testing.T) {
}
}

func TestConfigValidWithGRPCRequestDurationSecondsBuckets(t *testing.T) {
m, err := plugins.New([]byte{}, "test", inmem.New())
if err != nil {
t.Fatal(err)
}

in := `{"grpc-request-duration-seconds-buckets": [1e-5, 0.2, 1, 5]}`
config, err := Validate(m, []byte(in))
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(config.GRPCRequestDurationSecondsBuckets, []float64{1e-5, 0.2, 1, 5}) {
t.Fatalf("Expected grpc_request_duration_seconds buckets to be [1e-5 0.2 1 5] but got %v", config.GRPCRequestDurationSecondsBuckets)
}
}

func TestConfigValidDefault(t *testing.T) {
m, err := plugins.New([]byte{}, "test", inmem.New())
if err != nil {
Expand Down Expand Up @@ -1289,6 +1306,10 @@ func TestConfigValidDefault(t *testing.T) {
if config.EnablePerformanceMetrics != defaultEnablePerformanceMetrics {
t.Fatalf("Expected enabled-prometheus-metrics to be disabled by default")
}

if !reflect.DeepEqual(config.GRPCRequestDurationSecondsBuckets, defaultGRPCRequestDurationSecondsBuckets) {
t.Fatalf("Exptected grpc_request_duration_seconds buckets %v but got %v", defaultGRPCRequestDurationSecondsBuckets, config.GRPCRequestDurationSecondsBuckets)
}
}

func TestConfigInvalid(t *testing.T) {
Expand Down

0 comments on commit c351554

Please sign in to comment.