-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Developed Http Extension and all its classes * Refactored all the tests and made the necessary changes to make them work * Tested the engine and the agent
- Loading branch information
1 parent
ba71684
commit 0db2850
Showing
37 changed files
with
2,214 additions
and
935 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...nt/src/test/java/org/sentrysoftware/metricshub/agent/extension/HttpTestConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.sentrysoftware.metricshub.agent.extension; | ||
|
||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Builder.Default; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import org.sentrysoftware.metricshub.engine.common.exception.InvalidConfigurationException; | ||
import org.sentrysoftware.metricshub.engine.common.helpers.StringHelper; | ||
import org.sentrysoftware.metricshub.engine.configuration.IConfiguration; | ||
import org.sentrysoftware.metricshub.engine.deserialization.TimeDeserializer; | ||
|
||
/** | ||
* The HttpConfiguration class represents the configuration for HTTP connections in the MetricsHub engine. | ||
*/ | ||
@Data | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class HttpTestConfiguration implements IConfiguration { | ||
|
||
@Default | ||
private final Boolean https = true; | ||
|
||
@Default | ||
private final Integer port = 443; | ||
|
||
@Default | ||
@JsonDeserialize(using = TimeDeserializer.class) | ||
private final Long timeout = 120L; | ||
|
||
private String username; | ||
private char[] password; | ||
|
||
@Override | ||
public String toString() { | ||
return String.format( | ||
"%s/%d%s", | ||
Boolean.TRUE.equals(https) ? "HTTPS" : "HTTP", | ||
port, | ||
username != null ? " as " + username : "" | ||
); | ||
} | ||
|
||
@Override | ||
public void validateConfiguration(String resourceKey) throws InvalidConfigurationException { | ||
StringHelper.validateConfigurationAttribute( | ||
timeout, | ||
attr -> attr == null || attr < 0L, | ||
() -> | ||
String.format( | ||
"Resource %s - Timeout value is invalid for protocol %s." + | ||
" Timeout value returned: %s. This resource will not be monitored. Please verify the configured timeout value.", | ||
resourceKey, | ||
"HTTP", | ||
timeout | ||
) | ||
); | ||
|
||
StringHelper.validateConfigurationAttribute( | ||
port, | ||
attr -> attr == null || attr < 1 || attr > 65535, | ||
() -> | ||
String.format( | ||
"Resource %s - Invalid port configured for protocol %s. Port value returned: %s." + | ||
" This resource will not be monitored. Please verify the configured port value.", | ||
resourceKey, | ||
"HTTP", | ||
port | ||
) | ||
); | ||
} | ||
} |
150 changes: 150 additions & 0 deletions
150
...-agent/src/test/java/org/sentrysoftware/metricshub/agent/extension/HttpTestExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package org.sentrysoftware.metricshub.agent.extension; | ||
|
||
/*- | ||
* ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲ | ||
* MetricsHub HTTP Extension | ||
* ჻჻჻჻჻჻ | ||
* Copyright 2023 - 2024 Sentry Software | ||
* ჻჻჻჻჻჻ | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱ | ||
*/ | ||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.MapperFeature; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.databind.json.JsonMapper; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.UnaryOperator; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.sentrysoftware.metricshub.engine.common.exception.InvalidConfigurationException; | ||
import org.sentrysoftware.metricshub.engine.configuration.IConfiguration; | ||
import org.sentrysoftware.metricshub.engine.connector.model.identity.criterion.Criterion; | ||
import org.sentrysoftware.metricshub.engine.connector.model.identity.criterion.HttpCriterion; | ||
import org.sentrysoftware.metricshub.engine.connector.model.monitor.task.source.HttpSource; | ||
import org.sentrysoftware.metricshub.engine.connector.model.monitor.task.source.Source; | ||
import org.sentrysoftware.metricshub.engine.extension.IProtocolExtension; | ||
import org.sentrysoftware.metricshub.engine.strategy.detection.CriterionTestResult; | ||
import org.sentrysoftware.metricshub.engine.strategy.source.SourceTable; | ||
import org.sentrysoftware.metricshub.engine.telemetry.TelemetryManager; | ||
|
||
/** | ||
* This class implements the {@link IProtocolExtension} contract, reports the supported features, | ||
* processes HTTP sources and criteria. | ||
*/ | ||
@Slf4j | ||
public class HttpTestExtension implements IProtocolExtension { | ||
|
||
/** | ||
* Protocol up status value '1.0' | ||
*/ | ||
public static final Double UP = 1.0; | ||
|
||
/** | ||
* Protocol down status value '0.0' | ||
*/ | ||
public static final Double DOWN = 0.0; | ||
|
||
/** | ||
* Up metric name format that will be saved by the metric factory | ||
*/ | ||
public static final String HTTP_UP_METRIC = "metricshub.host.up{protocol=\"http\"}"; | ||
|
||
@Override | ||
public boolean isValidConfiguration(IConfiguration configuration) { | ||
return configuration instanceof HttpTestConfiguration; | ||
} | ||
|
||
@Override | ||
public Set<Class<? extends Source>> getSupportedSources() { | ||
return Set.of(HttpSource.class); | ||
} | ||
|
||
@Override | ||
public Map<Class<? extends IConfiguration>, Set<Class<? extends Source>>> getConfigurationToSourceMapping() { | ||
return Map.of(HttpTestConfiguration.class, Set.of(HttpSource.class)); | ||
} | ||
|
||
@Override | ||
public Set<Class<? extends Criterion>> getSupportedCriteria() { | ||
return Set.of(HttpCriterion.class); | ||
} | ||
|
||
@Override | ||
public void checkProtocol(TelemetryManager telemetryManager, Long collectTime) {} | ||
|
||
@Override | ||
public SourceTable processSource(Source source, String connectorId, TelemetryManager telemetryManager) { | ||
return SourceTable.empty(); | ||
} | ||
|
||
@Override | ||
public CriterionTestResult processCriterion( | ||
Criterion criterion, | ||
String connectorId, | ||
TelemetryManager telemetryManager | ||
) { | ||
return CriterionTestResult.empty(); | ||
} | ||
|
||
@Override | ||
public boolean isSupportedConfigurationType(String configurationType) { | ||
return "http".equalsIgnoreCase(configurationType); | ||
} | ||
|
||
@Override | ||
public IConfiguration buildConfiguration(String configurationType, JsonNode jsonNode, UnaryOperator<char[]> decrypt) | ||
throws InvalidConfigurationException { | ||
try { | ||
final HttpTestConfiguration httpConfiguration = newObjectMapper() | ||
.treeToValue(jsonNode, HttpTestConfiguration.class); | ||
|
||
if (decrypt != null) { | ||
// Decrypt the community | ||
final char[] passwordDecypted = decrypt.apply(httpConfiguration.getPassword()); | ||
httpConfiguration.setPassword(passwordDecypted); | ||
} | ||
|
||
return httpConfiguration; | ||
} catch (Exception e) { | ||
final String errorMessage = String.format( | ||
"Error while reading HTTP Configuration: %s. Error: %s", | ||
jsonNode, | ||
e.getMessage() | ||
); | ||
log.error(errorMessage); | ||
log.debug("Error while reading HTTP Configuration: {}. Stack trace:", jsonNode, e); | ||
throw new InvalidConfigurationException(errorMessage, e); | ||
} | ||
} | ||
|
||
/** | ||
* Creates and configures a new instance of the Jackson ObjectMapper for handling YAML data. | ||
* | ||
* @return A configured ObjectMapper instance. | ||
*/ | ||
public static JsonMapper newObjectMapper() { | ||
return JsonMapper | ||
.builder(new YAMLFactory()) | ||
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS) | ||
.enable(SerializationFeature.INDENT_OUTPUT) | ||
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false) | ||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) | ||
.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false) | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.