Skip to content

Commit 0c19de0

Browse files
committed
integration test
Signed-off-by: William Zhang <[email protected]>
1 parent 387fe5e commit 0c19de0

File tree

6 files changed

+132
-17
lines changed

6 files changed

+132
-17
lines changed

integration/envoy.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ static_resources:
4747
dynamic_module_config:
4848
name: rust_module
4949
filter_name: metrics
50+
filter_config:
51+
"@type": "type.googleapis.com/google.protobuf.StringValue"
52+
value: |
53+
{
54+
"version": "v1.0.0"
55+
}
5056
- name: dynamic_modules/conditional_delay
5157
typed_config:
5258
# https://www.envoyproxy.io/docs/envoy/latest/api-v3/extensions/dynamic_modules/v3/dynamic_modules.proto#envoy-v3-api-msg-extensions-dynamic-modules-v3-dynamicmoduleconfig

integration/go.mod

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ go 1.24.0
44

55
require (
66
github.com/mccutchen/go-httpbin/v2 v2.18.0
7-
github.com/stretchr/testify v1.10.0
7+
github.com/prometheus/client_model v0.6.2
8+
github.com/prometheus/common v0.66.1
9+
github.com/stretchr/testify v1.11.1
810
)
911

1012
require (
1113
github.com/davecgh/go-spew v1.1.1 // indirect
14+
github.com/kr/pretty v0.3.1 // indirect
15+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
1216
github.com/pmezard/go-difflib v1.0.0 // indirect
17+
go.yaml.in/yaml/v2 v2.4.2 // indirect
18+
google.golang.org/protobuf v1.36.8 // indirect
1319
gopkg.in/yaml.v3 v3.0.1 // indirect
1420
)

integration/go.sum

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
1+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
12
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
23
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
5+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
6+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
7+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
8+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
9+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
310
github.com/mccutchen/go-httpbin/v2 v2.18.0 h1:WFU1OELp3nHYLvXct/3nrGVIgxU0X+RJfDPYRBnvicY=
411
github.com/mccutchen/go-httpbin/v2 v2.18.0/go.mod h1:GBy5I7XwZ4ZLhT3hcq39I4ikwN9x4QUt6EAxNiR8Jus=
12+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
13+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
14+
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
515
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
616
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7-
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
8-
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
9-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
17+
github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=
18+
github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE=
19+
github.com/prometheus/common v0.66.1 h1:h5E0h5/Y8niHc5DlaLlWLArTQI7tMrsfQjHV+d9ZoGs=
20+
github.com/prometheus/common v0.66.1/go.mod h1:gcaUsgf3KfRSwHY4dIMXLPV0K/Wg1oZ8+SbZk/HH/dA=
21+
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
22+
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
23+
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
24+
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
25+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
26+
go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI=
27+
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
28+
google.golang.org/protobuf v1.36.8 h1:xHScyCOEuuwZEc6UtSOvPbAT4zRh0xcNRYekJwfqyMc=
29+
google.golang.org/protobuf v1.36.8/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU=
1030
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
31+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
32+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
1133
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
1234
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

integration/main_test.go

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bytes"
45
"cmp"
56
"encoding/json"
67
"io"
@@ -13,6 +14,8 @@ import (
1314
"time"
1415

1516
"github.com/mccutchen/go-httpbin/v2/httpbin"
17+
io_prometheus_client "github.com/prometheus/client_model/go"
18+
"github.com/prometheus/common/expfmt"
1619
"github.com/stretchr/testify/require"
1720
)
1821

@@ -35,8 +38,8 @@ func TestIntegration(t *testing.T) {
3538
// Create a directory for the access logs to be written to.
3639
accessLogsDir := cwd + "/access_logs"
3740
require.NoError(t, os.RemoveAll(accessLogsDir))
38-
require.NoError(t, os.Mkdir(accessLogsDir, 0700))
39-
require.NoError(t, os.Chmod(accessLogsDir, 0777))
41+
require.NoError(t, os.Mkdir(accessLogsDir, 0o700))
42+
require.NoError(t, os.Chmod(accessLogsDir, 0o777))
4043

4144
cmd := exec.Command(
4245
"docker",
@@ -328,4 +331,77 @@ func TestIntegration(t *testing.T) {
328331
})
329332
}
330333
})
334+
335+
t.Run("http_metrics", func(t *testing.T) {
336+
// Send test request
337+
require.Eventually(t, func() bool {
338+
req, err := http.NewRequest("GET", "http://localhost:1062/uuid", nil)
339+
require.NoError(t, err)
340+
341+
resp, err := http.DefaultClient.Do(req)
342+
if err != nil {
343+
t.Logf("Envoy not ready yet: %v", err)
344+
return false
345+
}
346+
defer func() {
347+
require.NoError(t, resp.Body.Close())
348+
}()
349+
body, err := io.ReadAll(resp.Body)
350+
if err != nil {
351+
t.Logf("Envoy not ready yet: %v", err)
352+
return false
353+
}
354+
t.Logf("response: status=%d body=%s", resp.StatusCode, string(body))
355+
return resp.StatusCode == 200
356+
}, 30*time.Second, 200*time.Millisecond)
357+
358+
// Check the metrics endpoint
359+
lastStatsOutput := ""
360+
t.Cleanup(func() {
361+
t.Logf("last stats output:\n%s", lastStatsOutput)
362+
})
363+
require.Eventually(t, func() bool {
364+
req, err := http.NewRequest("GET", "http://localhost:9901/stats/prometheus", nil)
365+
require.NoError(t, err)
366+
367+
resp, err := http.DefaultClient.Do(req)
368+
require.NoError(t, err)
369+
defer func() {
370+
require.NoError(t, resp.Body.Close())
371+
}()
372+
373+
// Check that the route_latency_ms metric is present
374+
body, err := io.ReadAll(resp.Body)
375+
require.NoError(t, err)
376+
lastStatsOutput = string(body)
377+
378+
decoder := expfmt.NewDecoder(bytes.NewReader(body), expfmt.NewFormat(expfmt.TypeTextPlain))
379+
for {
380+
var metricFamily io_prometheus_client.MetricFamily
381+
err := decoder.Decode(&metricFamily)
382+
if err == io.EOF {
383+
break
384+
}
385+
require.NoError(t, err)
386+
387+
if metricFamily.GetName() != "route_latency_ms" {
388+
continue
389+
}
390+
for _, metric := range metricFamily.GetMetric() {
391+
hist := metric.GetHistogram()
392+
require.NotNil(t, hist)
393+
labels := make(map[string]string)
394+
for _, label := range metric.GetLabel() {
395+
labels[label.GetName()] = label.GetValue()
396+
}
397+
require.Equal(t, map[string]string{"version": "v1.0.0", "route_name": "catch_all"}, labels)
398+
if hist.GetSampleCount() > 0 {
399+
return true
400+
}
401+
}
402+
}
403+
t.Logf("route_latency_ms metric not found or no samples yet")
404+
return false
405+
}, 5*time.Second, 200*time.Millisecond)
406+
})
331407
}

