-
Notifications
You must be signed in to change notification settings - Fork 156
/
log_lists.go
114 lines (93 loc) · 3.37 KB
/
log_lists.go
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
package datadog
import (
"time"
)
const logsListPath = "/v1/logs-queries/list"
// LogsListRequest represents the request body sent to the list API
type LogsListRequest struct {
Index *string `json:"index,omitempty"`
Limit *int `json:"limit,omitempty"`
Query *string `json:"query"`
Sort *string `json:"sort,omitempty"`
StartAt *string `json:"startAt,omitempty"`
Time *LogsListRequestQueryTime `json:"time"`
}
// LogsListRequestQueryTime represents the time object for the request sent to the list API
type LogsListRequestQueryTime struct {
TimeFrom *string `json:"from"`
TimeTo *string `json:"to"`
TimeZone *string `json:"timezone,omitempty"`
Offset *int `json:"offset,omitempty"`
}
// LogsList represents the base API response returned by the list API
type LogsList struct {
Logs []Logs `json:"logs"`
NextLogID *string `json:"nextLogId"`
Status *string `json:"status"`
}
func (l *LogsList) next() bool {
if l.NextLogID != nil {
return true
}
return false
}
// Logs represents the data of a log entry and contains the UUID of that entry (which
// is used for the StartAt option in an API request) as well as the content of that log
type Logs struct {
ID *string `json:"id"`
Content LogsContent `json:"content"`
}
// LogsContent respresents the actual log content returned by the list API
type LogsContent struct {
Timestamp *time.Time `json:"timestamp"`
Tags []string `json:"tags,omitempty"`
Attributes LogsAttributes `json:"attributes,omitempty"`
Host *string `json:"host"`
Service *string `json:"service"`
Message *string `json:"message"`
}
// LogsAttributes represents the Content attribute object from the list API
type LogsAttributes map[string]interface{}
// GetLogsList gets a page of log entries based on the values in the provided LogListRequest
func (client *Client) GetLogsList(logsRequest *LogsListRequest) (logsList *LogsList, err error) {
out := &LogsList{}
if err = client.doJsonRequest("POST", logsListPath, logsRequest, out); err != nil {
return nil, err
}
return out, nil
}
// GetLogsListPages calls GetLogsList and handles the pagination performed by the 'logs-queries/list' API
func (client *Client) GetLogsListPages(logsRequest *LogsListRequest, maxResults int) (logs []Logs, err error) {
// Reduce the number of results we have to fetch if the limit in our request body is greater than the value of maxResults
if maxResults < logsRequest.GetLimit() && maxResults > 0 {
logsRequest.SetLimit(maxResults)
}
response, err := client.GetLogsList(logsRequest)
if err != nil {
return nil, err
}
logs = append(logs, response.Logs...)
if maxResults < 0 { // Retrieve all results
for response.next() && err == nil {
logsRequest.StartAt = response.NextLogID
response, err = client.GetLogsList(logsRequest)
if err != nil {
return logs, err
}
logs = append(logs, response.Logs...)
}
} else {
for response.next() && err == nil && len(logs) < maxResults {
logsRequest.StartAt = response.NextLogID
if maxResults-len(logs) < logsRequest.GetLimit() {
logsRequest.SetLimit(maxResults - len(logs))
}
response, err = client.GetLogsList(logsRequest)
if err != nil {
return logs, err
}
logs = append(logs, response.Logs...)
}
}
return logs, err
}