Metrics
When your application is running in production with real-world traffic — or if you’re carrying out targeted load testing — you might want some up-close insight into how it’s performing and what resources it’s using.
For example, you might want to answer questions like:
- How much memory is my application using? How is this changing over time?
- How many goroutines are currently in use? How is this changing over time?
- How many database connections are in use and how many are idle? Do I need to change the connection pool settings?
- What is the ratio of successful HTTP responses to both client and server errors? Are error rates elevated above normal?
Having insight into these things can help inform your hardware and configuration choices, and act as an early warning sign of potential problems (such as memory leaks).
To assist with this, Go’s standard library includes the expvar package which makes it easy to collate and view different application metrics at runtime.
In this section you’ll learn:
How to use the
expvarpackage to view application metrics in JSON format via an HTTP handler.What default application metrics are available, and how to create your own custom application metrics for monitoring the number of active goroutines and the database connection pool.
How to use middleware to monitor request-level application metrics, including the counts of different HTTP response status codes.