Skip to content

Commit

Permalink
Issue #518: Enable job duration through metricshub configuration
Browse files Browse the repository at this point in the history
* Add enableSelfMonitoring in AgentConfig, ResourceConfiguration and ResourceGroupConfiguration
* Transfer the new flag to HostConfiguration in Agent configuration helper
* Add the flag to HostConfiguration
* Update agent and engine tests
* Refactor the method setJobDurationMetricInHostMonitor to include the new flag value check
  • Loading branch information
MedMaalej committed Dec 16, 2024
1 parent 069a1e1 commit 3bf04aa
Show file tree
Hide file tree
Showing 13 changed files with 580 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ public class AgentConfig {

private boolean sequential;

@Default
private boolean enableSelfMonitoring = Boolean.TRUE;

private boolean resolveHostnameToFqdn;

@JsonSetter(nulls = SKIP)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public class ResourceConfig {
private Integer discoveryCycle;
private AlertingSystemConfig alertingSystemConfig;
private Boolean sequential;
private Boolean enableSelfMonitoring;
private Boolean resolveHostnameToFqdn;

@JsonSetter(nulls = SKIP)
Expand Down Expand Up @@ -126,6 +127,7 @@ public ResourceConfig copy() {
.discoveryCycle(discoveryCycle)
.alertingSystemConfig(alertingSystemConfig)
.sequential(sequential)
.enableSelfMonitoring(enableSelfMonitoring)
.monitorFilters(monitorFilters)
.resolveHostnameToFqdn(resolveHostnameToFqdn)
.jobTimeout(jobTimeout)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class ResourceGroupConfig {
private Integer discoveryCycle;
private AlertingSystemConfig alertingSystemConfig;
private Boolean sequential;
private Boolean enableSelfMonitoring;
private Boolean resolveHostnameToFqdn;

@JsonSetter(nulls = SKIP)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,11 @@ private static void normalizeResourceConfig(
resourceConfig.setSequential(resourceGroupConfig.getSequential());
}

// Set resource group configuration's enableSelfMonitoring flag in the resource configuration
if (resourceConfig.getEnableSelfMonitoring() == null) {
resourceConfig.setEnableSelfMonitoring(resourceGroupConfig.getEnableSelfMonitoring());
}

// Set resource group configuration's monitors filter in the resource configuration
if (resourceConfig.getMonitorFilters() == null) {
resourceConfig.setMonitorFilters(resourceGroupConfig.getMonitorFilters());
Expand Down Expand Up @@ -670,6 +675,11 @@ private static void normalizeResourceGroupConfig(
resourceGroupConfig.setSequential(agentConfig.isSequential());
}

// Set global enableSelfMonitoring flag in the resource group configuration
if (resourceGroupConfig.getEnableSelfMonitoring() == null) {
resourceGroupConfig.setEnableSelfMonitoring(agentConfig.isEnableSelfMonitoring());
}

// Set global configuration's monitors filter in the resource group configuration
if (resourceGroupConfig.getMonitorFilters() == null) {
resourceGroupConfig.setMonitorFilters(agentConfig.getMonitorFilters());
Expand Down Expand Up @@ -1081,6 +1091,7 @@ static HostConfiguration buildHostConfiguration(
.hostId(hostId)
.hostType(hostType)
.sequential(Boolean.TRUE.equals(resourceConfig.getSequential()))
.enableSelfMonitoring(resourceConfig.getEnableSelfMonitoring())
.includedMonitors(includedMonitors)
.excludedMonitors(excludedMonitors)
.configuredConnectorId(configuredConnectorId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -13,6 +14,7 @@
import static org.sentrysoftware.metricshub.agent.helper.ConfigHelper.TOP_LEVEL_VIRTUAL_RESOURCE_GROUP_KEY;
import static org.sentrysoftware.metricshub.agent.helper.TestConstants.SENTRY_PARIS_RESOURCE_GROUP_KEY;
import static org.sentrysoftware.metricshub.agent.helper.TestConstants.SERVER_1_RESOURCE_GROUP_KEY;
import static org.sentrysoftware.metricshub.agent.helper.TestConstants.TEST_CONFIG_FILE_PATH;
import static org.sentrysoftware.metricshub.agent.helper.TestConstants.TOP_LEVEL_RESOURCES_CONFIG_PATH;

import java.io.File;
Expand Down Expand Up @@ -255,6 +257,91 @@ void testBuildTelemetryManagersWithTopLevelResources() throws IOException {
);
}

@Test
void testEnableSelfMonitoringOnlyGlobalConfiguration() throws IOException {
// Find the configuration file
final File configFile = ConfigHelper.findConfigFile(TEST_CONFIG_FILE_PATH);

// Create the connector store
final ConnectorStore connectorStore = new ConnectorStore(Path.of("src/test/resources"));
final Connector connector = new Connector();
connector.getOrCreateConnectorIdentity().setCompiledFilename(PURE_STORAGE_REST_CONNECTOR_ID);
connectorStore.addOne(PURE_STORAGE_REST_CONNECTOR_ID, connector);

// Create the agent configuration
final AgentConfig agentConfig = JsonHelper.deserialize(
AgentContext.newAgentConfigObjectMapper(extensionManager),
new FileInputStream(configFile),
AgentConfig.class
);

// Normalize agent configuration
ConfigHelper.normalizeAgentConfiguration(agentConfig);

// Check self monitoring configuration
assertTrue(
agentConfig.getResourceGroups().get("sentry-paris").getResources().get("server-1").getEnableSelfMonitoring()
);
}

@Test
void testEnableSelfMonitoringConfigurationOverride() throws IOException {
// Find the configuration file
final File configFile = ConfigHelper.findConfigFile(
"src/test/resources/config/metricshub-enable-self-monitoring-override.yaml"
);

// Create the connector store
final ConnectorStore connectorStore = new ConnectorStore(Path.of("src/test/resources"));
final Connector connector = new Connector();
connector.getOrCreateConnectorIdentity().setCompiledFilename(PURE_STORAGE_REST_CONNECTOR_ID);
connectorStore.addOne(PURE_STORAGE_REST_CONNECTOR_ID, connector);

// Create the agent configuration
final AgentConfig agentConfig = JsonHelper.deserialize(
AgentContext.newAgentConfigObjectMapper(extensionManager),
new FileInputStream(configFile),
AgentConfig.class
);

// Normalize agent configuration
ConfigHelper.normalizeAgentConfiguration(agentConfig);

// Check self monitoring configuration
assertFalse(
agentConfig.getResourceGroups().get("sentry-paris").getResources().get("server-1").getEnableSelfMonitoring()
);
}

@Test
void testEnableSelfMonitoringNoConfiguration() throws IOException {
// Find the configuration file
final File configFile = ConfigHelper.findConfigFile(
"src/test/resources/config/metricshub-enable-self-monitoring-no-config.yaml"
);

// Create the connector store
final ConnectorStore connectorStore = new ConnectorStore(Path.of("src/test/resources"));
final Connector connector = new Connector();
connector.getOrCreateConnectorIdentity().setCompiledFilename(PURE_STORAGE_REST_CONNECTOR_ID);
connectorStore.addOne(PURE_STORAGE_REST_CONNECTOR_ID, connector);

// Create the agent configuration
final AgentConfig agentConfig = JsonHelper.deserialize(
AgentContext.newAgentConfigObjectMapper(extensionManager),
new FileInputStream(configFile),
AgentConfig.class
);

// Normalize agent configuration
ConfigHelper.normalizeAgentConfiguration(agentConfig);

// Check self monitoring configuration
assertTrue(
agentConfig.getResourceGroups().get("sentry-paris").getResources().get("server-1").getEnableSelfMonitoring()
);
}

@Test
void testUpdateConnectorStore() {
// Create a custom connectors Map
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Enables the debug mode of the core engine (Default: error). Possible values are: all, trace, debug, info, warn, error, or fatal.
loggerLevel: error

# Sets the number of jobs that MetricsHub can run simultaneously (Default: 20).
jobPoolSize: 20

# Sets the debug output directory for all the monitored resources.
outputDirectory: /opt/metricshub/logs

# Sets the collect period that MetricsHub uses to collects metrics from the monitored resources (Default: 2m).
collectPeriod: 1m

# Sets the cycle that MetricsHub uses to perform discoveries and detect new components in your monitored environment. By default, MetricsHub runs a discovery after 30 collects.
discoveryCycle: 30

# Configures MetricsHub internal alerting system
alertingSystem:

# Disables MetricsHub's alerts for all the monitored resources (Default: false).
disable: false

# Overrides the default problem template used to build the alert body for all the monitored resources.
problemTemplate: Problem on ${FQDN} with ${MONITOR_NAME}.${NEWLINE}${NEWLINE}${ALERT_DETAILS}${NEWLINE}${NEWLINE}${FULLREPORT}

# Forces all the network calls to be executed in sequential order for all the monitored resources - NOT RECOMMENDED (Default: false).
sequential: false

# Displays the configured host.name attribute in the Host Resource `host.name` attribute instead of the resolved FQDN (Default: false).
resolveHostnameToFqdn: false

# Resource Groups configuration
resourceGroups:
# Resource Group identifier
sentry-paris:
# Adds additional static attributes to all the resources in the group.
attributes:
site: Sentry-Paris

# Adds additional static metrics to all the resources in the group.
metrics:
hw.site.carbon_intensity: 230 # in g/kWh
hw.site.electricity_cost: 0.12 # in $/kWh
hw.site.pue: 1.8 # Power Usage Effectiveness. The ideal ratio is 1
resources:
# Resource configuration
server-1:
# Adds additional static attributes to the resource.
attributes:
host.name: server-1
host.type: storage
metrics:
hw.host.configured: 1
protocols:
http:
https: true
port: 443
username: username
password: password
# Resource configuration
snmp-resources:
# Adds additional static attributes to the resource.
attributes:
host.names: [ server-2, server-3 ] # Deprecated, change to host.name in the future
host.type: storage
metrics:
hw.host.configured: 1
protocols:
snmp:
timeout: 30s
community: public
version: v2c
# Resource configuration
grafana-service:
attributes:
service.name: Grafana
metrics:
metrics.grafana.configured: 1
monitors:
grafana:
simple: # simple job. Creates monitors and collects assicated metrics.
sources:
grafanaHealth:
type: http
url: https://hws-demo.sentrysoftware.com/api/health
method: GET
header: "Accept: application/json"
resultContent: body
mapping:
source: ${source::monitors.grafana.simple.sources.grafanaHealth}
attributes:
id: $1
service.version: $3
metrics:
grafana.db.state: $2
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Enables the debug mode of the core engine (Default: error). Possible values are: all, trace, debug, info, warn, error, or fatal.
loggerLevel: error

# Sets the number of jobs that MetricsHub can run simultaneously (Default: 20).
jobPoolSize: 20

# Sets the debug output directory for all the monitored resources.
outputDirectory: /opt/metricshub/logs

# Sets the collect period that MetricsHub uses to collects metrics from the monitored resources (Default: 2m).
collectPeriod: 1m

# Sets the cycle that MetricsHub uses to perform discoveries and detect new components in your monitored environment. By default, MetricsHub runs a discovery after 30 collects.
discoveryCycle: 30

# Configures MetricsHub internal alerting system
alertingSystem:

# Disables MetricsHub's alerts for all the monitored resources (Default: false).
disable: false

# Overrides the default problem template used to build the alert body for all the monitored resources.
problemTemplate: Problem on ${FQDN} with ${MONITOR_NAME}.${NEWLINE}${NEWLINE}${ALERT_DETAILS}${NEWLINE}${NEWLINE}${FULLREPORT}

# Forces all the network calls to be executed in sequential order for all the monitored resources - NOT RECOMMENDED (Default: false).
sequential: false

# Enables or disables the self monitoring (e.g: job duration metrics reporting)
enableSelfMonitoring: true

# Displays the configured host.name attribute in the Host Resource `host.name` attribute instead of the resolved FQDN (Default: false).
resolveHostnameToFqdn: false

# Resource Groups configuration
resourceGroups:
# Resource Group identifier
sentry-paris:
enableSelfMonitoring: false
# Adds additional static attributes to all the resources in the group.
attributes:
site: Sentry-Paris

# Adds additional static metrics to all the resources in the group.
metrics:
hw.site.carbon_intensity: 230 # in g/kWh
hw.site.electricity_cost: 0.12 # in $/kWh
hw.site.pue: 1.8 # Power Usage Effectiveness. The ideal ratio is 1
resources:
# Resource configuration
server-1:
# Adds additional static attributes to the resource.
attributes:
host.name: server-1
host.type: storage
metrics:
hw.host.configured: 1
protocols:
http:
https: true
port: 443
username: username
password: password
# Resource configuration
snmp-resources:
# Adds additional static attributes to the resource.
attributes:
host.names: [ server-2, server-3 ] # Deprecated, change to host.name in the future
host.type: storage
metrics:
hw.host.configured: 1
protocols:
snmp:
timeout: 30s
community: public
version: v2c
# Resource configuration
grafana-service:
attributes:
service.name: Grafana
metrics:
metrics.grafana.configured: 1
monitors:
grafana:
simple: # simple job. Creates monitors and collects assicated metrics.
sources:
grafanaHealth:
type: http
url: https://hws-demo.sentrysoftware.com/api/health
method: GET
header: "Accept: application/json"
resultContent: body
mapping:
source: ${source::monitors.grafana.simple.sources.grafanaHealth}
attributes:
id: $1
service.version: $3
metrics:
grafana.db.state: $2
3 changes: 3 additions & 0 deletions metricshub-agent/src/test/resources/config/metricshub.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ alertingSystem:
# Forces all the network calls to be executed in sequential order for all the monitored resources - NOT RECOMMENDED (Default: false).
sequential: false

# Enables or disables the self monitoring (e.g: job duration metrics reporting)
enableSelfMonitoring: true

# Displays the configured host.name attribute in the Host Resource `host.name` attribute instead of the resolved FQDN (Default: false).
resolveHostnameToFqdn: false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class HostConfiguration {

private Set<String> connectors;
private boolean sequential;
private Boolean enableSelfMonitoring;
private Consumer<AlertInfo> alertTrigger;
private long retryDelay;

Expand Down
Loading

0 comments on commit 3bf04aa

Please sign in to comment.