/
influx_service.go
53 lines (47 loc) · 1.03 KB
/
influx_service.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
package dispatchers
import (
"errors"
"fmt"
"net/http"
)
type InfluxService struct {
Host string
Database string
}
func NewInfluxService(host string, database string) *InfluxService {
return &InfluxService{
Host: host,
Database: database,
}
}
func (svc *InfluxService) CreateBatch() Batch {
return NewInfluxBatch()
}
func (svc *InfluxService) Send(batch Batch) error {
b := batch.(*InfluxBatch)
uri, err := influxUri(svc.Host, svc.Database)
if err != nil {
return err
}
// TODO: introduce a timeout in the request
resp, err := http.Post(uri, "application/x-www-form-urlencoded", &b.lines)
if err != nil {
return err
}
switch resp.StatusCode {
case 204:
return nil
default:
return errors.New(resp.Status)
}
}
func influxUri(host string, database string) (string, error) {
switch {
case host == "":
return "", errors.New("Influx host can not be empty")
case database == "":
return "", errors.New("Influx database can not be empty")
default:
return fmt.Sprintf("%s/write?db=%s", host, database), nil
}
}