-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_fetch.go
More file actions
90 lines (71 loc) · 2.36 KB
/
Copy pathgraph_fetch.go
File metadata and controls
90 lines (71 loc) · 2.36 KB
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
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"strings"
"time"
"go.uber.org/zap"
)
// fetchSeries is responsible for constructing the HTTP request to the PMM Victoria Metrics API,
// fetching the series data, marshaling, and returning the response
func fetchSeries(lumoConfig *LumoConfig, expr, legend string) (*VMResponse, error) {
// Handle trailing slash in endpoint URL
urlPath, err := url.JoinPath(strings.TrimRight(lumoConfig.Endpoint, "/"), "victoriametrics/prometheus/api/v1/query_range")
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrCreateRequest, err)
}
// Construct the query parameters
q := url.Values{}
q.Set("query", interpolateGraphConfig(expr, lumoConfig))
q.Set("step", lumoConfig.Interval)
q.Set("start", strconv.FormatInt(lumoConfig.Start.Unix(), 10))
q.Set("end", strconv.FormatInt(lumoConfig.End.Unix(), 10))
urlPath += "?" + q.Encode()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, urlPath, http.NoBody)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrCreateRequest, err)
}
// Set the Authorization header
req.Header.Set("Authorization", "Bearer "+lumoConfig.Token)
if lumoConfig.Debug {
dump, err := httputil.DumpRequestOut(req, true)
if err == nil {
zap.S().Debugf("--- DEBUG: HTTP Request (%s) ---\n%s\n---------------------------", legend, dump)
}
}
resp, err := httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrExecRequest, err)
}
defer func() { _ = resp.Body.Close() }()
if lumoConfig.Debug {
dump, err := httputil.DumpResponse(resp, true)
if err == nil {
zap.S().Debugf("--- DEBUG: HTTP Response (%s) ---\n%s\n----------------------------", legend, dump)
}
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%w: %d", ErrUnexpectedHTTPStatus, resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrReadResponse, err)
}
// Unmarshal the response body into a VMResponse struct
var vmResp VMResponse
if err := json.Unmarshal(body, &vmResp); err != nil {
return nil, fmt.Errorf("%w: %w", ErrParsingJSON, err)
}
if vmResp.Status != "success" {
return nil, fmt.Errorf("%w: %s", ErrAPIStatus, vmResp.Status)
}
return &vmResp, nil
}