Skip to content

Commit f615913

Browse files
authored
feat: Add counter metric for http.send network requests (#7851)
Signed-off-by: Anivar A Aravind <[email protected]>
1 parent 3d2b936 commit f615913

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

v1/topdown/http.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ var (
9999
requiredKeys = ast.NewSet(ast.InternedTerm("method"), ast.InternedTerm("url"))
100100
httpSendLatencyMetricKey = "rego_builtin_http_send"
101101
httpSendInterQueryCacheHits = httpSendLatencyMetricKey + "_interquery_cache_hits"
102+
httpSendNetworkRequests = httpSendLatencyMetricKey + "_network_requests"
102103
)
103104

104105
type httpSendKey string
@@ -1535,6 +1536,9 @@ func (c *interQueryCache) ExecuteHTTPRequest() (*http.Response, error) {
15351536
return nil, handleHTTPSendErr(c.bctx, err)
15361537
}
15371538

1539+
// Increment counter for actual network requests
1540+
c.bctx.Metrics.Counter(httpSendNetworkRequests).Incr()
1541+
15381542
return executeHTTPRequest(c.httpReq, c.httpClient, c.req)
15391543
}
15401544

@@ -1586,6 +1590,10 @@ func (c *intraQueryCache) ExecuteHTTPRequest() (*http.Response, error) {
15861590
if err != nil {
15871591
return nil, handleHTTPSendErr(c.bctx, err)
15881592
}
1593+
1594+
// Increment counter for actual network requests
1595+
c.bctx.Metrics.Counter(httpSendNetworkRequests).Incr()
1596+
15891597
return executeHTTPRequest(httpReq, httpClient, c.req)
15901598
}
15911599

v1/topdown/http_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3428,6 +3428,45 @@ func TestHTTPSendMetrics(t *testing.T) {
34283428
t.Fatalf("expected %d cache hits, got %d", exp, act)
34293429
}
34303430
})
3431+
3432+
t.Run("network requests", func(t *testing.T) {
3433+
// Test that network requests counter is incremented correctly
3434+
m := metrics.New()
3435+
3436+
// Test 1: Single request - verify counter increments
3437+
q := NewQuery(ast.MustParseBody(fmt.Sprintf(`http.send({"method": "get", "url": %q})`, ts.URL))).WithMetrics(m)
3438+
_, err := q.Run(context.Background())
3439+
if err != nil {
3440+
t.Fatal(err)
3441+
}
3442+
3443+
if exp, act := uint64(1), m.Counter(httpSendNetworkRequests).Value(); exp != act {
3444+
t.Fatalf("expected %d network requests, got %d", exp, act)
3445+
}
3446+
3447+
// Test 2: Another request to different URL
3448+
q2 := NewQuery(ast.MustParseBody(fmt.Sprintf(`http.send({"method": "get", "url": %q})`, ts.URL+"/other"))).WithMetrics(m)
3449+
_, err = q2.Run(context.Background())
3450+
if err != nil {
3451+
t.Fatal(err)
3452+
}
3453+
3454+
if exp, act := uint64(2), m.Counter(httpSendNetworkRequests).Value(); exp != act {
3455+
t.Fatalf("expected %d network requests, got %d", exp, act)
3456+
}
3457+
3458+
// Test 3: Request with error should still increment counter
3459+
badURL := "http://localhost:1" // Port 1 should fail quickly
3460+
q3 := NewQuery(ast.MustParseBody(fmt.Sprintf(`http.send({"method": "get", "url": %q, "raise_error": false})`, badURL))).WithMetrics(m)
3461+
_, err = q3.Run(context.Background())
3462+
if err != nil {
3463+
t.Fatal(err)
3464+
}
3465+
3466+
if exp, act := uint64(3), m.Counter(httpSendNetworkRequests).Value(); exp != act {
3467+
t.Fatalf("expected %d network requests including failed one, got %d", exp, act)
3468+
}
3469+
})
34313470
}
34323471

34333472
// Warning(philipc): This test cannot be run in parallel with other tests, due

0 commit comments

Comments
 (0)