INTERNAL: Symptom improvements 81/8381/1 v0.9.13
authorJuha Hyttinen <juha.hyttinen@nokia.com>
Tue, 24 May 2022 09:38:59 +0000 (12:38 +0300)
committerJuha Hyttinen <juha.hyttinen@nokia.com>
Tue, 24 May 2022 10:57:10 +0000 (13:57 +0300)
- Symptom uptime
- GetIpAddress set to public
- GetLocalMetrics will use directly prometheus api

Signed-off-by: Juha Hyttinen <juha.hyttinen@nokia.com>
Change-Id: I8fa22ff718f59fbed9b8e40886d9f43a584650b8

go.mod
pkg/xapp/restapi.go
pkg/xapp/xapp.go

diff --git a/go.mod b/go.mod
index 1156053..0e018c7 100644 (file)
--- 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
index 00f8aba..12101a8 100755 (executable)
@@ -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 {
index 7aa8bb2..12f1628 100755 (executable)
@@ -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 "<nil>", 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 "<nil>", 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 "<nil>", 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 == "<nil>" {
                host = fmt.Sprintf(":%d", GetPortData("http").Port)