From a6b1c271eedd59e8754ec284ba1245452c5b3831 Mon Sep 17 00:00:00 2001 From: Ankit Nayan Date: Sun, 2 May 2021 12:52:57 +0530 Subject: [PATCH] changed errors to percent in external calls --- pkg/query-service/druidQuery/mysql-query.go | 54 ++++++++++++++++++--- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/pkg/query-service/druidQuery/mysql-query.go b/pkg/query-service/druidQuery/mysql-query.go index 56d864daa0..584b43c579 100644 --- a/pkg/query-service/druidQuery/mysql-query.go +++ b/pkg/query-service/druidQuery/mysql-query.go @@ -49,6 +49,8 @@ type ServiceExternalItem struct { AvgDuration float32 `json:"avgDuration,omitempty"` NumCalls int `json:"numCalls,omitempty"` CallRate float32 `json:"callRate,omitempty"` + NumErrors int `json:"numErrors"` + ErrorRate float32 `json:"errorRate"` } type ServiceDBOverviewItem struct { @@ -295,15 +297,51 @@ func GetServiceExternalErrors(client *SqlClient, query *model.GetServiceOverview return nil, fmt.Errorf("Error in unmarshalling response from druid") } - for i, _ := range *res { - timeObj, _ := time.Parse(time.RFC3339Nano, (*res)[i].Time) - (*res)[i].Timestamp = int64(timeObj.UnixNano()) - (*res)[i].Time = "" - (*res)[i].CallRate = float32((*res)[i].NumCalls) / float32(query.StepSeconds) + sqlQuery = fmt.Sprintf(`SELECT TIME_FLOOR(__time, '%s') as "time", COUNT(SpanId) as "numCalls", ExternalHttpUrl as externalHttpUrl FROM %s WHERE ServiceName='%s' AND Kind='3' AND ExternalHttpUrl != '' AND "__time" >= '%s' AND "__time" <= '%s' + GROUP BY TIME_FLOOR(__time, '%s'), ExternalHttpUrl`, query.Period, constants.DruidDatasource, query.ServiceName, query.StartTime, query.EndTime, query.Period) + + // zap.S().Debug(sqlQuery) + + responseTotal, err := client.Query(sqlQuery, "object") + + if err != nil { + zap.S().Error(query, err) + return nil, fmt.Errorf("Something went wrong in druid query") + } + + // responseStr := string(response) + // zap.S().Info(responseStr) + + resTotal := new([]ServiceExternalItem) + err = json.Unmarshal(responseTotal, resTotal) + if err != nil { + zap.S().Error(err) + return nil, fmt.Errorf("Error in unmarshalling response from druid") + } + + m := make(map[int64]int) + + for j, _ := range *res { + timeObj, _ := time.Parse(time.RFC3339Nano, (*res)[j].Time) + m[int64(timeObj.UnixNano())] = (*res)[j].NumCalls + } + + for i, _ := range *resTotal { + timeObj, _ := time.Parse(time.RFC3339Nano, (*resTotal)[i].Time) + (*resTotal)[i].Timestamp = int64(timeObj.UnixNano()) + (*resTotal)[i].Time = "" + (*resTotal)[i].CallRate = float32((*resTotal)[i].NumCalls) / float32(query.StepSeconds) + + if val, ok := m[(*resTotal)[i].Timestamp]; ok { + (*resTotal)[i].NumErrors = val + (*resTotal)[i].ErrorRate = float32((*resTotal)[i].NumErrors) * 100 / float32((*resTotal)[i].NumCalls) + } + (*resTotal)[i].CallRate = 0 + (*resTotal)[i].NumCalls = 0 } - servicesExternalResponse := (*res)[1:] + servicesExternalResponse := (*resTotal)[1:] return &servicesExternalResponse, nil } @@ -438,7 +476,7 @@ func GetServiceOverview(client *SqlClient, query *model.GetServiceOverviewParams if val, ok := m[(*res)[i].Timestamp]; ok { (*res)[i].NumErrors = val } - (*res)[i].ErrorRate = float32((*res)[i].NumErrors) / float32((*res)[i].NumCalls) + (*res)[i].ErrorRate = float32((*res)[i].NumErrors) * 100 / float32((*res)[i].NumCalls) (*res)[i].CallRate = float32((*res)[i].NumCalls) / float32(query.StepSeconds) } @@ -501,7 +539,7 @@ func GetServices(client *SqlClient, query *model.GetServicesParams) (*[]ServiceI (*res)[i].NumErrors = val } - (*res)[i].ErrorRate = float32((*res)[i].NumErrors) / float32((*res)[i].NumCalls) + (*res)[i].ErrorRate = float32((*res)[i].NumErrors) * 100 / float32((*res)[i].NumCalls) (*res)[i].CallRate = float32((*res)[i].NumCalls) / float32(query.Period) }