Skip to content

Commit

Permalink
Merge pull request #416 from dilallkx/release-1.9.0_kd_fix2
Browse files Browse the repository at this point in the history
Metrics Package UT fixes
  • Loading branch information
dilallkx authored Dec 19, 2022
2 parents 1bf5fdb + 9dccc66 commit 4c9a57d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 56 deletions.
48 changes: 37 additions & 11 deletions go-packages/meep-metrics/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,49 +42,49 @@ func TestHttpMetricsGetSet(t *testing.T) {
ms.Flush()

fmt.Println("Set http metrics")
err = ms.SetHttpMetric(HttpMetric{"logger1", "RX", 1, "url1", "endpoint1", "method1", "src1", "dst1", "body1", "respBody1", "201", "101", "time", "memaid", "seuqence"})
err = ms.SetHttpMetric(HttpMetric{"logger1", HttpMsgTypeResponse, 1, "url1", "endpoint1", "method1", "src1", "dst1", "body1", "respBody1", "201", "101", "time", "memaid", "seuqence"})
if err != nil {
t.Fatalf("Unable to set http metric")
}
err = ms.SetHttpMetric(HttpMetric{"logger1", "TX", 2, "url2", "endpoint2", "method2", "src2", "dst2", "body2", "respBody2", "202", "102", "time", "memaid", "seuqence"})
err = ms.SetHttpMetric(HttpMetric{"logger1", HttpMsgTypeNotification, 2, "url2", "endpoint2", "method2", "src2", "dst2", "body2", "respBody2", "202", "102", "time", "memaid", "seuqence"})
if err != nil {
t.Fatalf("Unable to set http metric")
}
err = ms.SetHttpMetric(HttpMetric{"logger1", "TX", 3, "url3", "endpoint3", "method3", "src3", "dst3", "body3", "respBody3", "203", "103", "time", "memaid", "seuqence"})
err = ms.SetHttpMetric(HttpMetric{"logger1", HttpMsgTypeNotification, 3, "url3", "endpoint3", "method3", "src3", "dst3", "body3", "respBody3", "203", "103", "time", "memaid", "seuqence"})
if err != nil {
t.Fatalf("Unable to set http metric")
}
err = ms.SetHttpMetric(HttpMetric{"logger2", "RX", 4, "url4", "endpoint4", "method4", "src4", "dst4", "body4", "respBody4", "204", "104", "time", "memaid", "seuqence"})
err = ms.SetHttpMetric(HttpMetric{"logger2", HttpMsgTypeResponse, 4, "url4", "endpoint4", "method4", "src4", "dst4", "body4", "respBody4", "204", "104", "time", "memaid", "seuqence"})
if err != nil {
t.Fatalf("Unable to set http metric")
}

fmt.Println("Get http metrics")
hml, err := ms.GetHttpMetric("logger3", "RX", "", 0)
hml, err := ms.GetHttpMetric("logger3", HttpMsgTypeResponse, "", 0)
if err != nil || len(hml) != 0 {
t.Fatalf("No metrics should be found for logger3")
}
hml, err = ms.GetHttpMetric("logger1", "RX", "", 0)
hml, err = ms.GetHttpMetric("logger1", HttpMsgTypeResponse, "", 0)
if err != nil || len(hml) != 1 {
t.Fatalf("Failed to get metric")
}
if !validateHttpMetric(hml[0], "logger1", "RX", 1, "url1", "endpoint1", "method1", "src1", "dst1", "body1", "respBody1", "201", "101") {
if !validateHttpMetric(hml[0], "logger1", HttpMsgTypeResponse, 1, "url1", "endpoint1", "method1", "src1", "dst1", "body1", "respBody1", "201", "101") {
t.Fatalf("Invalid http metric")
}

hml, err = ms.GetHttpMetric("logger1", "TX", "", 0)
hml, err = ms.GetHttpMetric("logger1", HttpMsgTypeNotification, "", 0)
if err != nil || len(hml) != 2 {
t.Fatalf("Failed to get metric")
}
hml, err = ms.GetHttpMetric("logger1", "", "", 0)
if err != nil || len(hml) != 3 {
t.Fatalf("Failed to get metric")
}
hml, err = ms.GetHttpMetric("", "RX", "", 0)
hml, err = ms.GetHttpMetric("", HttpMsgTypeResponse, "", 0)
if err != nil || len(hml) != 2 {
t.Fatalf("Failed to get metric")
}
hml, err = ms.GetHttpMetric("logger1,logger2", "RX", "", 0)
hml, err = ms.GetHttpMetric("logger1,logger2", HttpMsgTypeResponse, "", 0)
if err != nil || len(hml) != 2 {
t.Fatalf("Failed to get metric")
}
Expand All @@ -93,5 +93,31 @@ func TestHttpMetricsGetSet(t *testing.T) {
}

func validateHttpMetric(h HttpMetric, loggerName string, direction string, id int32, url string, endpoint string, method string, src string, dst string, body string, respBody string, respCode string, procTime string) bool {
return h.LoggerName == loggerName && h.Id == id && h.Url == url && h.Endpoint == endpoint && h.Method == method && h.Body == body && h.RespBody == respBody && h.RespCode == respCode && h.ProcTime == procTime && h.Src == src && h.Dst == dst
if h.LoggerName != loggerName {
fmt.Println("h.LoggerName[" + h.LoggerName + "] != loggerName[" + loggerName + "]")
} else if h.Id != id {
fmt.Println("h.Id != id")
} else if h.Url != url {
fmt.Println("h.Url[" + h.Url + "] != url[" + url + "]")
} else if h.Endpoint != endpoint {
fmt.Println("h.Endpoint[" + h.Endpoint + "] != endpoint[" + endpoint + "]")
} else if h.Method != method {
fmt.Println("h.Method[" + h.Method + "] != method[" + method + "]")
} else if h.Body != body {
fmt.Println("h.Body[" + h.Body + "] != body[" + body + "]")
} else if h.RespBody != respBody {
fmt.Println("h.RespBody[" + h.RespBody + "] != respBody[" + respBody + "]")
} else if h.RespCode != respCode {
fmt.Println("h.RespCode[" + h.RespCode + "] != respCode[" + respCode + "]")
} else if h.ProcTime != procTime {
fmt.Println("h.ProcTime[" + h.ProcTime + "] != procTime[" + procTime + "]")
} else if h.Src != src {
fmt.Println("h.Src[" + h.Src + "] != src[" + src + "]")
} else if h.Dst != dst {
fmt.Println("h.Dst[" + h.Dst + "] != dst[" + dst + "]")
} else {
// Valid metric
return true
}
return false
}
47 changes: 13 additions & 34 deletions go-packages/meep-metrics/metric-store.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,13 @@ func (ms *MetricStore) GetInfluxMetric(metric string, tags map[string]string, fi

metricCount := 0
startTime := ""
// Set start time if duration is set
if duration != "" {
influxDuration, _ := time.ParseDuration(duration)
startTime = strconv.FormatInt(time.Now().Add(-1*influxDuration).UnixNano(), 10)
startTime = strconv.FormatInt(time.Now().UnixNano(), 10) + " - " + duration
}

// Fetch metrics from DB
// Run multiple times if more than MAX_LIMIT metrics retrieved
for {
stopTime := ""
if len(values) > 0 {
Expand All @@ -335,7 +338,7 @@ func (ms *MetricStore) GetInfluxMetric(metric string, tags map[string]string, fi
}

metricCount += count
tagStrTime := ms.getTagStr(tagStr, startTime, stopTime, duration)
tagStrTime := ms.setTimeFilter(tagStr, startTime, stopTime)
// Count
countStr := ""
if count != 0 {
Expand Down Expand Up @@ -375,7 +378,7 @@ func (ms *MetricStore) GetInfluxMetric(metric string, tags map[string]string, fi
return values, nil
}

func (ms *MetricStore) getTagStr(inputTagStr string, timeStart string, timeStop string, duration string) string {
func (ms *MetricStore) setTimeFilter(inputTagStr string, timeStart string, timeStop string) string {
tagStr := inputTagStr
if timeStart != "" && timeStop != "" {
if tagStr == "" {
Expand All @@ -384,40 +387,16 @@ func (ms *MetricStore) getTagStr(inputTagStr string, timeStart string, timeStop
tagStr += " AND time > " + timeStart + " AND time < " + timeStop
}
} else if timeStart != "" {
if duration != "" {
if tagStr == "" {
tagStr = " WHERE time > " + timeStart + " AND time < " + timeStart + " + " + duration
} else {
tagStr += " AND time > " + timeStart + " AND time < " + timeStart + " + " + duration
}
if tagStr == "" {
tagStr = " WHERE time > " + timeStart
} else {
if tagStr == "" {
tagStr = " WHERE time > " + timeStart
} else {
tagStr += " AND time > " + timeStart
}
tagStr += " AND time > " + timeStart
}
} else if timeStop != "" {
if duration != "" {
if tagStr == "" {
tagStr = " WHERE time < " + timeStop + " AND time > " + timeStop + " - " + duration
} else {
tagStr += " AND time < " + timeStop + " AND time > " + timeStop + " - " + duration
}
if tagStr == "" {
tagStr = " WHERE time < " + timeStop
} else {
if tagStr == "" {
tagStr = " WHERE time < " + timeStop
} else {
tagStr += " AND time < " + timeStop
}
}
} else {
if duration != "" {
if tagStr == "" {
tagStr = " WHERE time > now() - " + duration
} else {
tagStr += " AND time > now() - " + duration
}
tagStr += " AND time < " + timeStop
}
}
return tagStr
Expand Down
28 changes: 17 additions & 11 deletions go-packages/meep-metrics/metric-store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,24 +533,30 @@ func TestIncrementalInfluxQuery(t *testing.T) {
if err != nil || len(result) != 100 {
t.Fatalf("Failed to get metric")
}

fmt.Println("Check long Duration")
MAX_LIMIT = 10
tags = map[string]string{tag1: "tag1", tag2: "tag2"}
fields = []string{field1, field2, field3, field4}
result, err = ms.GetInfluxMetric(metric1, tags, fields, "1d", 100)
fmt.Println("Length of results: ", len(result))
if err != nil || len(result) != 100 {
t.Fatalf("Failed to get metric")
}
}

func validateMetric(result map[string]interface{}, v1 bool, v2 string, v3 int32, v4 float64) bool {
if result[field1] != v1 {
fmt.Println("Invalid " + field1)
return false
}
if result[field2] != v2 {
} else if result[field2] != v2 {
fmt.Println("Invalid " + field2)
return false
}
if val, ok := result[field3].(json.Number); !ok || JsonNumToInt32(val) != v3 {
} else if val, ok := result[field3].(json.Number); !ok || JsonNumToInt32(val) != v3 {
fmt.Println("Invalid " + field3)
return false
}
if val, ok := result[field4].(json.Number); !ok || JsonNumToFloat64(val) != v4 {
} else if val, ok := result[field4].(json.Number); !ok || JsonNumToFloat64(val) != v4 {
fmt.Println("Invalid " + field4)
return false
} else {
// Valid metric
return true
}
return true
return false
}

0 comments on commit 4c9a57d

Please sign in to comment.