From 8fd8b8d5fffb319b31eac745ff1d93c941bd9723 Mon Sep 17 00:00:00 2001 From: Juha Hyttinen Date: Tue, 24 May 2022 12:38:59 +0300 Subject: [PATCH] INTERNAL: Symptom improvements - Symptom uptime - GetIpAddress set to public - GetLocalMetrics will use directly prometheus api Signed-off-by: Juha Hyttinen Change-Id: I8fa22ff718f59fbed9b8e40886d9f43a584650b8 --- go.mod | 1 + pkg/xapp/restapi.go | 68 +++++++++++++++++++++++++++++++++++++---------------- pkg/xapp/xapp.go | 31 ++++++++++++++++-------- 3 files changed, 70 insertions(+), 30 deletions(-) diff --git a/go.mod b/go.mod index 1156053..0e018c7 100644 --- a/go.mod +++ b/go.mod @@ -21,6 +21,7 @@ require ( github.com/gorilla/mux v1.7.1 github.com/jessevdk/go-flags v1.4.0 github.com/prometheus/client_golang v0.9.3 + github.com/prometheus/common v0.4.0 github.com/spf13/viper v1.4.0 github.com/stretchr/testify v1.5.1 golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7 diff --git a/pkg/xapp/restapi.go b/pkg/xapp/restapi.go index 00f8aba..12101a8 100755 --- a/pkg/xapp/restapi.go +++ b/pkg/xapp/restapi.go @@ -20,6 +20,7 @@ package xapp import ( + "bytes" "encoding/json" "fmt" "io/ioutil" @@ -27,11 +28,13 @@ import ( "os" "path" "strings" + "time" + "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models" "github.com/gorilla/mux" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/common/expfmt" "github.com/spf13/viper" - - "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models" ) const ( @@ -136,6 +139,24 @@ func (r *Router) CollectDefaultSymptomData(fileName string, data interface{}) st return "" } + // + // Collect some general information into one file + // + var lines []string + + // uptime + d := XappUpTime() + h := d / time.Hour + d -= h * time.Hour + m := d / time.Minute + d -= m * time.Minute + s := d / time.Second + lines = append(lines, fmt.Sprintf("uptime: %02d:%02d:%02d", h, m, s)) + + Util.WriteToFile(baseDir+"information.txt", strings.Join(lines, "\n")) + + // + // Collect metrics // if metrics, err := r.GetLocalMetrics(GetPortData("http").Port); err == nil { if err := Util.WriteToFile(baseDir+"metrics.json", metrics); err != nil { @@ -144,12 +165,7 @@ func (r *Router) CollectDefaultSymptomData(fileName string, data interface{}) st } // - if data != nil { - if b, err := json.MarshalIndent(data, "", " "); err == nil { - Util.WriteToFile(baseDir+fileName, string(b)) - } - } - + // Collect currently used config file // cfile := viper.ConfigFileUsed() input, err := ioutil.ReadFile(cfile) @@ -160,8 +176,12 @@ func (r *Router) CollectDefaultSymptomData(fileName string, data interface{}) st } // - Util.WriteToFile(baseDir+"environment", strings.Join(os.Environ(), "\n")) + // Collect environment + // + Util.WriteToFile(baseDir+"environment.txt", strings.Join(os.Environ(), "\n")) + // + // Collect RMR rt table // rtPath := os.Getenv("RMR_STASH_RT") if rtPath != "" { @@ -173,6 +193,15 @@ func (r *Router) CollectDefaultSymptomData(fileName string, data interface{}) st } } + // + // Put data that was provided as argument + // + if data != nil { + if b, err := json.MarshalIndent(data, "", " "); err == nil { + Util.WriteToFile(baseDir+fileName, string(b)) + } + } + return baseDir } @@ -207,20 +236,19 @@ func (r *Router) SendSymptomDataError(w http.ResponseWriter, req *http.Request, } func (r *Router) GetLocalMetrics(port int) (string, error) { - resp, err := http.Get(fmt.Sprintf("http://localhost:%d/ric/v1/metrics", port)) + buf := &bytes.Buffer{} + enc := expfmt.NewEncoder(buf, expfmt.FmtText) + vals, err := prometheus.DefaultGatherer.Gather() if err != nil { - Logger.Error("GetLocalMetrics: http.Get failed: %v", err) - return "", err + return fmt.Sprintf("#metrics get error: %s\n", err.Error()), fmt.Errorf("Could get local metrics %w", err) } - defer resp.Body.Close() - - metrics, err := ioutil.ReadAll(resp.Body) - if err != nil { - Logger.Error("GetLocalMetrics: ioutil.ReadAll failed: %v", err) - return "", err + for _, val := range vals { + err = enc.Encode(val) + if err != nil { + buf.WriteString(fmt.Sprintf("#metrics enc err:%s\n", err.Error())) + } } - - return string(metrics), nil + return string(buf.Bytes()), nil } func IsHealthProbeReady() bool { diff --git a/pkg/xapp/xapp.go b/pkg/xapp/xapp.go index 7aa8bb2..12f1628 100755 --- a/pkg/xapp/xapp.go +++ b/pkg/xapp/xapp.go @@ -66,6 +66,16 @@ var ( shutdownCnt int32 ) +var startTime time.Time + +func XappUpTime() time.Duration { + return time.Since(startTime) +} + +func init() { + startTime = time.Now() +} + func IsReady() bool { return Rmr != nil && Rmr.IsReady() && SdlStorage != nil && SdlStorage.IsReady() } @@ -274,34 +284,35 @@ func init() { InstallSignalHandler() } -func getIpAdress() string { - var ip net.IP - itf, err := net.InterfaceByName(os.Getenv("INTERFACE_NAME")) +func GetIpAddress() (string, error) { + ifname := os.Getenv("INTERFACE_NAME") + itf, err := net.InterfaceByName(ifname) if err != nil { - Logger.Info("Interface name is not able to resolve " + err.Error()) - return ip.String() + return "", fmt.Errorf("Interface (%s) %w", ifname, err) } item, err := itf.Addrs() if err != nil { - Logger.Info("IP address is not able to resolve " + err.Error()) - return ip.String() + return "", fmt.Errorf("Interface (%s) %w", ifname, err) } for _, addr := range item { switch v := addr.(type) { case *net.IPNet: if !v.IP.IsLinkLocalUnicast() { - ip = v.IP + return v.IP.String(), nil } } } - return ip.String() + return "", fmt.Errorf("Interface (%s) couldn't find ip", ifname) } func RunWithParams(c MessageConsumer, sdlcheck bool) { Rmr = NewRMRClient() Rmr.SetReadyCB(XappReadyCb, nil) - ipString := getIpAdress() + ipString, err := GetIpAddress() + if err != nil { + Logger.Info("IP address is not able to resolve " + err.Error()) + } var host string if ipString == "" { host = fmt.Sprintf(":%d", GetPortData("http").Port) -- 2.16.6