Skip to content
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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
})
}
Copy link
Contributor

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.


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
}
Loading