Skip to content

Commit 95e4575

Browse files
Add fixes for rpm and add a flag to disable log collection (#167)
1 parent 34f37c7 commit 95e4575

File tree

9 files changed

+127
-31
lines changed

9 files changed

+127
-31
lines changed

cmd/host-agent/main.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,32 @@ func getFlags(execPath string, cfg *agent.HostConfig) []cli.Flag {
135135
DefaultText: "1",
136136
Value: 1, // default value is 1MB
137137
}),
138+
/* infra monitoring flag is deprecated. See log-collection flag */
138139
altsrc.NewBoolFlag(&cli.BoolFlag{
139140
Name: "agent-features.infra-monitoring",
140-
Usage: "Flag to enable or disable infrastructure monitoring.",
141+
Usage: "Flag to enable or disable metric collection",
141142
EnvVars: []string{"MW_AGENT_FEATURES_INFRA_MONITORING"},
142-
Destination: &cfg.AgentFeatures.InfraMonitoring,
143+
Destination: &cfg.AgentFeatures.MetricCollection,
143144
DefaultText: "true",
144145
Value: true, // infra monitoring is enabled by default
145146
}),
147+
altsrc.NewBoolFlag(&cli.BoolFlag{
148+
Name: "agent-features.metric-collection",
149+
Usage: "Flag to enable or disable infrastructure monitoring.",
150+
EnvVars: []string{"MW_AGENT_FEATURES_METRIC_COLLECTION"},
151+
Destination: &cfg.AgentFeatures.MetricCollection,
152+
Aliases: []string{"infra-monitoring"},
153+
DefaultText: "true",
154+
Value: true, // infra monitoring is enabled by default
155+
}),
156+
altsrc.NewBoolFlag(&cli.BoolFlag{
157+
Name: "agent-features.log-collection",
158+
Usage: "Flag to enable or disable log collection.",
159+
EnvVars: []string{"MW_AGENT_FEATURES_LOG_COLLECTION"},
160+
Destination: &cfg.AgentFeatures.LogCollection,
161+
DefaultText: "true",
162+
Value: true, // log collection is enabled by default
163+
}),
146164
altsrc.NewBoolFlag(&cli.BoolFlag{
147165
Name: "mw-agent-self-profiling",
148166
Usage: "For Profiling MW Agent itself.",

cmd/kube-agent/main.go

+20-3
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,31 @@ func getFlags(cfg *agent.KubeConfig) []cli.Flag {
6868
DefaultText: "unix:///var/run/docker.sock",
6969
Value: "unix:///var/run/docker.sock",
7070
}),
71-
71+
/* infra monitoring flag is deprecated. See log-collection flag */
7272
altsrc.NewBoolFlag(&cli.BoolFlag{
7373
Name: "agent-features.infra-monitoring",
74-
Usage: "Flag to enable or disable infrastructure monitoring",
74+
Usage: "Flag to enable or disable metric collection",
7575
EnvVars: []string{"MW_AGENT_FEATURES_INFRA_MONITORING"},
76-
Destination: &cfg.AgentFeatures.InfraMonitoring,
76+
Destination: &cfg.AgentFeatures.MetricCollection,
77+
DefaultText: "true",
78+
Value: true, // infra monitoring is enabled by default
79+
}),
80+
altsrc.NewBoolFlag(&cli.BoolFlag{
81+
Name: "agent-features.metric-collection",
82+
Usage: "Flag to enable or disable metric collection",
83+
EnvVars: []string{"MW_AGENT_FEATURES_METRIC_COLLECTION"},
84+
Destination: &cfg.AgentFeatures.MetricCollection,
85+
DefaultText: "true",
7786
Value: true, // infra monitoring is enabled by default
7887
}),
88+
altsrc.NewBoolFlag(&cli.BoolFlag{
89+
Name: "agent-features.log-collection",
90+
Usage: "Flag to enable or disable log collection.",
91+
EnvVars: []string{"MW_AGENT_FEATURES_LOG_COLLECTION"},
92+
Destination: &cfg.AgentFeatures.LogCollection,
93+
DefaultText: "true",
94+
Value: true, // log collection is enabled by default
95+
}),
7996
altsrc.NewBoolFlag(&cli.BoolFlag{
8097
Name: "mw-agent-self-profiling",
8198
Usage: "For Profiling the agent itself",

package-tooling/agent-config.yaml.sample

+9-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ agent-internal-metrics-port: 8888
3333
# agent-features provides feature flags to enable or disable features.
3434
# The list of agent-features that can be enabled or disabled is given below
3535
#
36-
# infra-monitoring: By setting this flag to false, you can disable infrastructure
37-
# monitoring from this agent. infra-monitoring is set to true by default.
38-
# agent-features:
39-
# infra-monitoring: false
36+
# metric-collection: By setting this flag to false, you can disable metric
37+
# collection from this agent (including custom metrics). log-collection is
38+
# set to true by default.
39+
#
40+
# log-collection: By setting this flag to false, you can disable log
41+
# collection from this agent. log-collection is set to true by default.
42+
#agent-features:
43+
# metric-collection: true
44+
# log-collection: true

package-tooling/linux/rpm/mw-agent.spec

+16-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ License: GPL
66
Group: Development/Tools
77
Source0: %{package_name}-%{release_version}-%{arch}.tar.gz
88
Provides: %{package_name}
9-
Obsoletes: %{package_name} <= %{release_version}
9+
Obsoletes: %{package_name} < %{release_version}
1010

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

4242
%preun
43-
systemctl stop %{package_name}
44-
systemctl disable %{package_name}
43+
if [ $1 -gt 0 ]; then
44+
echo "Upgrade in progress, skipping pre-uninstallation steps."
45+
else
46+
systemctl stop %{package_name}
47+
systemctl disable %{package_name}
48+
fi
4549

4650
%postun
47-
rm -f /etc/%{package_name}/agent-config.yaml
48-
rm -f /etc/%{package_name}/otel-config.yaml
49-
rmdir /etc/%{package_name}
50-
rmdir /opt/%{package_name}/bin
51-
rmdir /opt/%{package_name}
51+
if [ $1 -gt 0 ]; then
52+
echo "Upgrade in progress, skipping post-uninstallation steps."
53+
else
54+
rm -f /etc/%{package_name}/agent-config.yaml
55+
rm -f /etc/%{package_name}/otel-config.yaml
56+
rmdir /etc/%{package_name}
57+
rmdir /opt/%{package_name}/bin
58+
rmdir /opt/%{package_name}
59+
fi
5260

pkg/agent/definitions.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ func (p InfraPlatform) String() string {
6363
}
6464

6565
type AgentFeatures struct {
66-
InfraMonitoring bool
66+
MetricCollection bool
67+
LogCollection bool
6768
}
6869

6970
// BaseConfig stores general configuration for all agent types

pkg/agent/hostagent.go

+51
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,50 @@ func convertTabsToSpaces(input []byte, tabWidth int) []byte {
172172
return output
173173
}
174174

175+
func (c *HostAgent) updateConfigWithRestrictions(config map[string]interface{}) (map[string]interface{}, error) {
176+
177+
receiversData, ok := config[Receivers].(map[string]interface{})
178+
if !ok {
179+
return nil, ErrParseReceivers
180+
}
181+
182+
serviceData, ok := config[Service].(map[string]interface{})
183+
if !ok {
184+
return nil, ErrParseService
185+
}
186+
187+
pipelinesData, ok := serviceData[Pipelines].(map[string]interface{})
188+
if !ok {
189+
return nil, ErrParsePipelines
190+
}
191+
192+
for key, _ := range pipelinesData {
193+
if !c.HostConfig.AgentFeatures.LogCollection && strings.HasPrefix(key, "logs") {
194+
delete(pipelinesData, key)
195+
}
196+
197+
if !c.HostConfig.AgentFeatures.MetricCollection && strings.HasPrefix(key, "metrics") {
198+
delete(pipelinesData, key)
199+
}
200+
}
201+
202+
if !c.HostConfig.AgentFeatures.LogCollection {
203+
delete(receiversData, "filelog")
204+
delete(receiversData, "windowseventlog")
205+
}
206+
207+
if !c.HostConfig.AgentFeatures.MetricCollection {
208+
delete(receiversData, "hostmetrics")
209+
delete(receiversData, "windowsperfcounters")
210+
delete(receiversData, "docker_stats")
211+
delete(receiversData, "prometheus")
212+
delete(receiversData, "kubeletstats")
213+
delete(receiversData, "k8s_cluster")
214+
}
215+
216+
return config, nil
217+
}
218+
175219
func (c *HostAgent) updateConfig(config map[string]interface{}, cnf integrationConfiguration) (map[string]interface{}, error) {
176220

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

319363
}
320364

365+
if !c.AgentFeatures.LogCollection || !c.AgentFeatures.MetricCollection {
366+
apiYAMLConfig, err = c.updateConfigWithRestrictions(apiYAMLConfig)
367+
if err != nil {
368+
return err
369+
}
370+
}
371+
321372
apiYAMLBytes, err := yaml.Marshal(apiYAMLConfig)
322373
if err != nil {
323374
c.logger.Error("failed to marshal api data", zap.Error(err))

pkg/agent/hostagent_linux.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/postgresqlreceiver"
2929
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver"
3030
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/redisreceiver"
31-
"go.uber.org/zap"
3231

3332
"go.opentelemetry.io/collector/exporter"
3433
"go.opentelemetry.io/collector/exporter/loggingexporter"
@@ -59,6 +58,7 @@ func (c *HostAgent) GetFactories(_ context.Context) (otelcol.Factories, error) {
5958
kafkametricsreceiver.NewFactory(),
6059
otlpreceiver.NewFactory(),
6160
fluentforwardreceiver.NewFactory(),
61+
hostmetricsreceiver.NewFactory(),
6262
filelogreceiver.NewFactory(),
6363
dockerstatsreceiver.NewFactory(),
6464
prometheusreceiver.NewFactory(),
@@ -79,13 +79,6 @@ func (c *HostAgent) GetFactories(_ context.Context) (otelcol.Factories, error) {
7979
awsecscontainermetricsreceiver.NewFactory())
8080
}
8181

82-
// if infra monitoring is enabled, add hostmetricsreceiver
83-
c.logger.Info("InfraMonitoring", zap.Bool("infra-monitoring", c.AgentFeatures.InfraMonitoring))
84-
if c.AgentFeatures.InfraMonitoring {
85-
receiverfactories = append(receiverfactories,
86-
hostmetricsreceiver.NewFactory())
87-
}
88-
8982
factories.Receivers, err = receiver.MakeFactoryMap(receiverfactories...)
9083
if err != nil {
9184
return otelcol.Factories{}, err

pkg/agent/hostagent_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ func assertContainsComponent(t *testing.T, factoryMap interface{}, componentName
227227
func TestHostAgentGetFactories(t *testing.T) {
228228
baseConfig := BaseConfig{
229229
AgentFeatures: AgentFeatures{
230-
InfraMonitoring: true,
230+
MetricCollection: true,
231+
LogCollection: true,
231232
},
232233
}
233234

pkg/agent/hostagent_windows.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ func (c *HostAgent) GetFactories(ctx context.Context) (otelcol.Factories, error)
5353
return otelcol.Factories{}, err
5454
}
5555

56-
factories.Receivers, err = receiver.MakeFactoryMap([]receiver.Factory{
56+
receiverfactories := []receiver.Factory{
5757
kafkametricsreceiver.NewFactory(),
5858
jmxreceiver.NewFactory(),
5959
otlpreceiver.NewFactory(),
60-
fluentforwardreceiver.NewFactory(),
60+
hostmetricsreceiver.NewFactory(),
6161
filelogreceiver.NewFactory(),
62+
fluentforwardreceiver.NewFactory(),
6263
dockerstatsreceiver.NewFactory(),
63-
hostmetricsreceiver.NewFactory(),
6464
prometheusreceiver.NewFactory(),
6565
postgresqlreceiver.NewFactory(),
6666
windowseventlogreceiver.NewFactory(),
@@ -71,7 +71,9 @@ func (c *HostAgent) GetFactories(ctx context.Context) (otelcol.Factories, error)
7171
redisreceiver.NewFactory(),
7272
apachereceiver.NewFactory(),
7373
oracledbreceiver.NewFactory(),
74-
}...)
74+
}
75+
76+
factories.Receivers, err = receiver.MakeFactoryMap(receiverfactories...)
7577
if err != nil {
7678
return otelcol.Factories{}, err
7779
}

0 commit comments

Comments
 (0)