Commit e0397f0
[Cosmos] Add additional response headers (#3960)
## Summary
Adds type-safe response types for Cosmos DB operations with access to
additional response headers for diagnostics and metrics. Fixes #3905.
## Design
Uses dedicated wrapper types for each operation category. A design spec
is at `sdk/cosmos/azure_data_cosmos/docs/RESPONSE_METADATA_SPEC.md`.
### Response Types
| Type | Operations | Extra Fields |
|------|-----------|-------------|
| `ItemResponse<T>` | create/read/replace/upsert/delete item | `etag()
-> Option<&Etag>` |
| `ResourceResponse<T>` | create/read/delete database/container,
throughput | (future-proof placeholder) |
| `BatchResponse` | execute_transactional_batch | `etag() ->
Option<&Etag>` |
| `QueryFeedPage<T>` | query_items, query_containers, query_databases |
`index_metrics()`, `query_metrics()` |
| `FeedPage<T>` | generic feed page (reusable for future read-many,
change feed) | common fields only |
| `CosmosDiagnostics` | all operations (via `diagnostics()`) |
`activity_id()`, `server_duration_ms()` |
### Principle
Each operation category gets a dedicated wrapper type. Common accessors
(`request_charge()`, `session_token()`, `diagnostics()`) are on all
types. Operation-specific fields are only on the relevant type.
`QueryFeedPage<T>` composes over `FeedPage<T>`, adding query-specific
metadata. `CosmosResponse<T>` is internal (`pub(crate)`).
## Driver Changes (`azure_data_cosmos_driver`)
- Added `index_metrics`, `query_metrics`, `server_duration_ms`, and
`lsn` fields to `CosmosResponseHeaders`.
- `index_metrics` is base64-decoded (matching Java/.NET) with
`tracing::warn!` on decode failure.
- `server_duration_ms` filters non-finite and negative values.
- Added `server_duration_ms` to `RequestDiagnostics`, populated from
response headers in transport pipeline.
- Made `from_headers()` public for cross-crate access.
## SDK Changes (`azure_data_cosmos`)
- `ItemResponse<T>` wraps `CosmosResponse<T>` with `etag()` using
`azure_core::http::Etag`.
- `ResourceResponse<T>` wraps `CosmosResponse<T>` for resource
management operations.
- `BatchResponse` wraps `CosmosResponse<TransactionalBatchResponse>`
with batch-level `etag()`.
- `QueryFeedPage<T>` composes over `FeedPage<T>`, adding
`index_metrics()` and `query_metrics()`.
- `FeedPage<T>` is a public generic type with common feed fields,
reusable for future read-many and change feed.
- `CosmosDiagnostics` provides `activity_id()` and
`server_duration_ms()` on all response types.
- SDK delegates all header parsing to the driver's
`CosmosResponseHeaders`.
## Tests
- Driver unit tests: base64 decode, invalid base64, NaN/inf/negative
filtering, JSON serialization.
- SDK unit tests: wrapper types, diagnostics accessors, etag with `Etag`
type, edge cases.
- Integration tests: `assert_response` helper validates
`diagnostics().activity_id()` and `diagnostics().server_duration_ms()`
on all point operations; `query_returns_index_and_query_metrics`
validates query-specific metadata with opt-in headers.
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent 212efbd commit e0397f0
28 files changed
Lines changed: 1057 additions & 270 deletions
File tree
- sdk/cosmos
- azure_data_cosmos_driver
- src
- diagnostics
- driver/transport
- models
- tests/emulator_tests
- azure_data_cosmos
- docs
- src
- clients
- models
- query
- routing
- tests
- emulator_tests
- framework
- multi_write_tests
- eng/scripts
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
| 118 | + | |
| 119 | + | |
118 | 120 | | |
119 | 121 | | |
120 | 122 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| 12 | + | |
11 | 13 | | |
12 | 14 | | |
13 | 15 | | |
| |||
19 | 21 | | |
20 | 22 | | |
21 | 23 | | |
22 | | - | |
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
| |||
Lines changed: 90 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
Lines changed: 32 additions & 18 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
7 | 10 | | |
8 | 11 | | |
9 | 12 | | |
10 | | - | |
| 13 | + | |
11 | 14 | | |
12 | 15 | | |
13 | 16 | | |
| |||
106 | 109 | | |
107 | 110 | | |
108 | 111 | | |
109 | | - | |
| 112 | + | |
110 | 113 | | |
111 | 114 | | |
112 | 115 | | |
113 | 116 | | |
114 | 117 | | |
115 | 118 | | |
116 | 119 | | |
117 | | - | |
| 120 | + | |
118 | 121 | | |
119 | 122 | | |
120 | 123 | | |
| |||
150 | 153 | | |
151 | 154 | | |
152 | 155 | | |
153 | | - | |
| 156 | + | |
154 | 157 | | |
155 | 158 | | |
156 | 159 | | |
157 | 160 | | |
158 | 161 | | |
159 | 162 | | |
| 163 | + | |
160 | 164 | | |
161 | 165 | | |
162 | 166 | | |
| |||
199 | 203 | | |
200 | 204 | | |
201 | 205 | | |
202 | | - | |
| 206 | + | |
203 | 207 | | |
204 | 208 | | |
205 | 209 | | |
| |||
214 | 218 | | |
215 | 219 | | |
216 | 220 | | |
217 | | - | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
218 | 225 | | |
219 | 226 | | |
220 | 227 | | |
| |||
231 | 238 | | |
232 | 239 | | |
233 | 240 | | |
234 | | - | |
| 241 | + | |
235 | 242 | | |
236 | 243 | | |
237 | 244 | | |
238 | 245 | | |
239 | 246 | | |
| 247 | + | |
240 | 248 | | |
241 | 249 | | |
242 | 250 | | |
| |||
274 | 282 | | |
275 | 283 | | |
276 | 284 | | |
277 | | - | |
| 285 | + | |
278 | 286 | | |
279 | 287 | | |
280 | 288 | | |
| |||
307 | 315 | | |
308 | 316 | | |
309 | 317 | | |
310 | | - | |
| 318 | + | |
311 | 319 | | |
312 | 320 | | |
313 | 321 | | |
| |||
321 | 329 | | |
322 | 330 | | |
323 | 331 | | |
| 332 | + | |
324 | 333 | | |
325 | 334 | | |
326 | 335 | | |
| |||
359 | 368 | | |
360 | 369 | | |
361 | 370 | | |
362 | | - | |
| 371 | + | |
363 | 372 | | |
364 | 373 | | |
365 | 374 | | |
| |||
392 | 401 | | |
393 | 402 | | |
394 | 403 | | |
395 | | - | |
| 404 | + | |
396 | 405 | | |
397 | 406 | | |
398 | 407 | | |
| |||
406 | 415 | | |
407 | 416 | | |
408 | 417 | | |
| 418 | + | |
409 | 419 | | |
410 | 420 | | |
411 | 421 | | |
| |||
447 | 457 | | |
448 | 458 | | |
449 | 459 | | |
450 | | - | |
| 460 | + | |
451 | 461 | | |
452 | 462 | | |
453 | 463 | | |
| |||
479 | 489 | | |
480 | 490 | | |
481 | 491 | | |
482 | | - | |
| 492 | + | |
483 | 493 | | |
484 | 494 | | |
485 | 495 | | |
| |||
493 | 503 | | |
494 | 504 | | |
495 | 505 | | |
496 | | - | |
| 506 | + | |
| 507 | + | |
497 | 508 | | |
498 | 509 | | |
499 | 510 | | |
| |||
532 | 543 | | |
533 | 544 | | |
534 | 545 | | |
535 | | - | |
| 546 | + | |
536 | 547 | | |
537 | 548 | | |
538 | 549 | | |
| |||
549 | 560 | | |
550 | 561 | | |
551 | 562 | | |
| 563 | + | |
552 | 564 | | |
553 | 565 | | |
554 | 566 | | |
| |||
577 | 589 | | |
578 | 590 | | |
579 | 591 | | |
580 | | - | |
| 592 | + | |
581 | 593 | | |
582 | 594 | | |
583 | 595 | | |
| |||
590 | 602 | | |
591 | 603 | | |
592 | 604 | | |
| 605 | + | |
593 | 606 | | |
594 | 607 | | |
595 | 608 | | |
| |||
726 | 739 | | |
727 | 740 | | |
728 | 741 | | |
729 | | - | |
| 742 | + | |
730 | 743 | | |
731 | 744 | | |
732 | 745 | | |
| |||
740 | 753 | | |
741 | 754 | | |
742 | 755 | | |
| 756 | + | |
743 | 757 | | |
744 | 758 | | |
0 commit comments