-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathactioner.go
More file actions
339 lines (293 loc) · 11.9 KB
/
actioner.go
File metadata and controls
339 lines (293 loc) · 11.9 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
332
333
334
335
336
337
338
339
// SPDX-License-Identifier: Apache-2.0
package actioner
import (
"context"
"fmt"
"net/http"
"strings"
"sync"
"time"
"github.com/digitalocean/droplet-agent/internal/config"
"github.com/digitalocean/droplet-agent/internal/log"
"github.com/digitalocean/droplet-agent/internal/metadata"
metadataactioner "github.com/digitalocean/droplet-agent/internal/metadata/actioner"
"github.com/digitalocean/droplet-agent/internal/troubleshooting/command"
"github.com/digitalocean/droplet-agent/internal/troubleshooting/file"
"github.com/digitalocean/droplet-agent/internal/troubleshooting/otlp"
)
const (
// OTLPEndpoint is the default endpoint for submitting troubleshooting logs
OTLPEndpoint = "http://169.254.169.254/v1/sre-agents/droplets/logs"
// CompletedEndpoint is the endpoint to mark an investigation as completed
CompletedEndpoint = "http://169.254.169.254/v1/sre-agents/droplets/logs/completed"
// InvestigationArtifactSyslog is used to request /var/log/syslog file collection
InvestigationArtifactSyslog = "file:/var/log/syslog"
// InvestigationArtifactMessages is used to request /var/log/messages file collection
InvestigationArtifactMessages = "file:/var/log/messages"
// InvestigationArtifactTop is used to request the output of the top command
InvestigationArtifactTop = "command:top"
// InvestigationArtifactPs is used to request the output of the ps command
InvestigationArtifactPs = "command:ps"
// InvestigationArtifactJournalctl is used to request the output of the journalctl command
InvestigationArtifactJournalctl = "command:journalctl"
// defaultLastLines is the number of lines to emit from a log file if no
// alert timestamp is provided or we are unable to parse the timestamp of the file.
defaultLastLines = 100
)
var (
validInvestigationArtifacts = map[string]struct{}{
InvestigationArtifactSyslog: {},
InvestigationArtifactMessages: {},
InvestigationArtifactTop: {},
InvestigationArtifactPs: {},
InvestigationArtifactJournalctl: {},
}
)
// TroubleshootingExporter implements the MetadataActioner interface to export logs
// based on metadata updates
type TroubleshootingExporter struct {
// AgentConfig holds the main configuration for the droplet-agent
AgentConfig AgentConfig
// Endpoint is the OTLP endpoint to send logs to
Endpoint string
// CompletedEndpoint is the endpoint to mark investigation as completed
CompletedEndpoint string
// newOTLPClient creates an OTLP client.
newOTLPClient func(ctx context.Context, config otlp.ClientConfig) (otlp.Emitter, error)
// newCommandRunner creates a command runner (for testing).
newCommandRunner func(config command.Config, emitter otlp.Emitter) (command.Command, error)
// newFileTailer creates a file tailer (for testing).
newFileTailer func(config file.Config, emitter otlp.Emitter) (file.File, error)
// httpClient is used to make HTTP requests
httpClient httpClient
// mu protects the runningInvestigations field
mu sync.Mutex
// runningInvestigations holds the set of currently running investigation UUIDs
runningInvestigations map[string]struct{}
// investigationWg is used to wait for running investigations to complete during shutdown
investigationWg sync.WaitGroup
// shutdownCtx is a context that is canceled when Shutdown is called
shutdownCtx context.Context
// shutdownCancel is the cancel function for shutdownCtx
shutdownCancel context.CancelFunc
}
// httpClient interface for making HTTP requests
type httpClient interface {
Do(req *http.Request) (*http.Response, error)
}
// AgentConfig holds basic configuration for the droplet-agent
type AgentConfig struct {
Version string
UserAgent string
OTLPEndpoint string
}
// Ensure TroubleshootingExporter implements MetadataActioner interface
var _ metadataactioner.MetadataActioner = (*TroubleshootingExporter)(nil)
// NewTroubleshootingExporter creates a new TroubleshootingExporter instance
// with default (real) factories
func NewTroubleshootingExporter(agentConfig AgentConfig) *TroubleshootingExporter {
otlpEndpoint := agentConfig.OTLPEndpoint
if otlpEndpoint == "" {
otlpEndpoint = OTLPEndpoint
}
ctx, cancel := context.WithCancel(context.Background())
return &TroubleshootingExporter{
AgentConfig: agentConfig,
Endpoint: otlpEndpoint,
CompletedEndpoint: CompletedEndpoint,
newOTLPClient: func(ctx context.Context, config otlp.ClientConfig) (otlp.Emitter, error) {
return otlp.NewClient(ctx, config)
},
newCommandRunner: command.NewRunner,
newFileTailer: file.NewFileTailer,
runningInvestigations: make(map[string]struct{}),
httpClient: &http.Client{Timeout: 10 * time.Second},
shutdownCtx: ctx,
shutdownCancel: cancel,
}
}
// Do processes metadata updates and performs log export actions
func (te *TroubleshootingExporter) Do(md *metadata.Metadata) {
// Return early if there's no active investigation
if md.TroubleshootingAgent == nil || md.TroubleshootingAgent.InvestigationUUID == "" {
return
}
// Try-lock: skip if this specific investigation is already running
investigationUUID := md.TroubleshootingAgent.InvestigationUUID
if !te.tryAcquire(investigationUUID) {
log.Info("[Troubleshooting Actioner] Investigation %s already in progress, skipping duplicate request", investigationUUID)
return
}
te.investigationWg.Add(1)
defer te.investigationWg.Done()
defer te.release(investigationUUID)
log.Info("[Troubleshooting Actioner] Metadata contains investigation (%s) request", md.TroubleshootingAgent.InvestigationUUID)
// Validate requested artifacts against allowed list
var artifacts []string
for _, artifact := range md.TroubleshootingAgent.Requesting {
if _, valid := validInvestigationArtifacts[artifact]; valid {
artifacts = append(artifacts, artifact)
}
}
ctx, cancel := context.WithCancel(te.shutdownCtx)
defer cancel()
client, err := te.newOTLPClient(ctx, otlp.ClientConfig{
Endpoint: te.Endpoint,
DropletID: md.DropletID,
Hostname: md.Hostname,
Region: md.Region,
ServiceName: te.AgentConfig.UserAgent,
ServiceVersion: te.AgentConfig.Version,
Investigation: md.TroubleshootingAgent.InvestigationUUID,
})
if err != nil {
log.Error("[Troubleshooting Actioner] Failed to create OTLP client: %v", err)
// Note: Can't emit error since client creation failed
return
}
// Calculate time window: 15 minutes before and after the trigger time
var timeWindow *file.TimeWindow
triggered := md.TroubleshootingAgent.TriggeredAt
if triggered != "" {
parsedTime, err := time.Parse(time.RFC3339, triggered)
if err == nil {
timeWindow = &file.TimeWindow{
Start: parsedTime.Add(-15 * time.Minute),
End: parsedTime.Add(15 * time.Minute),
}
log.Info("[Troubleshooting Actioner] Using time window: %s to %s (±15 min from trigger)",
timeWindow.Start.Format(time.RFC3339), timeWindow.End.Format(time.RFC3339))
} else {
client.EmitError(ctx, "actioner", fmt.Sprintf("Failed to parse triggered_at time '%s': %v", triggered, err))
log.Error("[Troubleshooting Actioner] Failed to parse triggered_at time '%s': %v", triggered, err)
}
}
for _, artifact := range artifacts {
log.Info("[Troubleshooting Actioner] Collecting artifact: %s", artifact)
switch {
case strings.HasPrefix(artifact, command.CmdPrefix):
cmdConfig := command.Config{
Source: artifact,
TimeWindow: timeWindow,
}
cmd, err := te.newCommandRunner(cmdConfig, client)
if err != nil {
log.Error("[Troubleshooting Actioner] Failed to create command runner for artifact '%s': %v", artifact, err)
client.EmitError(ctx, "command_runner", fmt.Sprintf("Failed to create command runner for artifact '%s': %v", artifact, err))
continue
}
if err := cmd.Run(ctx); err != nil {
log.Error("[Troubleshooting Actioner] Failed to collect command artifact '%s': %v", artifact, err)
client.EmitError(ctx, "command_runner", fmt.Sprintf("Failed to collect command artifact '%s': %v", artifact, err))
}
case strings.HasPrefix(artifact, file.FilePrefix):
config := file.Config{
Source: artifact,
LastLines: defaultLastLines,
TimeWindow: timeWindow,
}
fileTailer, err := te.newFileTailer(config, client)
if err != nil {
log.Error("[Troubleshooting Actioner] Failed to create file tailer for '%s': %v", artifact, err)
client.EmitError(ctx, "file_tailer", fmt.Sprintf("Failed to create file tailer for '%s': %v", artifact, err))
continue
}
if err := fileTailer.Tail(ctx); err != nil {
log.Error("[Troubleshooting Actioner] Failed to collect file artifact '%s': %v", artifact, err)
client.EmitError(ctx, "file_tailer", fmt.Sprintf("Failed to collect file artifact '%s': %v", artifact, err))
}
}
}
err = client.Flush(ctx)
if err != nil {
log.Error("[Troubleshooting Actioner] Failed to flush OTLP client: %v", err)
client.EmitError(ctx, "otlp_flush", fmt.Sprintf("Failed to flush OTLP client: %v", err))
}
err = te.markInvestigationReady(ctx, investigationUUID)
if err != nil {
log.Error("[Troubleshooting Actioner] Failed to mark investigation as completed: %v", err)
client.EmitError(ctx, "investigation_completion", fmt.Sprintf("Failed to mark investigation as ready: %v", err))
}
}
// Shutdown gracefully shuts down the TroubleshootingExporter
func (te *TroubleshootingExporter) Shutdown() {
log.Info("[Troubleshooting Actioner] Shutting down...")
te.shutdownCancel()
// Wait for investigations to finish, with a timeout
waitChan := make(chan struct{})
go func() {
te.investigationWg.Wait()
close(waitChan)
}()
select {
case <-waitChan:
log.Info("[Troubleshooting Actioner] Shutdown successful.")
case <-time.After(30 * time.Second):
log.Info("[Troubleshooting Actioner] Shutdown timed out.")
}
}
// tryAcquire attempts to acquire the lock for the given investigation UUID.
// Returns true if acquired, false if the investigation is already running.
func (te *TroubleshootingExporter) tryAcquire(investigationUUID string) bool {
te.mu.Lock()
defer te.mu.Unlock()
// Don't start new investigations if shutdown has been initiated
if te.shutdownCtx.Err() != nil {
return false
}
if _, exists := te.runningInvestigations[investigationUUID]; exists {
return false
}
te.runningInvestigations[investigationUUID] = struct{}{}
return true
}
// release releases the lock for the given investigation.
func (te *TroubleshootingExporter) release(investigationUUID string) {
te.mu.Lock()
defer te.mu.Unlock()
delete(te.runningInvestigations, investigationUUID)
}
// markInvestigationReady sends a POST request to notify that log collection is
// completed, and that the investigation is ready for analysis.
func (te *TroubleshootingExporter) markInvestigationReady(ctx context.Context, investigationUUID string) error {
const (
maxRetries = 5
initialBackoff = 1 * time.Second
maxBackoff = 10 * time.Second
)
var lastErr error
backoff := initialBackoff
for attempt := 0; attempt < maxRetries; attempt++ {
if attempt > 0 {
log.Info("[Troubleshooting Actioner] Retrying completed endpoint (attempt %d/%d) after %v", attempt+1, maxRetries, backoff)
time.Sleep(backoff)
// Exponential backoff with cap
backoff *= 2
if backoff > maxBackoff {
backoff = maxBackoff
}
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, te.CompletedEndpoint, nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("User-Agent", config.UserAgent)
req.Header.Set(otlp.InvestigationUUIDHeader, investigationUUID)
resp, err := te.httpClient.Do(req)
if err != nil {
lastErr = fmt.Errorf("request failed: %w", err)
continue
}
defer func() {
if resp.Body != nil {
_ = resp.Body.Close()
}
}()
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
log.Info("[Troubleshooting Actioner] Successfully marked investigation %s as completed", investigationUUID)
return nil
}
lastErr = fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
return fmt.Errorf("failed after %d attempts: %w", maxRetries, lastErr)
}