-
Notifications
You must be signed in to change notification settings - Fork 7
/
writer.go
88 lines (73 loc) · 2.54 KB
/
writer.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
/*
Copyright 2022 Equinix, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package auditevent
import (
"encoding/json"
"io"
"github.com/prometheus/client_golang/prometheus"
"github.com/metal-toolbox/auditevent/metrics"
)
// EventEncoder allows for encoding audit events.
// The parameter to the `Encode` method is the audit event to encode
// and it must accept pointer to an AuditEvent struct.
type EventEncoder interface {
Encode(any) error
}
// EventWriter writes audit events to a writer using
// a given encoder.
type EventWriter struct {
enc EventEncoder
mts *metrics.PrometheusMetricsProvider
}
// AuditEventEncoderJSON is an encoder that encodes audit events
// using a default JSON encoder.
func NewDefaultAuditEventWriter(w io.Writer) *EventWriter {
enc := json.NewEncoder(w)
return NewAuditEventWriter(enc)
}
// NewAuditEventWriter is an encoder that encodes audit events
// using the given encoder.
func NewAuditEventWriter(enc EventEncoder) *EventWriter {
return &EventWriter{enc: enc, mts: nil}
}
// WithPrometheusMetricsForRegisterer adds prometheus metrics to this writer
// using the given prometheus registerer. It returns the writer itself for ease
// of use as the Builder pattern.
func (w *EventWriter) WithPrometheusMetricsForRegisterer(
component string,
pr prometheus.Registerer,
) *EventWriter {
w.mts = metrics.NewPrometheusMetricsProviderForRegisterer(component, pr)
return w
}
// WithPrometheusMetricsForRegisterer adds prometheus metrics to this writer
// using the default prometheus registerer (which is prometheus.DefaultRegisterer ).
// It returns the writer itself for ease of use as the Builder pattern.
func (w *EventWriter) WithPrometheusMetrics(component string) *EventWriter {
w.mts = metrics.NewPrometheusMetricsProvider(component)
return w
}
// Write writes an audit event to the writer.
func (w *EventWriter) Write(e *AuditEvent) error {
err := w.enc.Encode(e)
// We only increment the metrics if the
// provider is available and not nil
if w.mts != nil {
if err == nil {
w.mts.IncEvents()
} else {
w.mts.IncErrors()
}
}
return err
}