|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License 2.0; |
| 3 | +// you may not use this file except in compliance with the Elastic License 2.0. |
| 4 | + |
| 5 | +package manager |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "errors" |
| 10 | + "fmt" |
| 11 | + "strings" |
| 12 | + "syscall" |
| 13 | + |
| 14 | + "github.com/elastic/elastic-agent/internal/pkg/otel" |
| 15 | + |
| 16 | + "github.com/elastic/elastic-agent/pkg/component" |
| 17 | + "github.com/elastic/elastic-agent/pkg/component/runtime" |
| 18 | + "github.com/elastic/elastic-agent/pkg/control/v2/cproto" |
| 19 | +) |
| 20 | + |
| 21 | +// PerformDiagnostics executes the diagnostic action for the provided units. If no units are provided then |
| 22 | +// it performs diagnostics for all current units. If a given unit does not exist in the manager, then a warning |
| 23 | +// is logged. |
| 24 | +func (m *OTelManager) PerformDiagnostics(ctx context.Context, req ...runtime.ComponentUnitDiagnosticRequest) []runtime.ComponentUnitDiagnostic { |
| 25 | + var diagnostics []runtime.ComponentUnitDiagnostic |
| 26 | + m.mx.RLock() |
| 27 | + currentComponents := m.components |
| 28 | + m.mx.RUnlock() |
| 29 | + |
| 30 | + // if no request is provided, then perform diagnostics for all units |
| 31 | + if len(req) == 0 { |
| 32 | + for _, comp := range currentComponents { |
| 33 | + for _, unit := range comp.Units { |
| 34 | + diagnostics = append(diagnostics, runtime.ComponentUnitDiagnostic{ |
| 35 | + Component: comp, |
| 36 | + Unit: unit, |
| 37 | + }) |
| 38 | + } |
| 39 | + } |
| 40 | + return diagnostics |
| 41 | + } |
| 42 | + |
| 43 | + // create a map of unit by component and unit id, this is used to filter out units that |
| 44 | + // do not exist in the manager |
| 45 | + unitByID := make(map[string]map[string]*component.Unit) |
| 46 | + for _, r := range req { |
| 47 | + if unitByID[r.Component.ID] == nil { |
| 48 | + unitByID[r.Component.ID] = make(map[string]*component.Unit) |
| 49 | + } |
| 50 | + unitByID[r.Component.ID][r.Unit.ID] = &r.Unit |
| 51 | + } |
| 52 | + |
| 53 | + // create empty diagnostics for units that exist in the manager |
| 54 | + for _, existingComp := range currentComponents { |
| 55 | + inputComp, ok := unitByID[existingComp.ID] |
| 56 | + if !ok { |
| 57 | + m.logger.Warnf("requested diagnostics for component %s, but it does not exist in the manager", existingComp.ID) |
| 58 | + continue |
| 59 | + } |
| 60 | + for _, unit := range existingComp.Units { |
| 61 | + if _, ok := inputComp[unit.ID]; ok { |
| 62 | + diagnostics = append(diagnostics, runtime.ComponentUnitDiagnostic{ |
| 63 | + Component: existingComp, |
| 64 | + Unit: unit, |
| 65 | + }) |
| 66 | + } else { |
| 67 | + m.logger.Warnf("requested diagnostics for unit %s, but it does not exist in the manager", unit.ID) |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return diagnostics |
| 73 | +} |
| 74 | + |
| 75 | +// PerformComponentDiagnostics executes the diagnostic action for the provided components. If no components are provided, |
| 76 | +// then it performs the diagnostics for all current components. |
| 77 | +func (m *OTelManager) PerformComponentDiagnostics( |
| 78 | + ctx context.Context, additionalMetrics []cproto.AdditionalDiagnosticRequest, req ...component.Component, |
| 79 | +) ([]runtime.ComponentDiagnostic, error) { |
| 80 | + var diagnostics []runtime.ComponentDiagnostic |
| 81 | + m.mx.RLock() |
| 82 | + currentComponents := m.components |
| 83 | + m.mx.RUnlock() |
| 84 | + |
| 85 | + // if no request is provided, then perform diagnostics for all components |
| 86 | + if len(req) == 0 { |
| 87 | + req = currentComponents |
| 88 | + } |
| 89 | + |
| 90 | + // create a map of component by id, this is used to filter out components that do not exist in the manager |
| 91 | + compByID := make(map[string]component.Component) |
| 92 | + for _, comp := range req { |
| 93 | + compByID[comp.ID] = comp |
| 94 | + } |
| 95 | + |
| 96 | + // create empty diagnostics for components that exist in the manager |
| 97 | + for _, existingComp := range currentComponents { |
| 98 | + if inputComp, ok := compByID[existingComp.ID]; ok { |
| 99 | + diagnostics = append(diagnostics, runtime.ComponentDiagnostic{ |
| 100 | + Component: inputComp, |
| 101 | + }) |
| 102 | + } else { |
| 103 | + m.logger.Warnf("requested diagnostics for component %s, but it does not exist in the manager", existingComp.ID) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + extDiagnostics, err := otel.PerformDiagnosticsExt(ctx, false) |
| 108 | + |
| 109 | + // We're not running the EDOT if: |
| 110 | + // 1. Either the socket doesn't exist |
| 111 | + // 2. It is refusing the connections. |
| 112 | + // Return error for any other scenario. |
| 113 | + if err != nil { |
| 114 | + m.logger.Debugf("Couldn't fetch diagnostics from EDOT: %v", err) |
| 115 | + if !errors.Is(err, syscall.ENOENT) && !errors.Is(err, syscall.ECONNREFUSED) { |
| 116 | + return nil, fmt.Errorf("error fetching otel diagnostics: %w", err) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + for idx, diag := range diagnostics { |
| 121 | + found := false |
| 122 | + for _, extDiag := range extDiagnostics.ComponentDiagnostics { |
| 123 | + if strings.Contains(extDiag.Name, diag.Component.ID) { |
| 124 | + found = true |
| 125 | + diagnostics[idx].Results = append(diagnostics[idx].Results, extDiag) |
| 126 | + } |
| 127 | + } |
| 128 | + if !found { |
| 129 | + diagnostics[idx].Err = fmt.Errorf("failed to get diagnostics for %s", diag.Component.ID) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return diagnostics, nil |
| 134 | +} |
0 commit comments