10

I am using Prometheus to query metrics from Apache Flink. I want to measure the number of records In and Out per second of a Map function. When I query two different metrics in Prometheus, the chart only shows one of them.

flink_taskmanager_job_task_operator_numRecordsInPerSecond{operator_name="Map"} 
or flink_taskmanager_job_task_operator_numRecordsOutPerSecond{operator_name="Map"}

enter image description here Does not matter if I change the operator or to and. The chart shows only the first (flink_taskmanager_job_task_operator_numRecordsInPerSecond). I also have tried to edit the Prometheus config file /etc/prometheus/prometheus.yml but I don't have too much experience on Prometheus and there is something wrong in my configuration. I was basing my solution on this post.

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:9090']
  - job_name: 'node_exporter'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:9100']   
  - job_name: 'flink'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:9250', 'localhost:9251', '192.168.56.20:9250']
    metrics_path: /
# HOW TO ADD THE OPERATOR NAME ON THE METRIC NAME?
    metric_relabel_configs:
      - source_labels: [__name__]
      regex: '(flink_taskmanager_job_task_operator)_(\w+)'
      replacement: '${2}'
      target_label: pool
      - source_labels: [__name__]
      regex: '(flink_taskmanager_job_task_operator)_(\w+)'
      replacement: '${1}_bytes'
      target_label: __name__
1

2 Answers 2

12

First of all, for more complex graphing you should definitely investigate Grafana. The built-in Prometheus graphs are useful eg. for debugging, but definitely more limited. In particular one graph will only display the results of one query.

Now for a hack that I definitely do not recommend:

flink_taskmanager_job_task_operator_numRecordsInPerSecond{operator_name="Map"}
or
label_replace(flink_taskmanager_job_task_operator_numRecordsOutPerSecond{operator_name="Map"}, "distinct", "foo", "job", ".*")

Since, as documented

vector1 or vector2 results in a vector that contains all original elements (label sets + values) of vector1 and additionally all elements of vector2 which do not have matching label sets in vector1.

you can add a new label that is not present in the labels on the first vector to the second vector and thus keep all elements from both.

4
  • thanks @Michal. I am also using Grafana. However, why do you mean more complex graphing? Is it easier to do in Grafana? How should I solve it using Grafana?
    – Felipe
    Apr 3, 2019 at 10:53
  • 3
    @Felipe I mean in Grafana you could simply add several queries to one panel. Apr 3, 2019 at 11:14
  • @Felipe: can you please change the accepted answer to the one provided by the VictoriaMetrics lead dev? Apr 6, 2022 at 16:24
  • Both answers solve my problem. The second was a bit late. But indeed it shows 2 ways to do the same. I personally prefer to use only prometheus and not install another library "VictoriaMetrics". However I have saw a few answers that it simplies some complex prometheus queries. I will consider that VictoriaMetrics might pays off to have another library on top of prometheus. Thank you for letting me know :+1:
    – Felipe
    Apr 7, 2022 at 6:19
10

It is possible to select multiple metric names with a single PromQL query by using a regular expression filter on __name__ label:

{__name__=~"flink_taskmanager_job_task_operator_numRecords(In|Out)PerSecond",operator_name="Map"}

See docs about the __name__ label here.

There is another solution when using Prometheus-compatible query engine such as MetricsQL by using union function:

union(
 
 flink_taskmanager_job_task_operator_numRecordsInPerSecond{operator_name="Map"},
 
 flink_taskmanager_job_task_operator_numRecordsOutPerSecond{operator_name="Map"}
)

Note that selecting multiple time series via __name__ regexp can result in vector cannot contain metrics with the same labelset error if the selected series are wrapped in any PromQL function. For example:

max_over_time(
 
 {__name__=~"flink_taskmanager_job_task_operator_numRecords(In|Out)PerSecond",operator_name="Map"}[5m]
)

This is because Prometheus removes metric names from input series when applying PromQL functions. MetricsQL from VictoriaMetrics provides a solution for this issue - keep_metric_names modifier (see these docs for details):

max_over_time(
 
 {__name__=~"flink_taskmanager_job_task_operator_numRecords(In|Out)PerSecond",operator_name="Map"}[5m]
)
keep_metric_names

P.S. I work on VictoriaMetrics and MetricsQL.

5
  • 1
    When designing a new schema, is there a performance or storage (dis)advantage in separating series in different metrics like that, vs. having one series ..._numRecordsPerSecond with a label operation=In|Out? Why move the label values into the metric name if the label can only have two values? Apr 6, 2022 at 16:35
  • 1
    It is recommended to allocate a separate metric name per each logical thing (for example, network bandwidth, memory usage, requests count, temperature, etc.). This will simplify further querying, grouping and filtering for this data. See prometheus.io/docs/practices/naming for more details.
    – valyala
    Apr 6, 2022 at 17:31
  • What about very closely related things, for example the price and volume of one stock trade that occurred at a specific time? InfluxDB allows multiple values per point, but Prometheus only one. In the client I want something like AAPL{price=100, volume=5} time conceptually. I could create 2 metrics stock_price and stock_volume, or 1 metric with a label having values price and volume, but either way, when querying, these two series would need to be joined on the client using the timestamp as a key, which seems wasteful and much more complex than InfluxDB. Is there a better way? Apr 8, 2022 at 9:18
  • It is OK to have stock_price and stock_volume metrics. PromQL allows performing arithmetic calculations on metrics with different names. For example, stock_volume / stock_price would return the number of shares.
    – valyala
    Apr 8, 2022 at 10:01
  • See this article for details on PromQL capabilities
    – valyala
    Apr 8, 2022 at 10:19

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.