blob: 5fdda496a7e38b1dd407095b4b847c000afc9baf [file] [log] [blame]
Felipe Vicensf96bb452020-06-22 08:12:30 +02001# Licensed under the Apache License, Version 2.0 (the "License");
2# you may not use this file except in compliance with the License.
3# You may obtain a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS,
9# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10# See the License for the specific language governing permissions and
11# limitations under the License.
12
13*** Settings ***
garciadeblasf4ebaa82022-06-23 13:33:26 +020014Documentation Library to obtain metrics from Prometheus.
Felipe Vicensf96bb452020-06-22 08:12:30 +020015
16Library String
17Library Collections
18Library RequestsLibrary
19
20
21*** Variables ***
garciadeblasf4ebaa82022-06-23 13:33:26 +020022${timeout} 30
23${max_retries} 1
Felipe Vicensf96bb452020-06-22 08:12:30 +020024
25
26*** Keywords ***
27Get Metric
garciadeblasf4ebaa82022-06-23 13:33:26 +020028 [Documentation] Get the instant value of a metric from Prometheus using multiple filter parameters.
Felipe Vicensf96bb452020-06-22 08:12:30 +020029 ... The filter parameters are given to this function in key=value format (one argument per key/value pair).
30 ... Fails if the metric is not found or has multiple values.
31 ... Examples of execution:
Guillermo Calvino063677c2022-11-21 18:13:58 +010032 ... \${metric}= Get Metric \${prometheus_ip} \${prometheus_port} ${prometheus_password} \${metric}
33 ... \${metric}= Get Metric \${prometheus_ip} \${prometheus_port} ${prometheus_password} \${metric} \${param1}=\${value1} \${param2}=\${value2}
Felipe Vicensf96bb452020-06-22 08:12:30 +020034
Guillermo Calvino063677c2022-11-21 18:13:58 +010035 [Arguments] ${prometheus_ip} ${prometheus_port} ${prometheus_user} ${prometheus_password} ${metric} @{filter_parameters}
Felipe Vicensf96bb452020-06-22 08:12:30 +020036
garciadeblasf4ebaa82022-06-23 13:33:26 +020037 ${filter}= Set Variable ${EMPTY}
38 FOR ${param} IN @{filter_parameters}
39 ${match} ${param_name} ${param_value} = Should Match Regexp ${param} (.+)=(.+) msg=Syntax error in filter parameters
40 ${filter}= Catenate SEPARATOR= ${filter} ${param_name}="${param_value}",
Felipe Vicensf96bb452020-06-22 08:12:30 +020041 END
Guillermo Calvino063677c2022-11-21 18:13:58 +010042 ${resp}= Execute Prometheus Instant Query ${prometheus_host} ${prometheus_port} ${prometheus_user} ${prometheus_password} query=${metric}{${filter}}
garciadeblasf4ebaa82022-06-23 13:33:26 +020043 ${result_list}= Convert To List ${resp["data"]["result"]}
44 ${results}= Get Length ${result_list}
45 Should Not Be Equal As Numbers 0 ${results} msg=Metric ${metric} not found values=false
46 Should Be Equal As Integers 1 ${results} msg=Metric ${metric} with multiple values values=false
47 [Return] ${result_list[0]["value"][1]}
Felipe Vicensf96bb452020-06-22 08:12:30 +020048
49
50Execute Prometheus Instant Query
garciadeblasf4ebaa82022-06-23 13:33:26 +020051 [Documentation] Execute a Prometheus Instant Query using HTTP API.
Felipe Vicensf96bb452020-06-22 08:12:30 +020052 ... Return an inline json with the result of the query.
53 ... The requested URL is the next: http://\${prometheus_ip}:\${prometheus_port}/api/v1/query?\${querystring}
54
Guillermo Calvino063677c2022-11-21 18:13:58 +010055 [Arguments] ${prometheus_ip} ${prometheus_port} ${prometheus_user} ${prometheus_password} ${querystring}
56 ${auth}= IF '${prometheus_password}'!='${EMPTY}' Create List ${prometheus_user} ${prometheus_password} ELSE Set Variable None
57 Create Session prometheus http://${prometheus_ip}:${prometheus_port} timeout=${timeout} max_retries=${max_retries} verify=false auth=${auth}
garciadeblasf4ebaa82022-06-23 13:33:26 +020058 ${resp}= GET On Session prometheus /api/v1/query?${querystring} timeout=${timeout}
59 Status Should Be 200 ${resp}
60 [Return] ${resp.json()}