-
Notifications
You must be signed in to change notification settings - Fork 3
/
nntbs.go
80 lines (65 loc) · 1.97 KB
/
nntbs.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
package gosnowth
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/circonus-labs/gosnowth/fb/nntbs"
flatbuffers "github.com/google/flatbuffers/go"
)
const metricSourceGraphite = 0x2
func nntMergeFileIdentifier() []byte {
return []byte("CINN")
}
// WriteNNTBSFlatbuffer writes flatbuffer format NNTBS data to an IRONdb node.
func (sc *SnowthClient) WriteNNTBSFlatbuffer(merge *nntbs.NNTMergeT,
builder *flatbuffers.Builder, nodes ...*SnowthNode,
) error {
return sc.WriteNNTBSFlatbufferContext(context.Background(), merge,
builder, nodes...)
}
// WriteNNTBSFlatbufferContext is the context aware version of
// WriteNNTBSFlatbuffer.
func (sc *SnowthClient) WriteNNTBSFlatbufferContext(ctx context.Context,
merge *nntbs.NNTMergeT, builder *flatbuffers.Builder,
nodes ...*SnowthNode,
) error {
if merge == nil {
return fmt.Errorf("NNTBS merge data must not be null")
}
var node *SnowthNode
if len(nodes) > 0 && nodes[0] != nil {
node = nodes[0]
} else if len(merge.Ops) > 0 {
node = sc.GetActiveNode(sc.FindMetricNodeIDs(
string(merge.Ops[0].Metric.MetricLocator.CheckUuid),
merge.Ops[0].Metric.MetricLocator.MetricName))
}
if node == nil {
return fmt.Errorf("unable to get active node")
}
if builder == nil {
builder = flatbuffers.NewBuilder(1024)
} else {
builder.Reset()
}
offset := nntbs.NNTMergePack(builder, merge)
builder.FinishWithFileIdentifier(offset, nntMergeFileIdentifier())
data := builder.FinishedBytes()
hdrs := http.Header{"Content-Type": {"application/snowth-nntbs"}}
body, _, err := sc.DoRequestContext(ctx, node, "POST", "/nntbs",
bytes.NewReader(data), hdrs)
if err != nil {
return err
}
res := &IRONdbPutResponse{}
if err := json.NewDecoder(body).Decode(res); err != nil {
return fmt.Errorf("unable to decode IRONdb response: %w", err)
}
if res.Errors != 0 || res.Misdirected != 0 || res.Records != 1 ||
res.Updated != 1 {
return fmt.Errorf("failed to write nntbs data: %v", res)
}
return nil
}