forked from grafana/beyla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.go
459 lines (397 loc) · 14.5 KB
/
store.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package kube
import (
"log/slog"
"strings"
"sync"
"github.com/grafana/beyla/pkg/export/attributes"
"github.com/grafana/beyla/pkg/internal/helpers/container"
"github.com/grafana/beyla/pkg/internal/helpers/maps"
"github.com/grafana/beyla/pkg/kubecache/informer"
"github.com/grafana/beyla/pkg/kubecache/meta"
)
func dblog() *slog.Logger {
return slog.With("component", "kube.Store")
}
const (
serviceNameKey = "service.name"
serviceNamespaceKey = "service.namespace"
)
type OTelServiceNamePair struct {
Name string
Namespace string
}
type qualifiedName struct {
name string
namespace string
kind string
}
func qName(om *informer.ObjectMeta) qualifiedName {
return qualifiedName{name: om.Name, namespace: om.Namespace, kind: om.Kind}
}
// MetadataSources allow overriding some metadata from kubernetes labels and annotations
type MetadataSources struct {
ServiceNameAnnotations []string `yaml:"service_name_annotations" env:"BEYLA_KUBE_ANNOTATIONS_SERVICE_NAME" envSeparator:","`
ServiceNamespaceAnnotations []string `yaml:"service_namespace_annotations" env:"BEYLA_KUBE_ANNOTATIONS_SERVICE_NAMESPACE" envSeparator:","`
ServiceNameLabels []string `yaml:"service_name_labels" env:"BEYLA_KUBE_LABELS_SERVICE_NAME" envSeparator:","`
ServiceNamespaceLabels []string `yaml:"service_namespace_labels" env:"BEYLA_KUBE_LABELS_SERVICE_NAMESPACE" envSeparator:","`
}
var DefaultMetadataSources = MetadataSources{
ServiceNameAnnotations: []string{
"resource.opentelemetry.io/service.name",
},
ServiceNamespaceAnnotations: []string{
"resource.opentelemetry.io/service.namespace",
},
// empty by default. If a user sets useLabelsForResourceAttributes: true it its OTEL operator, also
// the values below should be populated so:
// - `app.kubernetes.io/name` becomes `service.name`
// - `app.kubernetes.io/part-of` becomes `service.namespace`
ServiceNameLabels: nil,
ServiceNamespaceLabels: nil,
}
// Store aggregates Kubernetes information from multiple sources:
// - the informer that keep an indexed copy of the existing pods and replicasets.
// - the inspected container.Info objects, indexed either by container ID and PID namespace
// - a cache of decorated PodInfo that would avoid reconstructing them on each trace decoration
type Store struct {
log *slog.Logger
access sync.RWMutex
metadataNotifier meta.Notifier
containerIDs map[string]*container.Info
// stores container info by PID. It is only required for
// deleting entries in namespaces and podsByContainer when DeleteProcess is called
containerByPID map[uint32]*container.Info
// a single namespace will point to any container inside the pod
// but we don't care which one
namespaces map[uint32]*container.Info
// container ID to pod matcher
podsByContainer map[string]*informer.ObjectMeta
// first key: pod owner ID, second key: container ID
containersByOwner maps.Map2[string, string, *informer.ContainerInfo]
// ip to generic IP info (Node, Service, *including* Pods)
objectMetaByIP map[string]*informer.ObjectMeta
// used to track the changed/removed IPs of a given object
// and remove them from objectMetaByIP on update or deletion
objectMetaByQName map[qualifiedName]*informer.ObjectMeta
otelServiceInfoByIP map[string]OTelServiceNamePair
// Instead of subscribing to the informer directly, the rest of components
// will subscribe to this store, to make sure that any "new object" notification
// they receive is already present in the store
meta.BaseNotifier
metadataSources MetadataSources
}
func NewStore(kubeMetadata meta.Notifier, metadataSources MetadataSources) *Store {
log := dblog()
db := &Store{
log: log,
containerIDs: map[string]*container.Info{},
namespaces: map[uint32]*container.Info{},
podsByContainer: map[string]*informer.ObjectMeta{},
containerByPID: map[uint32]*container.Info{},
objectMetaByIP: map[string]*informer.ObjectMeta{},
objectMetaByQName: map[qualifiedName]*informer.ObjectMeta{},
containersByOwner: maps.Map2[string, string, *informer.ContainerInfo]{},
otelServiceInfoByIP: map[string]OTelServiceNamePair{},
metadataNotifier: kubeMetadata,
BaseNotifier: meta.NewBaseNotifier(log),
metadataSources: metadataSources,
}
kubeMetadata.Subscribe(db)
return db
}
func (s *Store) ID() string { return "unique-metadata-observer" }
// On is invoked by the informer when a new Kube object is created, updated or deleted.
// It will forward the notification to all the Store subscribers
func (s *Store) On(event *informer.Event) error {
switch event.Type {
case informer.EventType_CREATED:
s.addObjectMeta(event.Resource)
case informer.EventType_UPDATED:
s.updateObjectMeta(event.Resource)
case informer.EventType_DELETED:
s.deleteObjectMeta(event.Resource)
}
s.BaseNotifier.Notify(event)
return nil
}
// InfoForPID is an injectable dependency for system-independent testing
var InfoForPID = container.InfoForPID
func (s *Store) AddProcess(pid uint32) {
ifp, err := InfoForPID(pid)
if err != nil {
s.log.Debug("failing to get container information", "pid", pid, "error", err)
return
}
s.log.Debug("Adding containerID for process", "pid", pid, "containerID", ifp.ContainerID, "pidNs", ifp.PIDNamespace)
s.access.Lock()
defer s.access.Unlock()
s.namespaces[ifp.PIDNamespace] = &ifp
s.containerIDs[ifp.ContainerID] = &ifp
s.containerByPID[pid] = &ifp
}
func (s *Store) DeleteProcess(pid uint32) {
s.access.Lock()
defer s.access.Unlock()
info, ok := s.containerByPID[pid]
if !ok {
return
}
delete(s.containerByPID, pid)
delete(s.namespaces, info.PIDNamespace)
delete(s.containerIDs, info.ContainerID)
}
func (s *Store) addObjectMeta(meta *informer.ObjectMeta) {
s.access.Lock()
defer s.access.Unlock()
s.unlockedAddObjectMeta(meta)
}
func (s *Store) updateObjectMeta(meta *informer.ObjectMeta) {
s.access.Lock()
defer s.access.Unlock()
// atomically remove the previously stored version of the updated object
// then re-adding it
// this will avoid to leak some IPs and containers that exist in the
// stored snapshot but not in the updated snapshot
if previousObject, ok := s.objectMetaByQName[qName(meta)]; ok {
s.unlockedDeleteObjectMeta(previousObject)
}
s.unlockedAddObjectMeta(meta)
}
// it's important to make sure that any element added here is removed when
// calling unlockedDeleteObjectMeta with the same ObjectMeta
func (s *Store) unlockedAddObjectMeta(meta *informer.ObjectMeta) {
qn := qName(meta)
s.objectMetaByQName[qn] = meta
for _, ip := range meta.Ips {
s.objectMetaByIP[ip] = meta
}
s.otelServiceInfoByIP = map[string]OTelServiceNamePair{}
if meta.Pod != nil {
oID := fetchOwnerID(meta)
s.log.Debug("adding pod to store",
"ips", meta.Ips, "pod", meta.Name, "namespace", meta.Namespace, "containers", meta.Pod.Containers)
for _, c := range meta.Pod.Containers {
s.podsByContainer[c.Id] = meta
// TODO: make sure we can handle when the containerIDs is set after this function is triggered
info, ok := s.containerIDs[c.Id]
if ok {
s.namespaces[info.PIDNamespace] = info
}
s.containersByOwner.Put(oID, c.Id, c)
}
}
}
func (s *Store) deleteObjectMeta(meta *informer.ObjectMeta) {
s.access.Lock()
defer s.access.Unlock()
// clean up the IP to service cache, we have to clean everything since
// Otel variables on specific pods can change the outcome.
s.otelServiceInfoByIP = map[string]OTelServiceNamePair{}
// cleanup both the objectMeta information from the received event
// as well as from any previous snapshot in the system whose IPs and/or
// containers could have been removed in the last snapshot
if previousObject, ok := s.objectMetaByQName[qName(meta)]; ok {
s.unlockedDeleteObjectMeta(previousObject)
}
s.unlockedDeleteObjectMeta(meta)
}
func (s *Store) unlockedDeleteObjectMeta(meta *informer.ObjectMeta) {
delete(s.objectMetaByQName, qName(meta))
for _, ip := range meta.Ips {
delete(s.objectMetaByIP, ip)
}
if meta.Pod != nil {
oID := fetchOwnerID(meta)
s.log.Debug("deleting pod from store",
"ips", meta.Ips, "pod", meta.Name, "namespace", meta.Namespace, "containers", meta.Pod.Containers)
for _, c := range meta.Pod.Containers {
info, ok := s.containerIDs[c.Id]
if ok {
delete(s.containerIDs, c.Id)
delete(s.namespaces, info.PIDNamespace)
}
delete(s.podsByContainer, c.Id)
s.containersByOwner.Delete(oID, c.Id)
}
}
}
func fetchOwnerID(meta *informer.ObjectMeta) string {
ownerName := meta.Name
if owner := TopOwner(meta.Pod); owner != nil {
ownerName = owner.Name
}
oID := ownerID(meta.Namespace, ownerName)
return oID
}
func (s *Store) PodByContainerID(cid string) *informer.ObjectMeta {
s.access.RLock()
defer s.access.RUnlock()
return s.podsByContainer[cid]
}
// PodContainerByPIDNs second return value: container Name
func (s *Store) PodContainerByPIDNs(pidns uint32) (*informer.ObjectMeta, string) {
s.access.RLock()
defer s.access.RUnlock()
if info, ok := s.namespaces[pidns]; ok {
if om, ok := s.podsByContainer[info.ContainerID]; ok {
oID := fetchOwnerID(om)
containerName := ""
if containerInfo, ok := s.containersByOwner.Get(oID, info.ContainerID); ok {
containerName = containerInfo.Name
}
return om, containerName
}
}
return nil, ""
}
func (s *Store) ObjectMetaByIP(ip string) *informer.ObjectMeta {
s.access.RLock()
defer s.access.RUnlock()
return s.objectMetaByIP[ip]
}
func (s *Store) ServiceNameNamespaceForMetadata(om *informer.ObjectMeta) (string, string) {
s.access.RLock()
defer s.access.RUnlock()
return s.serviceNameNamespaceForMetadata(om)
}
func (s *Store) serviceNameNamespaceForMetadata(om *informer.ObjectMeta) (string, string) {
var name string
var namespace string
if owner := TopOwner(om.Pod); owner != nil {
name, namespace = s.serviceNameNamespaceForPod(om, owner)
} else {
name, namespace = s.serviceNameNamespaceForOwner(om)
}
if nameFromMeta := s.valueFromMetadata(om,
s.metadataSources.ServiceNameAnnotations,
s.metadataSources.ServiceNameLabels,
); nameFromMeta != "" {
name = nameFromMeta
}
if nsFromMeta := s.valueFromMetadata(om,
s.metadataSources.ServiceNamespaceAnnotations,
s.metadataSources.ServiceNamespaceLabels,
); nsFromMeta != "" {
namespace = nsFromMeta
}
return name, namespace
}
// function implemented to provide consistent service metadata naming across multiple
// OTEL implementations: OTEL operator, Loki and Beyla
// https://github.com/grafana/k8s-monitoring-helm/issues/942
func (s *Store) valueFromMetadata(om *informer.ObjectMeta, annotationNames, labelNames []string) string {
for _, key := range annotationNames {
if val, ok := om.Annotations[key]; ok {
return val
}
}
for _, key := range labelNames {
if val, ok := om.Labels[key]; ok {
return val
}
}
return ""
}
// ServiceNameNamespaceForIP returns the service name and namespace for a given IP address
// This means that, for a given Pod, we will not return the Pod Name, but the Pod Owner Name
func (s *Store) ServiceNameNamespaceForIP(ip string) (string, string) {
s.access.RLock()
if serviceInfo, ok := s.otelServiceInfoByIP[ip]; ok {
s.access.RUnlock()
return serviceInfo.Name, serviceInfo.Namespace
}
name, namespace := "", ""
if om, ok := s.objectMetaByIP[ip]; ok {
name, namespace = s.serviceNameNamespaceForMetadata(om)
}
s.access.RUnlock()
s.access.Lock()
s.otelServiceInfoByIP[ip] = OTelServiceNamePair{Name: name, Namespace: namespace}
s.access.Unlock()
return name, namespace
}
func (s *Store) serviceNameNamespaceForOwner(om *informer.ObjectMeta) (string, string) {
ownerKey := ownerID(om.Namespace, om.Name)
return s.serviceNameNamespaceOwnerID(ownerKey, om.Name, om.Namespace)
}
func (s *Store) serviceNameNamespaceForPod(om *informer.ObjectMeta, owner *informer.Owner) (string, string) {
ownerKey := ownerID(om.Namespace, owner.Name)
return s.serviceNameNamespaceOwnerID(ownerKey, owner.Name, om.Namespace)
}
func (s *Store) serviceNameNamespaceOwnerID(ownerKey, name, namespace string) (string, string) {
serviceName := name
serviceNamespace := namespace
if envName, ok := s.serviceNameFromEnv(ownerKey); ok {
serviceName = envName
}
if envName, ok := s.serviceNamespaceFromEnv(ownerKey); ok {
serviceNamespace = envName
}
return serviceName, serviceNamespace
}
func (s *Store) nameFromResourceAttrs(variable string, c *informer.ContainerInfo) (string, bool) {
if resourceVars, ok := c.Env[meta.EnvResourceAttrs]; ok {
allVars := map[string]string{}
collect := func(k string, v string) {
allVars[k] = v
}
attributes.ParseOTELResourceVariable(resourceVars, collect)
if result, ok := allVars[variable]; ok {
return result, true
}
}
return "", false
}
func isValidServiceName(name string) bool {
return name != "" && !strings.HasPrefix(name, "$(")
}
func (s *Store) serviceNameFromEnv(ownerKey string) (string, bool) {
if containers, ok := s.containersByOwner[ownerKey]; ok {
for _, c := range containers {
if serviceName, ok := c.Env[meta.EnvServiceName]; ok {
return serviceName, isValidServiceName(serviceName)
}
if serviceName, ok := s.nameFromResourceAttrs(serviceNameKey, c); ok {
return serviceName, isValidServiceName(serviceName)
}
}
}
return "", false
}
func (s *Store) serviceNamespaceFromEnv(ownerKey string) (string, bool) {
if containers, ok := s.containersByOwner[ownerKey]; ok {
for _, c := range containers {
if namespace, ok := s.nameFromResourceAttrs(serviceNamespaceKey, c); ok {
return namespace, isValidServiceName(namespace)
}
}
}
return "", false
}
func ownerID(namespace, name string) string {
return namespace + "." + name
}
// Subscribe overrides BaseNotifier to send a "welcome message" to each new observer
// containing the whole metadata store
func (s *Store) Subscribe(observer meta.Observer) {
s.access.RLock()
defer s.access.RUnlock()
s.BaseNotifier.Subscribe(observer)
for _, pod := range s.podsByContainer {
if err := observer.On(&informer.Event{Type: informer.EventType_CREATED, Resource: pod}); err != nil {
s.log.Debug("observer failed sending Pod info. Unsubscribing it", "observer", observer.ID(), "error", err)
s.BaseNotifier.Unsubscribe(observer)
return
}
}
// the IPInfos could contain IPInfo data from Pods already sent in the previous loop
// is the subscriber the one that should decide whether to ignore such duplicates or
// incomplete info
for _, ips := range s.objectMetaByIP {
if err := observer.On(&informer.Event{Type: informer.EventType_CREATED, Resource: ips}); err != nil {
s.log.Debug("observer failed sending Object Meta. Unsubscribing it", "observer", observer.ID(), "error", err)
s.BaseNotifier.Unsubscribe(observer)
return
}
}
}