Skip to content

Commit

Permalink
Merge pull request wildfly#17632 from jasondlee/WFLY-18913
Browse files Browse the repository at this point in the history
[WFLY-18913] Restore the ignored service-name attribute in the opentelemetry subsystem
  • Loading branch information
darranl committed Apr 3, 2024
2 parents 9246b8f + 1bda423 commit 04ee985
Show file tree
Hide file tree
Showing 33 changed files with 1,043 additions and 504 deletions.
Expand Up @@ -43,7 +43,9 @@ public void deploy(DeploymentPhaseContext deploymentPhaseContext) throws Deploym
(OpenTelemetryConfig) support.getCapabilityRuntimeAPI("org.wildfly.extension.opentelemetry.config",
Supplier.class).get();
Map<String, String> properties = new HashMap<>(serverConfig.properties());
properties.put("otel.service.name", getServiceName(deploymentUnit));
if (!properties.containsKey("otel.service.name")) {
properties.put("otel.service.name", getServiceName(deploymentUnit));
}

weldCapability.registerExtensionInstance(new MicroProfileTelemetryCdiExtension(properties),
deploymentUnit);
Expand Down
Expand Up @@ -53,7 +53,9 @@ public void deploy(DeploymentPhaseContext deploymentPhaseContext) throws Deploym
}

Map<String, String> config = new HashMap<>(serverConfig.properties());
config.put(WildFlyOpenTelemetryConfig.OTEL_SERVICE_NAME, getServiceName(deploymentUnit));
if (!config.containsKey(WildFlyOpenTelemetryConfig.OTEL_SERVICE_NAME)) {
config.put(WildFlyOpenTelemetryConfig.OTEL_SERVICE_NAME, getServiceName(deploymentUnit));
}

