Skip to content

Commit

Permalink
Issue #435: Add the possibility to use the same connector with differ…
Browse files Browse the repository at this point in the history
…ent connector variables

* Refactored ConnectorVariablesLibraryParser for better clarity.
* Corrected some Javadoc errors.
* Moved some classes to a new package.
  • Loading branch information
CherfaElyes committed Oct 24, 2024
1 parent 97f200c commit 295894c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/*-
* ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
* MetricsHub Engine
* MetricsHub Agent
* ჻჻჻჻჻჻
* Copyright 2023 - 2024 Sentry Software
* ჻჻჻჻჻჻
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.sentrysoftware.metricshub.agent.deserialization;
package org.sentrysoftware.metricshub.agent.connector;

/*-
* ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
* MetricsHub Engine
* MetricsHub Agent
* ჻჻჻჻჻჻
* Copyright 2023 - 2024 Sentry Software
* ჻჻჻჻჻჻
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sentrysoftware.metricshub.agent.helper;
package org.sentrysoftware.metricshub.agent.connector;

/*-
* ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
Expand Down Expand Up @@ -38,7 +38,6 @@
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.sentrysoftware.metricshub.agent.config.AdditionalConnector;
import org.sentrysoftware.metricshub.agent.deserialization.AdditionalConnectorsParsingResult;
import org.sentrysoftware.metricshub.engine.connector.model.Connector;
import org.sentrysoftware.metricshub.engine.connector.model.identity.ConnectorDefaultVariable;
import org.sentrysoftware.metricshub.engine.connector.parser.ConnectorParser;
Expand Down Expand Up @@ -111,44 +110,15 @@ public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IO
// Construct a variables map from the default connector variables.
final Map<String, String> defaultVariables = new HashMap<>(getDefaultConnectorVariables(connectorNode));

// Parse the connector even if it is not configured as an additional connector.
// This ensures that the connector will function with the default variables if the user forces it.
if (filteredConnectors.isEmpty()) {
// Parse the connector even if it is not configured as an additional connector.
// This ensures that the connector will function with the default variables if the user forces it.
parseConnectorWithModifier(path, defaultVariables, connectorId, filename, connector -> {});
} else {
// Parse and generate connectors that have been added and configured.
generateNewConnectors(path, filename, filteredConnectors, defaultVariables);
}

// For each configuration, we create a new custom connector and a new variables map to be used in the connector update.
for (final Entry<String, AdditionalConnector> connectorConfigurationEntry : filteredConnectors.entrySet()) {
final String additionalConnectorId = connectorConfigurationEntry.getKey();
final AdditionalConnector additionalConnectorValue = connectorConfigurationEntry.getValue();

// Add the connector to the host connectors set.
connectorsParsingResult
.getHostConnectors()
.add(additionalConnectorValue.isForce() ? "+" + additionalConnectorId : additionalConnectorId);

// Retrieve and use default connector variables on this connector for this configuration.
final Map<String, String> connectorVariables = new HashMap<>(defaultVariables);

// Override the default connector variables by the connector variables that the user configured.
final Map<String, String> configuredVariables = additionalConnectorValue.getVariables();
if (configuredVariables != null) {
connectorVariables.putAll(configuredVariables);
}
// There are at least two additional connectors that use the current connector.
// This means that the compiled filename of these connectors needs to be modified.
if (filteredConnectors.size() > 1) {
parseConnectorWithModifier(
path,
connectorVariables,
additionalConnectorId,
filename,
connector -> connector.getConnectorIdentity().setCompiledFilename(additionalConnectorId)
);
continue;
}
parseConnectorWithModifier(path, connectorVariables, connectorId, filename, connector -> {});
}
return FileVisitResult.CONTINUE;
}

Expand Down Expand Up @@ -203,12 +173,63 @@ private static Map<String, String> getDefaultConnectorVariables(final JsonNode c
final String variableName = entry.getKey();
final JsonNode variableValue = entry.getValue();

connectorVariablesMap.put(variableName, variableValue.get("defaultValue").asText());
final JsonNode defaultValue = variableValue.get("defaultValue");
if (defaultValue != null && !defaultValue.isNull()) {
connectorVariablesMap.put(variableName, variableValue.get("defaultValue").asText());
}
});

return connectorVariablesMap;
}

/**
* Generates custom connectors for each additional connector configuration.
*
* <p>This method iterates over the provided map of filtered connectors, generating a new {@link Connector}
* object for each. It handles the default and user-configured variables, ensuring that any forced connectors
* are correctly identified. The method can modify the compiled filename of connectors when needed.</p>
*
* @param path The path to the directory containing the connector files.
* @param filename The filename of the connector file.
* @param filteredConnectors A map of filtered additional connectors, with their configurations.
* @param defaultVariables A map of default variables to be used during connector generation.
*/
private void generateNewConnectors(
final Path path,
final String filename,
final Map<String, AdditionalConnector> filteredConnectors,
final Map<String, String> defaultVariables
) {
// For each configuration, we create a new custom connector and a new variables map to be used in the connector update.
for (final Entry<String, AdditionalConnector> connectorConfigurationEntry : filteredConnectors.entrySet()) {
final String additionalConnectorId = connectorConfigurationEntry.getKey();
final AdditionalConnector additionalConnectorValue = connectorConfigurationEntry.getValue();

// Add the connector to the host connectors set.
connectorsParsingResult
.getHostConnectors()
.add(additionalConnectorValue.isForce() ? "+" + additionalConnectorId : additionalConnectorId);

// Retrieve and use default connector variables on this connector for this configuration.
final Map<String, String> connectorVariables = new HashMap<>(defaultVariables);

// Override the default connector variables by the connector variables that the user configured.
final Map<String, String> configuredVariables = additionalConnectorValue.getVariables();
if (configuredVariables != null) {
connectorVariables.putAll(configuredVariables);
}
// There are at least two additional connectors that use the current connector.
// This means that the compiled filename of these connectors needs to be modified.
parseConnectorWithModifier(
path,
connectorVariables,
additionalConnectorId,
filename,
connector -> connector.getConnectorIdentity().setCompiledFilename(additionalConnectorId)
);
}
}

