/
telemetry_linux.go
170 lines (145 loc) · 4.68 KB
/
telemetry_linux.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
// Package agent holds agent related files
package agent
import (
"context"
"errors"
"fmt"
"time"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta"
"github.com/DataDog/datadog-agent/pkg/security/metrics"
"github.com/DataDog/datadog-agent/pkg/security/proto/api"
sectelemetry "github.com/DataDog/datadog-agent/pkg/security/telemetry"
"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-go/v5/statsd"
)
// telemetry reports environment information (e.g containers running) when the runtime security component is running
type telemetry struct {
containers *sectelemetry.ContainersTelemetry
runtimeSecurityClient *RuntimeSecurityClient
profiledContainers map[profiledContainer]struct{}
logProfiledWorkloads bool
}
func newTelemetry(statsdClient statsd.ClientInterface, wmeta workloadmeta.Component, logProfiledWorkloads, ignoreDDAgentContainers bool) (*telemetry, error) {
runtimeSecurityClient, err := NewRuntimeSecurityClient()
if err != nil {
return nil, err
}
telemetrySender := sectelemetry.NewSimpleTelemetrySenderFromStatsd(statsdClient)
containersTelemetry, err := sectelemetry.NewContainersTelemetry(telemetrySender, wmeta)
if err != nil {
return nil, err
}
containersTelemetry.IgnoreDDAgent = ignoreDDAgentContainers
return &telemetry{
containers: containersTelemetry,
runtimeSecurityClient: runtimeSecurityClient,
profiledContainers: make(map[profiledContainer]struct{}),
logProfiledWorkloads: logProfiledWorkloads,
}, nil
}
func (t *telemetry) registerProfiledContainer(name, tag string) {
entry := profiledContainer{
name: name,
tag: tag,
}
if entry.isValid() {
t.profiledContainers[entry] = struct{}{}
}
}
func (t *telemetry) run(ctx context.Context, rsa *RuntimeSecurityAgent) {
log.Info("started collecting Runtime Security Agent telemetry")
defer log.Info("stopping Runtime Security Agent telemetry")
metricsTicker := time.NewTicker(1 * time.Minute)
defer metricsTicker.Stop()
profileCounterTicker := time.NewTicker(5 * time.Minute)
defer profileCounterTicker.Stop()
for {
select {
case <-ctx.Done():
return
case <-metricsTicker.C:
if err := t.reportContainers(); err != nil {
log.Debugf("couldn't report containers: %v", err)
}
if rsa.storage != nil {
rsa.storage.SendTelemetry()
}
case <-profileCounterTicker.C:
if err := t.reportProfiledContainers(); err != nil {
log.Debugf("couldn't report profiled containers: %v", err)
}
}
}
}
type profiledContainer struct {
name string
tag string
}
func (pc *profiledContainer) isValid() bool {
return pc.name != "" && pc.tag != ""
}
func (t *telemetry) fetchConfig() (*api.SecurityConfigMessage, error) {
cfg, err := t.runtimeSecurityClient.GetConfig()
if err != nil {
return cfg, errors.New("couldn't fetch config from runtime security module")
}
return cfg, nil
}
func (t *telemetry) reportProfiledContainers() error {
cfg, err := t.fetchConfig()
if err != nil {
return err
}
if !cfg.ActivityDumpEnabled {
return nil
}
profiled := make(map[profiledContainer]bool)
for _, container := range t.containers.ListRunningContainers() {
entry := profiledContainer{
name: container.Image.Name,
tag: container.Image.Tag,
}
if !entry.isValid() {
continue
}
profiled[entry] = false
}
doneProfiling := make([]string, 0)
for containerEntry := range t.profiledContainers {
profiled[containerEntry] = true
doneProfiling = append(doneProfiling, fmt.Sprintf("%s:%s", containerEntry.name, containerEntry.tag))
}
missing := make([]string, 0, len(profiled))
for entry, found := range profiled {
if !found {
missing = append(missing, fmt.Sprintf("%s:%s", entry.name, entry.tag))
}
}
if t.logProfiledWorkloads && len(missing) > 0 {
log.Infof("not yet profiled workloads (%d/%d): %v; finished profiling: %v", len(missing), len(profiled), missing, doneProfiling)
}
t.containers.TelemetrySender.Gauge(metrics.MetricActivityDumpNotYetProfiledWorkload, float64(len(missing)), nil)
return nil
}
func (t *telemetry) reportContainers() error {
// retrieve the runtime security module config
cfg, err := t.fetchConfig()
if err != nil {
return err
}
var metricName string
if cfg.RuntimeEnabled {
metricName = metrics.MetricSecurityAgentRuntimeContainersRunning
} else if cfg.FIMEnabled {
metricName = metrics.MetricSecurityAgentFIMContainersRunning
} else {
// nothing to report
return nil
}
t.containers.ReportContainers(metricName)
return nil
}