Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal: add grpc response timing bucket config #559

Merged
merged 1 commit into from
Jun 28, 2024
Merged
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
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
Loading