Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Building Data Science Applications with FastAPI - Second Edition

You're reading from  Building Data Science Applications with FastAPI - Second Edition

Product type Book
Published in Jul 2023
Publisher Packt
ISBN-13 9781837632749
Pages 422 pages
Edition 2nd Edition
Languages
Author (1):
François Voron François Voron
Profile icon François Voron

Table of Contents (21) Chapters

Preface 1. Part 1: Introduction to Python and FastAPI
2. Chapter 1: Python Development Environment Setup 3. Chapter 2: Python Programming Specificities 4. Chapter 3: Developing a RESTful API with FastAPI 5. Chapter 4: Managing Pydantic Data Models in FastAPI 6. Chapter 5: Dependency Injection in FastAPI 7. Part 2: Building and Deploying a Complete Web Backend with FastAPI
8. Chapter 6: Databases and Asynchronous ORMs 9. Chapter 7: Managing Authentication and Security in FastAPI 10. Chapter 8: Defining WebSockets for Two-Way Interactive Communication in FastAPI 11. Chapter 9: Testing an API Asynchronously with pytest and HTTPX 12. Chapter 10: Deploying a FastAPI Project 13. Part 3: Building Resilient and Distributed Data Science Systems with FastAPI
14. Chapter 11: Introduction to Data Science in Python 15. Chapter 12: Creating an Efficient Prediction API Endpoint with FastAPI 16. Chapter 13: Implementing a Real-Time Object Detection System Using WebSockets with FastAPI 17. Chapter 14: Creating a Distributed Text-to-Image AI System Using the Stable Diffusion Model 18. Chapter 15: Monitoring the Health and Performance of a Data Science System 19. Index 20. Other Books You May Enjoy

Monitoring the Health and Performance of a Data Science System

In this chapter, we will cover the extra mile so you are able to build robust, production-ready systems. One of the most important aspects to achieve this is to have all the data we need to ensure the system is operating correctly and detect as soon as possible when something goes wrong so we can take corrective actions. In this chapter, we’ll see how to set up a proper logging facility and how we can monitor the performance and health of our software in real time.

We’re near the end of our journey into FastAPI for data science. Until now, we’ve mainly focused on the functionality of the programs we implemented. However, there is another aspect that is often overlooked by developers but is actually very important: assessing whether the system is functioning correctly and reliably in production and being warned as soon as possible when that’s not the case.

For this, lot of tools and techniques...

Technical requirements

For this chapter, you’ll require a Python virtual environment, just as we set up in Chapter 1, Python Development Environment Setup.

To run a Dramatiq worker, you’ll need a running Redis server on your local computer. The easiest way is to run it as a Docker container. If you’ve never used Docker before, we recommend you read the Getting started tutorial in the official documentation at https://docs.docker.com/get-started/. Once done, you’ll be able to run a Redis server with this simple command:

$ docker run -d --name worker-redis -p 6379:6379 redis

You’ll find all the code examples of this chapter in the dedicated GitHub repository at https://github.com/PacktPublishing/Building-Data-Science-Applications-with-FastAPI-Second-Edition/tree/main/chapter15.

A note about the screenshots

In the course of this chapter, we’ll present several screenshots, in particular of the Grafana interface. Their goal is to show...

Configuring and using a logging facility with Loguru

In software development, logs are probably the simplest but most powerful way to control the behavior of a system. They usually consist of lines of plain text that are printed at specific points of a program. By reading them chronologically, we are able to trace the behavior of the program and check that everything goes well. Actually, we’ve already seen log lines in this book. When you run a FastAPI app with Uvicorn and make some requests, you’ll see these lines in the console output:

INFO:     Started server process [94918]INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     127.0.0.1:60736 - "POST /generated-images HTTP/1.1" 201 Created

Those are the logs generated...

Adding Prometheus metrics

In the previous section, we saw how logs can help us understand what our program is doing by finely tracing the operations it does over time. However, most of the time, you can’t afford to keep an eye on the logs all day: they are useful for understanding and debugging a particular situation but way less useful for getting global insights to alert you when something goes wrong.

To solve this, we’ll see in this section how to add metrics to our application. Their role is to measure things that matter in the execution of our program: the number of requests made, the time taken to give a response, the number of pending tasks in the worker queue, the accuracy of our ML predictions… Anything that we could easily monitor over time – usually, with charts and graphs – so we can easily monitor the health of our system. We say that we instrument our application.

To achieve this task, we’ll use two widely used technologies...

Monitoring metrics in Grafana

Having metrics is nice, but being able to visualize them is better! In this section, we’ll see how we can collect Prometheus metrics, send them to Grafana, and create dashboards to monitor them.

Grafana is an open source web application for data visualization and analytics. It’s able to connect to various data sources, such as timeseries databases and, of course, Prometheus. Its powerful query and graph builder allows us to create detailed dashboards where we can monitor our data in real time.

Configuring Grafana to collect metrics

Since it’s open source, you can run it from your own machine or server. Detailed instructions are available in the official documentation: https://grafana.com/docs/grafana/latest/setup-grafana/installation/. However, to speed things up and get you started quickly, we’ll rely here on Grafana Cloud, an official hosting platform. It offers a free plan, which should be enough for you to get started...

Summary

Congratulations! You are now able to report metrics and build your own dashboards in Grafana to monitor your data science applications. Over time, don’t hesitate to add new metrics or complete your dashboards if you notice some blind spots: the goal is to be able to watch over every important part at a glance so you can quickly take corrective actions. Those metrics can also be used to drive the evolution of your work: by monitoring the performance and accuracy of your ML models, you can track the effects of your changes and see whether you are going in the right direction.

This is the end of this book and our FastAPI journey. We sincerely hope that you liked it and that you learned a lot along the way. We’ve covered many subjects, sometimes just by scratching the surface, but you should now be ready to build your own projects with FastAPI and serve smart data science algorithms. Be sure to check all the external resources we proposed along the way, as they...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Building Data Science Applications with FastAPI - Second Edition
Published in: Jul 2023 Publisher: Packt ISBN-13: 9781837632749
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}