istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/components/prometheus/prometheus.go (about) 1 // Copyright Istio Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package prometheus 16 17 import ( 18 v1 "github.com/prometheus/client_golang/api/prometheus/v1" 19 prom "github.com/prometheus/common/model" 20 21 "istio.io/istio/pkg/test" 22 "istio.io/istio/pkg/test/framework/components/cluster" 23 "istio.io/istio/pkg/test/framework/resource" 24 ) 25 26 type Instance interface { 27 resource.Resource 28 29 // API Returns the core Prometheus APIs. 30 API() v1.API 31 APIForCluster(cluster cluster.Cluster) v1.API 32 33 // Query run the provided PromQL against the given cluster 34 RawQuery(cluster cluster.Cluster, promQL string) (prom.Value, error) 35 36 // Query Run the provided query against the given cluster 37 Query(cluster cluster.Cluster, query Query) (prom.Value, error) 38 39 // QuerySum is a help around Query to compute the sum 40 QuerySum(cluster cluster.Cluster, query Query) (float64, error) 41 } 42 43 type Config struct { 44 // If true, connect to an existing prometheus rather than creating a new one 45 SkipDeploy bool 46 } 47 48 // New returns a new instance of prometheus. 49 func New(ctx resource.Context, c Config) (i Instance, err error) { 50 return newKube(ctx, c) 51 } 52 53 // NewOrFail returns a new Prometheus instance or fails test. 54 func NewOrFail(t test.Failer, ctx resource.Context, c Config) Instance { 55 t.Helper() 56 i, err := New(ctx, c) 57 if err != nil { 58 t.Fatalf("prometheus.NewOrFail: %v", err) 59 } 60 61 return i 62 }