Skip to content

Commit

Permalink
✨ Add Jaeger service
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarlett committed Mar 20, 2023
1 parent 2cc93e9 commit 2b686c4
Show file tree
Hide file tree
Showing 20 changed files with 730 additions and 26 deletions.
48 changes: 27 additions & 21 deletions bom/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- This file is automatically updated by update_bom.sh script, do not edit this file manually -->
<parent>
<artifactId>tnb-parent</artifactId>
Expand Down Expand Up @@ -29,6 +30,11 @@
<artifactId>system-x-amq</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>software.tnb</groupId>
<artifactId>system-x-aws</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>software.tnb</groupId>
<artifactId>system-x-aws-cloudwatch</artifactId>
Expand All @@ -51,12 +57,12 @@
</dependency>
<dependency>
<groupId>software.tnb</groupId>
<artifactId>system-x-aws-kinesis-firehose</artifactId>
<artifactId>system-x-aws-kinesis</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>software.tnb</groupId>
<artifactId>system-x-aws-kinesis</artifactId>
<artifactId>system-x-aws-kinesis-firehose</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
Expand Down Expand Up @@ -91,7 +97,7 @@
</dependency>
<dependency>
<groupId>software.tnb</groupId>
<artifactId>system-x-aws</artifactId>
<artifactId>system-x-azure</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
Expand All @@ -114,11 +120,6 @@
<artifactId>system-x-azure-storage-queue</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>software.tnb</groupId>
<artifactId>system-x-azure</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>software.tnb</groupId>
<artifactId>system-x-cassandra</artifactId>
Expand All @@ -136,12 +137,12 @@
</dependency>
<dependency>
<groupId>software.tnb</groupId>
<artifactId>system-x-db-common</artifactId>
<artifactId>system-x-db</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>software.tnb</groupId>
<artifactId>system-x-db</artifactId>
<artifactId>system-x-db-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
Expand All @@ -164,14 +165,19 @@
<artifactId>system-x-ftp</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>software.tnb</groupId>
<artifactId>system-x-google-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>software.tnb</groupId>
<artifactId>system-x-google-api-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>software.tnb</groupId>
<artifactId>system-x-google-api</artifactId>
<artifactId>system-x-google</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
Expand All @@ -181,12 +187,12 @@
</dependency>
<dependency>
<groupId>software.tnb</groupId>
<artifactId>system-x-google-cloud-common</artifactId>
<artifactId>system-x-google-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>software.tnb</groupId>
<artifactId>system-x-google-cloud</artifactId>
<artifactId>system-x-google-cloud-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
Expand Down Expand Up @@ -214,11 +220,6 @@
<artifactId>system-x-google-storage</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>software.tnb</groupId>
<artifactId>system-x-google</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>software.tnb</groupId>
<artifactId>system-x-horreum</artifactId>
Expand Down Expand Up @@ -246,19 +247,24 @@
</dependency>
<dependency>
<groupId>software.tnb</groupId>
<artifactId>system-x-jira</artifactId>
<artifactId>system-x-jaeger</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>software.tnb</groupId>
<artifactId>system-x-jms-client</artifactId>
<artifactId>system-x-jira</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>software.tnb</groupId>
<artifactId>system-x-jms</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>software.tnb</groupId>
<artifactId>system-x-jms-client</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>software.tnb</groupId>
<artifactId>system-x-kafka</artifactId>
Expand Down
23 changes: 23 additions & 0 deletions system-x/services/jaeger/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>system-x-services</artifactId>
<groupId>software.tnb</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>system-x-jaeger</artifactId>
<version>1.0-SNAPSHOT</version>
<name>TNB :: System-X :: Services :: Jaeger</name>

<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package software.tnb.jaeger.client;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

