Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/loki/source/cloudflare/internal/cloudflaretarget/client.go
4097 views
1
package cloudflaretarget
2
3
// This code is copied from Promtail. The cloudflaretarget package is used to
4
// configure and run a target that can read from the Cloudflare Logpull API and
5
// forward entries to other loki components.
6
7
import (
8
"context"
9
"time"
10
11
"github.com/grafana/cloudflare-go"
12
)
13
14
// Client is a wrapper around the Cloudflare API that allow for testing and being zone/fields aware.
15
type Client interface {
16
LogpullReceived(ctx context.Context, start, end time.Time) (cloudflare.LogpullReceivedIterator, error)
17
}
18
19
type wrappedClient struct {
20
client *cloudflare.API
21
zoneID string
22
fields []string
23
}
24
25
func (w *wrappedClient) LogpullReceived(ctx context.Context, start, end time.Time) (cloudflare.LogpullReceivedIterator, error) {
26
return w.client.LogpullReceived(ctx, w.zoneID, start, end, cloudflare.LogpullReceivedOption{
27
Fields: w.fields,
28
})
29
}
30
31
var getClient = func(apiKey, zoneID string, fields []string) (Client, error) {
32
c, err := cloudflare.NewWithAPIToken(apiKey)
33
if err != nil {
34
return nil, err
35
}
36
return &wrappedClient{
37
client: c,
38
zoneID: zoneID,
39
fields: fields,
40
}, nil
41
}
42
43