Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WFLY-19351] Add Micrometer test to verify that applications with shared metrics names are not merged when exported #17898

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import jakarta.inject.Inject;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.search.Search;
import jakarta.inject.Inject;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
Expand All @@ -36,7 +35,9 @@
import org.junit.runner.RunWith;
import org.wildfly.test.integration.microprofile.faulttolerance.micrometer.deployment.FaultTolerantApplication;
import org.wildfly.test.integration.microprofile.faulttolerance.micrometer.deployment.TimeoutService;
import org.wildfly.test.integration.observability.micrometer.MicrometerSetupTask;
import org.wildfly.test.integration.observability.arquillian.TestContainer;
import org.wildfly.test.integration.observability.container.OpenTelemetryCollectorContainer;
import org.wildfly.test.integration.observability.setuptask.MicrometerSetupTask;

/**
* Test case to verify basic SmallRye Fault Tolerance integration with Micrometer. The test first invokes a REST
Expand All @@ -47,6 +48,7 @@
*/
@RunWith(Arquillian.class)
@ServerSetup(MicrometerSetupTask.class)
@TestContainer(OpenTelemetryCollectorContainer.class)
public class FaultToleranceMicrometerIntegrationTestCase {

// Let's use a slightly higher number of invocations, so we can at times differentiate between stale read and other problems
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.wildfly.test.integration.observability.arquillian;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.testcontainers.containers.GenericContainer;

@Inherited
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface TestContainer {
Class<? extends GenericContainer<?>> value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.wildfly.test.integration.observability.arquillian;

import java.lang.annotation.Annotation;

import org.jboss.arquillian.core.api.Instance;
import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.arquillian.test.spi.annotation.ClassScoped;
import org.jboss.as.arquillian.container.AbstractTargetsContainerProvider;
import org.testcontainers.containers.GenericContainer;

public class TestContainerProvider extends AbstractTargetsContainerProvider {
@Inject
@ClassScoped
private Instance<GenericContainer<?>> genericContainerInstance;

@Override
public Object doLookup(ArquillianResource resource, Annotation... qualifiers) {
return genericContainerInstance.get();
}

@Override
public boolean canProvide(Class<?> type) {
return GenericContainer.class.isAssignableFrom(type);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.wildfly.test.integration.observability.arquillian;

import org.jboss.arquillian.core.spi.LoadableExtension;
import org.jboss.arquillian.test.spi.enricher.resource.ResourceProvider;

public class TestContainersExtension implements LoadableExtension {
@Override
public void register(ExtensionBuilder builder) {
builder
.observer(TestContainersObserver.class)
.service(ResourceProvider.class, TestContainerProvider.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.wildfly.test.integration.observability.arquillian;

import static org.jboss.as.test.shared.util.AssumeTestGroupUtil.assumeDockerAvailable;

import org.jboss.arquillian.core.api.InstanceProducer;
import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.arquillian.core.api.annotation.Observes;
import org.jboss.arquillian.test.spi.TestClass;
import org.jboss.arquillian.test.spi.annotation.ClassScoped;
import org.jboss.arquillian.test.spi.event.suite.AfterClass;
import org.jboss.arquillian.test.spi.event.suite.BeforeClass;
import org.testcontainers.containers.GenericContainer;

public class TestContainersObserver {
@Inject
@ClassScoped
protected InstanceProducer<GenericContainer<?>> containerWrapper;

public void createContainer(@Observes(precedence = 500) BeforeClass beforeClass) {
TestClass javaClass = beforeClass.getTestClass();
TestContainer tcAnno = javaClass.getAnnotation(TestContainer.class);
if (tcAnno != null) {
assumeDockerAvailable();
Class<? extends GenericContainer<?>> clazz = tcAnno.value();
try {
final GenericContainer<?> container = clazz.getConstructor().newInstance();
container.start();
containerWrapper.set(container);
} catch (Exception e) { //Clean up
throw new RuntimeException(e);
}
}
}

public void stopContainer(@Observes AfterClass afterClass) {
GenericContainer<?> container = containerWrapper.get();
if (container != null) {
container.stop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
package org.wildfly.test.integration.observability.container;

import java.util.List;

import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.WebTarget;

import org.junit.Assert;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.wait.strategy.Wait;
import org.wildfly.common.annotation.NotNull;
import org.wildfly.test.integration.observability.opentelemetry.jaeger.JaegerResponse;
import org.wildfly.test.integration.observability.opentelemetry.jaeger.JaegerTrace;

Expand All @@ -28,23 +27,13 @@ class JaegerContainer extends BaseContainer<JaegerContainer> {

private String jaegerEndpoint;

private JaegerContainer() {
public JaegerContainer() {
super("Jaeger", "jaegertracing/all-in-one", "latest",
List.of(PORT_JAEGER_QUERY, PORT_JAEGER_OTLP),
List.of(Wait.forHttp("/").forPort(PORT_JAEGER_QUERY)));
}

@NotNull
public static synchronized JaegerContainer getInstance() {
if (INSTANCE == null) {
INSTANCE = new JaegerContainer()
.withNetwork(Network.SHARED)
.withNetworkAliases("jaeger")
.withEnv("JAEGER_DISABLED", "true");
INSTANCE.start();
}

return INSTANCE;
withNetwork(Network.SHARED)
.withNetworkAliases("jaeger")
.withEnv("JAEGER_DISABLED", "true");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@
*/
package org.wildfly.test.integration.observability.container;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.WebTarget;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.MountableFile;
import org.wildfly.common.annotation.NotNull;
import org.wildfly.test.integration.observability.opentelemetry.jaeger.JaegerTrace;

public class OpenTelemetryCollectorContainer extends BaseContainer<OpenTelemetryCollectorContainer> {
private static OpenTelemetryCollectorContainer INSTANCE = null;
private static JaegerContainer jaegerContainer;
private JaegerContainer jaegerContainer;

public static final int OTLP_GRPC_PORT = 4317;
public static final int OTLP_HTTP_PORT = 4318;
Expand All @@ -28,46 +34,32 @@ public class OpenTelemetryCollectorContainer extends BaseContainer<OpenTelemetry
private String otlpHttpEndpoint;
private String prometheusUrl;


private OpenTelemetryCollectorContainer() {
public OpenTelemetryCollectorContainer() {
super("OpenTelemetryCollector",
"otel/opentelemetry-collector",
"0.93.0",
List.of(OTLP_GRPC_PORT, OTLP_HTTP_PORT, HEALTH_CHECK_PORT, PROMETHEUS_PORT),
List.of(Wait.forHttp("/").forPort(HEALTH_CHECK_PORT)));
}

@NotNull
public static synchronized OpenTelemetryCollectorContainer getInstance() {
if (INSTANCE == null) {
jaegerContainer = JaegerContainer.getInstance();

INSTANCE = new OpenTelemetryCollectorContainer()
.withNetwork(Network.SHARED)
.withCopyToContainer(MountableFile.forClasspathResource(
"org/wildfly/test/integration/observability/container/otel-collector-config.yaml"),
OpenTelemetryCollectorContainer.OTEL_COLLECTOR_CONFIG_YAML)
.withCommand("--config " + OpenTelemetryCollectorContainer.OTEL_COLLECTOR_CONFIG_YAML);
INSTANCE.start();
}
return INSTANCE;
withNetwork(Network.SHARED)
.withCopyToContainer(MountableFile.forClasspathResource(
"org/wildfly/test/integration/observability/container/otel-collector-config.yaml"),
OpenTelemetryCollectorContainer.OTEL_COLLECTOR_CONFIG_YAML)
.withCommand("--config " + OpenTelemetryCollectorContainer.OTEL_COLLECTOR_CONFIG_YAML);
jaegerContainer = new JaegerContainer();
}

@Override
public void start() {
super.start();
jaegerContainer.start();
otlpGrpcEndpoint = "http://localhost:" + getMappedPort(OTLP_GRPC_PORT);
otlpHttpEndpoint = "http://localhost:" + getMappedPort(OTLP_HTTP_PORT);
prometheusUrl = "http://localhost:" + getMappedPort(PROMETHEUS_PORT) + "/metrics";
}

@Override
public synchronized void stop() {
if (jaegerContainer != null) {
jaegerContainer.stop();
jaegerContainer = null;
}
INSTANCE = null;
jaegerContainer.stop();
super.stop();
}

Expand All @@ -86,4 +78,63 @@ public String getPrometheusUrl() {
public List<JaegerTrace> getTraces(String serviceName) throws InterruptedException {
return (jaegerContainer != null ? jaegerContainer.getTraces(serviceName) : Collections.emptyList());
}

public List<PrometheusMetric> fetchMetrics(String nameToMonitor) throws InterruptedException {
String body = "";
try (Client client = ClientBuilder.newClient()) {
WebTarget target = client.target(prometheusUrl);

int attemptCount = 0;
boolean found = false;

// Request counts can vary. Setting high to help ensure test stability
while (!found && attemptCount < 30) {
// Wait to give Micrometer time to export
Thread.sleep(1000);

body = target.request().get().readEntity(String.class);
found = body.contains(nameToMonitor);
attemptCount++;
}
}

return buildPrometheusMetrics(body);
}

private List<PrometheusMetric> buildPrometheusMetrics(String body) {
if (body.isEmpty()) {
return Collections.emptyList();
}
String[] entries = body.split("\n");
Map<String, String> help = new HashMap<>();
Map<String, String> type = new HashMap<>();
List<PrometheusMetric> metrics = new LinkedList<>();
Arrays.stream(entries).forEach(e -> {
if (e.startsWith("# HELP")) {
extractMetadata(help, e);
} else if (e.startsWith("# TYPE")) {
extractMetadata(type, e);
} else {
String[] parts = e.split("[{}]");
String key = parts[0];
Map<String, String> tags = Arrays.stream(parts[1].split(","))
.map(t -> t.split("="))
.collect(Collectors.toMap(i -> i[0],
i -> i[1]
.replaceAll("^\"", "")
.replaceAll("\"$", "")
));
metrics.add(new PrometheusMetric(key, tags, parts[2].trim(), type.get(key), help.get(key)));
}
});

return metrics;
}

private void extractMetadata(Map<String, String> target, String source) {
String[] parts = source.split(" ");
target.put(parts[2],
Arrays.stream(Arrays.copyOfRange(parts, 3, parts.length))
.reduce("", (total, element) -> total + " " + element));
}
}
Loading