Skip to content

Commit 8452072

Browse files
Merge branch 'main' into otel/added-k8s-comps
2 parents 90714ec + c929f79 commit 8452072

31 files changed

+918
-484
lines changed

NOTICE.txt

+32-32
Original file line numberDiff line numberDiff line change
@@ -2220,36 +2220,6 @@ Contents of probable licence file $GOMODCACHE/github.com/elastic/go-licenser@v0.
22202220
limitations under the License.
22212221

22222222

2223-
--------------------------------------------------------------------------------
2224-
Dependency : github.com/elastic/go-service
2225-
Version: v0.0.0-20240611154109-f44f756f194f
2226-
Licence type (autodetected): Zlib
2227-
--------------------------------------------------------------------------------
2228-
2229-
Contents of probable licence file $GOMODCACHE/github.com/elastic/go-service@v0.0.0-20240611154109-f44f756f194f/LICENSE:
2230-
2231-
Copyright (c) 2015 Daniel Theophanes
2232-
2233-
This software is provided 'as-is', without any express or implied
2234-
warranty. In no event will the authors be held liable for any damages
2235-
arising from the use of this software.
2236-
2237-
Permission is granted to anyone to use this software for any purpose,
2238-
including commercial applications, and to alter it and redistribute it
2239-
freely, subject to the following restrictions:
2240-
2241-
1. The origin of this software must not be misrepresented; you must not
2242-
claim that you wrote the original software. If you use this software
2243-
in a product, an acknowledgment in the product documentation would be
2244-
appreciated but is not required.
2245-
2246-
2. Altered source versions must be plainly marked as such, and must not be
2247-
misrepresented as being the original software.
2248-
2249-
3. This notice may not be removed or altered from any source
2250-
distribution.
2251-
2252-
22532223
--------------------------------------------------------------------------------
22542224
Dependency : github.com/elastic/go-sysinfo
22552225
Version: v1.14.0
@@ -3812,6 +3782,36 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38123782
SOFTWARE.
38133783

38143784

3785+
--------------------------------------------------------------------------------
3786+
Dependency : github.com/kardianos/service
3787+
Version: v1.2.1-0.20210728001519-a323c3813bc7
3788+
Licence type (autodetected): Zlib
3789+
--------------------------------------------------------------------------------
3790+
3791+
Contents of probable licence file $GOMODCACHE/github.com/kardianos/service@v1.2.1-0.20210728001519-a323c3813bc7/LICENSE:
3792+
3793+
Copyright (c) 2015 Daniel Theophanes
3794+
3795+
This software is provided 'as-is', without any express or implied
3796+
warranty. In no event will the authors be held liable for any damages
3797+
arising from the use of this software.
3798+
3799+
Permission is granted to anyone to use this software for any purpose,
3800+
including commercial applications, and to alter it and redistribute it
3801+
freely, subject to the following restrictions:
3802+
3803+
1. The origin of this software must not be misrepresented; you must not
3804+
claim that you wrote the original software. If you use this software
3805+
in a product, an acknowledgment in the product documentation would be
3806+
appreciated but is not required.
3807+
3808+
2. Altered source versions must be plainly marked as such, and must not be
3809+
misrepresented as being the original software.
3810+
3811+
3. This notice may not be removed or altered from any source
3812+
distribution.
3813+
3814+
38153815
--------------------------------------------------------------------------------
38163816
Dependency : github.com/magefile/mage
38173817
Version: v1.15.0
@@ -18858,11 +18858,11 @@ Contents of probable licence file $GOMODCACHE/github.com/elastic/go-windows@v1.0
1885818858

1885918859
--------------------------------------------------------------------------------
1886018860
Dependency : github.com/elastic/gosigar
18861-
Version: v0.14.2
18861+
Version: v0.14.3
1886218862
Licence type (autodetected): Apache-2.0
1886318863
--------------------------------------------------------------------------------
1886418864

18865-
Contents of probable licence file $GOMODCACHE/github.com/elastic/gosigar@v0.14.2/LICENSE:
18865+
Contents of probable licence file $GOMODCACHE/github.com/elastic/gosigar@v0.14.3/LICENSE:
1886618866

1886718867
Apache License
1886818868
Version 2.0, January 2004
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Kind can be one of:
2+
# - breaking-change: a change to previously-documented behavior
3+
# - deprecation: functionality that is being removed in a later release
4+
# - bug-fix: fixes a problem in a previous version
5+
# - enhancement: extends functionality but does not break or fix existing behavior
6+
# - feature: new functionality
7+
# - known-issue: problems that we are aware of in a given version
8+
# - security: impacts on the security of a product or a user’s deployment.
9+
# - upgrade: important information for someone upgrading from a prior version
10+
# - other: does not fit into any of the other categories
11+
kind: feature
12+
13+
# Change summary; a 80ish characters long description of the change.
14+
summary: Add monitoring of endpoint usage metrics
15+
16+
# Long description; in case the summary is not enough to describe the change
17+
# this field accommodate a description without length limits.
18+
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment.
19+
#description:
20+
21+
# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc.
22+
component:
23+
24+
# PR URL; optional; the PR number that added the changeset.
25+
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
26+
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
27+
# Please provide it if you are adding a fragment for a different PR.
28+
pr: https://github.com/elastic/elastic-agent/pull/4789
29+
30+
# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
31+
# If not present is automatically filled by the tooling with the issue linked to the PR number.
32+
issue: https://github.com/elastic/elastic-agent/issues/4083

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ require (
1919
github.com/elastic/elastic-transport-go/v8 v8.6.0
2020
github.com/elastic/go-elasticsearch/v8 v8.14.0
2121
github.com/elastic/go-licenser v0.4.1
22-
github.com/elastic/go-service v0.0.0-20240611154109-f44f756f194f
2322
github.com/elastic/go-sysinfo v1.14.0
2423
github.com/elastic/go-ucfg v0.8.8
2524
github.com/elastic/mock-es v0.0.0-20240605193845-b5546a703d6f
@@ -37,6 +36,7 @@ require (
3736
github.com/jedib0t/go-pretty/v6 v6.4.6
3837
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901
3938
github.com/josephspurrier/goversioninfo v0.0.0-20190209210621-63e6d1acd3dd
39+
github.com/kardianos/service v1.2.1-0.20210728001519-a323c3813bc7
4040
github.com/magefile/mage v1.15.0
4141
github.com/mitchellh/gox v1.0.1
4242
github.com/mitchellh/hashstructure v1.1.0
@@ -144,7 +144,7 @@ require (
144144
github.com/elastic/go-elasticsearch/v7 v7.17.10 // indirect
145145
github.com/elastic/go-structform v0.0.10 // indirect
146146
github.com/elastic/go-windows v1.0.1 // indirect
147-
github.com/elastic/gosigar v0.14.2 // indirect
147+
github.com/elastic/gosigar v0.14.3 // indirect
148148
github.com/elastic/pkcs8 v1.0.0 // indirect
149149
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
150150
github.com/evanphx/json-patch v5.6.0+incompatible // indirect

go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -834,8 +834,6 @@ github.com/elastic/go-elasticsearch/v8 v8.14.0/go.mod h1:WRvnlGkSuZyp83M2U8El/LG
834834
github.com/elastic/go-licenser v0.3.1/go.mod h1:D8eNQk70FOCVBl3smCGQt/lv7meBeQno2eI1S5apiHQ=
835835
github.com/elastic/go-licenser v0.4.1 h1:1xDURsc8pL5zYT9R29425J3vkHdt4RT5TNEMeRN48x4=
836836
github.com/elastic/go-licenser v0.4.1/go.mod h1:V56wHMpmdURfibNBggaSBfqgPxyT1Tldns1i87iTEvU=
837-
github.com/elastic/go-service v0.0.0-20240611154109-f44f756f194f h1:kMtsJ3zfcBBR6wfbf5yUExmvWLru6R7zSLaJIfSjU3g=
838-
github.com/elastic/go-service v0.0.0-20240611154109-f44f756f194f/go.mod h1:3lVNFcuNMdWJrSpGF5SMPUVuC+qOYobfPIrMQUoizrk=
839837
github.com/elastic/go-structform v0.0.10 h1:oy08o/Ih2hHTkNcRY/1HhaYvIp5z6t8si8gnCJPDo1w=
840838
github.com/elastic/go-structform v0.0.10/go.mod h1:CZWf9aIRYY5SuKSmOhtXScE5uQiLZNqAFnwKR4OrIM4=
841839
github.com/elastic/go-sysinfo v1.1.1/go.mod h1:i1ZYdU10oLNfRzq4vq62BEwD2fH8KaWh6eh0ikPT9F0=
@@ -848,8 +846,8 @@ github.com/elastic/go-windows v1.0.0/go.mod h1:TsU0Nrp7/y3+VwE82FoZF8gC/XFg/Elz6
848846
github.com/elastic/go-windows v1.0.1 h1:AlYZOldA+UJ0/2nBuqWdo90GFCgG9xuyw9SYzGUtJm0=
849847
github.com/elastic/go-windows v1.0.1/go.mod h1:FoVvqWSun28vaDQPbj2Elfc0JahhPB7WQEGa3c814Ss=
850848
github.com/elastic/gojsonschema v1.2.1/go.mod h1:biw5eBS2Z4T02wjATMRSfecfjCmwaDPvuaqf844gLrg=
851-
github.com/elastic/gosigar v0.14.2 h1:Dg80n8cr90OZ7x+bAax/QjoW/XqTI11RmA79ZwIm9/4=
852-
github.com/elastic/gosigar v0.14.2/go.mod h1:iXRIGg2tLnu7LBdpqzyQfGDEidKCfWcCMS0WKyPWoMs=
849+
github.com/elastic/gosigar v0.14.3 h1:xwkKwPia+hSfg9GqrCUKYdId102m9qTJIIr7egmK/uo=
850+
github.com/elastic/gosigar v0.14.3/go.mod h1:iXRIGg2tLnu7LBdpqzyQfGDEidKCfWcCMS0WKyPWoMs=
853851
github.com/elastic/mock-es v0.0.0-20240605193845-b5546a703d6f h1:qo0Nn9G+p7HbG+CmiWwSoiKJ/cyRq59TRn9Z2PRHTi8=
854852
github.com/elastic/mock-es v0.0.0-20240605193845-b5546a703d6f/go.mod h1:mVdKBYYwt30xRFjCegbcURHh+3LWOCkQM33fgWbUiRI=
855853
github.com/elastic/package-spec/v2 v2.6.0/go.mod h1:ks9/FaVOS+vCrGRQcDvXAd2FlmB84mrLikbRiO6ACuk=
@@ -1326,6 +1324,8 @@ github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfV
13261324
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
13271325
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
13281326
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
1327+
github.com/kardianos/service v1.2.1-0.20210728001519-a323c3813bc7 h1:oohm9Rk9JAxxmp2NLZa7Kebgz9h4+AJDcc64txg3dQ0=
1328+
github.com/kardianos/service v1.2.1-0.20210728001519-a323c3813bc7/go.mod h1:CIMRFEJVL+0DS1a3Nx06NaMn4Dz63Ng6O7dl0qH0zVM=
13291329
github.com/karrick/godirwalk v1.15.6/go.mod h1:j4mkqPuvaLI8mp1DroR3P6ad7cyYd4c1qeJ3RV7ULlk=
13301330
github.com/karrick/godirwalk v1.16.1 h1:DynhcF+bztK8gooS0+NDJFrdNZjJ3gzVzC545UNA9iw=
13311331
github.com/karrick/godirwalk v1.16.1/go.mod h1:j4mkqPuvaLI8mp1DroR3P6ad7cyYd4c1qeJ3RV7ULlk=

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

+41-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"reflect"
1212
"strings"
13+
"sync/atomic"
1314
"time"
1415

1516
"github.com/hashicorp/go-multierror"
@@ -82,7 +83,12 @@ type MonitorManager interface {
8283
Reload(rawConfig *config.Config) error
8384

8485
// MonitoringConfig injects monitoring configuration into resolved ast tree.
85-
MonitoringConfig(map[string]interface{}, []component.Component, map[string]string) (map[string]interface{}, error)
86+
// args:
87+
// - the existing config policy
88+
// - a list of the expected running components
89+
// - a map of component IDs to binary names
90+
// - a map of component IDs to the PIDs of the running components.
91+
MonitoringConfig(map[string]interface{}, []component.Component, map[string]string, map[string]uint64) (map[string]interface{}, error)
8692
}
8793

8894
// Runner provides interface to run a manager and receive running errors.
@@ -284,6 +290,14 @@ type Coordinator struct {
284290
// loop in runLoopIteration() is active and listening.
285291
// Should only be interacted with via CoordinatorActive() or runLoopIteration()
286292
heartbeatChan chan struct{}
293+
294+
// if a component (mostly endpoint) has a new PID, we need to update
295+
// the monitoring components so they have a PID to monitor
296+
// however, if endpoint is in some kind of restart loop,
297+
// we could DOS the config system. Instead,
298+
// run a ticker that checks to see if we have a new PID.
299+
componentPIDTicker *time.Ticker
300+
componentPidRequiresUpdate *atomic.Bool
287301
}
288302

289303
// The channels Coordinator reads to receive updates from the various managers.
@@ -374,10 +388,12 @@ func New(logger *logger.Logger, cfg *configuration.Configuration, logLevel logp.
374388
// synchronization in the subscriber API, just set the input buffer to 0.
375389
stateBroadcaster: broadcaster.New(state, 64, 32),
376390

377-
logLevelCh: make(chan logp.Level),
378-
overrideStateChan: make(chan *coordinatorOverrideState),
379-
upgradeDetailsChan: make(chan *details.Details),
380-
heartbeatChan: make(chan struct{}),
391+
logLevelCh: make(chan logp.Level),
392+
overrideStateChan: make(chan *coordinatorOverrideState),
393+
upgradeDetailsChan: make(chan *details.Details),
394+
heartbeatChan: make(chan struct{}),
395+
componentPIDTicker: time.NewTicker(time.Second * 30),
396+
componentPidRequiresUpdate: &atomic.Bool{},
381397
}
382398
// Setup communication channels for any non-nil components. This pattern
383399
// lets us transparently accept nil managers / simulated events during
@@ -926,6 +942,8 @@ func (c *Coordinator) runner(ctx context.Context) error {
926942
ctx, cancel := context.WithCancel(ctx)
927943
defer cancel()
928944

945+
defer c.componentPIDTicker.Stop()
946+
929947
// We run nil checks before starting the various managers so that unit tests
930948
// only have to initialize / mock the specific components they're testing.
931949
// If a manager is nil, we prebuffer its return channel with nil also so
@@ -1038,6 +1056,18 @@ func (c *Coordinator) runLoopIteration(ctx context.Context) {
10381056

10391057
case c.heartbeatChan <- struct{}{}:
10401058

1059+
case <-c.componentPIDTicker.C:
1060+
// if we hit the ticker and we've got a new PID,
1061+
// reload the component model
1062+
if c.componentPidRequiresUpdate.Swap(false) {
1063+
err := c.refreshComponentModel(ctx)
1064+
if err != nil {
1065+
err = fmt.Errorf("error refreshing component model for PID update: %w", err)
1066+
c.setConfigManagerError(err)
1067+
c.logger.Errorf("%s", err)
1068+
}
1069+
}
1070+
10411071
case componentState := <-c.managerChans.runtimeManagerUpdate:
10421072
// New component change reported by the runtime manager via
10431073
// Coordinator.watchRuntimeComponents(), merge it with the
@@ -1277,11 +1307,17 @@ func (c *Coordinator) generateComponentModel() (err error) {
12771307
configInjector = c.monitorMgr.MonitoringConfig
12781308
}
12791309

1310+
var existingCompState = make(map[string]uint64, len(c.state.Components))
1311+
for _, comp := range c.state.Components {
1312+
existingCompState[comp.Component.ID] = comp.State.Pid
1313+
}
1314+
12801315
comps, err := c.specs.ToComponents(
12811316
cfg,
12821317
configInjector,
12831318
c.state.LogLevel,
12841319
c.agentInfo,
1320+
existingCompState,
12851321
)
12861322
if err != nil {
12871323
return fmt.Errorf("failed to render components: %w", err)

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

+9
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,24 @@ func (c *Coordinator) refreshState() {
135135
// Coordinator state and sets stateNeedsRefresh.
136136
// Must be called on the main Coordinator goroutine.
137137
func (c *Coordinator) applyComponentState(state runtime.ComponentComponentState) {
138+
139+
// check for any component updates to the known PID, so we can update the component monitoring
138140
found := false
139141
for i, other := range c.state.Components {
140142
if other.Component.ID == state.Component.ID {
143+
if other.State.Pid != state.State.Pid {
144+
c.componentPidRequiresUpdate.Store(true)
145+
}
141146
c.state.Components[i] = state
142147
found = true
143148
break
144149
}
145150
}
146151
if !found {
147152
c.state.Components = append(c.state.Components, state)
153+
if state.State.Pid != 0 {
154+
c.componentPidRequiresUpdate.Store(true)
155+
}
148156
}
149157

150158
// In the case that the component has stopped, it is now removed.
@@ -160,6 +168,7 @@ func (c *Coordinator) applyComponentState(state runtime.ComponentComponentState)
160168
}
161169

162170
c.stateNeedsRefresh = true
171+
163172
}
164173

165174
// generateReportableState aggregates the internal state of the Coordinator

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,6 @@ func TestCoordinator_StateSubscribeIsolatedUnits(t *testing.T) {
587587
resultChan <- ctx.Err()
588588
return
589589
case state := <-subChan:
590-
t.Logf("%+v", state)
591590
if len(state.Components) == 3 {
592591
compState0 := getComponentState(state.Components, "fake-isolated-units-default-fake-isolated-units-0")
593592
compState1 := getComponentState(state.Components, "fake-isolated-units-default-fake-isolated-units-1")
@@ -599,6 +598,11 @@ func TestCoordinator_StateSubscribeIsolatedUnits(t *testing.T) {
599598
(unit1.State == client.UnitStateHealthy && unit1.Message == "Healthy From Fake Isolated Units 1 Config") {
600599
resultChan <- nil
601600
return
601+
} else if unit0.State == client.UnitStateFailed && unit1.State == client.UnitStateFailed {
602+
// if you get a really strange failed state, check to make sure the mock binaries in
603+
// elastic-agent/pkg/component/fake/ are updated
604+
t.Fail()
605+
t.Logf("got units with failed state: %#v / %#v", unit1, unit0)
602606
}
603607
}
604608
}
@@ -1007,7 +1011,7 @@ func (*testMonitoringManager) Prepare(_ string) error
10071011
func (*testMonitoringManager) Cleanup(string) error { return nil }
10081012
func (*testMonitoringManager) Enabled() bool { return false }
10091013
func (*testMonitoringManager) Reload(rawConfig *config.Config) error { return nil }
1010-
func (*testMonitoringManager) MonitoringConfig(_ map[string]interface{}, _ []component.Component, _ map[string]string) (map[string]interface{}, error) {
1014+
func (*testMonitoringManager) MonitoringConfig(_ map[string]interface{}, _ []component.Component, _ map[string]string, _ map[string]uint64) (map[string]interface{}, error) {
10111015
return nil, nil
10121016
}
10131017

0 commit comments

Comments
 (0)