diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cbb400..88847e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ verbatim as the body of the matching GitHub release. | Version | Date | Notes | |---|---|---| +| v0.2.11 | 2026-11-24 | [changelog/v0.2.11.md](changelog/v0.2.11.md) | | v0.2.10 | 2026-11-24 | [changelog/v0.2.10.md](changelog/v0.2.10.md) | | v0.2.9 | 2026-11-24 | [changelog/v0.2.9.md](changelog/v0.2.9.md) | | v0.2.8 | 2026-11-24 | [changelog/v0.2.8.md](changelog/v0.2.8.md) | diff --git a/changelog/v0.2.11.md b/changelog/v0.2.11.md new file mode 100644 index 0000000..1b0c696 --- /dev/null +++ b/changelog/v0.2.11.md @@ -0,0 +1,14 @@ +# v0.2.11 + +Request IDs on failed calls. + +## Added + +- Usage records now store the gateway `X-Request-Id` on successes, cache hits and errors. +- `/admin/usage` and `/v1/usage` expose `request_id`. +- The dashboard **Recent failures** table falls back to the gateway request id when the + upstream provider did not return its own request id. + +## Fixed + +- Failed calls no longer show `—` in the Request id column. diff --git a/deploy/dokploy/README.md b/deploy/dokploy/README.md index ef33400..f726c69 100644 --- a/deploy/dokploy/README.md +++ b/deploy/dokploy/README.md @@ -20,7 +20,7 @@ database. The console is always a plain Application. 3. Compose path: `deploy/dokploy/compose.yml`. 4. **Trigger Type: On Tag**, with auto-deploy enabled. The gateway is a production service; a pull request or a push to `Develop` must not restart it. A release tag - (`v0.2.10`) is the only thing that deploys — see "When it deploys" below. + (`v0.2.11`) is the only thing that deploys — see "When it deploys" below. 5. **Environment** (the compose file references all of these): ```bash diff --git a/docs/SPRING-AI.md b/docs/SPRING-AI.md index b94c47f..0cd681d 100644 --- a/docs/SPRING-AI.md +++ b/docs/SPRING-AI.md @@ -13,7 +13,7 @@ on Spring AI; the gateway never depends on it. io.github.an1ndra path-spring-ai - 0.2.10 + 0.2.11 ``` diff --git a/path-gateway/pom.xml b/path-gateway/pom.xml index 8603b04..44cd357 100644 --- a/path-gateway/pom.xml +++ b/path-gateway/pom.xml @@ -7,7 +7,7 @@ io.github.an1ndra path-parent - 0.2.10 + 0.2.11 ../pom.xml diff --git a/path-gateway/src/main/java/com/anindra/path/admin/AdminReportingService.java b/path-gateway/src/main/java/com/anindra/path/admin/AdminReportingService.java index b1f8ae4..a94da93 100644 --- a/path-gateway/src/main/java/com/anindra/path/admin/AdminReportingService.java +++ b/path-gateway/src/main/java/com/anindra/path/admin/AdminReportingService.java @@ -78,6 +78,7 @@ public Map usage(Long keyId, Long projectId, String endUser, Str row.put("ttft_ms", r.getTtftMs()); row.put("tokens_per_second", r.getTokensPerSecond()); row.put("upstream_request_id", r.getUpstreamRequestId()); + row.put("request_id", r.getRequestId()); row.put("finish_reason", r.getFinishReason()); row.put("attempts", r.getAttempts()); row.put("fallback_from", r.getFallbackFrom()); diff --git a/path-gateway/src/main/java/com/anindra/path/domain/UsageRecord.java b/path-gateway/src/main/java/com/anindra/path/domain/UsageRecord.java index 80524b2..0daf92e 100644 --- a/path-gateway/src/main/java/com/anindra/path/domain/UsageRecord.java +++ b/path-gateway/src/main/java/com/anindra/path/domain/UsageRecord.java @@ -97,6 +97,10 @@ public enum Status {SUCCESS, ERROR} @Column(length = 128) private String upstreamRequestId; + /** Gateway correlation id (X-Request-Id), present on both success and error rows. */ + @Column(length = 128) + private String requestId; + @Column(length = 32) private String finishReason; diff --git a/path-gateway/src/main/java/com/anindra/path/gateway/UsageService.java b/path-gateway/src/main/java/com/anindra/path/gateway/UsageService.java index 7e63d86..e8feccb 100644 --- a/path-gateway/src/main/java/com/anindra/path/gateway/UsageService.java +++ b/path-gateway/src/main/java/com/anindra/path/gateway/UsageService.java @@ -122,6 +122,7 @@ public BigDecimal recordSuccess(ClientKey key, ChatRequestSpec spec, RouteResolv record.setTokensPerSecond(throughput(u, meta)); record.setStreamed(meta.streamed()); record.setUpstreamRequestId(meta.requestId()); + record.setRequestId(currentRequestId()); record.setFinishReason(meta.finishReason()); record.setAttempts(meta.attempts()); record.setFallbackFrom(meta.fallbackFrom()); @@ -154,6 +155,7 @@ public void recordCacheHit(ClientKey key, ChatRequestSpec spec, RouteResolver.Re record.setAttempts(0); record.setFinishReason("cache"); record.setCacheTier(tier); + record.setRequestId(currentRequestId()); record.setStatus(UsageRecord.Status.SUCCESS); usage.save(record); metrics.counter("path.cache.hits", @@ -179,6 +181,7 @@ public void recordError(ClientKey key, ChatRequestSpec spec, String providerSlug record.setStatus(UsageRecord.Status.ERROR); record.setErrorCode(code); record.setErrorMessage(message != null && message.length() > 500 ? message.substring(0, 500) : message); + record.setRequestId(currentRequestId()); usage.save(record); // Kept separate from path_requests_total so an alert can tell a 4xx from a 5xx // without a high-cardinality label on the request counter. @@ -349,6 +352,9 @@ private List> recentRecords(ClientKey key) { if (r.getUpstreamRequestId() != null) { row.put("upstream_request_id", r.getUpstreamRequestId()); } + if (r.getRequestId() != null) { + row.put("request_id", r.getRequestId()); + } if (r.getFinishReason() != null) { row.put("finish_reason", r.getFinishReason()); } @@ -452,4 +458,9 @@ private static double percentileOfDecimals(List sorted, double p) { private static long nz(Long value) { return value == null ? 0L : value; } + + private static String currentRequestId() { + String id = org.slf4j.MDC.get(com.anindra.path.config.RequestIdFilter.MDC_KEY); + return id == null || id.isBlank() ? null : id; + } } diff --git a/path-gateway/src/main/resources/db/migration/V22__usage_request_id.sql b/path-gateway/src/main/resources/db/migration/V22__usage_request_id.sql new file mode 100644 index 0000000..42d919d --- /dev/null +++ b/path-gateway/src/main/resources/db/migration/V22__usage_request_id.sql @@ -0,0 +1 @@ +alter table usage_records add column request_id varchar(128); diff --git a/path-gateway/src/test/java/com/anindra/path/gateway/ErrorMetricsTest.java b/path-gateway/src/test/java/com/anindra/path/gateway/ErrorMetricsTest.java index fa87c9e..160e2a5 100644 --- a/path-gateway/src/test/java/com/anindra/path/gateway/ErrorMetricsTest.java +++ b/path-gateway/src/test/java/com/anindra/path/gateway/ErrorMetricsTest.java @@ -1,12 +1,17 @@ package com.anindra.path.gateway; import com.anindra.path.config.GatewayProperties; +import com.anindra.path.config.RequestIdFilter; +import com.anindra.path.domain.UsageRecord; import com.anindra.path.repo.UsageRecordRepository; import io.micrometer.core.instrument.simple.SimpleMeterRegistry; import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; import org.mockito.Mockito; +import org.slf4j.MDC; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.verify; class ErrorMetricsTest { @@ -39,4 +44,22 @@ void aMissingCodeIsUnknownRatherThanACardinalityBomb() { assertThat(registry.counter("path.errors", "provider", "groq", "class", "5xx", "code", "unknown").count()).isEqualTo(1.0); } + + @Test + void anErrorRecordCarriesTheGatewayRequestId() { + UsageRecordRepository repo = Mockito.mock(UsageRecordRepository.class); + UsageService service = new UsageService(repo, Mockito.mock(ApiKeyService.class), + registry, new GatewayProperties()); + MDC.put(RequestIdFilter.MDC_KEY, "req-123"); + try { + service.recordError(null, null, "openai", "gpt-4o", 12, false, 1, 500, + "upstream_error", "the provider fell over"); + } finally { + MDC.remove(RequestIdFilter.MDC_KEY); + } + + ArgumentCaptor saved = ArgumentCaptor.forClass(UsageRecord.class); + verify(repo).save(saved.capture()); + assertThat(saved.getValue().getRequestId()).isEqualTo("req-123"); + } } diff --git a/path-spring-ai/pom.xml b/path-spring-ai/pom.xml index 120a08d..42aae67 100644 --- a/path-spring-ai/pom.xml +++ b/path-spring-ai/pom.xml @@ -7,7 +7,7 @@ io.github.an1ndra path-parent - 0.2.10 + 0.2.11 ../pom.xml diff --git a/pom.xml b/pom.xml index 2c50748..6897dcb 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ io.github.an1ndra path-parent - 0.2.10 + 0.2.11 pom path-parent Build parent for Path, the provider-agnostic LLM gateway, and its Spring AI adapter