Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
47 changes: 41 additions & 6 deletions v2/pkg/engine/resolve/cache_analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,15 @@ type CacheWriteEvent struct {

// FetchTimingEvent records the duration of a subgraph fetch or cache lookup.
type FetchTimingEvent struct {
DataSource string // subgraph name
EntityType string // entity type (empty for root fetches)
DurationMs int64 // time spent on this operation in milliseconds
Source FieldSource // what handled this: Subgraph (fetch), L2 (cache GET)
ItemCount int // number of entities in this fetch/lookup
IsEntityFetch bool // true for _entities, false for root field
DataSource string // subgraph name
EntityType string // entity type (empty for root fetches)
DurationMs int64 // time spent on this operation in milliseconds
Source FieldSource // what handled this: Subgraph (fetch), L2 (cache GET)
ItemCount int // number of entities in this fetch/lookup
IsEntityFetch bool // true for _entities, false for root field
HTTPStatusCode int // HTTP status code from subgraph response (0 for cache hits)
ResponseBytes int // response body size in bytes (0 for cache hits)
TTFBMs int64 // time to first byte in milliseconds (0 when unavailable)
}

// SubgraphErrorEvent records a subgraph error for analytics.
Expand Down Expand Up @@ -824,6 +827,38 @@ func (s *CacheAnalyticsSnapshot) ShadowFreshnessRateByEntityType() map[string]fl
return result
}

// SubgraphFetchMetrics holds metrics for a single subgraph fetch.
// Designed for export to external SLO systems (e.g., schema registry)
// where per-fetch granularity is needed for percentile computation.
type SubgraphFetchMetrics struct {
SubgraphName string
EntityType string
DurationMs int64
HTTPStatusCode int
ResponseBytes int
IsEntityFetch bool
}

// SubgraphFetches returns one entry per actual subgraph fetch for this request.
// Cache hits (L1/L2) are excluded. Returns nil if there are no subgraph fetches.
func (s *CacheAnalyticsSnapshot) SubgraphFetches() []SubgraphFetchMetrics {
var result []SubgraphFetchMetrics
for _, ft := range s.FetchTimings {
if ft.Source != FieldSourceSubgraph {
continue
}
result = append(result, SubgraphFetchMetrics{
SubgraphName: ft.DataSource,
EntityType: ft.EntityType,
DurationMs: ft.DurationMs,
HTTPStatusCode: ft.HTTPStatusCode,
ResponseBytes: ft.ResponseBytes,
IsEntityFetch: ft.IsEntityFetch,
})
}
return result
}

// computeCacheAgeMs computes cache age in milliseconds from remaining TTL and original TTL.
// Returns 0 if either value is zero or if the computed age would be negative.
func computeCacheAgeMs(remainingTTL, originalTTL time.Duration) int64 {
Expand Down
14 changes: 8 additions & 6 deletions v2/pkg/engine/resolve/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2263,12 +2263,14 @@ func (l *Loader) executeSourceLoad(ctx context.Context, fetchItem *FetchItem, so
isEntityFetch = info.OperationType == ast.OperationTypeQuery && (entityType != "Query" && entityType != "Mutation" && entityType != "Subscription")
}
res.l2FetchTimings = append(res.l2FetchTimings, FetchTimingEvent{
DataSource: res.ds.Name,
EntityType: entityType,
DurationMs: time.Since(fetchStart).Milliseconds(),
Source: FieldSourceSubgraph,
ItemCount: 1,
IsEntityFetch: isEntityFetch,
DataSource: res.ds.Name,
EntityType: entityType,
DurationMs: time.Since(fetchStart).Milliseconds(),
Source: FieldSourceSubgraph,
ItemCount: 1,
IsEntityFetch: isEntityFetch,
HTTPStatusCode: res.statusCode,
ResponseBytes: len(res.out),
})
}

