Skip to content

Commit

Permalink
Add fixes for rpm and add a flag to disable log collection (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
tejaskokje-mw authored Jul 23, 2024
1 parent 34f37c7 commit 95e4575
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 31 deletions.
22 changes: 20 additions & 2 deletions cmd/host-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,32 @@ func getFlags(execPath string, cfg *agent.HostConfig) []cli.Flag {
DefaultText: "1",
Value: 1, // default value is 1MB
}),
/* infra monitoring flag is deprecated. See log-collection flag */
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: "agent-features.infra-monitoring",
Usage: "Flag to enable or disable infrastructure monitoring.",
Usage: "Flag to enable or disable metric collection",
EnvVars: []string{"MW_AGENT_FEATURES_INFRA_MONITORING"},
Destination: &cfg.AgentFeatures.InfraMonitoring,
Destination: &cfg.AgentFeatures.MetricCollection,
DefaultText: "true",
Value: true, // infra monitoring is enabled by default
}),
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: "agent-features.metric-collection",
Usage: "Flag to enable or disable infrastructure monitoring.",
EnvVars: []string{"MW_AGENT_FEATURES_METRIC_COLLECTION"},
Destination: &cfg.AgentFeatures.MetricCollection,
Aliases: []string{"infra-monitoring"},
DefaultText: "true",
Value: true, // infra monitoring is enabled by default
}),
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: "agent-features.log-collection",
Usage: "Flag to enable or disable log collection.",
EnvVars: []string{"MW_AGENT_FEATURES_LOG_COLLECTION"},
Destination: &cfg.AgentFeatures.LogCollection,
DefaultText: "true",
Value: true, // log collection is enabled by default
}),
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: "mw-agent-self-profiling",
Usage: "For Profiling MW Agent itself.",
Expand Down
23 changes: 20 additions & 3 deletions cmd/kube-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,31 @@ func getFlags(cfg *agent.KubeConfig) []cli.Flag {
DefaultText: "unix:///var/run/docker.sock",
Value: "unix:///var/run/docker.sock",
}),

/* infra monitoring flag is deprecated. See log-collection flag */
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: "agent-features.infra-monitoring",
Usage: "Flag to enable or disable infrastructure monitoring",
Usage: "Flag to enable or disable metric collection",
EnvVars: []string{"MW_AGENT_FEATURES_INFRA_MONITORING"},
Destination: &cfg.AgentFeatures.InfraMonitoring,
Destination: &cfg.AgentFeatures.MetricCollection,
DefaultText: "true",
Value: true, // infra monitoring is enabled by default
}),
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: "agent-features.metric-collection",
Usage: "Flag to enable or disable metric collection",
EnvVars: []string{"MW_AGENT_FEATURES_METRIC_COLLECTION"},
Destination: &cfg.AgentFeatures.MetricCollection,
DefaultText: "true",
Value: true, // infra monitoring is enabled by default
}),
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: "agent-features.log-collection",
Usage: "Flag to enable or disable log collection.",
EnvVars: []string{"MW_AGENT_FEATURES_LOG_COLLECTION"},
Destination: &cfg.AgentFeatures.LogCollection,
DefaultText: "true",
Value: true, // log collection is enabled by default
}),
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: "mw-agent-self-profiling",
Usage: "For Profiling the agent itself",
Expand Down
13 changes: 9 additions & 4 deletions package-tooling/agent-config.yaml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ agent-internal-metrics-port: 8888
# agent-features provides feature flags to enable or disable features.
# The list of agent-features that can be enabled or disabled is given below
#
# infra-monitoring: By setting this flag to false, you can disable infrastructure
# monitoring from this agent. infra-monitoring is set to true by default.
# agent-features:
# infra-monitoring: false
# metric-collection: By setting this flag to false, you can disable metric
# collection from this agent (including custom metrics). log-collection is
# set to true by default.
#
# log-collection: By setting this flag to false, you can disable log
# collection from this agent. log-collection is set to true by default.
#agent-features:
# metric-collection: true
# log-collection: true
24 changes: 16 additions & 8 deletions package-tooling/linux/rpm/mw-agent.spec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ License: GPL
Group: Development/Tools
Source0: %{package_name}-%{release_version}-%{arch}.tar.gz
Provides: %{package_name}
Obsoletes: %{package_name} <= %{release_version}
Obsoletes: %{package_name} < %{release_version}

%description
Middleware Agent(%{package_name}) service enables you to monitor your infrastructure and applications.
Expand Down Expand Up @@ -40,13 +40,21 @@ chmod u+x /opt/%{package_name}/.postinstall.sh
/opt/%{package_name}/.postinstall.sh

%preun
systemctl stop %{package_name}
systemctl disable %{package_name}
if [ $1 -gt 0 ]; then
echo "Upgrade in progress, skipping pre-uninstallation steps."
else
systemctl stop %{package_name}
systemctl disable %{package_name}
fi

%postun
rm -f /etc/%{package_name}/agent-config.yaml
rm -f /etc/%{package_name}/otel-config.yaml
rmdir /etc/%{package_name}
rmdir /opt/%{package_name}/bin
rmdir /opt/%{package_name}
if [ $1 -gt 0 ]; then
echo "Upgrade in progress, skipping post-uninstallation steps."
else
rm -f /etc/%{package_name}/agent-config.yaml
rm -f /etc/%{package_name}/otel-config.yaml
rmdir /etc/%{package_name}
rmdir /opt/%{package_name}/bin
rmdir /opt/%{package_name}
fi

