| Felipe Vicens | f96bb45 | 2020-06-22 08:12:30 +0200 | [diff] [blame] | 1 | # 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 *** |
| garciadeblas | f4ebaa8 | 2022-06-23 13:33:26 +0200 | [diff] [blame] | 14 | Documentation Library to obtain metrics from Prometheus. |
| Felipe Vicens | f96bb45 | 2020-06-22 08:12:30 +0200 | [diff] [blame] | 15 | |
| 16 | Library String |
| 17 | Library Collections |
| garciadeblas | a79b7b5 | 2023-10-05 11:46:45 +0200 | [diff] [blame^] | 18 | Library OperatingSystem |
| Felipe Vicens | f96bb45 | 2020-06-22 08:12:30 +0200 | [diff] [blame] | 19 | Library RequestsLibrary |
| 20 | |
| 21 | |
| 22 | *** Variables *** |
| garciadeblas | f4ebaa8 | 2022-06-23 13:33:26 +0200 | [diff] [blame] | 23 | ${timeout} 30 |
| 24 | ${max_retries} 1 |
| garciadeblas | a79b7b5 | 2023-10-05 11:46:45 +0200 | [diff] [blame^] | 25 | ${prometheus_host} %{PROMETHEUS_HOSTNAME=UNKNOWN} |
| 26 | ${prometheus_port} %{PROMETHEUS_PORT=9090} |
| 27 | ${prometheus_user} %{PROMETHEUS_USER=""} |
| 28 | ${prometheus_password} %{PROMETHEUS_PASSWORD=""} |
| Felipe Vicens | f96bb45 | 2020-06-22 08:12:30 +0200 | [diff] [blame] | 29 | |
| 30 | |
| 31 | *** Keywords *** |
| garciadeblas | 2d4de33 | 2023-06-28 00:23:34 +0200 | [diff] [blame] | 32 | Set Testsuite Prometheus Variables |
| 33 | [Documentation] Set Testsuite Prometheus Variables to be used in subsequent test cases |
| 34 | |
| garciadeblas | a79b7b5 | 2023-10-05 11:46:45 +0200 | [diff] [blame^] | 35 | IF '${prometheus_host}' == 'UNKNOWN' |
| 36 | ${local_prometheus_host}= Get Environment Variable OSM_HOSTNAME |
| 37 | Set Suite Variable ${prometheus_host} ${local_prometheus_host} |
| 38 | Set Suite Variable ${prometheus_port} 9091 |
| 39 | END |
| garciadeblas | 2d4de33 | 2023-06-28 00:23:34 +0200 | [diff] [blame] | 40 | Log ${prometheus_port} |
| garciadeblas | 2d4de33 | 2023-06-28 00:23:34 +0200 | [diff] [blame] | 41 | Log ${prometheus_host} |
| 42 | Log ${prometheus_user} |
| 43 | Log ${prometheus_password} |
| garciadeblas | 2d4de33 | 2023-06-28 00:23:34 +0200 | [diff] [blame] | 44 | |
| 45 | |
| Felipe Vicens | f96bb45 | 2020-06-22 08:12:30 +0200 | [diff] [blame] | 46 | Get Metric |
| garciadeblas | f4ebaa8 | 2022-06-23 13:33:26 +0200 | [diff] [blame] | 47 | [Documentation] Get the instant value of a metric from Prometheus using multiple filter parameters. |
| Felipe Vicens | f96bb45 | 2020-06-22 08:12:30 +0200 | [diff] [blame] | 48 | ... The filter parameters are given to this function in key=value format (one argument per key/value pair). |
| 49 | ... Fails if the metric is not found or has multiple values. |
| 50 | ... Examples of execution: |
| Guillermo Calvino | 063677c | 2022-11-21 18:13:58 +0100 | [diff] [blame] | 51 | ... \${metric}= Get Metric \${prometheus_ip} \${prometheus_port} ${prometheus_password} \${metric} |
| 52 | ... \${metric}= Get Metric \${prometheus_ip} \${prometheus_port} ${prometheus_password} \${metric} \${param1}=\${value1} \${param2}=\${value2} |
| Felipe Vicens | f96bb45 | 2020-06-22 08:12:30 +0200 | [diff] [blame] | 53 | |
| Guillermo Calvino | 063677c | 2022-11-21 18:13:58 +0100 | [diff] [blame] | 54 | [Arguments] ${prometheus_ip} ${prometheus_port} ${prometheus_user} ${prometheus_password} ${metric} @{filter_parameters} |
| Felipe Vicens | f96bb45 | 2020-06-22 08:12:30 +0200 | [diff] [blame] | 55 | |
| garciadeblas | f4ebaa8 | 2022-06-23 13:33:26 +0200 | [diff] [blame] | 56 | ${filter}= Set Variable ${EMPTY} |
| 57 | FOR ${param} IN @{filter_parameters} |
| 58 | ${match} ${param_name} ${param_value} = Should Match Regexp ${param} (.+)=(.+) msg=Syntax error in filter parameters |
| 59 | ${filter}= Catenate SEPARATOR= ${filter} ${param_name}="${param_value}", |
| Felipe Vicens | f96bb45 | 2020-06-22 08:12:30 +0200 | [diff] [blame] | 60 | END |
| Guillermo Calvino | 063677c | 2022-11-21 18:13:58 +0100 | [diff] [blame] | 61 | ${resp}= Execute Prometheus Instant Query ${prometheus_host} ${prometheus_port} ${prometheus_user} ${prometheus_password} query=${metric}{${filter}} |
| garciadeblas | f4ebaa8 | 2022-06-23 13:33:26 +0200 | [diff] [blame] | 62 | ${result_list}= Convert To List ${resp["data"]["result"]} |
| 63 | ${results}= Get Length ${result_list} |
| 64 | Should Not Be Equal As Numbers 0 ${results} msg=Metric ${metric} not found values=false |
| 65 | Should Be Equal As Integers 1 ${results} msg=Metric ${metric} with multiple values values=false |
| 66 | [Return] ${result_list[0]["value"][1]} |
| Felipe Vicens | f96bb45 | 2020-06-22 08:12:30 +0200 | [diff] [blame] | 67 | |
| 68 | |
| 69 | Execute Prometheus Instant Query |
| garciadeblas | f4ebaa8 | 2022-06-23 13:33:26 +0200 | [diff] [blame] | 70 | [Documentation] Execute a Prometheus Instant Query using HTTP API. |
| Felipe Vicens | f96bb45 | 2020-06-22 08:12:30 +0200 | [diff] [blame] | 71 | ... Return an inline json with the result of the query. |
| 72 | ... The requested URL is the next: http://\${prometheus_ip}:\${prometheus_port}/api/v1/query?\${querystring} |
| 73 | |
| Guillermo Calvino | 063677c | 2022-11-21 18:13:58 +0100 | [diff] [blame] | 74 | [Arguments] ${prometheus_ip} ${prometheus_port} ${prometheus_user} ${prometheus_password} ${querystring} |
| 75 | ${auth}= IF '${prometheus_password}'!='${EMPTY}' Create List ${prometheus_user} ${prometheus_password} ELSE Set Variable None |
| 76 | Create Session prometheus http://${prometheus_ip}:${prometheus_port} timeout=${timeout} max_retries=${max_retries} verify=false auth=${auth} |
| garciadeblas | f4ebaa8 | 2022-06-23 13:33:26 +0200 | [diff] [blame] | 77 | ${resp}= GET On Session prometheus /api/v1/query?${querystring} timeout=${timeout} |
| 78 | Status Should Be 200 ${resp} |
| 79 | [Return] ${resp.json()} |