-
Notifications
You must be signed in to change notification settings - Fork 51
/
processor.go
169 lines (157 loc) · 4.66 KB
/
processor.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
package app
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/AlekSi/pointer"
"github.com/openconfig/gnmic/pkg/config"
"github.com/openconfig/gnmic/pkg/file"
"github.com/openconfig/gnmic/pkg/formatters"
promcom "github.com/openconfig/gnmic/pkg/outputs/prometheus_output"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
"github.com/spf13/cobra"
)
func (a *App) ProcessorPreRunE(cmd *cobra.Command, args []string) error {
a.Config.SetLocalFlagsFromFile(cmd)
err := a.initPluginManager()
if err != nil {
return err
}
return nil
}
func (a *App) ProcessorRunE(cmd *cobra.Command, args []string) error {
actionsConfig, err := a.Config.GetActions()
if err != nil {
return fmt.Errorf("failed reading actions config: %v", err)
}
pConfig, err := a.Config.GetEventProcessors()
if err != nil {
return fmt.Errorf("failed reading event processors config: %v", err)
}
tcs, err := a.Config.GetTargets()
if err != nil {
if !errors.Is(err, config.ErrNoTargetsFound) {
return err
}
}
// initialize processors
evps, err := formatters.MakeEventProcessors(
a.Logger,
a.Config.LocalFlags.ProcessorName,
pConfig,
tcs,
actionsConfig,
)
if err != nil {
return err
}
// read input file
inputBytes, err := file.ReadFile(cmd.Context(), a.Config.LocalFlags.ProcessorInput)
if err != nil {
return err
}
evInput := make([][]*formatters.EventMsg, 0)
msgs := bytes.Split(inputBytes, []byte(a.Config.LocalFlags.ProcessorInputDelimiter))
for i, bg := range msgs {
if len(bg) == 0 {
continue
}
mevs := make([]map[string]any, 0)
err = json.Unmarshal(bg, &mevs)
if err != nil {
return fmt.Errorf("failed json Unmarshal at msg index %d: %s: %v", i, bg, err)
}
evs := make([]*formatters.EventMsg, 0, len(mevs))
for _, mev := range mevs {
ev, err := formatters.EventFromMap(mev)
if err != nil {
return err
}
evs = append(evs, ev)
}
evInput = append(evInput, evs)
}
rrevs := make([][]*formatters.EventMsg, 0, len(evInput))
for _, evs := range evInput {
revs := evs
for _, p := range evps {
revs = p.Apply(revs...)
}
rrevs = append(rrevs, revs)
}
if len(a.Config.LocalFlags.ProcessorOutput) != 0 {
b, err := a.promFormat(rrevs, a.Config.LocalFlags.ProcessorOutput)
if err != nil {
return err
}
fmt.Println(string(b))
return nil
}
numEvOut := len(rrevs)
for i, rev := range rrevs {
b, err := json.MarshalIndent(rev, "", " ")
if err != nil {
return err
}
fmt.Println(string(b))
if i == numEvOut-1 {
break
}
}
return nil
}
func (a *App) InitProcessorFlags(cmd *cobra.Command) {
cmd.ResetFlags()
cmd.Flags().StringVarP(&a.Config.LocalFlags.ProcessorInput, "input", "", "", "processors input")
cmd.MarkFlagRequired("input")
cmd.Flags().StringVarP(&a.Config.LocalFlags.ProcessorInputDelimiter, "delimiter", "", "\n", "processors input delimiter")
cmd.Flags().StringSliceVarP(&a.Config.LocalFlags.ProcessorName, "name", "", nil, "list of processors to apply to the input")
cmd.MarkFlagRequired("name")
cmd.Flags().StringVarP(&a.Config.LocalFlags.ProcessorOutput, "output", "", "", "output name")
}
func (a *App) promFormat(rrevs [][]*formatters.EventMsg, outName string) ([]byte, error) {
// read output config
outputPath := "outputs/" + outName
outputConfig := a.Config.FileConfig.GetStringMap(outputPath)
if outputConfig == nil {
return nil, fmt.Errorf("unknown output name: %s", outName)
}
outType := a.Config.FileConfig.GetString(outputPath + "/type")
if outType != "prometheus" && outType != "remote_write" {
return nil, fmt.Errorf("output %q must be of type 'prometheus' or 'remote_write'", outName)
}
mb := &promcom.MetricBuilder{
Prefix: a.Config.FileConfig.GetString(outputPath + "/metric-prefix"),
AppendSubscriptionName: a.Config.FileConfig.GetBool(outputPath + "/append-subscription-name"),
StringsAsLabels: a.Config.FileConfig.GetBool(outputPath + "/strings-as-labels"),
OverrideTimestamps: a.Config.FileConfig.GetBool(outputPath + "/override-timestamps"),
ExportTimestamps: a.Config.FileConfig.GetBool(outputPath + "/export-timestamps"),
}
b := new(bytes.Buffer)
now := time.Now()
for _, revs := range rrevs {
for _, ev := range revs {
pms := mb.MetricsFromEvent(ev, now)
for _, pm := range pms {
m := &dto.Metric{}
err := pm.Write(m)
if err != nil {
return nil, err
}
_, err = expfmt.MetricFamilyToText(b, &dto.MetricFamily{
Name: pointer.ToString(pm.Name),
Help: pointer.ToString("gNMIc generated metric"),
Type: dto.MetricType_UNTYPED.Enum(),
Metric: []*dto.Metric{m},
})
if err != nil {
return nil, err
}
}
}
}
return b.Bytes(), nil
}