This is an automated email from the ASF dual-hosted git repository.

zhongxjian pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-kubernetes.git


The following commit(s) were added to refs/heads/master by this push:
     new 72be4bc2 [horus] Add horus monitoring query (#333)
72be4bc2 is described below

commit 72be4bc2c75d3a43bc54b0e0103bf041fce6599b
Author: mfordjody <[email protected]>
AuthorDate: Wed Sep 11 09:14:50 2024 +0800

    [horus] Add horus monitoring query (#333)
---
 app/horus/core/horuser/prome.go | 50 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)

diff --git a/app/horus/core/horuser/prome.go b/app/horus/core/horuser/prome.go
new file mode 100644
index 00000000..d83df40d
--- /dev/null
+++ b/app/horus/core/horuser/prome.go
@@ -0,0 +1,50 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You 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.
+
+package horuser
+
+import (
+       "context"
+       apiV1 "github.com/prometheus/client_golang/api"
+       prometheusV1 "github.com/prometheus/client_golang/api/prometheus/v1"
+       "github.com/prometheus/common/model"
+       "k8s.io/klog/v2"
+       "time"
+)
+
+func (h *Horuser) InstantQuery(address, ql string, timeWindowsSecond int64) 
(model.Vector, error) {
+       client, err := apiV1.NewClient(apiV1.Config{Address: address})
+       if err != nil {
+               klog.Errorf("prometheus InstantQuery creating NewClient 
error:%v", err)
+               return nil, err
+       }
+
+       apiV1 := prometheusV1.NewAPI(client)
+       ctx, cancel := context.WithTimeout(context.Background(), 
time.Duration(timeWindowsSecond)*time.Second)
+       defer cancel()
+
+       result, _, err := apiV1.Query(ctx, ql, time.Now())
+       if err != nil {
+               klog.Errorf("prometheus Query error:%v url:%+v ql:%v", err, 
address, ql)
+               return nil, err
+       }
+
+       vector, has := result.(model.Vector)
+       if !has {
+               klog.Errorf("prometheus Query result vector error %+v", result)
+               return nil, err
+       }
+       return vector, nil
+}

Reply via email to