weldCapability.registerExtensionInstance(new OpenTelemetryCdiExtension(useServerConfig, config), deploymentUnit);
weldCapability.registerExtensionInstance(new OpenTelemetryExtension(), deploymentUnit);
Expand Down
Expand Up @@ -70,7 +70,6 @@ class OpenTelemetrySubsystemDefinition extends PersistentResourceDefinition {
public static final WildFlyOpenTelemetryConfigSupplier CONFIG_SUPPLIER = new WildFlyOpenTelemetryConfigSupplier();
static final RuntimeCapability<WildFlyOpenTelemetryConfigSupplier> OPENTELEMETRY_CONFIG_CAPABILITY =
RuntimeCapability.Builder.of(OPENTELEMETRY_MODULE + ".config", false, CONFIG_SUPPLIER).build();
@Deprecated
public static final SimpleAttributeDefinition SERVICE_NAME = SimpleAttributeDefinitionBuilder
.create(OpenTelemetryConfigurationConstants.SERVICE_NAME, ModelType.STRING, true)
.setAllowExpression(true)
Expand Down
10 changes: 10 additions & 0 deletions testsuite/integration/microprofile/pom.xml
Expand Up @@ -326,6 +326,11 @@
<artifactId>smallrye-opentelemetry-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-opentelemetry-api</artifactId>
<scope>test</scope>
</dependency>

<!-- Other test dependencies -->
<dependency>
Expand Down Expand Up @@ -358,6 +363,11 @@
<artifactId>resteasy-json-p-provider</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-json-binding-provider</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
Expand Down
@@ -0,0 +1,70 @@
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.wildfly.test.integration.observability.container;

import java.time.Duration;
import java.util.List;

import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.WaitAllStrategy;
import org.testcontainers.containers.wait.strategy.WaitStrategy;
import org.testcontainers.utility.DockerImageName;

public abstract class BaseContainer<SELF extends GenericContainer<SELF>> extends GenericContainer<SELF> {
private final String containerName;
private final List<Integer> exposedPorts;

private static final int STARTUP_ATTEMPTS = Integer.parseInt(
System.getProperty("testsuite.integration.container.startup.attempts", "1"));
private static final Duration ATTEMPT_DURATION = Duration.parse(
System.getProperty("testsuite.integration.container.attempt.duration", "PT5S"));
protected final Boolean loggingEnabled; // Default: null/false

public BaseContainer(String containerName,
String imageName,
String imageVersion,
List<Integer> exposedPorts,
List<WaitStrategy> waitStrategies) {
super(DockerImageName.parse(imageName + ":" + imageVersion));

this.containerName = containerName;
this.exposedPorts = exposedPorts;

setWaitStrategy(buildWaitStrategy(waitStrategies));
setExposedPorts(exposedPorts);
setStartupAttempts(STARTUP_ATTEMPTS);

loggingEnabled =
Boolean.parseBoolean(System.getProperty("testsuite.integration.container.logging")) ||
Boolean.parseBoolean(System.getProperty("testsuite.integration.container." + containerName.toLowerCase() + ".logging"));

if (loggingEnabled) {
setLogConsumers(List.of(outputFrame -> {
byte[] bytes = outputFrame.getBytes();
if (bytes != null) {
debugLog(new String(bytes));
}
}));
}
}

protected void debugLog(String message) {
debugLog(containerName.toUpperCase(), message);
}

protected void debugLog(String prefix, String message) {
if (loggingEnabled) {
System.err.println("[" + prefix + "] " + message);
}
}

private WaitStrategy buildWaitStrategy(List<WaitStrategy> waitStrategies) {
WaitAllStrategy strategy = new WaitAllStrategy()
.withStartupTimeout(ATTEMPT_DURATION);
waitStrategies.forEach(strategy::withStrategy);

return strategy;
}
}
@@ -0,0 +1,94 @@
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
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;

/*
* This class is really intended to be called ONLY from OpenTelemetryCollectorContainer. Any test working with
* tracing data should be passing through the otel collector and any methods on its Container.
*/
class JaegerContainer extends BaseContainer<JaegerContainer> {
private static JaegerContainer INSTANCE = null;

public static final int PORT_JAEGER_QUERY = 16686;
public static final int PORT_JAEGER_OTLP = 4317;

private String jaegerEndpoint;

private 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;
}

@Override
public void start() {
super.start();
jaegerEndpoint = "http://localhost:" + getMappedPort(PORT_JAEGER_QUERY);
}

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

List<JaegerTrace> getTraces(String serviceName) throws InterruptedException {
try (Client client = ClientBuilder.newClient()) {
waitForDataToAppear(serviceName);
String jaegerUrl = jaegerEndpoint + "/api/traces?service=" + serviceName;
JaegerResponse jaegerResponse = client.target(jaegerUrl).request().get().readEntity(JaegerResponse.class);
return jaegerResponse.getData();
}
}

private void waitForDataToAppear(String serviceName) {
try (Client client = ClientBuilder.newClient()) {
String uri = jaegerEndpoint + "/api/services";
WebTarget target = client.target(uri);
boolean found = false;
int count = 0;
while (count < 10) {
String response = target.request().get().readEntity(String.class);
if (response.contains(serviceName)) {
found = true;
break;
}
count++;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
//
}
}

Assert.assertTrue("Expected service name not found", found);
}
}
}
@@ -0,0 +1,89 @@
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.wildfly.test.integration.observability.container;

import java.util.Collections;
import java.util.List;

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;

public static final int OTLP_GRPC_PORT = 4317;
public static final int OTLP_HTTP_PORT = 4318;
public static final int PROMETHEUS_PORT = 49152;
public static final int HEALTH_CHECK_PORT = 13133;

public static final String OTEL_COLLECTOR_CONFIG_YAML = "/etc/otel-collector-config.yaml";

private String otlpGrpcEndpoint;
private String otlpHttpEndpoint;
private String prometheusUrl;


private 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;
}

@Override
public void start() {
super.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;
super.stop();
}

public String getOtlpGrpcEndpoint() {
return otlpGrpcEndpoint;
}

public String getOtlpHttpEndpoint() {
return otlpHttpEndpoint;
}

public String getPrometheusUrl() {
return prometheusUrl;
}

public List<JaegerTrace> getTraces(String serviceName) throws InterruptedException {
return (jaegerContainer != null ? jaegerContainer.getTraces(serviceName) : Collections.emptyList());
}
}
Expand Up @@ -5,7 +5,6 @@

package org.wildfly.test.integration.observability.micrometer;

import io.micrometer.core.instrument.Tags;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
Expand All @@ -14,6 +13,7 @@

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.Timer;

/**
Expand Down

0 comments on commit 04ee985

Please sign in to comment.