-
Notifications
You must be signed in to change notification settings - Fork 14
/
utility_metric_bytes.go
71 lines (57 loc) · 1.7 KB
/
utility_metric_bytes.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
package transform
import (
"context"
"encoding/json"
"fmt"
"sync/atomic"
"github.com/brexhq/substation/config"
iconfig "github.com/brexhq/substation/internal/config"
"github.com/brexhq/substation/internal/metrics"
"github.com/brexhq/substation/message"
)
type utilityMetricBytesConfig struct {
Metric iconfig.Metric `json:"metric"`
}
func (c *utilityMetricBytesConfig) Decode(in interface{}) error {
return iconfig.Decode(in, c)
}
func newUtilityMetricBytes(ctx context.Context, cfg config.Config) (*utilityMetricBytes, error) {
// conf gets validated when calling metrics.New.
conf := utilityMetricBytesConfig{}
if err := conf.Decode(cfg.Settings); err != nil {
return nil, fmt.Errorf("transform: utility_metric_bytes: %v", err)
}
m, err := metrics.New(ctx, conf.Metric.Destination)
if err != nil {
return nil, fmt.Errorf("transform: utility_metric_bytes: %v", err)
}
tf := utilityMetricBytes{
conf: conf,
metric: m,
}
return &tf, nil
}
type utilityMetricBytes struct {
conf utilityMetricBytesConfig
metric metrics.Generator
bytes uint32
}
func (tf *utilityMetricBytes) Transform(ctx context.Context, msg *message.Message) ([]*message.Message, error) {
if msg.IsControl() {
if err := tf.metric.Generate(ctx, metrics.Data{
Name: tf.conf.Metric.Name,
Value: tf.bytes,
Attributes: tf.conf.Metric.Attributes,
}); err != nil {
return nil, fmt.Errorf("transform: utility_metric_bytes: %v", err)
}
atomic.StoreUint32(&tf.bytes, 0)
return []*message.Message{msg}, nil
}
atomic.AddUint32(&tf.bytes, uint32(len(msg.Data())))
return []*message.Message{msg}, nil
}
func (tf *utilityMetricBytes) String() string {
b, _ := json.Marshal(tf.conf)
return string(b)
}