Skip to content

Commit 351576b

Browse files
committed
cleanup
1 parent 26ffcc1 commit 351576b

File tree

7 files changed

+64
-117
lines changed

7 files changed

+64
-117
lines changed

internal/pkg/agent/application/coordinator/coordinator.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,6 @@ type OTelManager interface {
173173
// PerformComponentDiagnostics executes the diagnostic action for the provided components. If no components are provided,
174174
// then it performs the diagnostics for all current units.
175175
PerformComponentDiagnostics(ctx context.Context, additionalMetrics []cproto.AdditionalDiagnosticRequest, req ...component.Component) ([]runtime.ComponentDiagnostic, error)
176-
177-
// DiagnosticHooks returns the list of "global hooks" for the EDOT process.
178-
DiagnosticHooks() diagnostics.Hooks
179176
}
180177

181178
// ConfigChange provides an interface for receiving a new configuration.
@@ -1286,27 +1283,8 @@ func (c *Coordinator) DiagnosticHooks() diagnostics.Hooks {
12861283
return o
12871284
},
12881285
},
1289-
diagnostics.Hook{
1290-
Name: "otel-merged",
1291-
Filename: "otel-merged.yaml",
1292-
Description: "Final otel configuration used by the Elastic Agent. Includes hybrid mode config and component config.",
1293-
ContentType: "application/yaml",
1294-
Hook: func(_ context.Context) []byte {
1295-
mergedCfg := c.otelMgr.MergedOtelConfig()
1296-
if mergedCfg == nil {
1297-
return []byte("no active OTel configuration")
1298-
}
1299-
o, err := yaml.Marshal(mergedCfg.ToStringMap())
1300-
if err != nil {
1301-
return []byte(fmt.Sprintf("error: failed to convert to yaml: %v", err))
1302-
}
1303-
return o
1304-
},
1305-
},
13061286
}
13071287

1308-
hooks = append(hooks, c.otelMgr.DiagnosticHooks()...)
1309-
13101288
return hooks
13111289
}
13121290

