-
Notifications
You must be signed in to change notification settings - Fork 395
/
Copy patheventmetrics.go
146 lines (129 loc) · 4.94 KB
/
eventmetrics.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon
package eventmetrics
import (
"slices"
"github.com/cilium/tetragon/api/v1/tetragon"
"github.com/cilium/tetragon/api/v1/tetragon/codegen/helpers"
"github.com/cilium/tetragon/pkg/api/processapi"
"github.com/cilium/tetragon/pkg/filters"
"github.com/cilium/tetragon/pkg/logger"
"github.com/cilium/tetragon/pkg/metrics"
"github.com/cilium/tetragon/pkg/metrics/consts"
"github.com/cilium/tetragon/pkg/metrics/errormetrics"
"github.com/cilium/tetragon/pkg/metrics/syscallmetrics"
v1 "github.com/cilium/tetragon/pkg/oldhubble/api/v1"
"github.com/cilium/tetragon/pkg/reader/exec"
"github.com/cilium/tetragon/pkg/tracingpolicy"
"github.com/prometheus/client_golang/prometheus"
)
var (
EventsProcessed = metrics.MustNewGranularCounter(prometheus.CounterOpts{
Namespace: consts.MetricsNamespace,
Name: "events_total",
Help: "The total number of Tetragon events",
ConstLabels: nil,
}, []string{"type"})
MissedEvents = metrics.NewBPFCounter(prometheus.NewDesc(
prometheus.BuildFQName(consts.MetricsNamespace, "", "missed_events_total"),
"The total number of Tetragon events per type that are failed to sent from the kernel.",
[]string{"msg_op"}, nil,
))
FlagCount = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: consts.MetricsNamespace,
Name: "flags_total",
Help: "The total number of Tetragon flags. For internal use only.",
ConstLabels: nil,
}, []string{"type"})
NotifyOverflowedEvents = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: consts.MetricsNamespace,
Name: "notify_overflowed_events_total",
Help: "The total number of events dropped because listener buffer was full",
ConstLabels: nil,
})
policyStats = metrics.MustNewGranularCounter(prometheus.CounterOpts{
Namespace: consts.MetricsNamespace,
Name: "policy_events_total",
Help: "Policy events calls observed.",
ConstLabels: nil,
}, []string{"policy", "hook"})
)
func InitHealthMetrics(registry *prometheus.Registry) {
registry.MustRegister(FlagCount)
registry.MustRegister(NotifyOverflowedEvents)
// custom collectors are registered independently
// Initialize metrics with labels
for _, v := range exec.FlagStrings {
FlagCount.WithLabelValues(v).Add(0)
}
// NOTES:
// * op, msg_op, opcode - standardize on a label (+ add human-readable label)
// * event, event_type, type - standardize on a label
}
func InitEventsMetrics(registry *prometheus.Registry) {
registry.MustRegister(EventsProcessed.ToProm())
registry.MustRegister(policyStats.ToProm())
}
func InitEventsMetricsForDocs(registry *prometheus.Registry) {
InitEventsMetrics(registry)
// Initialize metrics with example labels
for ev, evString := range tetragon.EventType_name {
if tetragon.EventType(ev) != tetragon.EventType_UNDEF && tetragon.EventType(ev) != tetragon.EventType_TEST {
EventsProcessed.WithLabelValues(slices.Concat([]string{evString}, consts.ExampleProcessLabels)...).Add(0)
}
}
policyStats.WithLabelValues(slices.Concat([]string{consts.ExamplePolicyLabel, consts.ExampleKprobeLabel}, consts.ExampleProcessLabels)...).Add(0)
}
func GetProcessInfo(process *tetragon.Process) (binary, pod, workload, namespace string) {
if process != nil {
binary = process.Binary
if process.Pod != nil {
namespace = process.Pod.Namespace
workload = process.Pod.Workload
pod = process.Pod.Name
}
} else {
errormetrics.ErrorTotalInc(errormetrics.EventMissingProcessInfo)
}
return binary, pod, workload, namespace
}
func handleOriginalEvent(originalEvent interface{}) {
var flags uint32
switch msg := originalEvent.(type) {
case *processapi.MsgExecveEventUnix:
flags = msg.Process.Flags
}
for _, flag := range exec.DecodeCommonFlags(flags) {
FlagCount.WithLabelValues(flag).Inc()
}
}
func handleProcessedEvent(pInfo *tracingpolicy.PolicyInfo, processedEvent interface{}) {
var eventType, namespace, workload, pod, binary string
switch ev := processedEvent.(type) {
case *tetragon.GetEventsResponse:
binary, pod, workload, namespace = GetProcessInfo(filters.GetProcess(&v1.Event{Event: ev}))
var err error
eventType, err = helpers.ResponseTypeString(ev)
if err != nil {
logger.GetLogger().WithField("event", processedEvent).WithError(err).Warn("metrics: handleProcessedEvent: unhandled event")
eventType = "unhandled"
}
default:
eventType = "unknown"
}
EventsProcessed.WithLabelValues(eventType, namespace, workload, pod, binary).Inc()
if pInfo != nil && pInfo.Name != "" {
policyStats.
WithLabelValues(pInfo.Name, pInfo.Hook, namespace, workload, pod, binary).
Inc()
}
}
func ProcessEvent(originalEvent interface{}, processedEvent interface{}) {
handleOriginalEvent(originalEvent)
var policyInfo tracingpolicy.PolicyInfo
if policyEv, ok := originalEvent.(tracingpolicy.PolicyEvent); ok {
policyInfo = policyEv.PolicyInfo()
}
handleProcessedEvent(&policyInfo, processedEvent)
syscallmetrics.Handle(processedEvent)
}