mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2025-08-14 22:53:01 +02:00
Add new httpSink
This sink is compatible with the HTTP API of cc-metric-store. Example config.json section: ``` "sink": { "type": "http", "host": "localhost", "port": "8080", "database": "api/write", "password": "<JWT>" }, ``` The password/JWT can be omitted.
This commit is contained in:
68
sinks/httpSink.go
Normal file
68
sinks/httpSink.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package sinks
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
lp "github.com/influxdata/line-protocol"
|
||||
)
|
||||
|
||||
type HttpSink struct {
|
||||
Sink
|
||||
client *http.Client
|
||||
url, jwt string
|
||||
encoder *lp.Encoder
|
||||
buffer *bytes.Buffer
|
||||
}
|
||||
|
||||
func (s *HttpSink) Init(config SinkConfig) error {
|
||||
if len(config.Host) == 0 || len(config.Port) == 0 {
|
||||
return errors.New("`host`, `port` and `database` config options required for TCP sink")
|
||||
}
|
||||
|
||||
s.client = &http.Client{}
|
||||
s.url = fmt.Sprintf("http://%s:%s/%s", config.Host, config.Port, config.Database)
|
||||
s.port = config.Port
|
||||
s.jwt = config.Password
|
||||
s.buffer = &bytes.Buffer{}
|
||||
s.encoder = lp.NewEncoder(s.buffer)
|
||||
s.encoder.SetPrecision(time.Second)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *HttpSink) Write(point lp.MutableMetric) error {
|
||||
_, err := s.encoder.Encode(point)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *HttpSink) Flush() error {
|
||||
req, err := http.NewRequest(http.MethodPost, s.url, s.buffer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(s.jwt) != 0 {
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", s.jwt))
|
||||
}
|
||||
|
||||
res, err := s.client.Do(req)
|
||||
s.buffer.Reset()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if res.StatusCode != 200 {
|
||||
return errors.New(res.Status)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *HttpSink) Close() {
|
||||
s.client.CloseIdleConnections()
|
||||
}
|
Reference in New Issue
Block a user