-
Notifications
You must be signed in to change notification settings - Fork 738
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Metrics provider for deployments and services behind Envoy
Assumes `envoy:smi` as the mesh provider name as I've successfully tested the progressive delivery for Envoy + Crossover with it. This enhances Flagger to translate it to the metrics provider name of `envoy` for deployment targets, or `envoy:service` for service targets. The `envoy` metrics provider is equivalent to `appmesh`, as both relies on the same set of standard metrics exposed by Envoy itself. The `envoy:service` is almost the same as the `envoy` provider, but removing the condition on pod name, as we only need to filter on the backing service name = envoy_cluster_name. We don't consider other Envoy xDS implementations that uses anything that is different to original servicen ames as `envoy_cluster_name`, for now. Ref #385
- Loading branch information
Showing
6 changed files
with
170 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package metrics | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
var envoyServiceQueries = map[string]string{ | ||
"request-success-rate": ` | ||
sum( | ||
rate( | ||
envoy_cluster_upstream_rq{ | ||
kubernetes_namespace="{{ .Namespace }}", | ||
envoy_cluster_name="{{ .Name }}-canary", | ||
envoy_response_code!~"5.*" | ||
}[{{ .Interval }}] | ||
) | ||
) | ||
/ | ||
sum( | ||
rate( | ||
envoy_cluster_upstream_rq{ | ||
kubernetes_namespace="{{ .Namespace }}", | ||
envoy_cluster_name="{{ .Name }}-canary" | ||
}[{{ .Interval }}] | ||
) | ||
) | ||
* 100`, | ||
"request-duration": ` | ||
histogram_quantile( | ||
0.99, | ||
sum( | ||
rate( | ||
envoy_cluster_upstream_rq_time_bucket{ | ||
kubernetes_namespace="{{ .Namespace }}", | ||
envoy_cluster_name="{{ .Name }}-canary" | ||
}[{{ .Interval }}] | ||
) | ||
) by (le) | ||
)`, | ||
} | ||
|
||
type EnvoyServiceObserver struct { | ||
client *PrometheusClient | ||
} | ||
|
||
func (ob *EnvoyServiceObserver) GetRequestSuccessRate(name string, namespace string, interval string) (float64, error) { | ||
query, err := ob.client.RenderQuery(name, namespace, interval, envoyServiceQueries["request-success-rate"]) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
value, err := ob.client.RunQuery(query) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return value, nil | ||
} | ||
|
||
func (ob *EnvoyServiceObserver) GetRequestDuration(name string, namespace string, interval string) (time.Duration, error) { | ||
query, err := ob.client.RenderQuery(name, namespace, interval, envoyServiceQueries["request-duration"]) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
value, err := ob.client.RunQuery(query) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
ms := time.Duration(int64(value)) * time.Millisecond | ||
return ms, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package metrics | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestEnvoyServiceObserver_GetRequestSuccessRate(t *testing.T) { | ||
expected := ` sum( rate( envoy_cluster_upstream_rq{ kubernetes_namespace="default", envoy_cluster_name="podinfo-canary", envoy_response_code!~"5.*" }[1m] ) ) / sum( rate( envoy_cluster_upstream_rq{ kubernetes_namespace="default", envoy_cluster_name="podinfo-canary" }[1m] ) ) * 100` | ||
|
||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
promql := r.URL.Query()["query"][0] | ||
if promql != expected { | ||
t.Errorf("\nGot %s \nWanted %s", promql, expected) | ||
} | ||
|
||
json := `{"status":"success","data":{"resultType":"vector","result":[{"metric":{},"value":[1,"100"]}]}}` | ||
w.Write([]byte(json)) | ||
})) | ||
defer ts.Close() | ||
|
||
client, err := NewPrometheusClient(ts.URL, time.Second) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
observer := &EnvoyServiceObserver{ | ||
client: client, | ||
} | ||
|
||
val, err := observer.GetRequestSuccessRate("podinfo", "default", "1m") | ||
if err != nil { | ||
t.Fatal(err.Error()) | ||
} | ||
|
||
if val != 100 { | ||
t.Errorf("Got %v wanted %v", val, 100) | ||
} | ||
} | ||
|
||
func TestEnvoyServiceObserver_GetRequestDuration(t *testing.T) { | ||
expected := ` histogram_quantile( 0.99, sum( rate( envoy_cluster_upstream_rq_time_bucket{ kubernetes_namespace="default", envoy_cluster_name="podinfo-canary" }[1m] ) ) by (le) )` | ||
|
||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
promql := r.URL.Query()["query"][0] | ||
if promql != expected { | ||
t.Errorf("\nGot %s \nWanted %s", promql, expected) | ||
} | ||
|
||
json := `{"status":"success","data":{"resultType":"vector","result":[{"metric":{},"value":[1,"100"]}]}}` | ||
w.Write([]byte(json)) | ||
})) | ||
defer ts.Close() | ||
|
||
client, err := NewPrometheusClient(ts.URL, time.Second) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
observer := &EnvoyServiceObserver{ | ||
client: client, | ||
} | ||
|
||
val, err := observer.GetRequestDuration("podinfo", "default", "1m") | ||
if err != nil { | ||
t.Fatal(err.Error()) | ||
} | ||
|
||
if val != 100*time.Millisecond { | ||
t.Errorf("Got %v wanted %v", val, 100*time.Millisecond) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters