Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/go_modules/github.com/elastic/go-…
Browse files Browse the repository at this point in the history
…elasticsearch/v8-8.11.0
  • Loading branch information
cmacknz authored Feb 1, 2024
2 parents 0093188 + 08ac21a commit 2b23885
Show file tree
Hide file tree
Showing 14 changed files with 400 additions and 154 deletions.
76 changes: 38 additions & 38 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6139,6 +6139,44 @@ Contents of probable licence file $GOMODCACHE/github.com/spf13/cobra@v1.8.0/LICE
of your accepting any such warranty or additional liability.


--------------------------------------------------------------------------------
Dependency : github.com/spf13/pflag
Version: v1.0.5
Licence type (autodetected): BSD-3-Clause
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/github.com/spf13/pflag@v1.0.5/LICENSE:

Copyright (c) 2012 Alex Ogier. All rights reserved.
Copyright (c) 2012 The Go Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


--------------------------------------------------------------------------------
Dependency : github.com/stretchr/testify
Version: v1.8.4
Expand Down Expand Up @@ -26663,44 +26701,6 @@ Contents of probable licence file $GOMODCACHE/github.com/spf13/afero@v1.9.5/LICE
of your accepting any such warranty or additional liability.


--------------------------------------------------------------------------------
Dependency : github.com/spf13/pflag
Version: v1.0.5
Licence type (autodetected): BSD-3-Clause
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/github.com/spf13/pflag@v1.0.5/LICENSE:

Copyright (c) 2012 Alex Ogier. All rights reserved.
Copyright (c) 2012 The Go Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


--------------------------------------------------------------------------------
Dependency : github.com/stretchr/objx
Version: v0.5.0
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ require (
github.com/shirou/gopsutil/v3 v3.23.12
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
github.com/tsg/go-daemon v0.0.0-20200207173439-e704b93fd89b
go.elastic.co/apm/module/apmgorilla v1.15.0
Expand Down Expand Up @@ -187,7 +188,6 @@ require (
github.com/sergi/go-diff v1.2.0 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/agent/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ func New(

// testing mode uses a config manager that takes configuration from over the control protocol
configMgr = newTestingModeConfigManager(log)
} else if runAsOtel {
// ignoring configuration in elastic-agent.yml
configMgr = otel.NewOtelModeConfigManager()
} else if configuration.IsStandalone(cfg.Fleet) {
log.Info("Parsed configuration and determined agent is managed locally")

Expand All @@ -146,9 +149,6 @@ func New(
log.Debugf("Reloading of configuration is on, frequency is set to %s", cfg.Settings.Reload.Period)
configMgr = newPeriodic(log, cfg.Settings.Reload.Period, discover, loader)
}
} else if runAsOtel {
// ignoring configuration in elastic-agent.yml
configMgr = otel.NewOtelModeConfigManager()
} else {
isManaged = true
var store storage.Store
Expand Down
15 changes: 14 additions & 1 deletion internal/pkg/agent/application/paths/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
const (
// DefaultConfigName is the default name of the configuration file.
DefaultConfigName = "elastic-agent.yml"
// DefaultOtelConfigName is the default name of the otel configuration file.
DefaultOtelConfigName = "otel.yml"

// AgentLockFileName is the name of the overall Elastic Agent file lock.
AgentLockFileName = "agent.lock"
Expand Down Expand Up @@ -151,8 +153,19 @@ func SetConfig(path string) {

// ConfigFile returns the path to the configuration file.
func ConfigFile() string {

return configFileWithDefaultOverride(DefaultConfigName)
}

// OtelConfigFile returns the path to the otel configuration file.
func OtelConfigFile() string {
return configFileWithDefaultOverride(DefaultOtelConfigName)
}

// configFileWithDefaultOverride returns the path to the configuration file overriding default value.
func configFileWithDefaultOverride(defaultConfig string) string {
if configFilePath == "" || configFilePath == DefaultConfigName {
return filepath.Join(Config(), DefaultConfigName)
return filepath.Join(Config(), defaultConfig)
}
if filepath.IsAbs(configFilePath) {
return configFilePath
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/agent/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func NewCommandWithArgs(args []string, streams *cli.IOStreams) *cobra.Command {
cmd.AddCommand(newDiagnosticsCommand(args, streams))
cmd.AddCommand(newComponentCommandWithArgs(args, streams))
cmd.AddCommand(newLogsCommandWithArgs(args, streams))
cmd.AddCommand(newOtelCommandWithArgs(args, streams))

// windows special hidden sub-command (only added on Windows)
reexec := newReExecWindowsCommand(args, streams)
Expand Down
125 changes: 125 additions & 0 deletions internal/pkg/agent/cmd/otel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// 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 cmd

import (
"context"
"sync"

"github.com/hashicorp/go-multierror"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/elastic/elastic-agent-libs/service"
"github.com/elastic/elastic-agent/internal/pkg/agent/errors"
"github.com/elastic/elastic-agent/internal/pkg/cli"
"github.com/elastic/elastic-agent/internal/pkg/otel"
)

const (
configFlagName = "config"
setFlagName = "set"
)

func newOtelCommandWithArgs(_ []string, _ *cli.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: "otel",
Short: "Start the Elastic Agent in otel mode",
Long: "This command starts the Elastic Agent in otel mode.",
RunE: func(cmd *cobra.Command, _ []string) error {
cfgFiles, err := getConfigFiles(cmd)
if err != nil {
return err
}
return runCollector(cmd.Context(), cfgFiles)
},
PreRun: func(c *cobra.Command, args []string) {
// hide inherited flags not to bloat help with flags not related to otel
hideInheritedFlags(c)
},
SilenceUsage: true,
SilenceErrors: true,
}

cmd.SetHelpFunc(func(c *cobra.Command, s []string) {
hideInheritedFlags(c)
c.Parent().HelpFunc()(c, s)
})

cmd.Flags().StringArray(configFlagName, []string{}, "Locations to the config file(s), note that only a"+
" single location can be set per flag entry e.g. `--config=file:/path/to/first --config=file:path/to/second`.")

cmd.Flags().StringArray(setFlagName, []string{}, "Set arbitrary component config property. The component has to be defined in the config file and the flag"+
" has a higher precedence. Array config properties are overridden and maps are joined. Example --set=processors.batch.timeout=2s")
return cmd
}

func hideInheritedFlags(c *cobra.Command) {
c.InheritedFlags().VisitAll(func(f *pflag.Flag) {
f.Hidden = true
})
}

func runCollector(cmdCtx context.Context, configFiles []string) error {
// Windows: Mark service as stopped.
// After this is run, the service is considered by the OS to be stopped.
// This must be the first deferred cleanup task (last to execute).
defer func() {
service.NotifyTermination()
service.WaitExecutionDone()
}()

service.BeforeRun()
defer service.Cleanup()

stop := make(chan bool)
ctx, cancel := context.WithCancel(cmdCtx)

var stopCollector = func() {
close(stop)
}

defer cancel()
go service.ProcessWindowsControlEvents(stopCollector)

var otelStartWg sync.WaitGroup
var resErr error
var awaiters awaiters

otelAwaiter := make(chan struct{})
awaiters = append(awaiters, otelAwaiter)

otelStartWg.Add(1)
go func() {
otelStartWg.Done()
if err := otel.Run(ctx, stop, configFiles); err != nil {
resErr = multierror.Append(resErr, err)
// otel collector finished with an error, exit run loop
cancel()
}

// close awaiter handled in run loop
close(otelAwaiter)
}()

// wait for otel to start
otelStartWg.Wait()

if err := runElasticAgent(
ctx,
cancel,
nil, // no config overrides
stop, // service hook
false, // not in testing mode
0, // no fleet config
true, // is otel mode
awaiters, // wait for otel to finish
); err != nil && !errors.Is(err, context.Canceled) {
resErr = multierror.Append(resErr, err)
}

return resErr

}
57 changes: 57 additions & 0 deletions internal/pkg/agent/cmd/otel_flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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 cmd

import (
"fmt"
"strings"

"github.com/spf13/cobra"

"github.com/elastic/elastic-agent/internal/pkg/agent/application/paths"
)

func getConfigFiles(cmd *cobra.Command) ([]string, error) {
configFiles, err := cmd.Flags().GetStringArray(configFlagName)
if err != nil {
return nil, fmt.Errorf("failed to retrieve config flags: %w", err)
}

if len(configFiles) == 0 {
configFiles = append(configFiles, paths.OtelConfigFile())
}

setVals, err := cmd.Flags().GetStringArray(setFlagName)
if err != nil {
return nil, fmt.Errorf("failed to retrieve set flags: %w", err)
}

sets, err := getSets(setVals)
if err != nil {
return nil, err
}

configFiles = append(configFiles, sets...)
return configFiles, nil
}

func getSets(setVals []string) ([]string, error) {
var sets []string
for _, s := range setVals {
idx := strings.Index(s, "=")
if idx == -1 {
return nil, fmt.Errorf("missing equal sign for set value %q", s)
}
sets = append(sets, setToYaml(s, idx))
}
return sets, nil
}

func setToYaml(set string, eqIdx int) string {
if len(set) == 0 {
return set
}
return "yaml:" + strings.TrimSpace(strings.ReplaceAll(set[:eqIdx], ".", "::")) + ": " + strings.TrimSpace(set[eqIdx+1:])
}
Loading

0 comments on commit 2b23885

Please sign in to comment.