/
client.go
164 lines (140 loc) · 4.49 KB
/
client.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package metricemitter
import (
"context"
"time"
"code.cloudfoundry.org/go-loggregator/v9/rpc/loggregator_v2"
"google.golang.org/grpc"
)
// Client is used to initialize and emit metrics on a given pulse interval.
type Client struct {
ingressClient loggregator_v2.IngressClient
pulseInterval time.Duration
dialOpts []grpc.DialOption
sourceID string
tags map[string]string
}
type sendable interface {
WithEnvelope(func(*loggregator_v2.Envelope) error) error
}
// ClientOption is a function that can be passed into the NewClient for
// optional configuration on the client.
type ClientOption func(*Client)
// WithGRPCDialOptions is a ClientOption that will set the gRPC dial options on
// the Clients IngressClient.
func WithGRPCDialOptions(opts ...grpc.DialOption) ClientOption {
return func(c *Client) {
c.dialOpts = opts
}
}
// WithPulseInterval is a ClientOption will set the rate at which each metric
// will be sent to the IngressClient.
func WithPulseInterval(d time.Duration) ClientOption {
return func(c *Client) {
c.pulseInterval = d
}
}
// WithSourceID is a ClientOption that will set the SourceID to be set on
// every envelope sent to the IngressClient.
func WithSourceID(s string) ClientOption {
return func(c *Client) {
c.sourceID = s
}
}
// WithOrigin is a ClientOption that will set an origin tag to be added
// to every envelope sent to the IngressClient.
func WithOrigin(name string) ClientOption {
return func(c *Client) {
c.tags["origin"] = name
}
}
// WithDeployment is a ClientOption that will set a deployment, job and index
// tab on every envelope sent to the IngressClient.
func WithDeployment(deployment, job, index string) ClientOption {
return func(c *Client) {
c.tags["deployment"] = deployment
c.tags["job"] = job
c.tags["index"] = index
}
}
// NewClient initializes a new Client and opens a gRPC connection to the given
// address.
func NewClient(addr string, opts ...ClientOption) (*Client, error) {
client := &Client{
tags: make(map[string]string),
pulseInterval: 5 * time.Second,
}
for _, opt := range opts {
opt(client)
}
conn, err := grpc.NewClient(addr, client.dialOpts...)
if err != nil {
return nil, err
}
client.ingressClient = loggregator_v2.NewIngressClient(conn)
return client, nil
}
// NewCounter will return a new Counter metric that can be incremented. The
// value of the counter will be sent to the Clients IngressClient at the
// interval configured on the Client. When the counters value is sent to the
// IngressClient the value is reset to 0.
func (c *Client) NewCounter(name string, opts ...MetricOption) *Counter {
opts = append(opts, WithTags(c.tags))
m := NewCounter(name, c.sourceID, opts...)
go c.pulse(m)
return m
}
// NewGauge will return a new Gauge metric that has a value that can be set.
// The value of the gauge will be sent to the Clients IngressClient at the
// interval configured on the Client.
func (c *Client) NewGauge(name, unit string, opts ...MetricOption) *Gauge {
opts = append(opts, WithTags(c.tags))
m := NewGauge(name, unit, c.sourceID, opts...)
go c.pulse(m)
return m
}
// EmitEvent will emit an event as an asynchrounous metric.
// NOTE: Currently, due to the fact that loggregator only accepts envelopes
// via streaming, we are going to ignore the errors. Streams do not give
// proper feedback for errors due to batching. When/if loggregator accepts
// envelopes with a normal RPC call, we will be able to do something with
// an error.
func (c *Client) EmitEvent(title, body string) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
senderClient, err := c.ingressClient.Sender(ctx)
if err != nil {
return
}
defer senderClient.CloseAndRecv() //nolint:errcheck
err = senderClient.Send(&loggregator_v2.Envelope{
Timestamp: time.Now().UnixNano(),
SourceId: c.sourceID,
Message: &loggregator_v2.Envelope_Event{
Event: &loggregator_v2.Event{
Title: title,
Body: body,
},
},
})
// TODO: Handle error when non-streaming endpoint is available.
_ = err
}
func (c *Client) pulse(s sendable) {
var senderClient loggregator_v2.Ingress_SenderClient
for range time.Tick(c.pulseInterval) {
if senderClient == nil {
var err error
senderClient, err = c.ingressClient.Sender(context.Background())
if err != nil {
continue
}
}
err := s.WithEnvelope(func(env *loggregator_v2.Envelope) error {
return senderClient.Send(env)
})
if err != nil {
senderClient.CloseAndRecv() //nolint:errcheck
senderClient = nil
}
}
}