-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathclient.go
172 lines (140 loc) · 3.67 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package gitlab
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/heptiolabs/healthcheck"
"github.com/paulbellamy/ratecounter"
goGitlab "github.com/xanzy/go-gitlab"
"go.opentelemetry.io/otel"
"github.com/mvisonneau/gitlab-ci-pipelines-exporter/pkg/ratelimit"
)
const (
userAgent = "gitlab-ci-pipelines-exporter"
tracerName = "gitlab-ci-pipelines-exporter"
)
// Client ..
type Client struct {
*goGitlab.Client
Readiness struct {
URL string
HTTPClient *http.Client
}
RateLimiter ratelimit.Limiter
RateCounter *ratecounter.RateCounter
RequestsCounter atomic.Uint64
RequestsLimit int
RequestsRemaining int
version GitLabVersion
mutex sync.RWMutex
}
// ClientConfig ..
type ClientConfig struct {
URL string
Token string
UserAgentVersion string
DisableTLSVerify bool
ReadinessURL string
RateLimiter ratelimit.Limiter
}
// NewHTTPClient ..
func NewHTTPClient(disableTLSVerify bool) *http.Client {
// http.DefaultTransport contains useful settings such as the correct values for the picking
// up proxy informations from env variables
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: disableTLSVerify}
return &http.Client{
Transport: transport,
}
}
// NewClient ..
func NewClient(cfg ClientConfig) (*Client, error) {
opts := []goGitlab.ClientOptionFunc{
goGitlab.WithHTTPClient(NewHTTPClient(cfg.DisableTLSVerify)),
goGitlab.WithBaseURL(cfg.URL),
goGitlab.WithoutRetries(),
}
gc, err := goGitlab.NewOAuthClient(cfg.Token, opts...)
if err != nil {
return nil, err
}
gc.UserAgent = fmt.Sprintf("%s-%s", userAgent, cfg.UserAgentVersion)
readinessCheckHTTPClient := NewHTTPClient(cfg.DisableTLSVerify)
readinessCheckHTTPClient.Timeout = 5 * time.Second
return &Client{
Client: gc,
RateLimiter: cfg.RateLimiter,
Readiness: struct {
URL string
HTTPClient *http.Client
}{
URL: cfg.ReadinessURL,
HTTPClient: readinessCheckHTTPClient,
},
RateCounter: ratecounter.NewRateCounter(time.Second),
}, nil
}
// ReadinessCheck ..
func (c *Client) ReadinessCheck(ctx context.Context) healthcheck.Check {
ctx, span := otel.Tracer(tracerName).Start(ctx, "gitlab:ReadinessCheck")
defer span.End()
return func() error {
if c.Readiness.HTTPClient == nil {
return fmt.Errorf("readiness http client not configured")
}
req, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
c.Readiness.URL,
nil,
)
if err != nil {
return err
}
resp, err := c.Readiness.HTTPClient.Do(req)
if err != nil {
return err
}
if resp == nil {
return fmt.Errorf("HTTP error: empty response")
}
if err == nil && resp.StatusCode != http.StatusOK {
return fmt.Errorf("HTTP error: %d", resp.StatusCode)
}
return nil
}
}
func (c *Client) rateLimit(ctx context.Context) {
ctx, span := otel.Tracer(tracerName).Start(ctx, "gitlab:rateLimit")
defer span.End()
ratelimit.Take(ctx, c.RateLimiter)
// Used for monitoring purposes
c.RateCounter.Incr(1)
c.RequestsCounter.Add(1)
}
func (c *Client) UpdateVersion(version GitLabVersion) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.version = version
}
func (c *Client) Version() GitLabVersion {
c.mutex.RLock()
defer c.mutex.RUnlock()
return c.version
}
func (c *Client) requestsRemaining(response *goGitlab.Response) {
if response == nil {
return
}
if remaining := response.Header.Get("ratelimit-remaining"); remaining != "" {
c.RequestsRemaining, _ = strconv.Atoi(remaining)
}
if limit := response.Header.Get("ratelimit-limit"); limit != "" {
c.RequestsLimit, _ = strconv.Atoi(limit)
}
}