-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
73 lines (56 loc) · 1.34 KB
/
client.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
package ddcat
import (
"context"
"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
)
type Client struct {
apiKey string
appKey string
api *datadogV2.LogsApi
}
func NewClient(apiKey string, appKey string) *Client {
configuration := datadog.NewConfiguration()
apiClient := datadog.NewAPIClient(configuration)
api := datadogV2.NewLogsApi(apiClient)
client := &Client{
apiKey: apiKey,
appKey: appKey,
api: api,
}
return client
}
func (client *Client) withAPIKey(ctx context.Context) context.Context {
ctx = context.WithValue(
ctx,
datadog.ContextAPIKeys,
map[string]datadog.APIKey{
"apiKeyAuth": {
Key: client.apiKey,
},
"appKeyAuth": {
Key: client.appKey,
},
},
)
return ctx
}
func (client *Client) ListLogs(req datadogV2.LogsListRequest, callback func(datadogV2.LogsListResponse)) error {
ctx := client.withAPIKey(context.Background())
if req.Page == nil {
req.SetPage(*datadogV2.NewLogsListRequestPage())
}
req.Page.SetLimit(5000)
for {
resp, _, err := client.api.ListLogs(ctx, *datadogV2.NewListLogsOptionalParameters().WithBody(req))
if err != nil {
return err
}
callback(resp)
if resp.Meta.Page == nil || resp.Meta.Page.After == nil {
break
}
req.Page.SetCursor(*resp.Meta.Page.After)
}
return nil
}