3 changes: 2 additions & 1 deletion pkg/agent/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ func (p InfraPlatform) String() string {
}

type AgentFeatures struct {
InfraMonitoring bool
MetricCollection bool
LogCollection bool
}

// BaseConfig stores general configuration for all agent types
Expand Down
51 changes: 51 additions & 0 deletions pkg/agent/hostagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,50 @@ func convertTabsToSpaces(input []byte, tabWidth int) []byte {
return output
}

func (c *HostAgent) updateConfigWithRestrictions(config map[string]interface{}) (map[string]interface{}, error) {

receiversData, ok := config[Receivers].(map[string]interface{})
if !ok {
return nil, ErrParseReceivers
}

serviceData, ok := config[Service].(map[string]interface{})
if !ok {
return nil, ErrParseService
}

pipelinesData, ok := serviceData[Pipelines].(map[string]interface{})
if !ok {
return nil, ErrParsePipelines
}

for key, _ := range pipelinesData {
if !c.HostConfig.AgentFeatures.LogCollection && strings.HasPrefix(key, "logs") {
delete(pipelinesData, key)
}

if !c.HostConfig.AgentFeatures.MetricCollection && strings.HasPrefix(key, "metrics") {
delete(pipelinesData, key)
}
}

if !c.HostConfig.AgentFeatures.LogCollection {
delete(receiversData, "filelog")
delete(receiversData, "windowseventlog")
}

if !c.HostConfig.AgentFeatures.MetricCollection {
delete(receiversData, "hostmetrics")
delete(receiversData, "windowsperfcounters")
delete(receiversData, "docker_stats")
delete(receiversData, "prometheus")
delete(receiversData, "kubeletstats")
delete(receiversData, "k8s_cluster")
}

return config, nil
}

func (c *HostAgent) updateConfig(config map[string]interface{}, cnf integrationConfiguration) (map[string]interface{}, error) {

if c.isIPPortFormat(cnf.Endpoint) {
Expand Down Expand Up @@ -318,6 +362,13 @@ func (c *HostAgent) updateConfigFile(configType string) error {

}

if !c.AgentFeatures.LogCollection || !c.AgentFeatures.MetricCollection {
apiYAMLConfig, err = c.updateConfigWithRestrictions(apiYAMLConfig)
if err != nil {
return err
}
}

apiYAMLBytes, err := yaml.Marshal(apiYAMLConfig)
if err != nil {
c.logger.Error("failed to marshal api data", zap.Error(err))
Expand Down
9 changes: 1 addition & 8 deletions pkg/agent/hostagent_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/postgresqlreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/redisreceiver"
"go.uber.org/zap"

"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/exporter/loggingexporter"
Expand Down Expand Up @@ -59,6 +58,7 @@ func (c *HostAgent) GetFactories(_ context.Context) (otelcol.Factories, error) {
kafkametricsreceiver.NewFactory(),
otlpreceiver.NewFactory(),
fluentforwardreceiver.NewFactory(),
hostmetricsreceiver.NewFactory(),
filelogreceiver.NewFactory(),
dockerstatsreceiver.NewFactory(),
prometheusreceiver.NewFactory(),
Expand All @@ -79,13 +79,6 @@ func (c *HostAgent) GetFactories(_ context.Context) (otelcol.Factories, error) {
awsecscontainermetricsreceiver.NewFactory())
}

// if infra monitoring is enabled, add hostmetricsreceiver
c.logger.Info("InfraMonitoring", zap.Bool("infra-monitoring", c.AgentFeatures.InfraMonitoring))
if c.AgentFeatures.InfraMonitoring {
receiverfactories = append(receiverfactories,
hostmetricsreceiver.NewFactory())
}

factories.Receivers, err = receiver.MakeFactoryMap(receiverfactories...)
if err != nil {
return otelcol.Factories{}, err
Expand Down
3 changes: 2 additions & 1 deletion pkg/agent/hostagent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ func assertContainsComponent(t *testing.T, factoryMap interface{}, componentName
func TestHostAgentGetFactories(t *testing.T) {
baseConfig := BaseConfig{
AgentFeatures: AgentFeatures{
InfraMonitoring: true,
MetricCollection: true,
LogCollection: true,
},
}

Expand Down
10 changes: 6 additions & 4 deletions pkg/agent/hostagent_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ func (c *HostAgent) GetFactories(ctx context.Context) (otelcol.Factories, error)
return otelcol.Factories{}, err
}

factories.Receivers, err = receiver.MakeFactoryMap([]receiver.Factory{
receiverfactories := []receiver.Factory{
kafkametricsreceiver.NewFactory(),
jmxreceiver.NewFactory(),
otlpreceiver.NewFactory(),
fluentforwardreceiver.NewFactory(),
hostmetricsreceiver.NewFactory(),
filelogreceiver.NewFactory(),
fluentforwardreceiver.NewFactory(),
dockerstatsreceiver.NewFactory(),
hostmetricsreceiver.NewFactory(),
prometheusreceiver.NewFactory(),
postgresqlreceiver.NewFactory(),
windowseventlogreceiver.NewFactory(),
Expand All @@ -71,7 +71,9 @@ func (c *HostAgent) GetFactories(ctx context.Context) (otelcol.Factories, error)
redisreceiver.NewFactory(),
apachereceiver.NewFactory(),
oracledbreceiver.NewFactory(),
}...)
}

factories.Receivers, err = receiver.MakeFactoryMap(receiverfactories...)
if err != nil {
return otelcol.Factories{}, err
}
Expand Down

0 comments on commit 95e4575

Please sign in to comment.