|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Reporting & Monitoring |
| 4 | +parent: How-To |
| 5 | +nav_order: 6 |
| 6 | +--- |
| 7 | + |
| 8 | +# Reporting & Monitoring |
| 9 | + |
| 10 | +The `OpenSleigh.Reporting` package provides ready-to-use ASP.NET Core Minimal API endpoints for querying saga state at runtime. This is useful for building dashboards, debugging distributed workflows, and monitoring saga progress. |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +Install the NuGet package: |
| 15 | + |
| 16 | +``` |
| 17 | +dotnet add package OpenSleigh.Reporting |
| 18 | +``` |
| 19 | + |
| 20 | +## Setup |
| 21 | + |
| 22 | +Register the reporting services and map the endpoints in your ASP.NET Core application: |
| 23 | + |
| 24 | +```csharp |
| 25 | +var builder = WebApplication.CreateBuilder(args); |
| 26 | + |
| 27 | +builder.Services.AddOpenSleigh(cfg => |
| 28 | +{ |
| 29 | + // your transport and persistence configuration |
| 30 | +}); |
| 31 | + |
| 32 | +builder.Services.AddOpenSleighReporting(); |
| 33 | + |
| 34 | +var app = builder.Build(); |
| 35 | + |
| 36 | +app.MapOpenSleighReporting(); // default prefix: /opensleigh |
| 37 | +
|
| 38 | +app.Run(); |
| 39 | +``` |
| 40 | + |
| 41 | +You can customize the endpoint prefix: |
| 42 | + |
| 43 | +```csharp |
| 44 | +app.MapOpenSleighReporting("/api/sagas"); |
| 45 | +``` |
| 46 | + |
| 47 | +## REST Endpoints |
| 48 | + |
| 49 | +All endpoints are grouped under the configured prefix (default `/opensleigh`): |
| 50 | + |
| 51 | +| Method | Path | Description | |
| 52 | +|--------|------|-------------| |
| 53 | +| `GET` | `/sagas` | List saga instances (paginated, with optional filters) | |
| 54 | +| `GET` | `/sagas/{instanceId}` | Get a saga instance by its unique ID | |
| 55 | +| `GET` | `/sagas/correlation/{correlationId}?sagaType=...` | Get a saga instance by correlation ID and saga type | |
| 56 | +| `GET` | `/sagas/types` | List all registered message types with saga handlers | |
| 57 | + |
| 58 | +### Query Parameters |
| 59 | + |
| 60 | +The **list sagas** endpoint (`GET /sagas`) supports the following query parameters: |
| 61 | + |
| 62 | +| Parameter | Type | Default | Description | |
| 63 | +|-----------|------|---------|-------------| |
| 64 | +| `sagaType` | `string?` | `null` | Filter by saga type name | |
| 65 | +| `isCompleted` | `bool?` | `null` | Filter by completion status | |
| 66 | +| `page` | `int` | `1` | Page number (must be ≥ 1) | |
| 67 | +| `pageSize` | `int` | `20` | Results per page (1–100) | |
| 68 | + |
| 69 | +The **get by correlation ID** endpoint (`GET /sagas/correlation/{correlationId}`) requires a `sagaType` query parameter. |
| 70 | + |
| 71 | +### Response Format |
| 72 | + |
| 73 | +The list endpoint returns a `PagedResult<SagaInstanceInfo>`: |
| 74 | + |
| 75 | +```json |
| 76 | +{ |
| 77 | + "items": [ |
| 78 | + { |
| 79 | + "instanceId": "0193a7e2-...", |
| 80 | + "correlationId": "0193a7e2-...", |
| 81 | + "triggerMessageId": "0193a7e2-...", |
| 82 | + "sagaType": "OrderSaga", |
| 83 | + "sagaStateType": "OrderState", |
| 84 | + "isCompleted": false, |
| 85 | + "isLocked": false, |
| 86 | + "processedMessages": [ |
| 87 | + { "messageId": "0193a7e2-...", "when": "2025-01-10T12:00:00Z" } |
| 88 | + ], |
| 89 | + "stateData": { ... } |
| 90 | + } |
| 91 | + ], |
| 92 | + "totalCount": 42, |
| 93 | + "page": 1, |
| 94 | + "pageSize": 20 |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +## OpenAPI Support |
| 99 | + |
| 100 | +On .NET 9+, `AddOpenSleighReporting()` automatically registers OpenAPI services, and `MapOpenSleighReporting()` maps the OpenAPI document endpoint. The generated document is available at `/openapi/v1.json`. |
| 101 | + |
| 102 | +## Security |
| 103 | + |
| 104 | +{: .warning } |
| 105 | +> The reporting endpoints expose saga state data, which may contain sensitive information. Always configure appropriate authentication and authorization middleware **before** calling `MapOpenSleighReporting()` in production environments. |
| 106 | +
|
| 107 | +For example: |
| 108 | + |
| 109 | +```csharp |
| 110 | +app.UseAuthentication(); |
| 111 | +app.UseAuthorization(); |
| 112 | + |
| 113 | +app.MapOpenSleighReporting() |
| 114 | + .RequireAuthorization(); |
| 115 | +``` |
| 116 | + |
| 117 | +## Example: PizzaTracker Sample |
| 118 | + |
| 119 | +The [PizzaTracker sample](https://github.com/mizrael/OpenSleigh/tree/develop/samples/OpenSleigh.Samples.PizzaTracker) demonstrates a complete setup with reporting endpoints. It uses in-memory transport and persistence with a pizza order saga that progresses through multiple stages (Received → Preparing → Baking → Quality Check → Out for Delivery → Delivered). |
| 120 | + |
| 121 | +```csharp |
| 122 | +var builder = WebApplication.CreateBuilder(args); |
| 123 | + |
| 124 | +builder.Services.AddOpenSleigh(cfg => |
| 125 | +{ |
| 126 | + cfg.UseInMemoryTransport() |
| 127 | + .UseInMemoryPersistence() |
| 128 | + .AddSaga<OrderSaga, OrderState>(); |
| 129 | +}); |
| 130 | + |
| 131 | +builder.Services.AddOpenSleighReporting(); |
| 132 | + |
| 133 | +var app = builder.Build(); |
| 134 | + |
| 135 | +app.MapOpenSleighReporting(); |
| 136 | + |
| 137 | +app.MapPost("/orders", async (PlaceOrderRequest request, IMessageBus bus) => |
| 138 | +{ |
| 139 | + var message = new PlaceOrder(request.CustomerName, request.PizzaType); |
| 140 | + await bus.PublishAsync(message); |
| 141 | + return Results.Accepted(); |
| 142 | +}); |
| 143 | + |
| 144 | +app.Run(); |
| 145 | +``` |
| 146 | + |
| 147 | +After placing an order, use `GET /opensleigh/sagas` to track its progress through the workflow. |
0 commit comments