Expand Down
93 changes: 93 additions & 0 deletions v2/pkg/engine/resolve/subgraph_metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package resolve

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCacheAnalyticsSnapshot_SubgraphFetches(t *testing.T) {
t.Run("returns nil when no subgraph fetches", func(t *testing.T) {
snap := CacheAnalyticsSnapshot{
FetchTimings: []FetchTimingEvent{
{DataSource: "accounts", DurationMs: 10, Source: FieldSourceL1}, // L1 cache hit
{DataSource: "accounts", DurationMs: 5, Source: FieldSourceL2}, // L2 cache hit
},
}
assert.Equal(t, []SubgraphFetchMetrics(nil), snap.SubgraphFetches())
})

t.Run("returns one entry per subgraph fetch", func(t *testing.T) {
snap := CacheAnalyticsSnapshot{
FetchTimings: []FetchTimingEvent{
{DataSource: "accounts", EntityType: "User", DurationMs: 42, Source: FieldSourceSubgraph, ResponseBytes: 256, HTTPStatusCode: 200, IsEntityFetch: true},
{DataSource: "products", EntityType: "", DurationMs: 80, Source: FieldSourceSubgraph, ResponseBytes: 500, HTTPStatusCode: 200, IsEntityFetch: false},
{DataSource: "accounts", EntityType: "User", DurationMs: 15, Source: FieldSourceSubgraph, ResponseBytes: 90, HTTPStatusCode: 200, IsEntityFetch: true},
},
}
result := snap.SubgraphFetches()
assert.Equal(t, []SubgraphFetchMetrics{
{SubgraphName: "accounts", EntityType: "User", DurationMs: 42, HTTPStatusCode: 200, ResponseBytes: 256, IsEntityFetch: true},
{SubgraphName: "products", EntityType: "", DurationMs: 80, HTTPStatusCode: 200, ResponseBytes: 500, IsEntityFetch: false},
{SubgraphName: "accounts", EntityType: "User", DurationMs: 15, HTTPStatusCode: 200, ResponseBytes: 90, IsEntityFetch: true},
}, result)
})

t.Run("excludes cache hits", func(t *testing.T) {
snap := CacheAnalyticsSnapshot{
FetchTimings: []FetchTimingEvent{
{DataSource: "accounts", DurationMs: 0, Source: FieldSourceL1}, // L1 cache hit

Check failure on line 39 in v2/pkg/engine/resolve/subgraph_metrics_test.go

View workflow job for this annotation

GitHub Actions / Linters (1.25)

File is not properly formatted (gci)
{DataSource: "accounts", DurationMs: 5, Source: FieldSourceL2}, // L2 cache hit
{DataSource: "accounts", EntityType: "User", DurationMs: 30, Source: FieldSourceSubgraph, HTTPStatusCode: 200, ResponseBytes: 128}, // actual fetch
},
}
result := snap.SubgraphFetches()
assert.Equal(t, []SubgraphFetchMetrics{
{SubgraphName: "accounts", EntityType: "User", DurationMs: 30, HTTPStatusCode: 200, ResponseBytes: 128},
}, result)
})

t.Run("empty snapshot returns nil", func(t *testing.T) {
snap := CacheAnalyticsSnapshot{}
assert.Equal(t, []SubgraphFetchMetrics(nil), snap.SubgraphFetches())
})

t.Run("preserves error status codes", func(t *testing.T) {
snap := CacheAnalyticsSnapshot{
FetchTimings: []FetchTimingEvent{
{DataSource: "accounts", DurationMs: 100, Source: FieldSourceSubgraph, HTTPStatusCode: 500, ResponseBytes: 50},
{DataSource: "accounts", DurationMs: 20, Source: FieldSourceSubgraph, HTTPStatusCode: 200, ResponseBytes: 256},
},
}
result := snap.SubgraphFetches()
assert.Equal(t, 500, result[0].HTTPStatusCode)
assert.Equal(t, 200, result[1].HTTPStatusCode)
})
}

func TestFetchTimingEvent_NewFields(t *testing.T) {
t.Run("subgraph fetch carries HTTP status and response size", func(t *testing.T) {
event := FetchTimingEvent{
DataSource: "accounts",
DurationMs: 42,
Source: FieldSourceSubgraph,
HTTPStatusCode: 200,
ResponseBytes: 1024,
TTFBMs: 0, // not yet instrumented
}
assert.Equal(t, 200, event.HTTPStatusCode)
assert.Equal(t, 1024, event.ResponseBytes)
assert.Equal(t, int64(0), event.TTFBMs)
})

t.Run("cache hit has zero values for HTTP fields", func(t *testing.T) {
event := FetchTimingEvent{
DataSource: "accounts",
DurationMs: 1,
Source: FieldSourceL1,
}
assert.Equal(t, 0, event.HTTPStatusCode, "cache hits should have zero status code")
assert.Equal(t, 0, event.ResponseBytes, "cache hits should have zero response bytes")
assert.Equal(t, int64(0), event.TTFBMs, "cache hits should have zero TTFB")
})
}
Loading