-
Notifications
You must be signed in to change notification settings - Fork 52
/
output.go
151 lines (135 loc) · 3.79 KB
/
output.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
// © 2022 Nokia.
//
// This code is a Contribution to the gNMIc project (“Work”) made under the Google Software Grant and Corporate Contributor License Agreement (“CLA”) and governed by the Apache License 2.0.
// No other rights or licenses in or to any of Nokia’s intellectual property are granted for any other purpose.
// This code is provided on an “as is” basis without any warranties of any kind.
//
// SPDX-License-Identifier: Apache-2.0
package outputs
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"strings"
"text/template"
"github.com/mitchellh/mapstructure"
"github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/gnmic/formatters"
_ "github.com/openconfig/gnmic/formatters/all"
"github.com/openconfig/gnmic/types"
"github.com/openconfig/gnmic/utils"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/protobuf/proto"
)
type Output interface {
Init(context.Context, string, map[string]interface{}, ...Option) error
Write(context.Context, proto.Message, Meta)
WriteEvent(context.Context, *formatters.EventMsg)
Close() error
RegisterMetrics(*prometheus.Registry)
String() string
SetLogger(*log.Logger)
SetEventProcessors(map[string]map[string]interface{}, *log.Logger, map[string]*types.TargetConfig, map[string]map[string]interface{})
SetName(string)
SetClusterName(string)
SetTargetsConfig(map[string]*types.TargetConfig)
}
type Initializer func() Output
var Outputs = map[string]Initializer{}
var OutputTypes = map[string]struct{}{
"file": {},
"influxdb": {},
"kafka": {},
"nats": {},
"prometheus": {},
"prometheus_write": {},
"stan": {},
"tcp": {},
"udp": {},
"gnmi": {},
"jetstream": {},
}
func Register(name string, initFn Initializer) {
Outputs[name] = initFn
}
type Meta map[string]string
func DecodeConfig(src, dst interface{}) error {
decoder, err := mapstructure.NewDecoder(
&mapstructure.DecoderConfig{
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
Result: dst,
},
)
if err != nil {
return err
}
return decoder.Decode(src)
}
func AddSubscriptionTarget(msg proto.Message, meta Meta, addTarget string, tpl *template.Template) (*gnmi.SubscribeResponse, error) {
if addTarget == "" {
return nil, nil
}
msg = proto.Clone(msg)
switch trsp := msg.(type) {
case *gnmi.SubscribeResponse:
switch rrsp := trsp.Response.(type) {
case *gnmi.SubscribeResponse_Update:
if rrsp.Update.Prefix == nil {
rrsp.Update.Prefix = new(gnmi.Path)
}
switch addTarget {
case "overwrite":
sb := new(strings.Builder)
err := tpl.Execute(sb, meta)
if err != nil {
return nil, err
}
rrsp.Update.Prefix.Target = sb.String()
return trsp, nil
case "if-not-present":
if rrsp.Update.Prefix.Target == "" {
sb := new(strings.Builder)
err := tpl.Execute(sb, meta)
if err != nil {
return nil, err
}
rrsp.Update.Prefix.Target = sb.String()
}
return trsp, nil
}
}
}
return nil, nil
}
func ExecTemplate(content []byte, tpl *template.Template) ([]byte, error) {
var input interface{}
err := json.Unmarshal(content, &input)
if err != nil {
return nil, fmt.Errorf("failed to marshal input: %v", err)
}
bf := new(bytes.Buffer)
err = tpl.Execute(bf, input)
if err != nil {
return nil, fmt.Errorf("failed to execute msg template: %v", err)
}
return bf.Bytes(), nil
}
var (
DefaultTargetTemplate = template.Must(
template.New("target-template").
Funcs(TemplateFuncs).
Parse(defaultTargetTemplateString))
)
var TemplateFuncs = template.FuncMap{
"host": utils.GetHost,
}
const (
defaultTargetTemplateString = `
{{- if index . "subscription-target" -}}
{{ index . "subscription-target" }}
{{- else -}}
{{ index . "source" | host }}
{{- end -}}`
)