[Narrator] In this section we are going to discuss
Prometheus's Use Cases.
The first use case I'm going to start with
is monitoring a web application
and I'm going to use Prometheus
with Python-flask and MySQL.
I'm going to integrate Prometheus monitoring
with a web application based on Python.
I'll use the official Prometheus_client
library for Python.
Flask is the web framework I'm going to use.
It'll create an http server
and I'll be able to configure routes for that.
For example, I'll be able to say /query
We'll go to this method in Python.
I'll use library, mysqlclient for Python
to query MySQL database.
I will include one normal query
and one bad behaving query that will take between zero
and 10 seconds to execute that we should see
in Prometheus which query is doing well
and what query is problematic.
So this is the architecture
of what we are going to deploy
and we're going to start
a Python web application that will start
a flask http server on 45,000
and the same Python application.
Also use the prometheus library to expose
monitoring on another endpoint.
It'll do that on port 8,000.
I will then use a client,
for example, I can use curl, wget
or just a browser to access diva publication.
I will hit a few end points
and that will then create the MySQL database
and MySQL database is running on port 3306.
The Prometheus server will then scrape the metrics
from the Promethius monitoring on port 8,000 an hour.
Then go to the Promethius server on port 9090
and then I'll see the data that has been ingested
and I can create graphs based on this data.
I'm going to use the Counter
and the Histogram metric types to capture the data
a Counter to capture the amount of times
an http endpoint is hit + to capture the amount
of times and MySQL query is executed.
So we're going to have two counters,
one for the http endpoint and one for MySQL query.
The value of the counter must always increase.
That's why you should take the Counter type
for these types of data.
Then I'm going to use a Histogram
to capture the latency or the HTTP requests
and the latency of MySQL Queries.
A histogram samples observations
like latencies in our case
and counts them in configurable buckets.
It also provides a sum of all observed values.
The default buckets are intended
to cover a typical web/rpc requests
from milliseconds to seconds.
We didn't really have to confuse those buckets.
The default configuration is intended
to cover our use case.
So this is how going to define
the data types in Python for Prometheus.
From the Prometheus_Client,
we're going to import the counter and the Histogram.
Then I can define the request latency,
so the Flask_Request_Latency
and the Flask_Request_Count.
First one is the Histogram.
The name of this metric is Flask_Request_Latency SQL.
The description is Flask_Request_Latency
and we're gonna have two labels,
one for method and one for endpoint.
Then we're going to have the Flask_Request_Count,
which is a Counter with the name Flask_Request_Count.
Then we have the description
and this one is going to have three labels,
method, endpoint and HTTP status.
So when we are going to count our hits
we can also include the http status.
So then we will be able to see the metrics.
For example, how many http error codes
500s they were and 500 means
that something went wrong.
Then for MySQL we have exactly the same setup.
We have the MySQL_Request Latency
and my SQL_Request_Count.
The labels are different.
In this case we only going to use the query as a label,
so this is the query has been executed.
This is then how we can calculate the latency of a query
For example, in the demo I will go over the code,
but this is the most important part
so I will show this already right now.
We can the finer start time,
which is just the current time.
Then we're going to do a SQL Query.
Do SQL equals select star from table.
That is our SQL Query and then we need
to do the actual query.
I left out the actual query.
I just put a comment here because this is irrelevant
for what I wanna show you,
so we going to do the query.
And when the query is executed sometime will be passed
and then we can do query latency equals
the current time minus the start time
and it will give us a difference
and that is the latency.
And that latency we can then use to then log this
to our Prometheus library,
so we use the MySQL latency.
We add a label.
The label is going to be the SQL query
and we limit it to 50 characters
because SQL queries can be very long.
We just want to lock the beginning.
That's often enough to then find the query
somewhere in our code if you want
to remediate it if it is wrong.
We then use our observations,
we also, we see that observe query latency
and then this latency which can be half a second
one second, multiple seconds will be locked
for every latency that would be lock.
We also going to increase the counter
so we don't have to use MySQL request current
same label, and we're going
to use ink to increase the counter.
So that is the most important code to lock the latency.
So here you see that I use the Histogram
and the counter to then observe and lock these values.
In the demo I will go over a little bit more detail
so that you see from the beginning to the end
how to implement Prometheus in an application
to start monitoring your applications.
This is written in Python and Flask,
but it's a very simple example.
That's why I use Python and Flask
and MySQL so that it's easy for
you to understand and then use it
in other programming languages.
It's very similar to use it
in other programming language,
the idea how to implement it is always the same.