Kming
Kming

Reputation: 11

How can I make line chart by using label value in Prometheus/Grafana?

Update 1:

I understand it's not time-series data. The purpose is to avoid using the timestamp of the metrics since I'd like to specify an hour in the past.

I have some metrics that indicate the number of products being produced in a given hour.

some_metric{"hour"="1" "product"="a"}  5
some_metric{"hour"="2" "product"="a"}  7
some_metric{"hour"="1" "product"="b"}  3
some_metric{"hour"="2" "product"="b"}  2

The first metric interprets as 5 of product "a" is produced in hour=1

Now I'd like to make a chart in Grafana that uses the hour as x-axis and metric value as y-axis. In this case, I'd expect two lines (each represents the production of a and b respectively) on the same chart.

Any hints?

Thanks a lot.

Upvotes: 1

Views: 3758

Answers (2)

Hang
Hang

Reputation: 1204

You probably should just store your data in MySQL, and use the Grafana MySQL data source along with Grafana Table Panel.

https://grafana.com/docs/grafana/latest/datasources/mysql/#table-queries

Upvotes: 1

Hang
Hang

Reputation: 1204

First thing first, you need understand what time series is.

Prometheus, being a time series database, will automatically add timestamp to the data when it scrapes your metric endpoint.

In other words, with Prometheus, you will get "hour" label for free. You should drop "hour" label from your metric and do not worry about it during your metric schema design.

Now onto Grafana. When you use Grafana Graph panel to represent the Prometheus query result of PromQL query like:

some_metric

By default, Grafana will put timestamp on X-Axis and draw TWO lines based on the values of "product"="a" and the values of "product"="b" returned from the query.

Another way to explain is:

Prometheus TSDB stores your time series data in the format of

time series #1: some_metric{"product"="a"}  5@timestamp1, 7@timestamp2, ...
time series #2: some_metric{"product"="b"}  3@timestamp1, 2@timestamp2, ...

When Grafana queries "some_metric" from Prometheus, Prometheus will return 2 sets of key-value pairs, one set for "product"="a" and one set for "product"="b", with key being timestamp1, timestamp2, ..., and with value being number of your products.

Did I make it clear enough? If not, then you can check out a Grafana Graph example here. Click on the "Query Inspector" button, you will see how the actual time-series data returned from PromQL query looks like.

Upvotes: 3

Related Questions