9 Cross-cutting concerns: Monitoring and logging

 

This chapter covers

  • Monitoring in a microservice system
  • Exploring structured logging
  • Distributed tracing across microservices
  • Logging unhandled exceptions

In this chapter, you’ll start to learn how ASP.NET helps us tackle some important cross-cutting concerns: monitoring, logging, and tracing. All three are needed across all microservices, and they play an important role in making a microservice system operations friendly. Once your system is in production, you need to know whether all your microservices are up, which is why you need to monitor them. In addition, as discussed in chapter 7, you need good logging to be able to diagnose the system.

Here, you’ll configure middleware in the context of one microservice. Then, in chapter 11, you’ll take that configuration and put it into NuGet packages, ready to be reused easily across all your microservices.

9.1 Monitoring needs in microservices

When you deploy any server-side system into production, you need to be able to check the health of the system. You want to know whether it is up, whether it’s experiencing failures or errors, and whether it’s performing as well as it usually does. This is true of any system. With a traditional monolithic system, you’d most likely set up some monitoring and add logging to the system, as shown in figure 9.1. Logging is often done many places in the code base—where there’s something important to log—and the messages are often stored in a database.

9.2 Logging needs in microservices

9.2.1 Tracing requests across microservices

9.2.2 Structured logging with Serilog

9.3 Implementing the monitoring endpoints

9.3.1 Implementing the /health/live monitoring endpoint

9.3.2 Implementing the /health/startup monitoring endpoint

9.4 Implementing structured logging

9.4.1 Adding a trace ID to all log messages

9.4.2 Trace ID is included in outgoing HTTP requests

9.4.3 Logging unhandled exceptions

9.5 Implementing monitoring and logging in Kubernetes

9.5.1 Configure monitoring in Kubernetes

Summary