/**
* Parses a connector file located at the specified path, using the provided variables and connector Id.
* The parsed connector is then added to the custom connectors map, with the option to perform additional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@
import org.sentrysoftware.metricshub.agent.config.AlertingSystemConfig;
import org.sentrysoftware.metricshub.agent.config.ResourceConfig;
import org.sentrysoftware.metricshub.agent.config.ResourceGroupConfig;
import org.sentrysoftware.metricshub.agent.connector.AdditionalConnectorsParsingResult;
import org.sentrysoftware.metricshub.agent.connector.ConnectorVariablesLibraryParser;
import org.sentrysoftware.metricshub.agent.context.MetricDefinitions;
import org.sentrysoftware.metricshub.agent.deserialization.AdditionalConnectorsParsingResult;
import org.sentrysoftware.metricshub.agent.security.PasswordEncrypt;
import org.sentrysoftware.metricshub.engine.common.exception.InvalidConfigurationException;
import org.sentrysoftware.metricshub.engine.common.helpers.JsonHelper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.sentrysoftware.metricshub.agent.config.AdditionalConnector;
import org.sentrysoftware.metricshub.agent.deserialization.AdditionalConnectorsParsingResult;
import org.sentrysoftware.metricshub.agent.connector.AdditionalConnectorsParsingResult;
import org.sentrysoftware.metricshub.agent.connector.ConnectorVariablesLibraryParser;
import org.sentrysoftware.metricshub.engine.connector.model.Connector;
import org.sentrysoftware.metricshub.engine.connector.model.identity.Detection;
import org.sentrysoftware.metricshub.engine.connector.model.identity.criterion.SnmpGetNextCriterion;
Expand Down

0 comments on commit 295894c

Please sign in to comment.