| garciadeblas | 7a9e031 | 2023-12-11 22:24:46 +0100 | [diff] [blame] | 1 | *** Comments *** |
| Felipe Vicens | f96bb45 | 2020-06-22 08:12:30 +0200 | [diff] [blame] | 2 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | # you may not use this file except in compliance with the License. |
| 4 | # You may obtain a copy of the License at |
| 5 | # |
| 6 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | # |
| 8 | # Unless required by applicable law or agreed to in writing, software |
| 9 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | # See the License for the specific language governing permissions and |
| 12 | # limitations under the License. |
| 13 | |
| garciadeblas | 7a9e031 | 2023-12-11 22:24:46 +0100 | [diff] [blame] | 14 | |
| Felipe Vicens | f96bb45 | 2020-06-22 08:12:30 +0200 | [diff] [blame] | 15 | *** Settings *** |
| garciadeblas | f4ebaa8 | 2022-06-23 13:33:26 +0200 | [diff] [blame] | 16 | Documentation Library to obtain metrics from Prometheus. |
| Felipe Vicens | f96bb45 | 2020-06-22 08:12:30 +0200 | [diff] [blame] | 17 | |
| 18 | Library String |
| 19 | Library Collections |
| garciadeblas | a79b7b5 | 2023-10-05 11:46:45 +0200 | [diff] [blame] | 20 | Library OperatingSystem |
| Felipe Vicens | f96bb45 | 2020-06-22 08:12:30 +0200 | [diff] [blame] | 21 | Library RequestsLibrary |
| 22 | |
| 23 | |
| 24 | *** Variables *** |
| garciadeblas | 7a9e031 | 2023-12-11 22:24:46 +0100 | [diff] [blame] | 25 | ${TIMEOUT} 30 |
| 26 | ${MAX_RETRIES} 1 |
| 27 | ${PROMETHEUS_HOST} %{PROMETHEUS_HOSTNAME=UNKNOWN} |
| 28 | ${PROMETHEUS_PORT} %{PROMETHEUS_PORT=9090} |
| 29 | ${PROMETHEUS_USER} %{PROMETHEUS_USER=""} |
| 30 | ${PROMETHEUS_PASSWORD} %{PROMETHEUS_PASSWORD=""} |
| Felipe Vicens | f96bb45 | 2020-06-22 08:12:30 +0200 | [diff] [blame] | 31 | |
| 32 | |
| 33 | *** Keywords *** |
| garciadeblas | 2d4de33 | 2023-06-28 00:23:34 +0200 | [diff] [blame] | 34 | Set Testsuite Prometheus Variables |
| 35 | [Documentation] Set Testsuite Prometheus Variables to be used in subsequent test cases |
| 36 | |
| garciadeblas | d569106 | 2024-09-15 22:35:20 +0200 | [diff] [blame] | 37 | IF '${PROMETHEUS_HOST}' == 'UNKNOWN' |
| garciadeblas | a79b7b5 | 2023-10-05 11:46:45 +0200 | [diff] [blame] | 38 | ${local_prometheus_host}= Get Environment Variable OSM_HOSTNAME |
| garciadeblas | d569106 | 2024-09-15 22:35:20 +0200 | [diff] [blame] | 39 | IF '${local_prometheus_host}'.startswith('nbi.') |
| 40 | ${local_prometheus_host}= Replace String ${local_prometheus_host} nbi. prometheus. |
| 41 | END |
| garciadeblas | 7a9e031 | 2023-12-11 22:24:46 +0100 | [diff] [blame] | 42 | Set Suite Variable ${PROMETHEUS_HOST} ${local_prometheus_host} |
| garciadeblas | d569106 | 2024-09-15 22:35:20 +0200 | [diff] [blame] | 43 | Set Suite Variable ${PROMETHEUS_PORT} 80 |
| garciadeblas | a79b7b5 | 2023-10-05 11:46:45 +0200 | [diff] [blame] | 44 | END |
| garciadeblas | d569106 | 2024-09-15 22:35:20 +0200 | [diff] [blame] | 45 | Set Suite Variable ${PROMETHEUS_URL} http://${PROMETHEUS_HOST}:${PROMETHEUS_PORT} |
| garciadeblas | 7a9e031 | 2023-12-11 22:24:46 +0100 | [diff] [blame] | 46 | Log ${PROMETHEUS_PORT} |
| 47 | Log ${PROMETHEUS_HOST} |
| garciadeblas | d569106 | 2024-09-15 22:35:20 +0200 | [diff] [blame] | 48 | Log ${PROMETHEUS_URL} |
| garciadeblas | 7a9e031 | 2023-12-11 22:24:46 +0100 | [diff] [blame] | 49 | Log ${PROMETHEUS_USER} |
| 50 | Log ${PROMETHEUS_PASSWORD} |
| garciadeblas | 2d4de33 | 2023-06-28 00:23:34 +0200 | [diff] [blame] | 51 | |
| Felipe Vicens | f96bb45 | 2020-06-22 08:12:30 +0200 | [diff] [blame] | 52 | Get Metric |
| garciadeblas | f4ebaa8 | 2022-06-23 13:33:26 +0200 | [diff] [blame] | 53 | [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] | 54 | ... The filter parameters are given to this function in key=value format (one argument per key/value pair). |
| 55 | ... Fails if the metric is not found or has multiple values. |
| 56 | ... Examples of execution: |
| garciadeblas | d569106 | 2024-09-15 22:35:20 +0200 | [diff] [blame] | 57 | ... \${metric}= Get Metric \${prometheus_url} ${prometheus_password} \${metric} |
| 58 | ... \${metric}= Get Metric \${prometheus_url} ${prometheus_password} \${metric} \${param1}=\${value1} \${param2}=\${value2} |
| 59 | [Arguments] ${prometheus_url} ${prometheus_user} ${prometheus_password} ${metric} @{filter_parameters} |
| 60 | Log ${prometheus_url} |
| 61 | Log ${prometheus_user} |
| 62 | Log ${prometheus_password} |
| 63 | Log ${metric} |
| 64 | Log ${filter_parameters} |
| garciadeblas | f4ebaa8 | 2022-06-23 13:33:26 +0200 | [diff] [blame] | 65 | ${filter}= Set Variable ${EMPTY} |
| 66 | FOR ${param} IN @{filter_parameters} |
| garciadeblas | 7a9e031 | 2023-12-11 22:24:46 +0100 | [diff] [blame] | 67 | ${match} ${param_name} ${param_value}= Should Match Regexp ${param} (.+)=(.+) msg=Syntax error in filter parameters |
| 68 | Log ${match},${param_name},${param_value} |
| garciadeblas | f4ebaa8 | 2022-06-23 13:33:26 +0200 | [diff] [blame] | 69 | ${filter}= Catenate SEPARATOR= ${filter} ${param_name}="${param_value}", |
| Felipe Vicens | f96bb45 | 2020-06-22 08:12:30 +0200 | [diff] [blame] | 70 | END |
| garciadeblas | d569106 | 2024-09-15 22:35:20 +0200 | [diff] [blame] | 71 | ${resp}= Execute Prometheus Instant Query ${prometheus_url} ${prometheus_user} ${prometheus_password} query=${metric}{${filter}} |
| 72 | Log ${resp} |
| garciadeblas | f4ebaa8 | 2022-06-23 13:33:26 +0200 | [diff] [blame] | 73 | ${result_list}= Convert To List ${resp["data"]["result"]} |
| 74 | ${results}= Get Length ${result_list} |
| 75 | Should Not Be Equal As Numbers 0 ${results} msg=Metric ${metric} not found values=false |
| 76 | Should Be Equal As Integers 1 ${results} msg=Metric ${metric} with multiple values values=false |
| garciadeblas | 7a9e031 | 2023-12-11 22:24:46 +0100 | [diff] [blame] | 77 | RETURN ${result_list[0]["value"][1]} |
| Felipe Vicens | f96bb45 | 2020-06-22 08:12:30 +0200 | [diff] [blame] | 78 | |
| 79 | Execute Prometheus Instant Query |
| garciadeblas | f4ebaa8 | 2022-06-23 13:33:26 +0200 | [diff] [blame] | 80 | [Documentation] Execute a Prometheus Instant Query using HTTP API. |
| Felipe Vicens | f96bb45 | 2020-06-22 08:12:30 +0200 | [diff] [blame] | 81 | ... Return an inline json with the result of the query. |
| garciadeblas | d569106 | 2024-09-15 22:35:20 +0200 | [diff] [blame] | 82 | ... The requested URL is the next: \${prometheus_url}/api/v1/query?\${querystring} |
| 83 | [Arguments] ${prometheus_url} ${prometheus_user} ${prometheus_password} ${querystring} |
| 84 | Log ${prometheus_url} |
| 85 | Log ${prometheus_user} |
| 86 | Log ${prometheus_password} |
| 87 | Log ${querystring} |
| garciadeblas | 7a9e031 | 2023-12-11 22:24:46 +0100 | [diff] [blame] | 88 | ${auth}= IF '${prometheus_password}' != '${EMPTY}' Create List ${prometheus_user} ${prometheus_password} ELSE Set Variable None |
| garciadeblas | d569106 | 2024-09-15 22:35:20 +0200 | [diff] [blame] | 89 | Create Session prometheus ${prometheus_url} timeout=${TIMEOUT} max_retries=${MAX_RETRIES} verify=false auth=${auth} |
| garciadeblas | 7a9e031 | 2023-12-11 22:24:46 +0100 | [diff] [blame] | 90 | ${resp}= GET On Session prometheus /api/v1/query?${querystring} timeout=${TIMEOUT} |
| garciadeblas | f4ebaa8 | 2022-06-23 13:33:26 +0200 | [diff] [blame] | 91 | Status Should Be 200 ${resp} |
| garciadeblas | 7a9e031 | 2023-12-11 22:24:46 +0100 | [diff] [blame] | 92 | RETURN ${resp.json()} |