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 3f564e46 [horus] goroutine prometheus sdk metrics interface (#324)
3f564e46 is described below

commit 3f564e466873d1c57785923c7e2117fa15869061
Author: mfordjody <[email protected]>
AuthorDate: Sat Sep 7 17:38:39 2024 +0800

    [horus] goroutine prometheus sdk metrics interface (#324)
---
 app/horus/basic/group/group.go | 54 ------------------------------------
 app/horus/cmd/main.go          | 63 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 62 insertions(+), 55 deletions(-)

diff --git a/app/horus/basic/group/group.go b/app/horus/basic/group/group.go
deleted file mode 100644
index 1a66db71..00000000
--- a/app/horus/basic/group/group.go
+++ /dev/null
@@ -1,54 +0,0 @@
-// 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 group
-
-import (
-       "os"
-       "os/signal"
-       "sync"
-       "syscall"
-)
-
-type WaitGroup struct {
-       wg sync.WaitGroup
-}
-
-func (g *WaitGroup) Add(f func() error) {
-       g.wg.Add(1)
-       go func() {
-               defer g.wg.Done()
-               err := f()
-               if err != nil {
-                       return
-               }
-       }()
-}
-
-func (g *WaitGroup) Wait() {
-       g.wg.Wait()
-}
-
-func (g *WaitGroup) SetupStopChanWithContext() (*WaitGroup, chan struct{}) {
-       stopChan := make(chan struct{})
-       SignalChan := make(chan os.Signal, 1)
-       signal.Notify(SignalChan, syscall.SIGTERM, syscall.SIGQUIT)
-       g.Add(func() error {
-               <-stopChan
-               close(stopChan)
-               return nil
-       })
-       return g, stopChan
-}
diff --git a/app/horus/cmd/main.go b/app/horus/cmd/main.go
index ef1bb5c8..9300facd 100644
--- a/app/horus/cmd/main.go
+++ b/app/horus/cmd/main.go
@@ -16,10 +16,16 @@
 package main
 
 import (
+       "context"
        "flag"
        "github.com/apache/dubbo-kubernetes/app/horus/basic/config"
        "github.com/apache/dubbo-kubernetes/app/horus/core/db"
-       "k8s.io/klog/v2"
+       "k8s.io/klog"
+       "net/http"
+       "os"
+       "os/signal"
+       "sync"
+       "syscall"
 )
 
 var (
@@ -48,5 +54,60 @@ func main() {
        } else {
                klog.Infof("horus db initial success.")
        }
+       group, stopChan := setupStopChanWithContext()
+       ctx, cancel := context.WithCancel(context.Background())
+       group.Add(func() error {
+               for {
+                       select {
+                       case <-stopChan:
+                               cancel()
+                               return nil
+                       case <-ctx.Done():
+                               cancel()
+                               return nil
+                       }
+               }
+       })
+       group.Add(func() error {
+               http.Handle("/metrics", promhttp.Handler())
+               srv := http.Server{Addr: address}
+               err := srv.ListenAndServe()
+               if err != nil {
+                       klog.Errorf("horus metrics err:%v", err)
+               }
+               return nil
+       })
+       group.Wait()
+}
+
+type WaitGroup struct {
+       wg sync.WaitGroup
+}
+
+func (g *WaitGroup) Add(f func() error) {
+       g.wg.Add(1)
+       go func() {
+               defer g.wg.Done()
+               err := f()
+               if err != nil {
+                       return
+               }
+       }()
+}
+
+func (g *WaitGroup) Wait() {
+       g.wg.Wait()
+}
 
+func setupStopChanWithContext() (*WaitGroup, <-chan struct{}) {
+       stopChan := make(chan struct{})
+       SignalChan := make(chan os.Signal, 1)
+       signal.Notify(SignalChan, syscall.SIGTERM, syscall.SIGQUIT)
+       g := WaitGroup{}
+       g.Add(func() error {
+               <-stopChan
+               close(stopChan)
+               return nil
+       })
+       return &g, stopChan
 }

Reply via email to