-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.go
63 lines (54 loc) · 1.29 KB
/
worker.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
package main
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
"github.com/pkg/errors"
)
func (w *Work) Query(ctx context.Context) result {
// prepare URL
endpoint := "query_range"
values := url.Values{}
for k, v := range w.query.params {
if k == "endpoint" {
endpoint = v
}
values.Add(k, v)
}
url := fmt.Sprintf("%s/api/v1/%s?%s", w.serverURL, endpoint, values.Encode())
return callPromScale(ctx, url, w.clientTimeout)
}
func (w *Work) Health(ctx context.Context) result {
url := fmt.Sprintf("%s/%s", w.serverURL, "healthz")
return callPromScale(ctx, url, w.clientTimeout)
}
func callPromScale(ctx context.Context, url string, timeoutMillis int) result {
// log.Printf("req url: %v", url)
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return result{
isError: true,
err: errors.WithStack(err),
}
}
c := http.DefaultClient
c.Timeout = time.Duration(timeoutMillis * int(time.Millisecond))
r := result{st: time.Now()}
resp, err := c.Do(req)
if err != nil {
r.isError = true
r.err = errors.WithStack(err)
return r
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
r.isError = true
r.err = errors.New("non ok response from Promscale")
r.statusCode = resp.StatusCode
return r
}
r.et = time.Now()
return r
}