class BaseJaegerClient {

private static final String API_TRACES = "/api/traces";
protected final String queryUrl;

protected final ObjectMapper objectMapper;

BaseJaegerClient(final String queryUrl) {
this.queryUrl = queryUrl;
objectMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}

protected String apiTraceId(String traceId) {
return String.format("%s%s/%s", queryUrl, API_TRACES, traceId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package software.tnb.jaeger.client;

import software.tnb.jaeger.validation.model.Span;

import java.util.List;
import java.util.Map;

public interface JaegerClient {

Map<String, Object> getTrace(String traceId);

List<Span> getSpans(String traceId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package software.tnb.jaeger.client;

import software.tnb.common.utils.HTTPUtils;
import software.tnb.common.utils.WaitUtils;
import software.tnb.jaeger.validation.model.Span;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class UnauthenticatedJaegerClient extends BaseJaegerClient implements JaegerClient {

private static final int MAX_RETRIES_IN_SECONDS = 15;
private final HTTPUtils apiClient;

public UnauthenticatedJaegerClient(String queryUrl) {
super(queryUrl);
apiClient = HTTPUtils.getInstance(HTTPUtils.trustAllSslClient());
}

private String getRawJsonTrace(String traceId) {
final String url = apiTraceId(traceId);
WaitUtils.waitFor(() -> apiClient.get(url).isSuccessful(), MAX_RETRIES_IN_SECONDS, 1000L, "Wait for the trace to be elaborated");
return apiClient.get(url).getBody();
}

@Override
public Map<String, Object> getTrace(String traceId) {
try {
return objectMapper.readValue(getRawJsonTrace(traceId), Map.class);
} catch (JsonProcessingException e) {
throw new RuntimeException("unable to read json from response", e);
}
}

@Override
public List<Span> getSpans(String traceId) {
final Map data = ((List<Map>) getTrace(traceId).get("data")).get(0);
String serviceName = (String) ((Map) Optional.ofNullable(((Map) data.get("processes")).get("p1"))
.orElseGet(() -> Map.of("serviceName", ""))).get("serviceName");
final List<Span> spans = objectMapper.convertValue(data.get("spans"), new TypeReference<List<Span>>() {
});
return spans.stream().map(span -> span.withServiceName(serviceName)).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package software.tnb.jaeger.resource.local;

import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;

import java.util.Map;

public class JaegerContainer extends GenericContainer<JaegerContainer> {

public JaegerContainer(String image, Map<String, String> env) {
super(image);
withEnv(env);
withNetworkMode("host");
waitingFor(Wait.forLogMessage(".*Channel Connectivity change to IDLE\",\"system\":\"grpc\".*", 1));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package software.tnb.jaeger.resource.local;

import software.tnb.common.deployment.Deployable;
import software.tnb.common.deployment.WithDockerImage;
import software.tnb.jaeger.client.UnauthenticatedJaegerClient;
import software.tnb.jaeger.service.Jaeger;
import software.tnb.jaeger.service.configuration.JaegerConfiguration;
import software.tnb.jaeger.validation.JaegerValidation;

import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.auto.service.AutoService;

@AutoService(Jaeger.class)
public class LocalJaeger extends Jaeger implements Deployable, WithDockerImage {
private static final Logger LOG = LoggerFactory.getLogger(LocalJaeger.class);
private JaegerContainer container;

@Override
public void deploy() {
LOG.info("Starting Jaeger container");
container = new JaegerContainer(image(), env());
container.start();
LOG.info("Jaeger container started");
}

@Override
public void undeploy() {
if (container != null) {
LOG.info("Stopping Jaeger container");
container.stop();
}
}

@Override
public void openResources() {

}

@Override
public void closeResources() {

}

@Override
public String getLog() {
return container.getLogs();
}

@Override
public String getCollectorUrl(JaegerConfiguration.CollectorPort port) {
return getUrl(port);
}

@Override
public String getQueryUrl(JaegerConfiguration.QueryPort port) {
return getUrl(port);
}

@Override
public String getExternalUrl() {
return getQueryUrl(JaegerConfiguration.QueryPort.HTTP);
}

@Override
protected JaegerValidation getClientBasedValidation() {
return new JaegerValidation(new UnauthenticatedJaegerClient(getQueryUrl(JaegerConfiguration.QueryPort.HTTP)));
}

@NotNull
private String getUrl(JaegerConfiguration.WithPort port) {
return "http://localhost:" + port.portNumber();
}

@Override
public String defaultImage() {
return "registry.redhat.io/rhosdt/jaeger-all-in-one-rhel8:latest";
}
}
Loading

0 comments on commit 2b686c4

Please sign in to comment.