diff --git a/metricshub-doc/src/site/markdown/configuration/configure-monitoring.md b/metricshub-doc/src/site/markdown/configuration/configure-monitoring.md index e476261c3..5679e5f26 100644 --- a/metricshub-doc/src/site/markdown/configuration/configure-monitoring.md +++ b/metricshub-doc/src/site/markdown/configuration/configure-monitoring.md @@ -1209,6 +1209,81 @@ hw.status{state="degraded"} 1 In this case, only the `degraded` state is reported, and the zero values for `ok` and `failed` are suppressed after the initial state transition. +## Self-Monitoring + +**MetricsHub** includes **self-monitoring capabilities** to track its own performance. This feature can monitor key aspects such **job duration metrics**. + +### Configuration: `enableSelfMonitoring` + +This configuration controls whether **MetricsHub** reports internal signals such as job duration metrics. + +#### Supported Values + +- `true` (default): Enables self-monitoring capabilities. +- `false`: Disables self-monitoring capabilities. + +#### Configuration Scopes + +You can configure `enableSelfMonitoring` at the following levels: + +1. **Global Configuration** + Applies to all monitored resources. + + ```yaml + enableSelfMonitoring: true # Set to "false" to disable + resourceGroups: ... + ``` + +2. **Per Resource Group** + Applies to all resources within a specific group. + + ```yaml + resourceGroups: + : + enableSelfMonitoring: true # Set to "false" to disable + resources: ... + ``` + +3. **Per Resource** + Applies to an individual resource. + + ```yaml + resourceGroups: + : + resources: + : + enableSelfMonitoring: true # Set to "false" to disable + ``` + +### Examples of Self-Monitoring Metrics + +When enabled, **MetricsHub** reports the `metricshub.job.duration` metrics, for example: + +``` +metricshub.job.duration{job.type="discovery", monitor.type="enclosure", connector_id="HPEGen10IloREST"} 0.020 +metricshub.job.duration{job.type="discovery", monitor.type="cpu", connector_id="HPEGen10IloREST"} 0.030 +metricshub.job.duration{job.type="discovery", monitor.type="temperature", connector_id="HPEGen10IloREST"} 0.025 +metricshub.job.duration{job.type="discovery", monitor.type="connector", connector_id="HPEGen10IloREST"} 0.015 +metricshub.job.duration{job.type="collect", monitor.type="cpu", connector_id="HPEGen10IloREST"} 0.015 +``` + +Where: +- **`job.type`**: Specifies the type of operation performed by MetricsHub. + - Possible values: + - `discovery`: Identifies and registers components. + - `collect`: Gathers telemetry data from the monitored components. + - `simple`: Executes a single straightforward task. + - `beforeAll` or `afterAll`: Runs preparatory or cleanup operations. +- **`monitor.type`**: Indicates the specific category of component being monitored. + - Examples: + - Hardware components like `cpu`, `memory`, `physical_disk`, or `disk_controller`. + - Environmental metrics like `temperature` or `battery`. + - Logical entities like `connector`. +- **`connector_id`**: The unique identifier of the connector defining the method and protocol to collect metrics for the specified component. + - Example: `"HPEGen10IloREST"` denotes the HPE Gen10 iLO REST connector. + +These metrics provide granular insights into task execution times, enabling the identification of bottlenecks or inefficiencies and helping optimize monitoring performance. + #### Timeout, duration and period format Timeouts, durations and periods are specified with the below format: diff --git a/metricshub-engine/src/main/java/org/sentrysoftware/metricshub/engine/connector/model/Connector.java b/metricshub-engine/src/main/java/org/sentrysoftware/metricshub/engine/connector/model/Connector.java index 7944cad93..19ba49593 100644 --- a/metricshub-engine/src/main/java/org/sentrysoftware/metricshub/engine/connector/model/Connector.java +++ b/metricshub-engine/src/main/java/org/sentrysoftware/metricshub/engine/connector/model/Connector.java @@ -115,7 +115,7 @@ public class Connector implements Serializable { private Map afterAll = new HashMap<>(); /** - * Map of monitor jobs, where each key is the name of the monitor job and the value is its definition. + * Map of monitor jobs, where each key is the name of the monitor type and the value is the monitor job instance. */ @Default private Map monitors = new LinkedHashMap<>(); diff --git a/metricshub-engine/src/main/java/org/sentrysoftware/metricshub/engine/strategy/AbstractAllAtOnceStrategy.java b/metricshub-engine/src/main/java/org/sentrysoftware/metricshub/engine/strategy/AbstractAllAtOnceStrategy.java index 3d9bb37b2..4a537eec6 100644 --- a/metricshub-engine/src/main/java/org/sentrysoftware/metricshub/engine/strategy/AbstractAllAtOnceStrategy.java +++ b/metricshub-engine/src/main/java/org/sentrysoftware/metricshub/engine/strategy/AbstractAllAtOnceStrategy.java @@ -94,11 +94,18 @@ protected AbstractAllAtOnceStrategy( /** * This method processes each connector * - * @param currentConnector - * @param hostname + * @param currentConnector The current connector + * @param hostname The host name */ private void process(final Connector currentConnector, final String hostname) { - if (!validateConnectorDetectionCriteria(currentConnector, hostname)) { + // Check whether the strategy job name matches at least one of the monitor jobs names of the current connector + final boolean connectorHasExpectedJobTypes = hasExpectedJobTypes(currentConnector, getJobName()); + // If the connector doesn't define any monitor job that matches the given strategy job name, log a message then exit the current discovery or simple operation + if (!connectorHasExpectedJobTypes) { + log.debug("Connector doesn't define any monitor job of type {}.", getJobName()); + return; + } + if (!validateConnectorDetectionCriteria(currentConnector, hostname, getJobName())) { log.error( "Hostname {} - The connector {} no longer matches the host. Stopping the connector's {} job.", hostname, @@ -255,13 +262,8 @@ private void processMonitorJob( processSameTypeMonitors(currentConnector, mapping, monitorType, hostname, monitorJob); final long jobEndTime = System.currentTimeMillis(); - setJobDurationMetricInHostMonitor( - getJobName(), - monitorType, - currentConnector.getCompiledFilename(), - jobStartTime, - jobEndTime - ); + // Set the job duration metric in the host monitor + setJobDurationMetric(getJobName(), monitorType, currentConnector.getCompiledFilename(), jobStartTime, jobEndTime); } /** diff --git a/metricshub-engine/src/main/java/org/sentrysoftware/metricshub/engine/strategy/AbstractStrategy.java b/metricshub-engine/src/main/java/org/sentrysoftware/metricshub/engine/strategy/AbstractStrategy.java index 667a67546..1d2f079a3 100644 --- a/metricshub-engine/src/main/java/org/sentrysoftware/metricshub/engine/strategy/AbstractStrategy.java +++ b/metricshub-engine/src/main/java/org/sentrysoftware/metricshub/engine/strategy/AbstractStrategy.java @@ -45,6 +45,8 @@ import org.sentrysoftware.metricshub.engine.common.helpers.TextTableHelper; import org.sentrysoftware.metricshub.engine.configuration.HostConfiguration; import org.sentrysoftware.metricshub.engine.connector.model.Connector; +import org.sentrysoftware.metricshub.engine.connector.model.monitor.SimpleMonitorJob; +import org.sentrysoftware.metricshub.engine.connector.model.monitor.StandardMonitorJob; import org.sentrysoftware.metricshub.engine.connector.model.monitor.task.source.Source; import org.sentrysoftware.metricshub.engine.connector.model.monitor.task.source.compute.Compute; import org.sentrysoftware.metricshub.engine.extension.ExtensionManager; @@ -400,17 +402,59 @@ public long getStrategyTimeout() { return telemetryManager.getHostConfiguration().getStrategyTimeout(); } + /** + * Determines if the given strategy job name matches any monitor job in the connector. + * Matching is case-insensitive and based on the job type and its components. + * Supported strategy job names: + * - "discovery": Matches a {@link StandardMonitorJob} with a non-null discovery component. + * - "collect": Matches a {@link StandardMonitorJob} with a non-null collect component. + * - "simple": Matches a {@link SimpleMonitorJob} with a non-null simple component. + * + * @param currentConnector the connector containing monitor jobs + * @param strategyJobName the strategy job name to check (case-insensitive) + * @return {@code true} if a monitor job matches the strategy, {@code false} otherwise + */ + protected boolean hasExpectedJobTypes(final Connector currentConnector, final String strategyJobName) { + if (currentConnector == null || currentConnector.getMonitors() == null) { + return false; + } + + return currentConnector + .getMonitors() + .values() + .stream() + .anyMatch(monitorJob -> { + switch (strategyJobName.toLowerCase()) { + case "discovery": + return monitorJob instanceof StandardMonitorJob standardJob && standardJob.getDiscovery() != null; + case "collect": + return monitorJob instanceof StandardMonitorJob standardJob && standardJob.getCollect() != null; + case "simple": + return monitorJob instanceof SimpleMonitorJob simpleJob && simpleJob.getSimple() != null; + default: + throw new IllegalArgumentException("Unknown strategy job name: " + strategyJobName); + } + }); + } + /** * Validates the connector's detection criteria * * @param currentConnector Connector instance * @param hostname Hostname + * @param jobName The strategy job name * @return boolean representing the success of the tests */ - protected boolean validateConnectorDetectionCriteria(final Connector currentConnector, final String hostname) { + protected boolean validateConnectorDetectionCriteria( + final Connector currentConnector, + final String hostname, + final String jobName + ) { if (currentConnector.getConnectorIdentity().getDetection() == null) { return true; } + // Track the connector detection criteria execution start time + final long jobStartTime = System.currentTimeMillis(); final ConnectorTestResult connectorTestResult = new ConnectorSelection( telemetryManager, @@ -419,6 +463,19 @@ protected boolean validateConnectorDetectionCriteria(final Connector currentConn extensionManager ) .runConnectorDetectionCriteria(currentConnector, hostname); + + // Track the connector detection criteria execution end time + final long jobEndTime = System.currentTimeMillis(); + + // Set the job duration metric of the connector monitor in the host monitor + setJobDurationMetric( + jobName, + KnownMonitorType.CONNECTOR.getKey(), + currentConnector.getCompiledFilename(), + jobStartTime, + jobEndTime + ); + final String connectorId = currentConnector.getCompiledFilename(); final Monitor monitor = telemetryManager.findMonitorByTypeAndId( KnownMonitorType.CONNECTOR.getKey(), @@ -508,18 +565,54 @@ public boolean isMonitorFiltered(final String monitorType) { } /** - * Sets the job duration metric in the host monitor. + * Sets the job duration metric in the host monitor with a monitor type. * * @param jobName the name of the job - * @param monitorType the type of monitor + * @param monitorType the monitor type in the job * @param connectorId the ID of the connector - * @param startTime the start time of the job in milliseconds - * @param endTime the end time of the job in milliseconds + * @param jobStartTime the start time of the job in milliseconds + * @param jobEndTime the end time of the job in milliseconds */ - protected void setJobDurationMetricInHostMonitor( + protected void setJobDurationMetric( final String jobName, final String monitorType, final String connectorId, + final long jobStartTime, + final long jobEndTime + ) { + setJobDurationMetric( + () -> generateJobDurationMetricKey(jobName, monitorType, connectorId), + jobStartTime, + jobEndTime + ); + } + + /** + * Sets the job duration metric in the host monitor without a monitor type. + * + * @param jobName the name of the job + * @param connectorId the ID of the connector + * @param jobStartTime the start time of the job in milliseconds + * @param jobEndTime the end time of the job in milliseconds + */ + protected void setJobDurationMetric( + final String jobName, + final String connectorId, + final long jobStartTime, + final long jobEndTime + ) { + setJobDurationMetric(() -> generateJobDurationMetricKey(jobName, connectorId), jobStartTime, jobEndTime); + } + + /** + * Sets the job duration metric in the host monitor with a monitor type. + * + * @param metricKeySupplier the supplier of the metric key + * @param startTime the start time of the job in milliseconds + * @param endTime the end time of the job in milliseconds + */ + private void setJobDurationMetric( + final Supplier metricKeySupplier, final long startTime, final long endTime ) { @@ -527,24 +620,67 @@ protected void setJobDurationMetricInHostMonitor( // set the job duration metric on the monitor. Otherwise, don't set it. // By default, self monitoring is enabled if (telemetryManager.getHostConfiguration().isEnableSelfMonitoring()) { - final Monitor endpointHostMonitor = telemetryManager.getEndpointHostMonitor(); - final MetricFactory metricFactory = new MetricFactory(); + // Build the job duration metric key + final String jobDurationMetricKey = metricKeySupplier.get(); // Collect the job duration metric - final String jobDurationMetricKey = new StringBuilder() - .append("metricshub.job.duration{job.type=\"") - .append(jobName) - .append("\", monitor.type=\"") - .append(monitorType) - .append("\", connector_id=\"") - .append(connectorId) - .append("\"}") - .toString(); - metricFactory.collectNumberMetric( - endpointHostMonitor, - jobDurationMetricKey, - (endTime - startTime) / 1000.0, // Job duration in seconds - strategyTime - ); + collectJobDurationMetric(jobDurationMetricKey, startTime, endTime); } } + + /** + * Generates the job duration metric key. + * @param jobName the name of the job + * @param monitorType the monitor type + * @param connectorId the ID of the connector + * @return the job duration metric key. + */ + private String generateJobDurationMetricKey( + final String jobName, + final String monitorType, + final String connectorId + ) { + return new StringBuilder() + .append("metricshub.job.duration{job.type=\"") + .append(jobName) + .append("\", monitor.type=\"") + .append(monitorType) + .append("\", connector_id=\"") + .append(connectorId) + .append("\"}") + .toString(); + } + + /** + * Generate the job duration metric key. + * @param jobName the name of the job + * @param connectorId the ID of the + * @return the job duration metric key. + */ + private String generateJobDurationMetricKey(final String jobName, final String connectorId) { + return new StringBuilder() + .append("metricshub.job.duration{job.type=\"") + .append(jobName) + .append("\", connector_id=\"") + .append(connectorId) + .append("\"}") + .toString(); + } + + /** + * Collects and records the job duration metric. + * + * @param jobDurationMetricKey the key identifying the job duration metric + * @param startTime the start time of the job in milliseconds + * @param endTime the end time of the job in milliseconds + */ + private void collectJobDurationMetric(final String jobDurationMetricKey, final long startTime, final long endTime) { + final Monitor endpointHostMonitor = telemetryManager.getEndpointHostMonitor(); + final MetricFactory metricFactory = new MetricFactory(); + metricFactory.collectNumberMetric( + endpointHostMonitor, + jobDurationMetricKey, + (endTime - startTime) / 1000.0, // Job duration in seconds + strategyTime + ); + } } diff --git a/metricshub-engine/src/main/java/org/sentrysoftware/metricshub/engine/strategy/collect/CollectStrategy.java b/metricshub-engine/src/main/java/org/sentrysoftware/metricshub/engine/strategy/collect/CollectStrategy.java index 1e2d7be58..4f7f928f0 100644 --- a/metricshub-engine/src/main/java/org/sentrysoftware/metricshub/engine/strategy/collect/CollectStrategy.java +++ b/metricshub-engine/src/main/java/org/sentrysoftware/metricshub/engine/strategy/collect/CollectStrategy.java @@ -84,6 +84,8 @@ @EqualsAndHashCode(callSuper = true) public class CollectStrategy extends AbstractStrategy { + private static final String JOB_NAME = "collect"; + /** * Constructs a new {@code CollectStrategy} using the provided telemetry manager, strategy time, and * clients executor. @@ -106,10 +108,17 @@ public CollectStrategy( /** * This method collects the monitors and their metrics * @param currentConnector Connector instance - * @param hostname the host name + * @param hostname The host name */ private void collect(final Connector currentConnector, final String hostname) { - if (!validateConnectorDetectionCriteria(currentConnector, hostname)) { + // Check whether the strategy job name matches at least one of the monitor jobs names of the current connector + final boolean connectorHasExpectedJobTypes = hasExpectedJobTypes(currentConnector, JOB_NAME); + // If the connector doesn't define any monitor job of type collect, log a message then exit the current collect operation + if (!connectorHasExpectedJobTypes) { + log.debug("Connector doesn't define any monitor job of type collect."); + return; + } + if (!validateConnectorDetectionCriteria(currentConnector, hostname, JOB_NAME)) { log.error( "Hostname {} - The connector {} no longer matches the host. Stopping the connector's collect job.", hostname, @@ -280,13 +289,8 @@ private void processMonitorJob( } } final long jobEndTime = System.currentTimeMillis(); - setJobDurationMetricInHostMonitor( - "collect", - monitorType, - currentConnector.getCompiledFilename(), - jobStartTime, - jobEndTime - ); + // Set the job duration metric in the host monitor + setJobDurationMetric(JOB_NAME, monitorType, currentConnector.getCompiledFilename(), jobStartTime, jobEndTime); } } @@ -414,7 +418,7 @@ private void processMonitors( .connectorId(connectorId) .hostname(hostname) .monitorType(monitorType) - .jobName("collect") + .jobName(JOB_NAME) .build() ) .collectTime(strategyTime) diff --git a/metricshub-engine/src/main/java/org/sentrysoftware/metricshub/engine/strategy/surrounding/SurroundingStrategy.java b/metricshub-engine/src/main/java/org/sentrysoftware/metricshub/engine/strategy/surrounding/SurroundingStrategy.java index a2de9a39b..c66c2df95 100644 --- a/metricshub-engine/src/main/java/org/sentrysoftware/metricshub/engine/strategy/surrounding/SurroundingStrategy.java +++ b/metricshub-engine/src/main/java/org/sentrysoftware/metricshub/engine/strategy/surrounding/SurroundingStrategy.java @@ -122,7 +122,8 @@ public void run() { processSourcesAndComputes(orderedSources.getSources(), jobInfo); final long jobEndTime = System.currentTimeMillis(); - setJobDurationMetricInHostMonitor(jobName, "none", connectorId, jobStartTime, jobEndTime); + // Set the job duration metric in the host monitor + setJobDurationMetric(jobName, connectorId, jobStartTime, jobEndTime); } /** diff --git a/metricshub-engine/src/test/java/org/sentrysoftware/metricshub/engine/strategy/AbstractStrategyTest.java b/metricshub-engine/src/test/java/org/sentrysoftware/metricshub/engine/strategy/AbstractStrategyTest.java index e85aaf5b4..b8cbe162a 100644 --- a/metricshub-engine/src/test/java/org/sentrysoftware/metricshub/engine/strategy/AbstractStrategyTest.java +++ b/metricshub-engine/src/test/java/org/sentrysoftware/metricshub/engine/strategy/AbstractStrategyTest.java @@ -1,7 +1,11 @@ package org.sentrysoftware.metricshub.engine.strategy; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.sentrysoftware.metricshub.engine.constants.Constants.CONNECTOR; import static org.sentrysoftware.metricshub.engine.constants.Constants.HOST; import static org.sentrysoftware.metricshub.engine.constants.Constants.HOST_ID; @@ -17,16 +21,22 @@ import org.sentrysoftware.metricshub.engine.client.ClientsExecutor; import org.sentrysoftware.metricshub.engine.common.helpers.KnownMonitorType; import org.sentrysoftware.metricshub.engine.configuration.HostConfiguration; +import org.sentrysoftware.metricshub.engine.connector.model.Connector; +import org.sentrysoftware.metricshub.engine.connector.model.monitor.MonitorJob; +import org.sentrysoftware.metricshub.engine.connector.model.monitor.SimpleMonitorJob; +import org.sentrysoftware.metricshub.engine.connector.model.monitor.task.Simple; import org.sentrysoftware.metricshub.engine.extension.ExtensionManager; import org.sentrysoftware.metricshub.engine.extension.TestConfiguration; import org.sentrysoftware.metricshub.engine.strategy.collect.CollectStrategy; +import org.sentrysoftware.metricshub.engine.strategy.simple.SimpleStrategy; +import org.sentrysoftware.metricshub.engine.strategy.surrounding.BeforeAllStrategy; import org.sentrysoftware.metricshub.engine.telemetry.Monitor; import org.sentrysoftware.metricshub.engine.telemetry.TelemetryManager; class AbstractStrategyTest { @Test - void testSetJobDurationMetricInHostMonitorNoConfiguration() { + void testSetJobDurationMetricWithMonitorTypeNoConfiguration() { // The job duration metrics are not configured in metricshub.yaml // Create host and connector monitors and set them in the telemetry manager @@ -66,7 +76,7 @@ void testSetJobDurationMetricInHostMonitorNoConfiguration() { .clientsExecutor(new ClientsExecutor()) .extensionManager(new ExtensionManager()) .build(); - collectStrategy.setJobDurationMetricInHostMonitor( + collectStrategy.setJobDurationMetric( "collect", KnownMonitorType.CONNECTOR.getKey(), TEST_CONNECTOR_ID, @@ -87,7 +97,7 @@ void testSetJobDurationMetricInHostMonitorNoConfiguration() { } @Test - void testSetJobDurationMetricInHostMonitorEnabledConfiguration() { + void testSetJobDurationMetricWithMonitorTypeEnabledConfiguration() { // The job duration metrics are configured and enabled // Create host and connector monitors and set them in the telemetry manager @@ -128,7 +138,7 @@ void testSetJobDurationMetricInHostMonitorEnabledConfiguration() { .clientsExecutor(new ClientsExecutor()) .extensionManager(new ExtensionManager()) .build(); - collectStrategy.setJobDurationMetricInHostMonitor( + collectStrategy.setJobDurationMetric( "collect", KnownMonitorType.CONNECTOR.getKey(), TEST_CONNECTOR_ID, @@ -149,7 +159,7 @@ void testSetJobDurationMetricInHostMonitorEnabledConfiguration() { } @Test - void testSetJobDurationMetricInHostMonitorDisabledConfiguration() { + void testSetJobDurationMetricWithMonitorTypeDisabledConfiguration() { // The job duration metrics are configured and disabled // Create host and connector monitors and set them in the telemetry manager @@ -190,7 +200,7 @@ void testSetJobDurationMetricInHostMonitorDisabledConfiguration() { .clientsExecutor(new ClientsExecutor()) .extensionManager(new ExtensionManager()) .build(); - collectStrategy.setJobDurationMetricInHostMonitor( + collectStrategy.setJobDurationMetric( "collect", KnownMonitorType.CONNECTOR.getKey(), TEST_CONNECTOR_ID, @@ -208,4 +218,125 @@ void testSetJobDurationMetricInHostMonitorDisabledConfiguration() { ) ); } + + @Test + void testSetJobDurationMetricWithSurroundingStrategy() { + // The job duration metrics are configured and enabled + + // Create host and connector monitors and set them in the telemetry manager + final Monitor hostMonitor = Monitor.builder().type(KnownMonitorType.HOST.getKey()).isEndpoint(true).build(); + final Monitor connectorMonitor = Monitor.builder().type(KnownMonitorType.CONNECTOR.getKey()).build(); + final Map> monitors = new HashMap<>( + Map.of( + HOST, + Map.of(MONITOR_ID_ATTRIBUTE_VALUE, hostMonitor), + CONNECTOR, + Map.of( + String.format(CONNECTOR_ID_FORMAT, KnownMonitorType.CONNECTOR.getKey(), TEST_CONNECTOR_ID), + connectorMonitor + ) + ) + ); + + final TestConfiguration snmpConfig = TestConfiguration.builder().build(); + + final TelemetryManager telemetryManager = TelemetryManager + .builder() + .monitors(monitors) + .hostConfiguration( + HostConfiguration + .builder() + .hostId(HOST_ID) + .hostname(HOST_NAME) + .sequential(false) + .enableSelfMonitoring(true) + .configurations(Map.of(TestConfiguration.class, snmpConfig)) + .build() + ) + .build(); + final BeforeAllStrategy beforeAllStrategy = BeforeAllStrategy + .builder() + .telemetryManager(telemetryManager) + .strategyTime(new Date().getTime()) + .clientsExecutor(new ClientsExecutor()) + .extensionManager(new ExtensionManager()) + .connector(new Connector()) + .build(); + beforeAllStrategy.setJobDurationMetric( + "beforeAll", + TEST_CONNECTOR_ID, + System.currentTimeMillis() - 200, + System.currentTimeMillis() + ); + // Check job duration metrics values + assertNotNull( + telemetryManager + .getMonitors() + .get("host") + .get("anyMonitorId") + .getMetric("metricshub.job.duration{job.type=\"beforeAll\", connector_id=\"TestConnector\"}") + .getValue() + ); + } + + @Test + void testHasExpectedJobTypesMatching() { + // Set the monitor jobs in the connector + final Connector connector = new Connector(); + final SimpleMonitorJob simpleJob = SimpleMonitorJob.simpleBuilder().simple(new Simple()).build(); + final Map monitors = new HashMap<>(Map.of("simple", simpleJob)); + connector.setMonitors(monitors); + + // Check whether there is a connector monitor job that matches the strategy job name + final SimpleStrategy simpleStrategy = SimpleStrategy + .builder() + .strategyTime(120L) + .telemetryManager(TelemetryManager.builder().build()) + .clientsExecutor(new ClientsExecutor()) + .extensionManager(new ExtensionManager()) + .build(); + assertTrue(simpleStrategy.hasExpectedJobTypes(connector, "simple")); + } + + @Test + void testHasExpectedJobTypesNotMatching() { + // Set the monitor jobs in the connector + final Connector connector = new Connector(); + final SimpleMonitorJob simpleJob = SimpleMonitorJob.simpleBuilder().simple(new Simple()).build(); + final Map monitors = new HashMap<>(Map.of("simple", simpleJob)); + connector.setMonitors(monitors); + + // Check whether there is a connector monitor job that matches the strategy job name + final SimpleStrategy simpleStrategy = SimpleStrategy + .builder() + .strategyTime(120L) + .telemetryManager(TelemetryManager.builder().build()) + .clientsExecutor(new ClientsExecutor()) + .extensionManager(new ExtensionManager()) + .build(); + assertFalse(simpleStrategy.hasExpectedJobTypes(connector, "collect")); + } + + @Test + void testHasExpectedJobTypesUnknownJobType() { + // Set the monitor jobs in the connector + final Connector connector = new Connector(); + final SimpleMonitorJob simpleJob = SimpleMonitorJob.simpleBuilder().simple(new Simple()).build(); + final Map monitors = new HashMap<>(Map.of("simple", simpleJob)); + connector.setMonitors(monitors); + + // Check whether an IllegalArgumentException is thrown when the strategy job name is invalid + final SimpleStrategy simpleStrategy = SimpleStrategy + .builder() + .strategyTime(120L) + .telemetryManager(TelemetryManager.builder().build()) + .clientsExecutor(new ClientsExecutor()) + .extensionManager(new ExtensionManager()) + .build(); + final IllegalArgumentException exception = assertThrows( + IllegalArgumentException.class, + () -> simpleStrategy.hasExpectedJobTypes(connector, "unknown") + ); + assertEquals("Unknown strategy job name: unknown", exception.getMessage()); + } } diff --git a/metricshub-engine/src/test/java/org/sentrysoftware/metricshub/engine/strategy/collect/CollectStrategyTest.java b/metricshub-engine/src/test/java/org/sentrysoftware/metricshub/engine/strategy/collect/CollectStrategyTest.java index e207caded..1221b7c48 100644 --- a/metricshub-engine/src/test/java/org/sentrysoftware/metricshub/engine/strategy/collect/CollectStrategyTest.java +++ b/metricshub-engine/src/test/java/org/sentrysoftware/metricshub/engine/strategy/collect/CollectStrategyTest.java @@ -252,6 +252,16 @@ void testRun() throws Exception { ) .getValue() ); + assertNotNull( + telemetryManager + .getMonitors() + .get("host") + .get("anyMonitorId") + .getMetric( + "metricshub.job.duration{job.type=\"collect\", monitor.type=\"connector\", connector_id=\"TestConnector\"}" + ) + .getValue() + ); // Mock detection criteria result to switch to a failing criterion processing case doReturn(CriterionTestResult.failure(snmpGetNextCriterion, "1.3.6.1.4.1.795.10.1.1.3.1.1.0 ASN_OCTET_STR Test")) diff --git a/metricshub-engine/src/test/java/org/sentrysoftware/metricshub/engine/strategy/discovery/DiscoveryStrategyTest.java b/metricshub-engine/src/test/java/org/sentrysoftware/metricshub/engine/strategy/discovery/DiscoveryStrategyTest.java index 41aa564dc..8e33ef3ee 100644 --- a/metricshub-engine/src/test/java/org/sentrysoftware/metricshub/engine/strategy/discovery/DiscoveryStrategyTest.java +++ b/metricshub-engine/src/test/java/org/sentrysoftware/metricshub/engine/strategy/discovery/DiscoveryStrategyTest.java @@ -294,6 +294,16 @@ void testRun() throws Exception { ) .getValue() ); + assertNotNull( + telemetryManager + .getMonitors() + .get("host") + .get("anyMonitorId") + .getMetric( + "metricshub.job.duration{job.type=\"discovery\"," + " monitor.type=\"connector\", connector_id=\"AAC\"}" + ) + .getValue() + ); } @Test diff --git a/metricshub-engine/src/test/java/org/sentrysoftware/metricshub/engine/strategy/simple/SimpleStrategyTest.java b/metricshub-engine/src/test/java/org/sentrysoftware/metricshub/engine/strategy/simple/SimpleStrategyTest.java index 20cec6fc7..2afe76d73 100644 --- a/metricshub-engine/src/test/java/org/sentrysoftware/metricshub/engine/strategy/simple/SimpleStrategyTest.java +++ b/metricshub-engine/src/test/java/org/sentrysoftware/metricshub/engine/strategy/simple/SimpleStrategyTest.java @@ -221,6 +221,16 @@ void testRun() throws Exception { ) .getValue() ); + assertNotNull( + telemetryManager + .getMonitors() + .get("host") + .get("monitor1") + .getMetric( + "metricshub.job.duration{job.type=\"simple\", monitor.type=\"connector\", connector_id=\"TestConnectorWithSimple\"}" + ) + .getValue() + ); // Mock detection criteria result to switch to a failing criterion processing case doReturn(CriterionTestResult.failure(snmpGetNextCriterion, "1.3.6.1.4.1.795.10.1.1.3.1.1.0 ASN_OCTET_STR Test")) diff --git a/metricshub-engine/src/test/java/org/sentrysoftware/metricshub/engine/strategy/surrounding/AfterAllStrategyTest.java b/metricshub-engine/src/test/java/org/sentrysoftware/metricshub/engine/strategy/surrounding/AfterAllStrategyTest.java index 0bb2710bc..bfabbc34e 100644 --- a/metricshub-engine/src/test/java/org/sentrysoftware/metricshub/engine/strategy/surrounding/AfterAllStrategyTest.java +++ b/metricshub-engine/src/test/java/org/sentrysoftware/metricshub/engine/strategy/surrounding/AfterAllStrategyTest.java @@ -281,9 +281,7 @@ void testRunFromCollect() { .getMonitors() .get("host") .get("anyMonitorId") - .getMetric( - "metricshub.job.duration{job.type=\"afterAll\", monitor.type=\"none\", connector_id=\"afterAllSource\"}" - ) + .getMetric("metricshub.job.duration{job.type=\"afterAll\", connector_id=\"afterAllSource\"}") .getValue() ); assertNotNull( diff --git a/metricshub-engine/src/test/java/org/sentrysoftware/metricshub/engine/strategy/surrounding/BeforeAllStrategyTest.java b/metricshub-engine/src/test/java/org/sentrysoftware/metricshub/engine/strategy/surrounding/BeforeAllStrategyTest.java index e487dd7b6..f6d95b50f 100644 --- a/metricshub-engine/src/test/java/org/sentrysoftware/metricshub/engine/strategy/surrounding/BeforeAllStrategyTest.java +++ b/metricshub-engine/src/test/java/org/sentrysoftware/metricshub/engine/strategy/surrounding/BeforeAllStrategyTest.java @@ -281,9 +281,7 @@ void testRunFromCollect() { .getMonitors() .get("host") .get("anyMonitorId") - .getMetric( - "metricshub.job.duration{job.type=\"beforeAll\", monitor.type=\"none\", connector_id=\"beforeAllSource\"}" - ) + .getMetric("metricshub.job.duration{job.type=\"beforeAll\", connector_id=\"beforeAllSource\"}") .getValue() ); assertNotNull(