X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2Ftests.git;a=blobdiff_plain;f=robot-systest%2Flib%2Fprometheus_lib.robot;fp=robot-systest%2Flib%2Fprometheus_lib.robot;h=0f4f07c0a8ff14bf5754b2091bff1f84eee8f8f7;hp=0000000000000000000000000000000000000000;hb=23ff8f980f66ef57fb4d254336a018e3a697a187;hpb=5e001f506b744021d5ef25999c9da28cf56d8fbc diff --git a/robot-systest/lib/prometheus_lib.robot b/robot-systest/lib/prometheus_lib.robot new file mode 100644 index 0000000..0f4f07c --- /dev/null +++ b/robot-systest/lib/prometheus_lib.robot @@ -0,0 +1,79 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +*** Settings *** +Documentation Library to obtain metrics from Prometheus. + +Library String +Library Collections +Library OperatingSystem +Library RequestsLibrary + + +*** Variables *** +${timeout} 30 +${max_retries} 1 +${prometheus_host} %{PROMETHEUS_HOSTNAME=UNKNOWN} +${prometheus_port} %{PROMETHEUS_PORT=9090} +${prometheus_user} %{PROMETHEUS_USER=""} +${prometheus_password} %{PROMETHEUS_PASSWORD=""} + + +*** Keywords *** +Set Testsuite Prometheus Variables + [Documentation] Set Testsuite Prometheus Variables to be used in subsequent test cases + + IF '${prometheus_host}' == 'UNKNOWN' + ${local_prometheus_host}= Get Environment Variable OSM_HOSTNAME + Set Suite Variable ${prometheus_host} ${local_prometheus_host} + Set Suite Variable ${prometheus_port} 9091 + END + Log ${prometheus_port} + Log ${prometheus_host} + Log ${prometheus_user} + Log ${prometheus_password} + + +Get Metric + [Documentation] Get the instant value of a metric from Prometheus using multiple filter parameters. + ... The filter parameters are given to this function in key=value format (one argument per key/value pair). + ... Fails if the metric is not found or has multiple values. + ... Examples of execution: + ... \${metric}= Get Metric \${prometheus_ip} \${prometheus_port} ${prometheus_password} \${metric} + ... \${metric}= Get Metric \${prometheus_ip} \${prometheus_port} ${prometheus_password} \${metric} \${param1}=\${value1} \${param2}=\${value2} + + [Arguments] ${prometheus_ip} ${prometheus_port} ${prometheus_user} ${prometheus_password} ${metric} @{filter_parameters} + + ${filter}= Set Variable ${EMPTY} + FOR ${param} IN @{filter_parameters} + ${match} ${param_name} ${param_value} = Should Match Regexp ${param} (.+)=(.+) msg=Syntax error in filter parameters + ${filter}= Catenate SEPARATOR= ${filter} ${param_name}="${param_value}", + END + ${resp}= Execute Prometheus Instant Query ${prometheus_host} ${prometheus_port} ${prometheus_user} ${prometheus_password} query=${metric}{${filter}} + ${result_list}= Convert To List ${resp["data"]["result"]} + ${results}= Get Length ${result_list} + Should Not Be Equal As Numbers 0 ${results} msg=Metric ${metric} not found values=false + Should Be Equal As Integers 1 ${results} msg=Metric ${metric} with multiple values values=false + [Return] ${result_list[0]["value"][1]} + + +Execute Prometheus Instant Query + [Documentation] Execute a Prometheus Instant Query using HTTP API. + ... Return an inline json with the result of the query. + ... The requested URL is the next: http://\${prometheus_ip}:\${prometheus_port}/api/v1/query?\${querystring} + + [Arguments] ${prometheus_ip} ${prometheus_port} ${prometheus_user} ${prometheus_password} ${querystring} + ${auth}= IF '${prometheus_password}'!='${EMPTY}' Create List ${prometheus_user} ${prometheus_password} ELSE Set Variable None + Create Session prometheus http://${prometheus_ip}:${prometheus_port} timeout=${timeout} max_retries=${max_retries} verify=false auth=${auth} + ${resp}= GET On Session prometheus /api/v1/query?${querystring} timeout=${timeout} + Status Should Be 200 ${resp} + [Return] ${resp.json()}