rust/src/http_metrics.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ use envoy_proxy_dynamic_modules_rust_sdk::*;
66
///
77
/// The trait corresponds to a Envoy filter chain configuration.
88
pub struct FilterConfig {
9-
_filter_config: String,
9+
config: Config,
1010
route_latency: EnvoyHistogramVecId,
1111
}
1212

13+
#[derive(serde::Deserialize)]
14+
pub struct Config {
15+
version: String,
16+
}
17+
1318
impl FilterConfig {
1419
/// This is the constructor for the [`FilterConfig`].
1520
///
@@ -18,23 +23,24 @@ impl FilterConfig {
1823
pub fn new<EC: EnvoyHttpFilterConfig>(
1924
filter_config: &str,
2025
envoy_filter_config: &mut EC,
21-
) -> Self {
22-
Self {
23-
_filter_config: filter_config.to_string(),
26+
) -> Option<Self> {
27+
Some(Self {
28+
config: serde_json::from_str::<Config>(filter_config).ok()?,
2429
// Handles to metrics such as counters, gauges, and histograms are allocated at filter config creation time. These handles
2530
// are opaque ids that can be used to record statistics during the lifecycle of the filter. These handles last until the
2631
// filter config is destroyed.
2732
route_latency: envoy_filter_config
28-
.define_histogram_vec("route_latency_ms", &["route_name"])
33+
.define_histogram_vec("route_latency_ms", &["version", "route_name"])
2934
.unwrap(),
30-
}
35+
})
3136
}
3237
}
3338

3439
impl<EHF: EnvoyHttpFilter> HttpFilterConfig<EHF> for FilterConfig {
3540
/// This is called for each new HTTP filter.
3641
fn new_http_filter(&mut self, _envoy: &mut EHF) -> Box<dyn HttpFilter<EHF>> {
3742
Box::new(Filter {
43+
version: self.config.version.clone(),
3844
start_time: None,
3945
route_name: None,
4046
route_latency: self.route_latency,
@@ -46,6 +52,7 @@ impl<EHF: EnvoyHttpFilter> HttpFilterConfig<EHF> for FilterConfig {
4652
///
4753
/// This is a metrics filter that records per-route metrics of the request.
4854
pub struct Filter {
55+
version: String,
4956
start_time: Option<Instant>,
5057
route_latency: EnvoyHistogramVecId,
5158
route_name: Option<String>,
@@ -63,7 +70,7 @@ impl Filter {
6370
envoy_filter
6471
.record_histogram_value_vec(
6572
self.route_latency,
66-
&[&route_name],
73+
&[&self.version, &route_name],
6774
start_time.elapsed().as_millis() as u64,
6875
)
6976
.unwrap();

rust/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,8 @@ fn new_http_filter_config_fn<EC: EnvoyHttpFilterConfig, EHF: EnvoyHttpFilter>(
4444
.map(|config| Box::new(config) as Box<dyn HttpFilterConfig<EHF>>),
4545
"header_mutation" => http_header_mutation::FilterConfig::new(filter_config)
4646
.map(|config| Box::new(config) as Box<dyn HttpFilterConfig<EHF>>),
47-
"metrics" => Some(Box::new(http_metrics::FilterConfig::new(
48-
filter_config,
49-
envoy_filter_config,
50-
))),
47+
"metrics" => http_metrics::FilterConfig::new(filter_config, envoy_filter_config)
48+
.map(|config| Box::new(config) as Box<dyn HttpFilterConfig<EHF>>),
5149
_ => panic!("Unknown filter name: {filter_name}"),
5250
}
5351
}

0 commit comments

Comments
 (0)