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
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 Documentation Library to obtain metrics from Prometheus.
20 Library OperatingSystem
21 Library RequestsLibrary
27 ${PROMETHEUS_HOST} %{PROMETHEUS_HOSTNAME=UNKNOWN}
28 ${PROMETHEUS_PORT} %{PROMETHEUS_PORT=9090}
29 ${PROMETHEUS_USER} %{PROMETHEUS_USER=""}
30 ${PROMETHEUS_PASSWORD} %{PROMETHEUS_PASSWORD=""}
34 Set Testsuite Prometheus Variables
35 [Documentation] Set Testsuite Prometheus Variables to be used in subsequent test cases
37 IF ${PROMETHEUS_HOST} == 'UNKNOWN'
38 ${local_prometheus_host}= Get Environment Variable OSM_HOSTNAME
39 Set Suite Variable ${PROMETHEUS_HOST} ${local_prometheus_host}
40 Set Suite Variable ${PROMETHEUS_HOST} 9091
42 Log ${PROMETHEUS_PORT}
43 Log ${PROMETHEUS_HOST}
44 Log ${PROMETHEUS_USER}
45 Log ${PROMETHEUS_PASSWORD}
48 [Documentation] Get the instant value of a metric from Prometheus using multiple filter parameters.
49 ... 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:
52 ... \${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}
54 [Arguments] ${prometheus_ip} ${prometheus_port} ${prometheus_user} ${prometheus_password} ${metric} @{filter_parameters}
55 ${filter}= Set Variable ${EMPTY}
56 FOR ${param} IN @{filter_parameters}
57 ${match} ${param_name} ${param_value}= Should Match Regexp ${param} (.+)=(.+) msg=Syntax error in filter parameters
58 Log ${match},${param_name},${param_value}
59 ${filter}= Catenate SEPARATOR= ${filter} ${param_name}="${param_value}",
61 ${resp}= Execute Prometheus Instant Query ${prometheus_ip} ${prometheus_port} ${prometheus_user} ${prometheus_password} query=${metric}{${filter}}
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]}
68 Execute Prometheus Instant Query
69 [Documentation] Execute a Prometheus Instant Query using HTTP API.
70 ... 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}
72 [Arguments] ${prometheus_ip} ${prometheus_port} ${prometheus_user} ${prometheus_password} ${querystring}
73 ${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}
76 Status Should Be 200 ${resp}