Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
a6d9cc6
edot diagnostics
VihasMakwana Sep 19, 2025
d0ac8ef
revert otel.yml
VihasMakwana Sep 19, 2025
26ffcc1
add for supervised
VihasMakwana Sep 19, 2025
351576b
cleanup
VihasMakwana Sep 19, 2025
f43056b
more fixes
VihasMakwana Sep 20, 2025
c577a9f
spelling and comments
VihasMakwana Sep 22, 2025
55f5d4f
docs and comments
VihasMakwana Sep 22, 2025
e4748bd
test case
VihasMakwana Sep 22, 2025
0505bb5
diagram
VihasMakwana Sep 22, 2025
0a2d4f8
Merge branch 'main' into edot-diagnostics
VihasMakwana Sep 22, 2025
e40eb22
fix test
VihasMakwana Sep 22, 2025
8540565
fix CI
VihasMakwana Sep 22, 2025
7122db9
cleanup and npipe
VihasMakwana Sep 23, 2025
648e6d8
comments
VihasMakwana Sep 23, 2025
27c79fd
rename
VihasMakwana Sep 24, 2025
9c3b7d8
Merge branch 'main' into edot-diagnostics
VihasMakwana Sep 24, 2025
f42929e
go.mod and notice
VihasMakwana Sep 24, 2025
a168b5a
go.mod and notice
VihasMakwana Sep 24, 2025
dcd4b41
restore otel-merged.yaml
VihasMakwana Sep 24, 2025
7adba49
refactor ZipArchiveEDOT
VihasMakwana Sep 24, 2025
ddc4822
checkci
VihasMakwana Sep 24, 2025
144c806
license
VihasMakwana Sep 24, 2025
1eaf1ae
initial commit
VihasMakwana Sep 24, 2025
2ab3171
timeout
VihasMakwana Sep 24, 2025
e670fa1
fix tests
VihasMakwana Sep 25, 2025
acc4b0b
fix tests
VihasMakwana Sep 25, 2025
7ecb4d8
comments
VihasMakwana Sep 25, 2025
b60459e
error handling
VihasMakwana Sep 25, 2025
cdb3684
lint
VihasMakwana Sep 25, 2025
0069ae6
test case
VihasMakwana Sep 25, 2025
2031093
readme
VihasMakwana Sep 25, 2025
65d9080
gofmt
VihasMakwana Sep 25, 2025
e30ec8c
remove println
VihasMakwana Sep 25, 2025
8e32595
test
VihasMakwana Sep 25, 2025
1cdf78a
add cpu profile
VihasMakwana Sep 25, 2025
6abeca1
remove telemetry for now.
VihasMakwana Sep 25, 2025
e34f760
Merge branch 'main' into edot-extension-only
VihasMakwana Sep 25, 2025
82062b8
remove redundant code
VihasMakwana Sep 25, 2025
84c55b0
doc
VihasMakwana Sep 25, 2025
cb18e45
fix npipe
VihasMakwana Sep 25, 2025
12d114c
Update internal/pkg/otel/extension/elasticdiagnostics/extension.go
VihasMakwana Sep 26, 2025
3c59f14
readme
VihasMakwana Sep 26, 2025
09f543d
readme
VihasMakwana Sep 26, 2025
cc9731a
Merge branch 'main' into edot-diagnostics
VihasMakwana Sep 26, 2025
a281488
Merge branch 'edot-extension-only' into edot-diagnostics
VihasMakwana Sep 26, 2025
ea8e734
Update internal/pkg/otel/extension/elasticdiagnostics/extension.go
VihasMakwana Sep 26, 2025
4a337c6
mutex
VihasMakwana Sep 26, 2025
b7f88e1
Merge branch 'main' into edot-extension-only
VihasMakwana Sep 26, 2025
c0ac15d
comments
VihasMakwana Sep 26, 2025
2d87ba0
Update generated_component_test.go
VihasMakwana Sep 26, 2025
848f2e6
Update generated_component_test.go
VihasMakwana Sep 26, 2025
a7f947d
comments
VihasMakwana Sep 27, 2025
36f37fe
test thorough
VihasMakwana Sep 27, 2025
050580a
use eventuallyWith
VihasMakwana Sep 28, 2025
559ba60
update go.mod and notice
VihasMakwana Sep 28, 2025
4eca3ba
Merge branch 'edot-extension-only' into edot-diagnostics
VihasMakwana Sep 28, 2025
4d8212a
add cpu
VihasMakwana Sep 29, 2025
b377c31
comment
VihasMakwana Sep 29, 2025
d40ca9c
Merge branch 'main' into edot-diagnostics
VihasMakwana Sep 29, 2025
71b2669
test
VihasMakwana Sep 29, 2025
f0d4b4c
Merge branch 'main' into edot-diagnostics
VihasMakwana Sep 29, 2025
be617fb
fix test
VihasMakwana Sep 30, 2025
97bad15
lint
VihasMakwana Sep 30, 2025
ccfff1d
lint
VihasMakwana Sep 30, 2025
0cef9d5
test
VihasMakwana Sep 30, 2025
420f379
comments
VihasMakwana Sep 30, 2025
dd87904
cleanup
VihasMakwana Sep 30, 2025
710c642
comments
VihasMakwana Sep 30, 2025
51808e4
fix test
VihasMakwana Sep 30, 2025
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
41 changes: 30 additions & 11 deletions internal/pkg/agent/application/paths/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ const (
// ControlSocketName is the control socket name.
ControlSocketName = "elastic-agent.sock"

// Our DiagnosticsExtension will use DiagnosticsExtensionSocketName to listen and serve diagnostic requests.
DiagnosticsExtensionSocketName = "edot-diagnostics-extension.sock"

// WindowsControlSocketInstalledPath is the control socket path used when installed on Windows.
WindowsControlSocketInstalledPath = `npipe:///elastic-agent-system`

Expand All @@ -52,16 +55,17 @@ const (
var ExternalInputsPattern = filepath.Join("inputs.d", "*.yml")

var (
topPath string
configPath string
configFilePath string
logsPath string
downloadsPath string
componentsPath string
installPath string
controlSocketPath string
unversionedHome bool
tmpCreator sync.Once
topPath string
configPath string
configFilePath string
logsPath string
downloadsPath string
componentsPath string
installPath string
controlSocketPath string
diagnosticsExtensionSocket string
unversionedHome bool
tmpCreator sync.Once
)

func init() {
Expand All @@ -70,6 +74,7 @@ func init() {
configPath = topPath
logsPath = topPath
controlSocketPath = initialControlSocketPath(topPath)
diagnosticsExtensionSocket = SocketFromPath(runtime.GOOS, topPath, DiagnosticsExtensionSocketName)
unversionedHome = false // only versioned by container subcommand

// these should never change
Expand Down Expand Up @@ -339,8 +344,14 @@ func RunningInstalled() bool {
// ControlSocketFromPath returns the control socket path for an Elastic Agent running
// on the defined platform, and its executing directory.
func ControlSocketFromPath(platform string, path string) string {
return SocketFromPath(platform, path, ControlSocketName)
}

// SocketFromPath returns the socket path for an Elastic Agent running
// on the defined platform for a given socket, and its executing directory.
func SocketFromPath(platform string, path string, socketName string) string {
// socket should be inside this directory
socketPath := filepath.Join(path, ControlSocketName)
socketPath := filepath.Join(path, socketName)
if platform == "windows" {
// on windows the control socket always uses the fallback
return utils.SocketURLWithFallback(socketPath, path)
Expand All @@ -355,6 +366,14 @@ func ControlSocketFromPath(platform string, path string) string {
return utils.SocketURLWithFallback(socketPath, path)
}

func DiagnosticsExtensionSocket() string {
return diagnosticsExtensionSocket
}

func SetDiagnosticsExtensionSocket(socket string) {
diagnosticsExtensionSocket = SocketFromPath(runtime.GOOS, topPath, socket)
}

func pathSplit(path string) []string {
dir, file := filepath.Split(path)
if dir == "" && file == "" {
Expand Down
113 changes: 113 additions & 0 deletions internal/pkg/agent/cmd/diagnostics_otel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License 2.0;
// you may not use this file except in compliance with the Elastic License 2.0.

package cmd

import (
"fmt"
"os"
"time"

"github.com/spf13/cobra"

"github.com/elastic/elastic-agent/internal/pkg/agent/application/paths"
"github.com/elastic/elastic-agent/internal/pkg/cli"
"github.com/elastic/elastic-agent/internal/pkg/diagnostics"
"github.com/elastic/elastic-agent/internal/pkg/otel"
"github.com/elastic/elastic-agent/pkg/control/v2/client"
)

func newOtelDiagnosticsCommand(streams *cli.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: "diagnostics",
Short: "Gather diagnostics information from the EDOT and write it to a zip archive",
Long: "This command gathers diagnostics information from the EDOT and writes it to a zip archive",
RunE: func(cmd *cobra.Command, _ []string) error {
if err := otelDiagnosticCmd(streams, cmd); err != nil {
fmt.Fprintf(streams.Err, "Error: %v\n%s\n", err, troubleshootMessage())
os.Exit(1)
}
return nil
},
SilenceUsage: true,
SilenceErrors: true,
}
cmd.Flags().StringP("file", "f", "", "name of the output diagnostics zip archive")
cmd.Flags().BoolP("cpu-profile", "p", false, "wait to collect a CPU profile")
return cmd
}

func otelDiagnosticCmd(streams *cli.IOStreams, cmd *cobra.Command) error {
cpuProfile, _ := cmd.Flags().GetBool("cpu-profile")
resp, err := otel.PerformDiagnosticsExt(cmd.Context(), cpuProfile)
if err != nil {
return fmt.Errorf("failed to get edot diagnostics: %w", err)
}

agentDiag := make([]client.DiagnosticFileResult, 0)
for _, r := range resp.GlobalDiagnostics {
agentDiag = append(agentDiag, client.DiagnosticFileResult{
Name: r.Name,
Filename: r.Filename,
ContentType: r.ContentType,
Content: r.Content,
Description: r.Description,
})
}

componentDiag := make([]client.DiagnosticComponentResult, 0)
for _, r := range resp.ComponentDiagnostics {
res := client.DiagnosticComponentResult{
Results: make([]client.DiagnosticFileResult, 0),
}
res.Results = append(res.Results, client.DiagnosticFileResult{
Name: r.Name,
Filename: r.Filename,
ContentType: r.ContentType,
Content: r.Content,
Description: r.Description,
})
res.ComponentID = r.Name
componentDiag = append(componentDiag, res)
}
componentDiag = aggregateComponentDiagnostics(componentDiag)

filepath, _ := cmd.Flags().GetString("file")
if filepath == "" {
ts := time.Now().UTC()
filepath = "edot-diagnostics-" + ts.Format("2006-01-02T15-04-05Z07-00") + ".zip" // RFC3339 format that replaces : with -, so it will work on Windows
}
f, err := createFile(filepath)
if err != nil {
return fmt.Errorf("could not create diagnostics file %q: %w", filepath, err)
}
defer f.Close()

// In EDOT, the logs path does not exist, so we ignore that error.
if err := diagnostics.ZipArchive(streams.Err, f, paths.Top(), agentDiag, nil, componentDiag, false); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("unable to create archive %q: %w", filepath, err)
}
fmt.Fprintf(streams.Out, "Created diagnostics archive %q\n", filepath)
fmt.Fprintln(streams.Out, "** WARNING **\nCreated archive may contain plain text credentials.\nEnsure that files in archive are redacted before sharing.\n*******")
return nil
}

// aggregateComponentDiagnostics takes a slice of DiagnosticComponentResult and merges
// results for components with the same ComponentID.
func aggregateComponentDiagnostics(diags []client.DiagnosticComponentResult) []client.DiagnosticComponentResult {
m := make(map[string]client.DiagnosticComponentResult)
for _, d := range diags {
if existing, ok := m[d.ComponentID]; ok {
existing.Results = append(existing.Results, d.Results...)
m[d.ComponentID] = existing
} else {
m[d.ComponentID] = d
}
}
result := make([]client.DiagnosticComponentResult, 0, len(m))
for _, v := range m {
result = append(result, v)
}
return result
}
9 changes: 8 additions & 1 deletion internal/pkg/agent/cmd/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import (
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/service"

"github.com/elastic/elastic-agent/internal/pkg/agent/application/paths"
"github.com/elastic/elastic-agent/internal/pkg/cli"
"github.com/elastic/elastic-agent/internal/pkg/otel"
"github.com/elastic/elastic-agent/internal/pkg/otel/agentprovider"
"github.com/elastic/elastic-agent/internal/pkg/otel/extension/elasticdiagnostics"
"github.com/elastic/elastic-agent/internal/pkg/otel/manager"
"github.com/elastic/elastic-agent/internal/pkg/otel/monitoring"
"github.com/elastic/elastic-agent/internal/pkg/release"
Expand Down Expand Up @@ -71,6 +73,7 @@ func newOtelCommandWithArgs(args []string, streams *cli.IOStreams) *cobra.Comman
setupOtelFlags(cmd.Flags())
cmd.AddCommand(newValidateCommandWithArgs(args, streams))
cmd.AddCommand(newComponentsCommandWithArgs(args, streams))
cmd.AddCommand(newOtelDiagnosticsCommand(streams))

return cmd
}
Expand Down Expand Up @@ -128,6 +131,9 @@ type edotSettings struct {

func prepareCollectorSettings(configFiles []string, supervised bool, supervisedLoggingLevel string) (edotSettings, error) {
var settings edotSettings
conf := map[string]any{
"endpoint": paths.DiagnosticsExtensionSocket(),
}
if supervised {
// add stdin config provider
configProvider, err := agentprovider.NewBufferProvider(os.Stdin)
Expand All @@ -136,6 +142,7 @@ func prepareCollectorSettings(configFiles []string, supervised bool, supervisedL
}
settings.otelSettings = otel.NewSettings(release.Version(), []string{configProvider.URI()},
otel.WithConfigProviderFactory(configProvider.NewFactory()),
otel.WithConfigConvertorFactory(manager.NewForceExtensionConverterFactory(elasticdiagnostics.DiagnosticsExtensionID.String(), conf)),
)

// setup logger
Expand Down Expand Up @@ -173,7 +180,7 @@ func prepareCollectorSettings(configFiles []string, supervised bool, supervisedL

settings.otelSettings.DisableGracefulShutdown = false
} else {
settings.otelSettings = otel.NewSettings(release.Version(), configFiles)
settings.otelSettings = otel.NewSettings(release.Version(), configFiles, otel.WithConfigConvertorFactory(manager.NewForceExtensionConverterFactory(elasticdiagnostics.DiagnosticsExtensionID.String(), conf)))
}
return settings, nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/agent/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"github.com/elastic/elastic-agent-libs/service"
"github.com/elastic/elastic-agent-system-metrics/report"
"github.com/elastic/elastic-agent/internal/pkg/agent/vault"
"github.com/elastic/elastic-agent/internal/pkg/diagnostics"

"github.com/elastic/elastic-agent/internal/pkg/agent/application"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/coordinator"
Expand All @@ -50,7 +51,6 @@
"github.com/elastic/elastic-agent/internal/pkg/cli"
"github.com/elastic/elastic-agent/internal/pkg/config"
monitoringCfg "github.com/elastic/elastic-agent/internal/pkg/core/monitoring/config"
"github.com/elastic/elastic-agent/internal/pkg/diagnostics"
"github.com/elastic/elastic-agent/internal/pkg/release"
"github.com/elastic/elastic-agent/pkg/component"
"github.com/elastic/elastic-agent/pkg/control/v2/server"
Expand Down Expand Up @@ -665,7 +665,7 @@
tracer *apm.Tracer,
coord *coordinator.Coordinator,
) (*reload.ServerReloader, error) {
if err := report.SetupMetrics(logger, agentName, version.GetDefaultVersion()); err != nil {

Check failure on line 668 in internal/pkg/agent/cmd/run.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

SA1019: report.SetupMetrics is deprecated: use SetupMetricsOptions (staticcheck)
return nil, err
}

Expand Down
102 changes: 51 additions & 51 deletions internal/pkg/diagnostics/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,9 @@ func ZipArchive(
if err != nil {
return fmt.Errorf("error creating .zip header for components/ directory: %w", err)
}

// iterate over components
for dirName, units := range compDirs {
for dirName, comp := range componentResults {
_, err := zw.CreateHeader(&zip.FileHeader{
Name: fmt.Sprintf("components/%s/", dirName),
Method: zip.Deflate,
Expand All @@ -236,66 +237,65 @@ func ZipArchive(
if err != nil {
return fmt.Errorf("error creating .zip header for component directory: %w", err)
}
// create component diags
if comp, ok := componentResults[dirName]; ok {
// check for component-level errors
if comp.Err != nil {
err = writeErrorResult(zw, fmt.Sprintf("components/%s/error.txt", dirName), comp.Err.Error())
if comp.Err != nil {
err = writeErrorResult(zw, fmt.Sprintf("components/%s/error.txt", dirName), comp.Err.Error())
if err != nil {
return fmt.Errorf("error while writing error result for component %s: %w", comp.ComponentID, err)
}
} else {
for _, res := range comp.Results {

filePath := fmt.Sprintf("components/%s/%s", dirName, res.Filename)
resFileWriter, err := zw.CreateHeader(&zip.FileHeader{
Name: filePath,
Method: zip.Deflate,
Modified: ts,
})
if err != nil {
return fmt.Errorf("error while writing error result for component %s: %w", comp.ComponentID, err)
return fmt.Errorf("error creating .zip header for %s: %w", res.Filename, err)
}
} else {
for _, res := range comp.Results {

filePath := fmt.Sprintf("components/%s/%s", dirName, res.Filename)
resFileWriter, err := zw.CreateHeader(&zip.FileHeader{
Name: filePath,
Method: zip.Deflate,
Modified: ts,
})
if err != nil {
return fmt.Errorf("error creating .zip header for %s: %w", res.Filename, err)
}
err = writeRedacted(errOut, resFileWriter, filePath, res)
if err != nil {
return fmt.Errorf("error writing %s in zip file: %w", res.Filename, err)
}
err = writeRedacted(errOut, resFileWriter, filePath, res)
if err != nil {
return fmt.Errorf("error writing %s in zip file: %w", res.Filename, err)
}
}

}
// create unit diags
for _, ud := range units {
unitDir := strings.ReplaceAll(strings.TrimPrefix(ud.UnitID, ud.ComponentID+"-"), "/", "-")
_, err := zw.CreateHeader(&zip.FileHeader{
Name: fmt.Sprintf("components/%s/%s/", dirName, unitDir),
Method: zip.Deflate,
Modified: ts,
})
if err != nil {
return fmt.Errorf("error creating .zip header for unit directory: %w", err)
}
// check for unit-level errors
if ud.Err != nil {
err = writeErrorResult(zw, fmt.Sprintf("components/%s/%s/error.txt", dirName, unitDir), ud.Err.Error())
if err != nil {
return fmt.Errorf("error while writing error result for unit %s: %w", ud.UnitID, err)
}
continue
}
for _, fr := range ud.Results {
filePath := fmt.Sprintf("components/%s/%s/%s", dirName, unitDir, fr.Filename)
w, err := zw.CreateHeader(&zip.FileHeader{
Name: filePath,
if units, ok := compDirs[dirName]; ok {
// check for component-level errors
// create unit diags
for _, ud := range units {
unitDir := strings.ReplaceAll(strings.TrimPrefix(ud.UnitID, ud.ComponentID+"-"), "/", "-")
_, err := zw.CreateHeader(&zip.FileHeader{
Name: fmt.Sprintf("components/%s/%s/", dirName, unitDir),
Method: zip.Deflate,
Modified: fr.Generated,
Modified: ts,
})
if err != nil {
return err
return fmt.Errorf("error creating .zip header for unit directory: %w", err)
}
err = writeRedacted(errOut, w, filePath, fr)
if err != nil {
return err
// check for unit-level errors
if ud.Err != nil {
err = writeErrorResult(zw, fmt.Sprintf("components/%s/%s/error.txt", dirName, unitDir), ud.Err.Error())
if err != nil {
return fmt.Errorf("error while writing error result for unit %s: %w", ud.UnitID, err)
}
continue
}
for _, fr := range ud.Results {
filePath := fmt.Sprintf("components/%s/%s/%s", dirName, unitDir, fr.Filename)
w, err := zw.CreateHeader(&zip.FileHeader{
Name: filePath,
Method: zip.Deflate,
Modified: fr.Generated,
})
if err != nil {
return err
}
err = writeRedacted(errOut, w, filePath, fr)
if err != nil {
return err
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions internal/pkg/otel/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ import (
filestorage "github.com/open-telemetry/opentelemetry-collector-contrib/extension/storage/filestorage"
"go.opentelemetry.io/collector/extension/memorylimiterextension" // for putting backpressure when approach a memory limit

elasticdiagnostics "github.com/elastic/elastic-agent/internal/pkg/otel/extension/elasticdiagnostics"

"github.com/elastic/opentelemetry-collector-components/extension/apikeyauthextension"
"github.com/elastic/opentelemetry-collector-components/extension/apmconfigextension"

Expand Down Expand Up @@ -199,6 +201,7 @@ func components(extensionFactories ...extension.Factory) func() (otelcol.Factori
apmconfigextension.NewFactory(),
headersetterextension.NewFactory(),
beatsauthextension.NewFactory(),
elasticdiagnostics.NewFactory(),
}
extensions = append(extensions, extensionFactories...)
factories.Extensions, err = otelcol.MakeFactoryMap[extension.Factory](extensions...)
Expand Down
Loading
Loading