Skip to content
Snippets Groups Projects
Commit 7d54e7f7 authored by Jan Semmelink's avatar Jan Semmelink
Browse files

Added search to logs actions

parent dffd8a76
Branches
Tags
No related merge requests found
...@@ -111,6 +111,22 @@ func LogSQSSent(startTime time.Time, queueName string, messageType string, reque ...@@ -111,6 +111,22 @@ func LogSQSSent(startTime time.Time, queueName string, messageType string, reque
actionListMutex.Unlock() actionListMutex.Unlock()
} }
func LogSearch(startTime time.Time, index string, query string) {
endTime := time.Now()
actionListMutex.Lock()
actionList = append(actionList, ActionLog{
StartTime: startTime,
EndTime: endTime,
DurMs: endTime.Sub(startTime).Milliseconds(),
Type: ActionTypeSearch,
Search: &SearchLog{
Index: index,
Query: query,
},
})
actionListMutex.Unlock()
}
//Call LogSleep() after doing time.Sleep() //Call LogSleep() after doing time.Sleep()
//to capture the details //to capture the details
//and add it to the current handler log story for reporting/metrics //and add it to the current handler log story for reporting/metrics
...@@ -139,6 +155,7 @@ type ActionLog struct { ...@@ -139,6 +155,7 @@ type ActionLog struct {
ApiCall *ApiCallLog `json:"api_call,omitempty"` ApiCall *ApiCallLog `json:"api_call,omitempty"`
SQSSent *SQSSentLog `json:"sqs_sent,omitempty"` SQSSent *SQSSentLog `json:"sqs_sent,omitempty"`
SQLQuery *SQLQueryLog `json:"sql_query,omitempty"` SQLQuery *SQLQueryLog `json:"sql_query,omitempty"`
Search *SearchLog `json:"search"`
} }
func (action ActionLog) Relative(relTime time.Time) RelativeActionLog { func (action ActionLog) Relative(relTime time.Time) RelativeActionLog {
...@@ -170,6 +187,7 @@ var ActionTypeList = []ActionType{ ...@@ -170,6 +187,7 @@ var ActionTypeList = []ActionType{
ActionTypeApiCall, ActionTypeApiCall,
ActionTypeSqsSent, ActionTypeSqsSent,
ActionTypeSqlQuery, ActionTypeSqlQuery,
ActionTypeSearch,
ActionTypeSleep, ActionTypeSleep,
} }
...@@ -178,6 +196,7 @@ const ( ...@@ -178,6 +196,7 @@ const (
ActionTypeApiCall ActionType = "api-call" ActionTypeApiCall ActionType = "api-call"
ActionTypeSqsSent ActionType = "sqs-sent" ActionTypeSqsSent ActionType = "sqs-sent"
ActionTypeSqlQuery ActionType = "sql-query" ActionTypeSqlQuery ActionType = "sql-query"
ActionTypeSearch ActionType = "search"
ActionTypeSleep ActionType = "sleep" ActionTypeSleep ActionType = "sleep"
) )
...@@ -210,6 +229,11 @@ type SQLQueryLog struct { ...@@ -210,6 +229,11 @@ type SQLQueryLog struct {
Error string `json:"error,omitempty"` Error string `json:"error,omitempty"`
} }
type SearchLog struct {
Index string `json:"index"`
Query string `json:"query"`
}
//compile the relative action list that took place during this handler //compile the relative action list that took place during this handler
//copy then reset actionList for the next handler //copy then reset actionList for the next handler
//we copy it with relation to this API's start..end time, rather than full timestamps, which are hard to read in the list //we copy it with relation to this API's start..end time, rather than full timestamps, which are hard to read in the list
...@@ -263,6 +287,12 @@ func relativeActionList(startTime, endTime time.Time) []RelativeActionLog { ...@@ -263,6 +287,12 @@ func relativeActionList(startTime, endTime time.Time) []RelativeActionLog {
action.ApiCall.Response.Body = action.ApiCall.Response.Body[:cfg.ActionsMaxAPIResBodyLength] action.ApiCall.Response.Body = action.ApiCall.Response.Body[:cfg.ActionsMaxAPIResBodyLength]
} }
} }
case ActionTypeSearch:
if action.Search != nil {
if len(action.Search.Query) > int(cfg.ActionsMaxSearchQueryLength) {
action.Search.Query = action.Search.Query[:cfg.ActionsMaxSearchQueryLength]
}
}
} }
//make relative and append to the list //make relative and append to the list
......
...@@ -15,6 +15,7 @@ type Config struct { ...@@ -15,6 +15,7 @@ type Config struct {
ActionsMaxSQSReqBodyLength int64 `json:"actions_max_sqs_req_body_length" doc:"Set length of SQS Request body to keep in action list (default 0 = delete)"` ActionsMaxSQSReqBodyLength int64 `json:"actions_max_sqs_req_body_length" doc:"Set length of SQS Request body to keep in action list (default 0 = delete)"`
ActionsMaxAPIReqBodyLength int64 `json:"actions_max_api_req_body_length" doc:"Set length of API Request body to keep in action list (default 0 = delete)"` ActionsMaxAPIReqBodyLength int64 `json:"actions_max_api_req_body_length" doc:"Set length of API Request body to keep in action list (default 0 = delete)"`
ActionsMaxAPIResBodyLength int64 `json:"actions_max_api_res_body_length" doc:"Set length of API Response body to keep in action list (default 0 = delete)"` ActionsMaxAPIResBodyLength int64 `json:"actions_max_api_res_body_length" doc:"Set length of API Response body to keep in action list (default 0 = delete)"`
ActionsMaxSearchQueryLength int64 `json:"actions_max_search_query_length" doc:"Set length of search query to keep in action list (default 0 = delete)"`
} }
const configPrefix = "LOGS" const configPrefix = "LOGS"
......
...@@ -8,10 +8,12 @@ import ( ...@@ -8,10 +8,12 @@ import (
"net/http" "net/http"
"reflect" "reflect"
"strings" "strings"
"time"
opensearchapi "github.com/opensearch-project/opensearch-go/opensearchapi" opensearchapi "github.com/opensearch-project/opensearch-go/opensearchapi"
"gitlab.com/uafrica/go-utils/errors" "gitlab.com/uafrica/go-utils/errors"
"gitlab.com/uafrica/go-utils/logger" "gitlab.com/uafrica/go-utils/logger"
"gitlab.com/uafrica/go-utils/logs"
"gitlab.com/uafrica/go-utils/reflection" "gitlab.com/uafrica/go-utils/reflection"
) )
...@@ -195,12 +197,18 @@ func (ds *documentStore) Search(query Query, limit int64) (ids []string, totalCo ...@@ -195,12 +197,18 @@ func (ds *documentStore) Search(query Query, limit int64) (ids []string, totalCo
Size: limit, Size: limit,
Query: query, Query: query,
} }
jsonBody, _ := json.Marshal(body) jsonBody, _ := json.Marshal(body)
search := opensearchapi.SearchRequest{ search := opensearchapi.SearchRequest{
Index: []string{ds.name}, Index: []string{ds.name},
Body: bytes.NewReader(jsonBody), Body: bytes.NewReader(jsonBody),
} }
startTime := time.Now()
defer func() {
logs.LogSearch(startTime, ds.name, string(jsonBody))
}()
searchResponse, err := search.Do(context.Background(), ds.w.client) searchResponse, err := search.Do(context.Background(), ds.w.client)
if err != nil { if err != nil {
err = errors.Wrapf(err, "failed to search documents") err = errors.Wrapf(err, "failed to search documents")
......
...@@ -13,6 +13,7 @@ import ( ...@@ -13,6 +13,7 @@ import (
opensearchapi "github.com/opensearch-project/opensearch-go/opensearchapi" opensearchapi "github.com/opensearch-project/opensearch-go/opensearchapi"
"gitlab.com/uafrica/go-utils/errors" "gitlab.com/uafrica/go-utils/errors"
"gitlab.com/uafrica/go-utils/logger" "gitlab.com/uafrica/go-utils/logger"
"gitlab.com/uafrica/go-utils/logs"
"gitlab.com/uafrica/go-utils/reflection" "gitlab.com/uafrica/go-utils/reflection"
) )
...@@ -383,6 +384,7 @@ func (ts *timeSeries) Search(query Query, limit int64) (docs interface{}, totalC ...@@ -383,6 +384,7 @@ func (ts *timeSeries) Search(query Query, limit int64) (docs interface{}, totalC
Size: limit, Size: limit,
Query: query, Query: query,
} }
jsonBody, _ := json.Marshal(body) jsonBody, _ := json.Marshal(body)
logger.Debugf("Search: %s", string(jsonBody)) logger.Debugf("Search: %s", string(jsonBody))
search := opensearchapi.SearchRequest{ search := opensearchapi.SearchRequest{
...@@ -390,6 +392,11 @@ func (ts *timeSeries) Search(query Query, limit int64) (docs interface{}, totalC ...@@ -390,6 +392,11 @@ func (ts *timeSeries) Search(query Query, limit int64) (docs interface{}, totalC
Body: bytes.NewReader(jsonBody), Body: bytes.NewReader(jsonBody),
} }
startTime := time.Now()
defer func() {
logs.LogSearch(startTime, ts.name+"-*", string(jsonBody))
}()
searchResponse, err := search.Do(context.Background(), ts.w.client) searchResponse, err := search.Do(context.Background(), ts.w.client)
if err != nil { if err != nil {
err = errors.Wrapf(err, "failed to search documents") err = errors.Wrapf(err, "failed to search documents")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment