6 Integration testing and monitoring
In this chapter, the application is made more resilient by combining proactive monitoring with pre-production testing. The discussion begins by explaining why observability matters: it helps detect failures, performance issues, security problems, and compliance risks early, while also improving reliability, scalability, cost efficiency, and user experience. Aspire is presented as a convenient platform for handling this because its starter setup already includes the dependencies needed for telemetry and operational insight.
The chapter then breaks monitoring down into its three core parts: logs, metrics, and traces. Logs capture events and diagnostic details, metrics provide numerical measurements of performance and resource use over time, and traces show how requests move across services and where delays occur. Aspire relies on OpenTelemetry to collect and export this telemetry, and the text walks through how logging, metrics, and tracing are configured in the application and then viewed centrally in the Aspire dashboard.
It also explains how Aspire uses health checks to track whether services are currently running correctly, using endpoints such as /health and /alive and the orchestrator’s status reporting to mark services as healthy, unhealthy, or degraded. The final part focuses on integration testing, showing how Aspire can spin up a full distributed app in a lightweight test host and verify behavior with xUnit, NUnit, or MSTest. It also demonstrates richer UI validation with HTML parsing and component-level testing with bUnit, emphasizing that Aspire supports both end-to-end verification and finer-grained component checks.
Components of a monitoring system
Console logs for an individual service
Structured logs in Aspire
Traces in Aspire setup
Metrics tab in Aspire
State of services in the Aspire dashboard
Service startup error shown in the Aspire dashboard
Adding a test project to Aspire app
Products page in the Blazor UI app hosted by Aspire
Counter page in Aspire-hosted Blazor application
Summary
- By default, Aspire uses OpenTelemetry, a standardized and platform-agnostic way of collecting telemetry.
- Logging records relatively detailed information about events happening inside an application.
- Metrics record simple data, such as event counts and event durations.
- Traces link several recorded events so that you can trace the sequence of events.
- Health checks determine whether an app is ready to interact with other systems.
- The Aspire host has its own internal health-checking mechanism, which allows you to see immediately whether any services aren’t running as expected.
- The Aspire Starter Project has placeholders for creating health-check endpoints in HTTP-enabled applications hosted by Aspire.
- The exact health-check endpoint implementation is typically environment-specific due to factors such as security.
- Aspire not only allows developers to build distributed, orchestrated applications relatively easily compared with equivalent technologies but also makes it easy to write integration tests against these applications.
- Special libraries let developers add tests to Aspire. The Aspire test libraries are fully compatible with any testing framework available in .NET. Lower-level tests, such as unit tests, can be applied to individual services hosted by Aspire.
- Any library can be used to test individual apps hosted by Aspire, including those that parse HTML and Blazor components.
FAQ
What are the main components of a monitoring system in distributed applications?
Logging, metrics, and traces are the three core components. Logging records events and errors, metrics provide numerical measurements over time, and traces show the end-to-end flow of a request through multiple services.Why is application monitoring important in Aspire-based distributed systems?
Monitoring helps detect errors, failures, performance bottlenecks, and security issues early. It also improves reliability, availability, capacity planning, cost efficiency, compliance reporting, and overall user experience.How does Aspire use OpenTelemetry for observability?
Aspire relies on OpenTelemetry as its standardized, vendor-agnostic observability framework. It uses OpenTelemetry to collect logs, metrics, and traces, and then exports them to a configured endpoint such as an OTLP collector or monitoring backend.Which OpenTelemetry packages are commonly used in the Aspire ServiceDefaults project?
The standard ServiceDefaults setup typically includesOpenTelemetry.Exporter.OpenTelemetryProtocol, OpenTelemetry.Extensions.Hosting, OpenTelemetry.Instrumentation.AspNetCore, OpenTelemetry.Instrumentation.Http, and OpenTelemetry.Instrumentation.Runtime.How are telemetry settings configured in an Aspire application?
Telemetry is configured in the ServiceDefaults project by calling methods such asbuilder.ConfigureOpenTelemetry(), enabling logging, metrics, and tracing, and then conditionally enabling OTLP export with builder.Services.AddOpenTelemetry().UseOtlpExporter() when OTEL_EXPORTER_OTLP_ENDPOINT is set.Where can you view logs, traces, and metrics in Aspire?
You can view them in the Aspire dashboard after launching the app. The Console tab shows console logs, the Structured tab shows structured logs, the Traces tab shows distributed traces, and the Metrics tab shows collected metrics.How does Aspire determine whether a service is healthy?
Aspire uses health-check endpoints configured on services. The orchestrator periodically pings those endpoints and checks whether the service responds as expected, using methods such asWithHttpHealthCheck(), WaitFor(), and health-check mappings in the app.What health states does Aspire recognize for services?
Aspire commonly recognizes three states: Healthy, Unhealthy, and Degraded. Healthy means the app is functioning normally, Unhealthy means it is not working as expected, and Degraded means it works but below optimal performance.How do you set up integration tests for an Aspire application?
You can create a test project using the Aspire test templates, such asdotnet new aspire-xunit, or use NUnit or MSTest equivalents. Then add the Aspire.Hosting.Testing package, reference the AppHost project, build the distributed app with DistributedApplicationTestingBuilder, start it, and send HTTP requests through the test host.How can you test the UI of an Aspire-hosted Blazor app?
You can write integration tests to verify the rendered page content, for example by parsing HTML withAngleSharp and checking elements like an h1. For component-level UI tests, you can use bUnit to render components directly and assert their markup or behavior, such as clicking a button to increment a counter.
Building Distributed Applications that Work ebook for free