| 1 | /* |
| 2 | Copyright 2023 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package frameworkscip-go gomod sigs.k8s.io/apiserver-network-proxy . `sigs.k8s.io/apiserver-network-proxy/tests/framework`/ |
| 18 | |
| 19 | import ( |
| 20 | "fmtscip-go gomod github.com/golang/go/src go1.23.6 fmt/" |
| 21 | "net/httpscip-go gomod github.com/golang/go/src go1.23.6 `net/http`/" |
| 22 | |
| 23 | dtoscip-go gomod github.com/prometheus/client_model v0.6.1 `github.com/prometheus/client_model/go`/ "github.com/prometheus/client_model/goscip-go gomod github.com/prometheus/client_model v0.6.1 `github.com/prometheus/client_model/go`/" |
| 24 | "github.com/prometheus/common/expfmtscip-go gomod github.com/prometheus/common v0.63.0 `github.com/prometheus/common/expfmt`/" |
| 25 | ) |
| 26 | |
| 27 | func readIntGaugescip-go gomod sigs.k8s.io/apiserver-network-proxy . `sigs.k8s.io/apiserver-network-proxy/tests/framework`/readIntGauge().(metricsURL, metricName string) (int, error) { |
| 28 | metrics, err := scrapeMetricsscip-go gomod sigs.k8s.io/apiserver-network-proxy . `sigs.k8s.io/apiserver-network-proxy/tests/framework`/scrapeMetrics().(metricsURL) |
| 29 | if err != nil { |
| 30 | return 0, err |
| 31 | } |
| 32 | |
| 33 | metric, found := metrics[metricName] |
| 34 | if !found { |
| 35 | return 0, fmtscip-go gomod github.com/golang/go/src go1.23.6 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.23.6 fmt/Errorf().("missing %s metric", metricName) |
| 36 | } |
| 37 | |
| 38 | vals := metric.GetMetricscip-go gomod github.com/prometheus/client_model v0.6.1 `github.com/prometheus/client_model/go`/MetricFamily#GetMetric().() |
| 39 | if len(vals) == 0 { |
| 40 | return 0, fmtscip-go gomod github.com/golang/go/src go1.23.6 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.23.6 fmt/Errorf().("missing value for %s metric", metricName) |
| 41 | } |
| 42 | |
| 43 | return int(vals[0].GetGaugescip-go gomod github.com/prometheus/client_model v0.6.1 `github.com/prometheus/client_model/go`/Metric#GetGauge().().GetValuescip-go gomod github.com/prometheus/client_model v0.6.1 `github.com/prometheus/client_model/go`/Gauge#GetValue().()), nil |
| 44 | } |
| 45 | |
| 46 | func scrapeMetricsscip-go gomod sigs.k8s.io/apiserver-network-proxy . `sigs.k8s.io/apiserver-network-proxy/tests/framework`/scrapeMetrics().(metricsURL string) (map[string]*dtoscip-go gomod github.com/prometheus/client_model v0.6.1 `github.com/prometheus/client_model/go`/.MetricFamilyscip-go gomod github.com/prometheus/client_model v0.6.1 `github.com/prometheus/client_model/go`/MetricFamily#, error) { |
| 47 | resp, err := httpscip-go gomod github.com/golang/go/src go1.23.6 `net/http`/.Getscip-go gomod github.com/golang/go/src go1.23.6 `net/http`/Get().(metricsURL) |
| 48 | if err != nil { |
| 49 | return nil, fmtscip-go gomod github.com/golang/go/src go1.23.6 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.23.6 fmt/Errorf().("failed to scrape metrics from %s: %w", metricsURL, err) |
| 50 | } |
| 51 | defer resp.Bodyscip-go gomod github.com/golang/go/src go1.23.6 `net/http`/Response#Body..Closescip-go gomod github.com/golang/go/src go1.23.6 io/Closer#Close().() |
| 52 | |
| 53 | if resp.StatusCodescip-go gomod github.com/golang/go/src go1.23.6 `net/http`/Response#StatusCode. != httpscip-go gomod github.com/golang/go/src go1.23.6 `net/http`/.StatusOKscip-go gomod github.com/golang/go/src go1.23.6 `net/http`/StatusOK. { |
| 54 | return nil, fmtscip-go gomod github.com/golang/go/src go1.23.6 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.23.6 fmt/Errorf().("unexpected %q status scraping metrics from %s", resp.Statusscip-go gomod github.com/golang/go/src go1.23.6 `net/http`/Response#Status., metricsURL) |
| 55 | } |
| 56 | |
| 57 | var parser expfmtscip-go gomod github.com/prometheus/common v0.63.0 `github.com/prometheus/common/expfmt`/.TextParserscip-go gomod github.com/prometheus/common v0.63.0 `github.com/prometheus/common/expfmt`/TextParser# |
| 58 | mf, err := parser.TextToMetricFamiliesscip-go gomod github.com/prometheus/common v0.63.0 `github.com/prometheus/common/expfmt`/TextParser#TextToMetricFamilies().(resp.Bodyscip-go gomod github.com/golang/go/src go1.23.6 `net/http`/Response#Body.) |
| 59 | if err != nil { |
| 60 | return nil, fmtscip-go gomod github.com/golang/go/src go1.23.6 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.23.6 fmt/Errorf().("failed to parse metrics: %w", err) |
| 61 | } |
| 62 | return mf, nil |
| 63 | } |