blob: 4ad3028fc823480794a26ca5711f719ce9dbc483 [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
garciadeblasd5691062024-09-15 22:35:20 +020037 IF '${PROMETHEUS_HOST}' == 'UNKNOWN'
garciadeblasa79b7b52023-10-05 11:46:45 +020038 ${local_prometheus_host}= Get Environment Variable OSM_HOSTNAME
garciadeblasd5691062024-09-15 22:35:20 +020039 IF '${local_prometheus_host}'.startswith('nbi.')
40 ${local_prometheus_host}= Replace String ${local_prometheus_host} nbi. prometheus.
41 END
garciadeblas7a9e0312023-12-11 22:24:46 +010042 Set Suite Variable ${PROMETHEUS_HOST} ${local_prometheus_host}
garciadeblasd5691062024-09-15 22:35:20 +020043 Set Suite Variable ${PROMETHEUS_PORT} 80
garciadeblasa79b7b52023-10-05 11:46:45 +020044 END
garciadeblasd5691062024-09-15 22:35:20 +020045 Set Suite Variable ${PROMETHEUS_URL} http://${PROMETHEUS_HOST}:${PROMETHEUS_PORT}
garciadeblas7a9e0312023-12-11 22:24:46 +010046 Log ${PROMETHEUS_PORT}
47 Log ${PROMETHEUS_HOST}
garciadeblasd5691062024-09-15 22:35:20 +020048 Log ${PROMETHEUS_URL}
garciadeblas7a9e0312023-12-11 22:24:46 +010049 Log ${PROMETHEUS_USER}
50 Log ${PROMETHEUS_PASSWORD}
garciadeblas2d4de332023-06-28 00:23:34 +020051
Felipe Vicensf96bb452020-06-22 08:12:30 +020052Get Metric
garciadeblasf4ebaa82022-06-23 13:33:26 +020053 [Documentation] Get the instant value of a metric from Prometheus using multiple filter parameters.
Felipe Vicensf96bb452020-06-22 08:12:30 +020054 ... 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:
garciadeblasd5691062024-09-15 22:35:20 +020057 ... \${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}
garciadeblasf4ebaa82022-06-23 13:33:26 +020065 ${filter}= Set Variable ${EMPTY}
66 FOR ${param} IN @{filter_parameters}
garciadeblas7a9e0312023-12-11 22:24:46 +010067 ${match} ${param_name} ${param_value}= Should Match Regexp ${param} (.+)=(.+) msg=Syntax error in filter parameters
68 Log ${match},${param_name},${param_value}
garciadeblasf4ebaa82022-06-23 13:33:26 +020069 ${filter}= Catenate SEPARATOR= ${filter} ${param_name}="${param_value}",
Felipe Vicensf96bb452020-06-22 08:12:30 +020070 END
garciadeblasd5691062024-09-15 22:35:20 +020071 ${resp}= Execute Prometheus Instant Query ${prometheus_url} ${prometheus_user} ${prometheus_password} query=${metric}{${filter}}
72 Log ${resp}
garciadeblasf4ebaa82022-06-23 13:33:26 +020073 ${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
garciadeblas7a9e0312023-12-11 22:24:46 +010077 RETURN ${result_list[0]["value"][1]}
Felipe Vicensf96bb452020-06-22 08:12:30 +020078
79Execute Prometheus Instant Query
garciadeblasf4ebaa82022-06-23 13:33:26 +020080 [Documentation] Execute a Prometheus Instant Query using HTTP API.
Felipe Vicensf96bb452020-06-22 08:12:30 +020081 ... Return an inline json with the result of the query.
garciadeblasd5691062024-09-15 22:35:20 +020082 ... 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}
garciadeblas7a9e0312023-12-11 22:24:46 +010088 ${auth}= IF '${prometheus_password}' != '${EMPTY}' Create List ${prometheus_user} ${prometheus_password} ELSE Set Variable None
garciadeblasd5691062024-09-15 22:35:20 +020089 Create Session prometheus ${prometheus_url} timeout=${TIMEOUT} max_retries=${MAX_RETRIES} verify=false auth=${auth}
garciadeblas7a9e0312023-12-11 22:24:46 +010090 ${resp}= GET On Session prometheus /api/v1/query?${querystring} timeout=${TIMEOUT}
garciadeblasf4ebaa82022-06-23 13:33:26 +020091 Status Should Be 200 ${resp}
garciadeblas7a9e0312023-12-11 22:24:46 +010092 RETURN ${resp.json()}