-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathopenmeter.go
More file actions
331 lines (261 loc) · 7.58 KB
/
openmeter.go
File metadata and controls
331 lines (261 loc) · 7.58 KB
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
package output
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/redpanda-data/benthos/v4/public/bloblang"
"github.com/redpanda-data/benthos/v4/public/service"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
openmeter "github.com/openmeterio/openmeter/api/client/go"
)
const (
urlField = "url"
tokenField = "token"
maxInFlightField = "max_in_flight"
batchingField = "batching"
tracingAttrsMapField = "tracing_attrs_map"
)
const (
otelName = "benthos-openmeter-output"
)
const defaultTracingAttrsMapField = `
root = {}
root.openmeter.event = this.with("id", "source", "subject")
`
func openmeterOutputConfig() *service.ConfigSpec {
return service.NewConfigSpec().
Beta().
Categories("Services").
Summary("Sends events the OpenMeter ingest API.").
Description("This output is used to send events to the OpenMeter ingest API.").
Fields(
service.NewURLField(urlField).
Description("OpenMeter API endpoint"),
service.NewStringField(tokenField).
Description("OpenMeter API token").
Secret().
Optional(),
service.NewBatchPolicyField(batchingField),
service.NewOutputMaxInFlightField().Default(10),
service.NewBloblangField(tracingAttrsMapField).
Description("An optional Bloblang mapping that can be defined in order to set attributes on tracing Span.").
Optional().
Advanced().
Default(defaultTracingAttrsMapField),
)
}
func init() {
err := service.RegisterBatchOutput("openmeter", openmeterOutputConfig(),
func(conf *service.ParsedConfig, mgr *service.Resources) (
output service.BatchOutput,
batchPolicy service.BatchPolicy,
maxInFlight int,
err error,
) {
if maxInFlight, err = conf.FieldInt(maxInFlightField); err != nil {
return output, batchPolicy, maxInFlight, err
}
if batchPolicy, err = conf.FieldBatchPolicy(batchingField); err != nil {
return output, batchPolicy, maxInFlight, err
}
output, err = newOpenMeterOutput(conf, mgr)
return output, batchPolicy, maxInFlight, err
})
if err != nil {
panic(err)
}
}
type openmeterOutput struct {
client openmeter.ClientWithResponsesInterface
tracingAttrsMap *bloblang.Executor
logger *service.Logger
tracer trace.Tracer
}
func newOpenMeterOutput(conf *service.ParsedConfig, mgr *service.Resources) (*openmeterOutput, error) {
o := &openmeterOutput{
logger: mgr.Logger(),
tracer: mgr.OtelTracer().Tracer(otelName),
}
url, err := conf.FieldString(urlField)
if err != nil {
return nil, err
}
// TODO: custom HTTP client
var client openmeter.ClientWithResponsesInterface
if conf.Contains(tokenField) {
token, err := conf.FieldString(tokenField)
if err != nil {
return nil, err
}
client, err = openmeter.NewAuthClientWithResponses(url, token)
if err != nil {
return nil, err
}
} else {
var err error
client, err = openmeter.NewClientWithResponses(url)
if err != nil {
return nil, err
}
}
o.client = client
if conf.Contains(tracingAttrsMapField) {
if o.tracingAttrsMap, err = conf.FieldBloblang(tracingAttrsMapField); err != nil {
return nil, err
}
}
return o, nil
}
func (o *openmeterOutput) Connect(_ context.Context) error {
return nil
}
// TODO: add schema validation
func (o *openmeterOutput) WriteBatch(ctx context.Context, batch service.MessageBatch) error {
// if there is only one message use the single message endpoint
// otherwise use the batch endpoint
// if validation is enabled, try to parse the message as cloudevents first
//
var err error
ctx, span := o.tracer.Start(ctx, "output_openmeter_write_batch")
defer func() {
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
} else {
span.SetStatus(codes.Ok, "batch write was successful")
}
span.End()
}()
o.logger.Debugf("received message batch [size=%d]", len(batch))
if len(batch) == 0 {
return nil
}
var events []any
walkFn := func(_ int, msg *service.Message) error {
if msg == nil {
o.logger.Error("received nil message in batch")
err = errors.New("received nil message in batch")
return err
}
var e any
e, err = msg.AsStructured()
if err != nil {
err = fmt.Errorf("failed to convert message to structed data: %w", err)
return err
}
events = append(events, e)
o.UpdateMessageSpan(ctx, msg)
return nil
}
if err = batch.WalkWithBatchedErrors(walkFn); err != nil {
return fmt.Errorf("failed to process event: %w", err)
}
if len(events) == 0 {
o.logger.Error("no valid messages found in batch")
err = errors.New("no valid messages found in batch")
return err
}
var body bytes.Buffer
err = json.NewEncoder(&body).Encode(events)
if err != nil {
return err
}
resp, err := o.client.IngestEventsWithBodyWithResponse(ctx, "application/json", &body)
if err != nil {
return err
}
span.SetAttributes(attribute.Int("openmeter.http.status_code", resp.StatusCode()))
if resp.StatusCode() >= 400 {
if resp.ApplicationproblemJSON400 != nil {
return resp.ApplicationproblemJSON400
}
if resp.ApplicationproblemJSON401 != nil {
return resp.ApplicationproblemJSON401
}
if resp.ApplicationproblemJSON403 != nil {
return resp.ApplicationproblemJSON403
}
if resp.ApplicationproblemJSON500 != nil {
return resp.ApplicationproblemJSON500
}
if resp.ApplicationproblemJSONDefault != nil {
return resp.ApplicationproblemJSONDefault
}
return errors.New(http.StatusText(resp.StatusCode()))
}
return nil
}
func (o *openmeterOutput) Close(_ context.Context) error {
return nil
}
func (o *openmeterOutput) UpdateMessageSpan(ctx context.Context, msg *service.Message) {
// Add reference of write_batch Span to message Span
msgSpan := trace.SpanFromContext(msg.Context())
if msgSpan == nil {
o.logger.Debug("no span found for message")
return
}
msgSpan.AddLink(trace.LinkFromContext(ctx))
// Enrich message Span with additional tracing information extracted from message itself
// Return early if mapping expression is not provided
if o.tracingAttrsMap != nil {
return
}
spanAttrsMsg, err := msg.BloblangQuery(o.tracingAttrsMap)
if err != nil {
o.logger.Debugf("failed to extract tracing attributes from message: %v", err)
return
}
var spanAttrsVal any
if spanAttrsMsg != nil {
if spanAttrsVal, err = spanAttrsMsg.AsStructured(); err != nil {
o.logger.Debugf("failed to construct structured tracing data from message: %v", err)
return
}
}
if spanAttrsVal == nil {
return
}
spanAttrMap, ok := spanAttrsVal.(map[string]interface{})
if !ok {
o.logger.Debugf("tracing attributes mapping resulted in a non-object mapping: %T", spanAttrsVal)
return
}
var spanAttrs []attribute.KeyValue
for k, v := range spanAttrMap {
attrs := toAttrs(k, v)
spanAttrs = append(spanAttrs, attrs...)
}
msgSpan.SetAttributes(spanAttrs...)
}
func toAttrs(prefix string, v interface{}) []attribute.KeyValue {
var attrs []attribute.KeyValue
switch value := v.(type) {
case map[string]interface{}:
for k, v := range value {
a := toAttrs(fmt.Sprintf("%s.%s", prefix, k), v)
attrs = append(attrs, a...)
}
case string:
attrs = append(attrs, attribute.String(prefix, value))
case fmt.Stringer:
attrs = append(attrs, attribute.Stringer(prefix, value))
case int:
attrs = append(attrs, attribute.Int(prefix, value))
case int64:
attrs = append(attrs, attribute.Int64(prefix, value))
case float32:
attrs = append(attrs, attribute.Float64(prefix, float64(value)))
case float64:
attrs = append(attrs, attribute.Float64(prefix, value))
case bool:
attrs = append(attrs, attribute.Bool(prefix, value))
default:
}
return attrs
}