forked from streamingfast/dmetering
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metering.go
86 lines (70 loc) · 1.81 KB
/
metering.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
package dmetering
import (
"context"
"fmt"
"net/url"
"time"
pbmetering "github.com/streamingfast/dmetering/pb/sf/metering/v1"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/protobuf/types/known/timestamppb"
)
type Event struct {
Endpoint string `json:"endpoint"`
Metrics map[string]float64 `json:"metrics,omitempty"`
UserID string `json:"user_id"`
ApiKeyID string `json:"api_key_id"`
IpAddress string `json:"ip_address"`
Meta string `json:"meta"`
Timestamp time.Time `json:"timestamp"`
}
func (ev Event) MarshalLogObject(enc zapcore.ObjectEncoder) error {
if ev.UserID != "" {
enc.AddString("user_id", ev.UserID)
}
if ev.ApiKeyID != "" {
enc.AddString("api_key_id", ev.ApiKeyID)
}
if ev.IpAddress != "" {
enc.AddString("ip_address", ev.IpAddress)
}
enc.AddString("endpoint", ev.Endpoint)
enc.AddTime("timestamp", ev.Timestamp)
for k, v := range ev.Metrics {
enc.AddFloat64(k, v)
}
return nil
}
func (ev Event) ToProto(network string) *pbmetering.Event {
pbev := new(pbmetering.Event)
pbev.Endpoint = ev.Endpoint
pbev.Network = network
pbev.Timestamp = timestamppb.New(ev.Timestamp)
pbev.UserId = ev.UserID
pbev.ApiKeyId = ev.ApiKeyID
pbev.IpAddress = ev.IpAddress
pbev.Meta = ev.Meta
pbev.Metrics = []*pbmetering.Metric{}
for k, v := range ev.Metrics {
pbev.Metrics = append(pbev.Metrics, &pbmetering.Metric{
Key: k,
Value: v,
})
}
return pbev
}
type EventEmitter interface {
Shutdown(error)
Emit(ctx context.Context, ev Event)
}
func New(config string, logger *zap.Logger) (EventEmitter, error) {
u, err := url.Parse(config)
if err != nil {
return nil, err
}
factory := registry[u.Scheme]
if factory == nil {
panic(fmt.Sprintf("no Metering plugin named \"%s\" is currently registered.", u.Scheme))
}
return factory(config, logger)
}