MCPcopy Create free account
hub / github.com/APIParkLab/APIPark / LogInfo

Method LogInfo

log-driver/loki/loki.go:63–118  ·  view source on GitHub ↗
(clusterId string, id string)

Source from the content-addressed store, hash-verified

61}
62
63func (d *Driver) LogInfo(clusterId string, id string) (*log_driver.LogInfo, error) {
64 if id == "" {
65 return nil, fmt.Errorf("id is empty")
66 }
67 queries := url.Values{}
68 queries.Set("query", fmt.Sprintf("{cluster=\"%s\"} | json | request_id = `%s`", clusterId, id))
69 now := time.Now()
70 start := now.Add(-time.Hour * 24 * 30)
71 queries.Set("start", strconv.FormatInt(start.UnixNano(), 10))
72 queries.Set("end", strconv.FormatInt(now.UnixNano(), 10))
73 queries.Set("limit", "1")
74 log.Debug("query is ", queries.Get("query"))
75
76 list, err := send[LogInfo](http.MethodGet, fmt.Sprintf("%s/loki/api/v1/query_range", d.url), d.headers, queries, "")
77 if err != nil {
78 return nil, err
79 }
80 if len(list) < 1 || list[0].Stream == nil {
81 return nil, fmt.Errorf("no log found")
82 }
83 stream := list[0].Stream
84 requestBody := stream.RequestBody
85 proxyRequestBody := stream.ProxyBody
86 proxyResponseBody := stream.ProxyResponseBody
87 responseBody := stream.ResponseBody
88 if len(list[0].Values) > 0 {
89 switch t := list[0].Values[0].(type) {
90 case []interface{}:
91 if len(t) > 1 {
92 v, ok := t[1].(string)
93 if !ok {
94 break
95 }
96 var tmp LogBodyDetail
97 err = json.Unmarshal([]byte(v), &tmp)
98 if err == nil {
99 requestBody = tmp.RequestBody
100 proxyRequestBody = tmp.ProxyBody
101 responseBody = tmp.ResponseBody
102 proxyResponseBody = tmp.ProxyBody
103 }
104 }
105 }
106 }
107 msec, _ := strconv.ParseInt(stream.Msec, 10, 64)
108 return &log_driver.LogInfo{
109 LogItem: ToLogItem(stream, msec),
110 ContentType: stream.ContentType,
111 RequestBody: requestBody,
112 ProxyBody: proxyRequestBody,
113 ProxyResponseBody: proxyResponseBody,
114 ResponseBody: responseBody,
115 RequestHeader: stream.RequestHeader,
116 ResponseHeader: stream.ResponseHeader,
117 }, nil
118}
119
120func (d *Driver) LogCount(clusterId string, conditions map[string]string, spendHour int64, group string) (map[string]int64, error) {

Callers 1

TestLokiFunction · 0.95

Calls 3

ToLogItemFunction · 0.85
SetMethod · 0.65
GetMethod · 0.65

Tested by 1

TestLokiFunction · 0.76