1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| package main
import ( "context" "encoding/json" "fmt" "github.com/olivere/elastic/v7" "log" )
func generateKQL(path string, requestBody string, flagSuccess *string, flagFail *string, flagExcept []string) string { kql := fmt.Sprintf("path:\"%s\" AND requestBody:\"%s\"", path, requestBody)
if flagSuccess != nil { kql += fmt.Sprintf(" AND responseBody:\"%s\"", *flagSuccess) }
if flagFail != nil { kql += fmt.Sprintf(" AND responseBody:\"%s\"", *flagFail) }
if len(flagExcept) > 0 { for _, flag := range flagExcept { kql += fmt.Sprintf(" AND NOT responseBody:\"%s\"", flag) } }
return kql }
func RegisterAllInfoTotalCount(interFaceName string, yesterday string) { ctx := context.Background()
esURL := "http://192.168.64.xx:30092"
client, err := elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(esURL)) if err != nil { log.Fatalf("Error creating Elastic client: %s", err) }
reqTimePattern := fmt.Sprintf("reqTime:*%s*", yesterday) flagPatternSuccess := fmt.Sprintf("flag:%d", 1) flagPatternFail := fmt.Sprintf("flag:%d", 0) queries := []struct { desc string query elastic.Query }{ { desc: fmt.Sprintf("%s-接口总计", interFaceName), query: elastic.NewBoolQuery(). Must( elastic.NewMatchQuery("path", interFaceName), elastic.NewMatchPhraseQuery("requestBody", reqTimePattern), ), }, { desc: fmt.Sprintf("%s-接口成功数", interFaceName), query: elastic.NewBoolQuery(). Must( elastic.NewMatchQuery("path", interFaceName), elastic.NewMatchPhraseQuery("requestBody", reqTimePattern), elastic.NewMatchPhraseQuery("responseBody", flagPatternSuccess), ), }, { desc: fmt.Sprintf("%s-接口失败数", interFaceName), query: elastic.NewBoolQuery(). Must( elastic.NewMatchQuery("path", interFaceName), elastic.NewMatchPhraseQuery("requestBody", reqTimePattern), elastic.NewMatchPhraseQuery("responseBody", flagPatternFail), ), }, { desc: fmt.Sprintf("%s-接口异常数", interFaceName), query: elastic.NewBoolQuery(). Must( elastic.NewMatchQuery("path", interFaceName), elastic.NewMatchPhraseQuery("requestBody", reqTimePattern), ). MustNot( elastic.NewMatchPhraseQuery("responseBody", flagPatternFail), elastic.NewMatchPhraseQuery("responseBody", flagPatternSuccess), ), }, }
kql_total := generateKQL(interFaceName, reqTimePattern, nil, nil, nil) kql_success := generateKQL(interFaceName, reqTimePattern, &flagPatternSuccess, nil, nil) kql_fail := generateKQL(interFaceName, reqTimePattern, nil, &flagPatternFail, nil) kql_except := generateKQL(interFaceName, reqTimePattern, nil, nil, []string{flagPatternSuccess, flagPatternFail}) fmt.Printf("%s-接口总计 KQL 查询:%s\n", interFaceName, kql_total) fmt.Printf("%s-接口成功数 KQL 查询:%s\n", interFaceName, kql_success) fmt.Printf("%s-接口失败数 KQL 查询:%s\n", interFaceName, kql_fail) fmt.Printf("%s-接口异常数 KQL 查询:%s\n", interFaceName, kql_except)
fileName := fmt.Sprintf("注册接口统计-%s.txt", yesterday) WriteToFile(fileName, "====================================================================================================\n") for _, q := range queries { res, err := client.Search(). Index("shenyu-access-logging"). Query(q.query). Size(0). Do(ctx)
if err != nil { log.Fatalf("Error executing query for %s: %s", q.desc, err) }
output := fmt.Sprintf("%s为:%d\n", q.desc, res.TotalHits()) WriteToFile(fileName, output) }
exceptionQuery := queries[3].query exceptionRes, err := client.Search(). Index("shenyu-access-logging"). Query(exceptionQuery). Size(10000). Do(ctx)
if err != nil { log.Fatalf("Error executing exception query: %s", err) }
exceptDataName := fmt.Sprintf("%s-接口异常数据:\n", interFaceName) WriteToFile(fileName, exceptDataName) for _, hit := range exceptionRes.Hits.Hits { var logEntry LogEntry if err := json.Unmarshal(hit.Source, &logEntry); err != nil { log.Printf("Error unmarshalling log entry: %s", err) } else { WriteToFile(fileName, "##################################################\n") output := fmt.Sprintf("timeLocal: %s; status: %d; requestBody:\n %s\n", logEntry.TimeLocal, logEntry.Status, logEntry.RequestBody) WriteToFile(fileName, output) } } }
|