-
Notifications
You must be signed in to change notification settings - Fork 148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
V2 agent diagnostics action #1631
Closed
michel-laterman
wants to merge
6
commits into
elastic:feature-arch-v2
from
michel-laterman:v2-agent-diagnostics-action
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0cbbd3a
Add diagnostics action to agent
michel-laterman 5d177c5
Diagnostic hooks return times, add start of retrier for uploads
michel-laterman 2a281e9
Add tests
michel-laterman fe206ee
Merge branch 'feature-arch-v2' into v2-agent-diagnostics-action
michel-laterman 5125724
linter fixes
michel-laterman a50f098
linter fixes
michel-laterman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
internal/pkg/agent/application/actions/handlers/handler_action_diagnostics.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package handlers | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
|
||
"github.com/elastic/elastic-agent/internal/pkg/agent/application/coordinator" | ||
"github.com/elastic/elastic-agent/internal/pkg/agent/control/client" | ||
"github.com/elastic/elastic-agent/internal/pkg/diagnostics" | ||
"github.com/elastic/elastic-agent/internal/pkg/fleetapi" | ||
"github.com/elastic/elastic-agent/internal/pkg/fleetapi/acker" | ||
"github.com/elastic/elastic-agent/pkg/core/logger" | ||
) | ||
|
||
// Uploader is the interface used to upload a diagnostics bundle to fleet-server. | ||
type Uploader interface { | ||
UploadDiagnostics(context.Context, string, *bytes.Buffer) error | ||
} | ||
|
||
// Diagnostics is the handler to process Diagnostics actions. | ||
// When a Diagnostics action is received a full diagnostics bundle is taken and uploaded to fleet-server. | ||
type Diagnostics struct { | ||
log *logger.Logger | ||
coord *coordinator.Coordinator // TODO use of coordinator or control server/client? | ||
uploader Uploader | ||
} | ||
|
||
// NewDiagnostics returns a new Diagnostics handler. | ||
func NewDiagnostics(log *logger.Logger, coord *coordinator.Coordinator, uploader Uploader) *Diagnostics { | ||
return &Diagnostics{ | ||
log: log, | ||
coord: coord, | ||
uploader: uploader, | ||
} | ||
} | ||
|
||
// Handle processes the passed Diagnostics action. | ||
func (h *Diagnostics) Handle(ctx context.Context, a fleetapi.Action, ack acker.Acker) error { | ||
h.log.Debugf("handlerDiagnostics: action '%+v' received", a) | ||
action, ok := a.(*fleetapi.ActionDiagnostics) | ||
if !ok { | ||
return fmt.Errorf("invalid type, expected ActionDiagnostics and received %T", a) | ||
} | ||
|
||
// Gather agent diagnostics | ||
diagHooks := append(diagnostics.GlobalHooks(), h.coord.DiagnosticHooks()()...) | ||
aDiag := make([]client.DiagnosticFileResult, 0, len(diagHooks)) | ||
for _, hook := range diagHooks { | ||
if ctx.Err() != nil { | ||
return ctx.Err() | ||
} | ||
|
||
p, ts := hook.Hook(ctx) | ||
aDiag = append(aDiag, client.DiagnosticFileResult{ | ||
Name: hook.Name, | ||
Filename: hook.Filename, | ||
Description: hook.Description, | ||
ContentType: hook.ContentType, | ||
Content: p, | ||
Generated: ts, | ||
}) | ||
} | ||
|
||
runtimeDiag := h.coord.PerformDiagnostics(ctx) | ||
uDiag := make([]client.DiagnosticUnitResult, 0, len(runtimeDiag)) | ||
for _, diag := range runtimeDiag { | ||
files := make([]client.DiagnosticFileResult, 0, diag.Results) | ||
for _, f := range diag.Results { | ||
files = append(files, client.DiagnosticFileResult{ | ||
Name: f.Name, | ||
Filename: f.Filename, | ||
Description: f.Description, | ||
ContentType: f.ContentType, | ||
Content: f.Content, | ||
Generated: f.Generated.AsTime(), | ||
}) | ||
} | ||
uDiag = append(uDiag, client.DiagnosticUnitResult{ | ||
ComponentID: diag.Component.ID, | ||
UnitID: diag.Unit.ID, | ||
UnitType: diag.Unit.Type, | ||
Err: diag.Err, | ||
Results: files, | ||
}) | ||
} | ||
|
||
var b bytes.Buffer | ||
err := diagnostics.ZipArchive(b, aDiag, uDiag) // TODO Do we want to pass a buffer/a reader around? or write the file to a temp dir and read (to avoid memory usage)? file usage may need more thought for containerized deployments | ||
if err != nil { | ||
return fmt.Errorf("error creating diagnostics bundle: %w", err) | ||
} | ||
|
||
err = h.uploader.UploadDiagnostics(ctx, action.ActionID, &b) | ||
_ = ack.Ack(ctx, action) // TODO ack should have the file upload ID in it | ||
if err != nil { | ||
return fmt.Errorf("unable to upload diagnostics: %w", err) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This whole code block looks to be a duplicate of what is in the control server code. We should place this code in a common place instead of copying it.