blob: be3c120d6739e09712e3c2b4a17523ec169b964f [file] [log] [blame]
garciadeblas7a9e0312023-12-11 22:24:46 +01001*** Comments ***
Felipe Vicensf96bb452020-06-22 08:12:30 +02002# 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
garciadeblas7a9e0312023-12-11 22:24:46 +010014
Felipe Vicensf96bb452020-06-22 08:12:30 +020015*** Settings ***
garciadeblasf4ebaa82022-06-23 13:33:26 +020016Documentation Library to obtain metrics from Prometheus.
Felipe Vicensf96bb452020-06-22 08:12:30 +020017
18Library String
19Library Collections
garciadeblasa79b7b52023-10-05 11:46:45 +020020Library OperatingSystem
Felipe Vicensf96bb452020-06-22 08:12:30 +020021Library RequestsLibrary
22
23
24*** Variables ***
garciadeblas7a9e0312023-12-11 22:24:46 +010025${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 Vicensf96bb452020-06-22 08:12:30 +020031
32
33*** Keywords ***
garciadeblas2d4de332023-06-28 00:23:34 +020034Set Testsuite Prometheus Variables
35 [Documentation] Set Testsuite Prometheus Variables to be used in subsequent test cases
36
garciadeblas7a9e0312023-12-11 22:24:46 +010037 IF ${PROMETHEUS_HOST} == 'UNKNOWN'
garciadeblasa79b7b52023-10-05 11:46:45 +020038 ${local_prometheus_host}= Get Environment Variable OSM_HOSTNAME
garciadeblas7a9e0312023-12-11 22:24:46 +010039 Set Suite Variable ${PROMETHEUS_HOST} ${local_prometheus_host}
40 Set Suite Variable ${PROMETHEUS_HOST} 9091
garciadeblasa79b7b52023-10-05 11:46:45 +020041 END
garciadeblas7a9e0312023-12-11 22:24:46 +010042 Log ${PROMETHEUS_PORT}
43 Log ${PROMETHEUS_HOST}
44 Log ${PROMETHEUS_USER}
45 Log ${PROMETHEUS_PASSWORD}
garciadeblas2d4de332023-06-28 00:23:34 +020046
Felipe Vicensf96bb452020-06-22 08:12:30 +020047Get Metric
garciadeblasf4ebaa82022-06-23 13:33:26 +020048 [Documentation] Get the instant value of a metric from Prometheus using multiple filter parameters.
Felipe Vicensf96bb452020-06-22 08:12:30 +020049 ... The filter parameters are given to this function in key=value format (one argument per key/value pair).
50 ... Fails if the metric is not found or has multiple values.
51 ... Examples of execution:
Guillermo Calvino063677c2022-11-21 18:13:58 +010052 ... \${metric}= Get Metric \${prometheus_ip} \${prometheus_port} ${prometheus_password} \${metric}
53 ... \${metric}= Get Metric \${prometheus_ip} \${prometheus_port} ${prometheus_password} \${metric} \${param1}=\${value1} \${param2}=\${value2}
Guillermo Calvino063677c2022-11-21 18:13:58 +010054 [Arguments] ${prometheus_ip} ${prometheus_port} ${prometheus_user} ${prometheus_password} ${metric} @{filter_parameters}
garciadeblasf4ebaa82022-06-23 13:33:26 +020055 ${filter}= Set Variable ${EMPTY}
56 FOR ${param} IN @{filter_parameters}
garciadeblas7a9e0312023-12-11 22:24:46 +010057 ${match} ${param_name} ${param_value}= Should Match Regexp ${param} (.+)=(.+) msg=Syntax error in filter parameters
58 Log ${match},${param_name},${param_value}
garciadeblasf4ebaa82022-06-23 13:33:26 +020059 ${filter}= Catenate SEPARATOR= ${filter} ${param_name}="${param_value}",
Felipe Vicensf96bb452020-06-22 08:12:30 +020060 END
garciadeblas7a9e0312023-12-11 22:24:46 +010061 ${resp}= Execute Prometheus Instant Query ${prometheus_ip} ${prometheus_port} ${prometheus_user} ${prometheus_password} query=${metric}{${filter}}
garciadeblasf4ebaa82022-06-23 13:33:26 +020062 ${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
garciadeblas7a9e0312023-12-11 22:24:46 +010066 RETURN ${result_list[0]["value"][1]}
Felipe Vicensf96bb452020-06-22 08:12:30 +020067
68Execute Prometheus Instant Query
garciadeblasf4ebaa82022-06-23 13:33:26 +020069 [Documentation] Execute a Prometheus Instant Query using HTTP API.
Felipe Vicensf96bb452020-06-22 08:12:30 +020070 ... Return an inline json with the result of the query.
71 ... The requested URL is the next: http://\${prometheus_ip}:\${prometheus_port}/api/v1/query?\${querystring}
Guillermo Calvino063677c2022-11-21 18:13:58 +010072 [Arguments] ${prometheus_ip} ${prometheus_port} ${prometheus_user} ${prometheus_password} ${querystring}
garciadeblas7a9e0312023-12-11 22:24:46 +010073 ${auth}= IF '${prometheus_password}' != '${EMPTY}' Create List ${prometheus_user} ${prometheus_password} ELSE Set Variable None
74 Create Session prometheus http://${prometheus_ip}:${prometheus_port} timeout=${TIMEOUT} max_retries=${MAX_RETRIES} verify=false auth=${auth}
75 ${resp}= GET On Session prometheus /api/v1/query?${querystring} timeout=${TIMEOUT}
garciadeblasf4ebaa82022-06-23 13:33:26 +020076 Status Should Be 200 ${resp}
garciadeblas7a9e0312023-12-11 22:24:46 +010077 RETURN ${resp.json()}