Overview of PromQL for Prometheus, covering query concepts, data types (scalar, instant and range vectors), selectors, operators, aggregations, subqueries, histograms and visualization.
In this section, we’re going to be discussing PromQL.So what is PromQL?Well, it’s short for Prometheus Query Language, and it’s the mainway to query metrics within Prometheus.So you’ll send a request to the Prometheus server, provide your specificPromQL expression, and it’s going to return the data for that expression.And the data that you get returned can be visualized in dashboards,whether that’s through Grafana or some ofthe built-in dashboarding tools within Prometheus.
And you can also use PromQL to build alerting rules to notifyadministrators when certain thresholds have been crossed.And so in this section, we’re going tocover a lot of different things.This is going to be one of the larger sections.We’re going to go over the different expression datastructures and what they can return.We’re going to cover different selectors and modifiers so that we canget the exact data that we’re interested in.We’re going to go over different operators and functions.And I’m going to discuss vector matching.We’ll go over the various aggregators that’s built in with Prometheus.We’ll cover subqueries, and then I’ll do a little bit of adeep dive into what we can do with histogram and summary metrics.
So when you run a PromQL expression, what gets returned from thePrometheus server can be one of four types.The first one is a string, which is a simplestring value, which is currently unused.You have a scalar, which is anumeric value, so some floating-point value.You have an instant vector, which is a set of time seriescontaining a single sample for each timeseries, all sharing the same timestamp.
And a range vector is a set of time series containing arange of data points over time for each time series.So let’s go over a scalar.Now, as I mentioned, a string is just a simple string value,and it’s currently unused, but it would justbe some random text like that.A scalar is going to be a simple numeric floating-point value.So something like fifty-four point seven four three orone hundred twenty-seven point four three.So it’s just some number.An instant vector is going to return a set of time seriescontaining just one single sample for each time series,all sharing the same exact timestamp.So if we perform this query, which is just taking the nameof a metric we’re interested in and then just querying it usingthat, Prometheus is going to return the following data.So what it does is it finds this specific metricwith all of its unique labels.And remember, every combination of metric and unique labels is
going to be one time series.So we have four total time series.And we’re going to get the specific value foreach one of these time series.Now, the specific or the most important thing
about instant vectors is the timestamp.So it’s going to return one single sample for each time series.And they’re all going to be at the same exact timestamp.So for each time series, we’re going to get the data atone specific point in time, which is at March 3rd, elevenoh five a.m., the same timestamp.And so it’s going to just return a single point in time,whatever the value of the metric is.Now, range vector is going to be a little bit different.Range vector is going to return a set of time series containinga range of different data points over time for each time series.So if we run the same query for the same metric, butthis time we do this bracket and then three M and thenclose bracket, what this means is I want to get this metricdata not just for a single point in time,but for the past three minutes.And that’s what this means, the three M. So what it’s goingto return is something like this.So you’re going to get the metric, and you’re going toget the unique combination of labels.And remember, every metric with a combination ofunique labels is a time series.So in this example, there are two time series it returned.you’re gonna get the value and a timestamp.Now where it differs is you’re gonna getmore than one timestamp and value.So it’s actually going to return all of the values and timestampsthat it got for the past three minutes.So it looks like it scraped this specific server threetimes in the past three minutes.It just so happened, I just used three as an example.It can be more than that, it can be less than that.And for the other time series, we’re going to get thevalues at the same exact time.So we got it at eight oh five, eight oh six, eightoh seven, and you can see the same thing at eight ohfive, eight oh six, eight oh seven.So we get the value for thismetric at those three data points.And so it’s going to contain allthe data during that three-minute window.And that’s really the main difference between arange vector and an instant vector.A range vector is going to return metrics over the course of