-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathwrite.go
177 lines (145 loc) · 3.41 KB
/
write.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
173
174
175
176
177
package influxdb
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"mime"
"net/http"
"net/url"
"path"
"strconv"
"github.com/influxdata/influxdb-client-go/internal/gzip"
lp "github.com/influxdata/line-protocol"
)
// Write writes metrics to a bucket, and org. It retries intelligently.
// If the write is too big, it retries again, after breaking the payloads into two requests.
func (c *Client) Write(ctx context.Context, bucket, org string, m ...Metric) (n int, err error) {
var (
buf = &bytes.Buffer{}
e = lp.NewEncoder(buf)
)
e.FailOnFieldErr(c.errOnFieldErr)
select {
case <-ctx.Done():
return 0, ctx.Err()
default:
}
for i := range m {
if _, err := e.Encode(m[i]); err != nil {
return 0, err
}
}
req, err := c.makeWriteRequest(bucket, org, buf)
if err != nil {
return 0, err
}
resp, err := c.httpClient.Do(req.WithContext(ctx))
if err != nil {
return 0, err
}
defer func() {
// discard body so connection can be reused
_, _ = io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}()
eerr, err := parseWriteError(resp)
if err != nil {
return 0, err
}
if eerr != nil {
return 0, eerr
}
return len(m), nil
}
func parseInt32(v string) (int32, error) {
retry, err := strconv.ParseInt(v, 10, 32)
if err != nil {
return 0, err
}
return int32(retry), nil
}
func (c *Client) makeWriteRequest(bucket, org string, body io.Reader) (*http.Request, error) {
var err error
if c.contentEncoding == "gzip" {
body, err = gzip.CompressWithGzip(body, c.compressionLevel)
if err != nil {
return nil, err
}
}
u, err := makeWriteURL(c.url, bucket, org)
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPost, u, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "text/plain; charset=utf-8")
if c.contentEncoding == "gzip" {
req.Header.Set("Content-Encoding", "gzip")
}
req.Header.Set("User-Agent", c.userAgent)
req.Header.Set("Authorization", c.authorization)
return req, nil
}
func makeWriteURL(loc *url.URL, bucket, org string) (string, error) {
if loc == nil {
return "", errors.New("nil url")
}
u, err := url.Parse(loc.String())
if err != nil {
return "", err
}
params := url.Values{}
params.Set("bucket", bucket)
params.Set("org", org)
u.RawQuery = params.Encode()
switch loc.Scheme {
case "http", "https":
u.Path = path.Join(u.Path, "/write")
case "unix":
default:
return "", fmt.Errorf("unsupported scheme: %q", u.Scheme)
}
return u.String(), nil
}
func parseWriteError(r *http.Response) (err *Error, perr error) {
// successful status code range
if r.StatusCode >= 200 && r.StatusCode < 300 {
return nil, nil
}
err = &Error{}
if v := r.Header.Get("Retry-After"); v != "" {
if retry, perr := parseInt32(v); perr == nil {
err.RetryAfter = &retry
}
}
switch r.StatusCode {
case http.StatusTooManyRequests:
err.Code = ETooManyRequests
err.Message = "exceeded rate limit"
case http.StatusServiceUnavailable:
err.Code = EUnavailable
err.Message = "service temporarily unavailable"
default:
// json encoded error
typ, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type"))
if typ == "application/json" {
perr = json.NewDecoder(r.Body).Decode(err)
return
}
// plain error body type
var body []byte
body, perr = ioutil.ReadAll(r.Body)
if perr != nil {
return
}
err.Code = r.Status
err.Message = string(body)
}
return
}