internal/pkg/agent/application/paths/common.go

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,18 @@ const (
5858
var ExternalInputsPattern = filepath.Join("inputs.d", "*.yml")
5959

6060
var (
61-
topPath string
62-
configPath string
63-
configFilePath string
64-
logsPath string
65-
downloadsPath string
66-
componentsPath string
67-
installPath string
68-
controlSocketPath string
69-
edotSocketPath string
70-
unversionedHome bool
71-
tmpCreator sync.Once
61+
topPath string
62+
configPath string
63+
configFilePath string
64+
logsPath string
65+
downloadsPath string
66+
componentsPath string
67+
installPath string
68+
controlSocketPath string
69+
edotSocketPath string
70+
diagnosticsExtensionSocket string
71+
unversionedHome bool
72+
tmpCreator sync.Once
7273
)
7374

7475
func init() {
@@ -77,7 +78,8 @@ func init() {
7778
configPath = topPath
7879
logsPath = topPath
7980
controlSocketPath = initialControlSocketPath(topPath)
80-
edotSocketPath = EDOTSocketFromPath(runtime.GOOS, topPath)
81+
edotSocketPath = SocketFromPath(runtime.GOOS, topPath, EDOTSocketName)
82+
diagnosticsExtensionSocket = SocketFromPath(runtime.GOOS, topPath, DiagnosticsExtensionSocketName)
8183
unversionedHome = false // only versioned by container subcommand
8284

8385
// these should never change
@@ -93,6 +95,7 @@ func init() {
9395
fs.StringVar(&configFilePath, "c", DefaultConfigName, "Configuration file, relative to path.config")
9496
fs.StringVar(&logsPath, "path.logs", logsPath, "Logs path contains Agent log output")
9597
fs.StringVar(&controlSocketPath, "path.socket", controlSocketPath, "Control protocol socket path for the Agent")
98+
fs.StringVar(&edotSocketPath, "path.edot_socket", edotSocketPath, "Control protocol socket path for the EDOT")
9699

97100
// enable user to download update artifacts to alternative place
98101
// TODO: remove path.downloads support on next major (this can be configured using `agent.download.targetDirectory`)
@@ -352,27 +355,14 @@ func RunningInstalled() bool {
352355
// ControlSocketFromPath returns the control socket path for an Elastic Agent running
353356
// on the defined platform, and its executing directory.
354357
func ControlSocketFromPath(platform string, path string) string {
355-
// socket should be inside this directory
356-
socketPath := filepath.Join(path, ControlSocketName)
357-
if platform == "windows" {
358-
// on windows the control socket always uses the fallback
359-
return utils.SocketURLWithFallback(socketPath, path)
360-
}
361-
unixSocket := fmt.Sprintf("unix://%s", socketPath)
362-
if len(unixSocket) < 104 {
363-
// small enough to fit
364-
return unixSocket
365-
}
366-
// place in global /tmp to ensure that its small enough to fit; current path is way to long
367-
// for it to be used, but needs to be unique per Agent (in the case that multiple are running)
368-
return utils.SocketURLWithFallback(socketPath, path)
358+
return SocketFromPath(platform, path, ControlSocketName)
369359
}
370360

371-
// EDOTSocketFromPath returns the EDOT socket path for an Elastic Agent running
372-
// on the defined platform, and its executing directory.
373-
func EDOTSocketFromPath(platform string, path string) string {
361+
// SocketFromPath returns the socket path for an Elastic Agent running
362+
// on the defined platform for a given socket, and its executing directory.
363+
func SocketFromPath(platform string, path string, socketName string) string {
374364
// socket should be inside this directory
375-
socketPath := filepath.Join(path, EDOTSocketName)
365+
socketPath := filepath.Join(path, socketName)
376366
if platform == "windows" {
377367
// on windows the control socket always uses the fallback
378368
return utils.SocketURLWithFallback(socketPath, path)
@@ -388,7 +378,11 @@ func EDOTSocketFromPath(platform string, path string) string {
388378
}
389379

390380
func DiagnosticsExtensionSocket() string {
391-
return filepath.Join(topPath, DiagnosticsExtensionSocketName)
381+
return diagnosticsExtensionSocket
382+
}
383+
384+
func SetDiagnosticsExtensionSocket(socket string) {
385+
diagnosticsExtensionSocket = SocketFromPath(runtime.GOOS, topPath, socket)
392386
}
393387

394388
func pathSplit(path string) []string {

internal/pkg/agent/cmd/run.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
"github.com/elastic/elastic-agent/internal/pkg/agent/application/enroll"
2020
fleetgateway "github.com/elastic/elastic-agent/internal/pkg/agent/application/gateway/fleet"
21+
"github.com/elastic/elastic-agent/internal/pkg/diagnostics"
2122

2223
"go.elastic.co/apm/v2"
2324
apmtransport "go.elastic.co/apm/v2/transport"
@@ -50,7 +51,6 @@ import (
5051
"github.com/elastic/elastic-agent/internal/pkg/cli"
5152
"github.com/elastic/elastic-agent/internal/pkg/config"
5253
monitoringCfg "github.com/elastic/elastic-agent/internal/pkg/core/monitoring/config"
53-
"github.com/elastic/elastic-agent/internal/pkg/diagnostics"
5454
"github.com/elastic/elastic-agent/internal/pkg/release"
5555
"github.com/elastic/elastic-agent/pkg/component"
5656
"github.com/elastic/elastic-agent/pkg/control/v2/server"
@@ -312,8 +312,10 @@ func runElasticAgent(
312312
}
313313
}()
314314

315+
diagHooks := diagnostics.GlobalHooks()
316+
diagHooks = append(diagHooks, coord.DiagnosticHooks()...)
315317
controlLog := l.Named("control")
316-
control := server.New(controlLog, agentInfo, coord, tracer, diagnostics.GlobalHooks(), cfg.Settings.GRPC)
318+
control := server.New(controlLog, agentInfo, coord, tracer, diagHooks, cfg.Settings.GRPC)
317319

318320
// if the configMgr implements the TestModeConfigSetter in means that Elastic Agent is in testing mode and
319321
// the configuration will come in over the control protocol, so we set the config setting on the control protocol

internal/pkg/otel/manager/diagnostics.go

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,18 @@ import (
1414
"fmt"
1515
"io"
1616
"io/fs"
17-
"net"
18-
"net/http"
1917
"os"
2018
"path/filepath"
2119
"regexp"
2220
"strings"
2321

2422
"github.com/elastic/elastic-agent/internal/pkg/agent/application/monitoring"
25-
"github.com/elastic/elastic-agent/internal/pkg/agent/application/paths"
26-
"github.com/elastic/elastic-agent/internal/pkg/diagnostics"
23+
"github.com/elastic/elastic-agent/internal/pkg/otel"
2724

2825
"google.golang.org/protobuf/types/known/timestamppb"
2926

3027
"github.com/elastic/elastic-agent-client/v7/pkg/proto"
3128

32-
"github.com/elastic/elastic-agent/internal/pkg/otel/elasticdiagnosticsextension"
3329
"github.com/elastic/elastic-agent/internal/pkg/otel/translate"
3430
"github.com/elastic/elastic-agent/pkg/core/logger"
3531

@@ -40,26 +36,6 @@ import (
4036

4137
var fileBeatRegistryPathRegExps = getRegexpsForRegistryFiles()
4238

43-
func (m *OTelManager) DiagnosticHooks() (hooks diagnostics.Hooks) {
44-
extDiagnostics, err := m.performDiagnosticsExt()
45-
if err != nil {
46-
m.logger.Errorf("error fetchign diagnostics: %v", err)
47-
return
48-
}
49-
for _, hook := range extDiagnostics.GlobalDiagnostics {
50-
hooks = append(hooks, diagnostics.Hook{
51-
Name: hook.Name,
52-
Filename: hook.Filename,
53-
Description: hook.Description,
54-
ContentType: hook.ContentType,
55-
Hook: func(ctx context.Context) []byte {
56-
return hook.Content
57-
},
58-
})
59-
}
60-
return
61-
}
62-
6339
// PerformDiagnostics executes the diagnostic action for the provided units. If no units are provided then
6440
// it performs diagnostics for all current units. If a given unit does not exist in the manager, then a warning
6541
// is logged.
@@ -185,7 +161,7 @@ func (m *OTelManager) PerformComponentDiagnostics(
185161
diagnostics[idx].Err = errors.Join(errs...)
186162
}
187163

188-
extDiagnostics, err := m.performDiagnosticsExt()
164+
extDiagnostics, err := otel.PerformDiagnosticsExt()
189165
if err != nil {
190166
m.logger.Errorf("error fetchign diagnostics: %v", err)
191167
return nil, err
@@ -435,29 +411,3 @@ func matchRegistryFiles(registryFileRegExps []*regexp.Regexp, path string) bool
435411
}
436412
return false
437413
}
438-
439-
func (m *OTelManager) performDiagnosticsExt() (*elasticdiagnosticsextension.Response, error) {
440-
tr := &http.Transport{
441-
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
442-
return net.Dial("unix", paths.DiagnosticsExtensionSocket())
443-
},
444-
}
445-
client := &http.Client{Transport: tr}
446-
resp, err := client.Get("http://localhost/diagnostics")
447-
if err != nil {
448-
return nil, err
449-
}
450-
defer resp.Body.Close()
451-
respBytes, err := io.ReadAll(resp.Body)
452-
if err != nil {
453-
return nil, err
454-
}
455-
456-
var respSerialized elasticdiagnosticsextension.Response
457-
458-
if err := json.Unmarshal(respBytes, &respSerialized); err != nil {
459-
return nil, err
460-
}
461-
462-
return &respSerialized, nil
463-
}

internal/pkg/otel/manager/manager.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,16 @@ func (m *OTelManager) buildMergedConfig(cfgUpdate configUpdate) (*confmap.Conf,
339339
if err := m.injectDiagnosticsExtension(mergedOtelCfg); err != nil {
340340
return nil, fmt.Errorf("failed to inject diagnostics: %w", err)
341341
}
342-
m.logger.Warnf("Here's complete config: %v", mergedOtelCfg.ToStringMap())
343342

344343
return mergedOtelCfg, nil
345344
}
346345

347346
func (m *OTelManager) injectDiagnosticsExtension(config *confmap.Conf) error {
347+
if _, err := os.Stat(paths.DiagnosticsExtensionSocket()); !os.IsNotExist(err) {
348+
// socket already exists. User is most likely using "development" namespace.
349+
// generate a unique socket name by fetching current timestamp
350+
paths.SetDiagnosticsExtensionSocket(fmt.Sprintf("%s-edot-diagnostics-extension.sock", time.Now().UnixMilli()))
351+
}
348352
extensionCfg := map[string]any{
349353
"extensions": map[string]any{
350354
"elastic_diagnsotics": map[string]any{

internal/pkg/otel/server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (s *server) Stop() error {
6262
}
6363

6464
func (s *server) DiagnosticAgent(ctx context.Context, _ *cproto.DiagnosticAgentRequest) (*cproto.DiagnosticAgentResponse, error) {
65-
resp, err := s.performDiagnosticsExt()
65+
resp, err := PerformDiagnosticsExt()
6666
if err != nil {
6767
return nil, err
6868
}
@@ -82,7 +82,7 @@ func (s *server) DiagnosticAgent(ctx context.Context, _ *cproto.DiagnosticAgentR
8282
}
8383

8484
func (s *server) DiagnosticComponents(req *cproto.DiagnosticComponentsRequest, respServ cproto.ElasticAgentControl_DiagnosticComponentsServer) error {
85-
resp, err := s.performDiagnosticsExt()
85+
resp, err := PerformDiagnosticsExt()
8686
if err != nil {
8787
return err
8888
}
@@ -106,7 +106,7 @@ func (s *server) DiagnosticComponents(req *cproto.DiagnosticComponentsRequest, r
106106
return nil
107107
}
108108

109-
func (s *server) performDiagnosticsExt() (*elasticdiagnosticsextension.Response, error) {
109+
func PerformDiagnosticsExt() (*elasticdiagnosticsextension.Response, error) {
110110
tr := &http.Transport{
111111
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
112112
return net.Dial("unix", paths.DiagnosticsExtensionSocket())

pkg/control/v2/server/server.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"fmt"
1212
"net"
1313
"os"
14+
"syscall"
1415
"time"
1516

1617
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/status"
@@ -31,6 +32,7 @@ import (
3132
"github.com/elastic/elastic-agent/internal/pkg/agent/application/info"
3233
"github.com/elastic/elastic-agent/internal/pkg/agent/configuration"
3334
"github.com/elastic/elastic-agent/internal/pkg/diagnostics"
35+
"github.com/elastic/elastic-agent/internal/pkg/otel"
3436
"github.com/elastic/elastic-agent/internal/pkg/release"
3537
"github.com/elastic/elastic-agent/pkg/component"
3638
"github.com/elastic/elastic-agent/pkg/component/runtime"
@@ -194,12 +196,8 @@ func (s *Server) Upgrade(ctx context.Context, request *cproto.UpgradeRequest) (*
194196

195197
// DiagnosticAgent returns diagnostic information for this running Elastic Agent.
196198
func (s *Server) DiagnosticAgent(ctx context.Context, req *cproto.DiagnosticAgentRequest) (*cproto.DiagnosticAgentResponse, error) {
197-
198-
// DiagnosticHooks() is called on every DiagnosticAgent request,
199-
// since otelMgr retrieves the hook list from diagnosticsExtension.
200-
diagHooks := append(s.diagHooks, s.coord.DiagnosticHooks()...)
201-
res := make([]*cproto.DiagnosticFileResult, 0, len(diagHooks))
202-
for _, h := range diagHooks {
199+
res := make([]*cproto.DiagnosticFileResult, 0, len(s.diagHooks))
200+
for _, h := range s.diagHooks {
203201
if ctx.Err() != nil {
204202
return nil, ctx.Err()
205203
}
@@ -234,6 +232,27 @@ func (s *Server) DiagnosticAgent(ctx context.Context, req *cproto.DiagnosticAgen
234232
}
235233
}
236234

235+
resp, err := otel.PerformDiagnosticsExt()
236+
if errors.Is(err, syscall.ENOENT) || errors.Is(err, syscall.ECONNREFUSED) {
237+
// We're not running the EDOT if:
238+
// 1. Either the socket doesn't exist
239+
// 2. It is refusing the connections.
240+
s.logger.Debugf("Couldn't fetch diagnostics from EDOT: %v", err)
241+
return &cproto.DiagnosticAgentResponse{Results: res}, nil
242+
}
243+
if err != nil {
244+
return nil, err
245+
}
246+
for _, r := range resp.GlobalDiagnostics {
247+
res = append(res, &cproto.DiagnosticFileResult{
248+
Name: r.Name,
249+
Filename: r.Filename,
250+
ContentType: r.ContentType,
251+
Content: r.Content,
252+
Description: r.Description,
253+
})
254+
}
255+
237256
if ctx.Err() != nil {
238257
return nil, ctx.Err()
239258
}

0 commit comments

Comments
 (0)