/
gnmi.go
347 lines (282 loc) · 7.55 KB
/
gnmi.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//: Copyright Verizon Media
//: Licensed under the terms of the Apache 2.0 License. See LICENSE file in the project root for terms.
package gnmi
import (
"bytes"
"context"
"encoding/json"
"fmt"
"math"
"net"
"strings"
"time"
"github.com/golang/protobuf/ptypes"
gpb "github.com/openconfig/gnmi/proto/gnmi"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/anypb"
"github.com/yahoo/panoptes-stream/config"
"github.com/yahoo/panoptes-stream/status"
"github.com/yahoo/panoptes-stream/telemetry"
"github.com/yahoo/panoptes-stream/telemetry/juniper/proto/GnmiJuniperTelemetryHeader"
)
var gnmiVersion = "0.7.0"
// GNMI represents a GNMI Juniper.
type GNMI struct {
conn *grpc.ClientConn
subscriptions []*gpb.Subscription
dataChan chan *gpb.SubscribeResponse
outChan telemetry.ExtDSChan
logger *zap.Logger
metrics map[string]status.Metrics
pathOutput map[string]string
}
// New creates a GNMI.
func New(logger *zap.Logger, conn *grpc.ClientConn, sensors []*config.Sensor, outChan telemetry.ExtDSChan) telemetry.NMI {
var metrics = make(map[string]status.Metrics)
metrics["gRPCDataTotal"] = status.NewCounter("juniper_gnmi_grpc_data_total", "")
metrics["dropsTotal"] = status.NewCounter("juniper_gnmi_drops_total", "")
metrics["errorsTotal"] = status.NewCounter("juniper_gnmi_errors_total", "")
metrics["processNSecond"] = status.NewGauge("juniper_gnmi_process_nanosecond", "")
status.Register(status.Labels{"host": conn.Target()}, metrics)
return &GNMI{
logger: logger,
conn: conn,
subscriptions: telemetry.GetGNMISubscriptions(sensors),
pathOutput: telemetry.GetPathOutput(sensors),
dataChan: make(chan *gpb.SubscribeResponse, 100),
outChan: outChan,
metrics: metrics,
}
}
// Start starts to get stream and fan-out to workers
func (g *GNMI) Start(ctx context.Context) error {
defer status.Unregister(status.Labels{"host": g.conn.Target()}, g.metrics)
client := gpb.NewGNMIClient(g.conn)
subReq := &gpb.SubscribeRequest{
Request: &gpb.SubscribeRequest_Subscribe{
Subscribe: &gpb.SubscriptionList{
Mode: gpb.SubscriptionList_STREAM,
Encoding: gpb.Encoding(gpb.Encoding_value["PROTO"]),
Subscription: g.subscriptions,
UpdatesOnly: false,
},
},
}
subClient, err := client.Subscribe(ctx)
if err != nil {
return err
}
err = subClient.Send(subReq)
if err != nil {
return err
}
workers := config.GetEnvInt("JUNIPER_GNMI_WORKERS", 1)
for i := 0; i < workers; i++ {
go g.worker(ctx)
}
for {
resp, err := subClient.Recv()
if err != nil && ctx.Err() == nil {
return err
}
if ctx.Err() != nil {
return nil
}
g.dataChan <- resp
g.metrics["gRPCDataTotal"].Inc()
}
}
func (g *GNMI) worker(ctx context.Context) {
var (
start time.Time
buf = new(bytes.Buffer)
systemID, _, _ = net.SplitHostPort(g.conn.Target())
)
for {
select {
case d, ok := <-g.dataChan:
if !ok {
return
}
start = time.Now()
resp, ok := d.Response.(*gpb.SubscribeResponse_Update)
if !ok {
continue
}
if err := g.datastore(buf, resp, systemID); err != nil {
g.logger.Error("juniper.gnmi", zap.Error(err))
}
buf.Reset()
g.metrics["processNSecond"].Set(uint64(time.Since(start).Nanoseconds()))
case <-ctx.Done():
return
}
}
}
func (g *GNMI) datastore(buf *bytes.Buffer, resp *gpb.SubscribeResponse_Update, systemID string) error {
var (
path, output string
timestamp interface{}
labels map[string]string
ok bool
)
prefix, prefixLabels := getPrefix(buf, resp.Update.Prefix.Elem)
for _, update := range resp.Update.Update {
buf.Reset()
key, keyLabels := getKey(buf, update.Path.Elem)
value, err := getValue(update.Val)
if err != nil {
continue
}
if key == "__juniper_telemetry_header__" {
path, err = getPath(value)
if err != nil {
return err
}
continue
}
if key == "__timestamp__" {
timestamp = resp.Update.GetTimestamp()
continue
}
if strings.HasPrefix(key, "__") {
continue
}
if output == "" && path != "" {
output, ok = g.pathOutput[path]
if !ok {
return fmt.Errorf("out not found - %s", path)
}
}
labels = telemetry.MergeLabels(keyLabels, prefixLabels, prefix)
dataStore := telemetry.DataStore{
"prefix": prefix,
"labels": labels,
"timestamp": timestamp,
"system_id": systemID,
"key": key,
"value": value,
}
select {
case g.outChan <- telemetry.ExtDataStore{
DS: dataStore,
Output: output,
}:
default:
g.metrics["dropsTotal"].Inc()
g.logger.Warn("juniper.gnmi", zap.String("error", "dataset drop"))
}
}
return nil
}
func getPrefix(buf *bytes.Buffer, path []*gpb.PathElem) (string, map[string]string) {
labels := make(map[string]string)
for _, elem := range path {
if len(elem.Name) > 0 {
buf.WriteRune('/')
buf.WriteString(elem.Name)
}
for key, value := range elem.Key {
labels[key] = value
}
}
return buf.String(), labels
}
func getKey(buf *bytes.Buffer, path []*gpb.PathElem) (string, map[string]string) {
labels := make(map[string]string)
for _, elem := range path {
if len(elem.Name) > 0 {
buf.WriteRune('/')
buf.WriteString(elem.Name)
}
for key, value := range elem.Key {
labels[key] = value
}
}
buf.ReadRune()
return buf.String(), labels
}
func getPath(value interface{}) (string, error) {
var path string
h, ok := value.(GnmiJuniperTelemetryHeader.GnmiJuniperTelemetryHeader)
if ok {
p := strings.Split(h.Path, ":")
if len(p) > 1 {
path = p[1]
}
}
if path == "" {
return path, fmt.Errorf("invalid juniper telemetry header")
}
return path, nil
}
func getValue(tv *gpb.TypedValue) (interface{}, error) {
var (
jsondata []byte
value interface{}
err error
)
switch tv.Value.(type) {
case *gpb.TypedValue_AsciiVal:
value = tv.GetAsciiVal()
case *gpb.TypedValue_BoolVal:
value = tv.GetBoolVal()
case *gpb.TypedValue_BytesVal:
value = tv.GetBytesVal()
case *gpb.TypedValue_DecimalVal:
value = float64(tv.GetDecimalVal().Digits) / math.Pow(10, float64(tv.GetDecimalVal().Precision))
case *gpb.TypedValue_FloatVal:
value = tv.GetFloatVal()
case *gpb.TypedValue_IntVal:
value = tv.GetIntVal()
case *gpb.TypedValue_StringVal:
value = tv.GetStringVal()
case *gpb.TypedValue_UintVal:
value = tv.GetUintVal()
case *gpb.TypedValue_JsonIetfVal:
jsondata = tv.GetJsonIetfVal()
case *gpb.TypedValue_JsonVal:
jsondata = tv.GetJsonVal()
case *gpb.TypedValue_AnyVal:
value, err = getAnyVal(tv.GetAnyVal())
case *gpb.TypedValue_LeaflistVal:
elems := tv.GetLeaflistVal().GetElement()
value, err = getLeafList(elems)
default:
err = fmt.Errorf("unknown value type %+v", tv.Value)
}
if jsondata != nil {
err = json.Unmarshal(jsondata, &value)
}
return value, err
}
func getLeafList(elems []*gpb.TypedValue) (interface{}, error) {
list := []interface{}{}
for _, v := range elems {
ev, err := getValue(v)
if err != nil {
return nil, fmt.Errorf("leaflist error: %v", err)
}
list = append(list, ev)
}
return list, nil
}
func getAnyVal(anyMsg *anypb.Any) (interface{}, error) {
anyMsgName, err := ptypes.AnyMessageName(anyMsg)
if err != nil {
return nil, fmt.Errorf("proto any message: %v", err)
}
if anyMsgName == "GnmiJuniperTelemetryHeader" {
hdr := GnmiJuniperTelemetryHeader.GnmiJuniperTelemetryHeader{}
if err := ptypes.UnmarshalAny(anyMsg, &hdr); err != nil {
return nil, err
}
return hdr, nil
}
return nil, fmt.Errorf("proto any unknown msg type")
}
// Version returns version
func Version() string {
return gnmiVersion
}