Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import com.netflix.spectator.api.Registry;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.config.MeterFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@EnableConfigurationProperties(CanaryAnalysisCasesConfigurationProperties.class)
@Configuration
Expand Down Expand Up @@ -56,9 +56,8 @@ public MetricsGenerator metricsGenerator(

@Bean
public StandaloneCanaryAnalysisSteps canaryAnalysisSteps(
@Value("${server.port}") int serverPort,
CanaryAnalysisCasesConfigurationProperties configuration) {
return new StandaloneCanaryAnalysisSteps(serverPort, configuration);
Environment environment, CanaryAnalysisCasesConfigurationProperties configuration) {
return new StandaloneCanaryAnalysisSteps(environment, configuration);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.netflix.kayenta.configuration;

import static com.playtika.test.common.utils.ContainerUtils.containerLogsConsumer;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.Duration;
import java.util.LinkedHashMap;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.testcontainers.Testcontainers;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;
import org.testcontainers.containers.wait.strategy.WaitStrategy;
import org.testcontainers.utility.MountableFile;

@Slf4j
@TestConfiguration
@RequiredArgsConstructor
public class TestPrometheusConfiguration {

private static final int PROMETHEUS_INTERNAL_PORT = 9090;
private final Environment environment;

@Bean
public WaitStrategy prometheusWaitStrategy() {
return new HttpWaitStrategy()
.forPath("/status")
.forPort(PROMETHEUS_INTERNAL_PORT)
.forStatusCode(200);
}

private GenericContainer<?> prometheusContainer;

@Bean
public ApplicationListener<ApplicationReadyEvent> prometheus(
ConfigurableEnvironment env, WaitStrategy prometheusWaitStrategy) {
return event -> {
int managementPort = waitForManagementPort();
exposeManagementPort(managementPort);

File prometheusConfigFile = createPrometheusConfigFile(managementPort);

prometheusContainer =
new GenericContainer<>("prom/prometheus:v2.10.0")
.withLogConsumer(containerLogsConsumer(log))
.withExposedPorts(PROMETHEUS_INTERNAL_PORT)
.withCopyFileToContainer(
MountableFile.forHostPath(prometheusConfigFile.getAbsolutePath()),
"/etc/prometheus/prometheus.yml")
.waitingFor(prometheusWaitStrategy)
.withStartupTimeout(Duration.ofSeconds(30));

prometheusContainer.start();

Map<String, Object> prometheusEnv =
registerEnvironment(env, prometheusContainer.getMappedPort(PROMETHEUS_INTERNAL_PORT));
log.info("Started Prometheus server. Connection details: {}", prometheusEnv);
};
}

private int waitForManagementPort() {
int retries = 30; // wait up to 30 seconds
while (retries-- > 0) {
String managementPortStr = environment.getProperty("local.management.port");
if (managementPortStr != null) {
return Integer.parseInt(managementPortStr);
}
try {
Thread.sleep(1000); // wait 1 second
} catch (InterruptedException ignored) {
}
}
throw new IllegalStateException(
"Property 'local.management.port' not available after waiting!");
}

private int getManagementPort() {
String managementPortStr = environment.getProperty("local.management.port");
if (managementPortStr == null) {
throw new IllegalStateException(
"Property 'local.management.port' not available yet! Maybe server not started?");
}
return Integer.parseInt(managementPortStr);
}

private void exposeManagementPort(int managementPort) {
log.info("Exposing management port {} to Testcontainers", managementPort);
Testcontainers.exposeHostPorts(managementPort);
}

private File createPrometheusConfigFile(int managementPort) {
try {
File tempFile = File.createTempFile("prometheus", ".yml");
try (FileWriter writer = new FileWriter(tempFile)) {
writer.write(
String.format(
"""
global:
scrape_interval: 1s

scrape_configs:
- job_name: kayenta-test-metrics
metrics_path: /prometheus
static_configs:
- targets: ['host.testcontainers.internal:%d']
""",
managementPort));
}
return tempFile;
} catch (IOException e) {
throw new RuntimeException("Failed to create prometheus.yml dynamically", e);
}
}

private Map<String, Object> registerEnvironment(ConfigurableEnvironment environment, int port) {
Map<String, Object> map = new LinkedHashMap<>();
map.put("embedded.prometheus.port", port);
com.netflix.kayenta.utils.EnvironmentUtils.registerPropertySource(
"embeddedPrometheusInfo", environment, map);
return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,27 @@
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import lombok.RequiredArgsConstructor;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;

@RequiredArgsConstructor
public class StandaloneCanaryAnalysisSteps {

private final int serverPort;
private final Environment environment;
private final CanaryAnalysisCasesConfigurationProperties cases;

private Integer serverPort;

private int getServerPort() {
if (serverPort != null) {
return serverPort;
}

serverPort = environment.getProperty("local.server.port", Integer.class);

return serverPort;
}

public String createCanaryAnalysis(
String caseName,
String metricsAccountName,
Expand All @@ -50,7 +63,7 @@ public String createCanaryAnalysis(

ValidatableResponse createAnalysisResponse =
given()
.port(serverPort)
.port(getServerPort())
.header("Content-Type", "application/json")
.queryParam("metricsAccountName", metricsAccountName)
.queryParam("storageAccountName", storageAccountName)
Expand Down Expand Up @@ -85,7 +98,7 @@ public ValidatableResponse waitUntilCanaryAnalysisCompleted(String canaryAnalysi

public ValidatableResponse getCanaryAnalysisExecution(String canaryAnalysisExecutionId) {
return given()
.port(serverPort)
.port(getServerPort())
.get("/standalone_canary_analysis/" + canaryAnalysisExecutionId)
.then();
}
Expand Down
Loading
Loading