Skip to content

Commit

Permalink
Add smoke test for Logback MDC instrumentation (#1227)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rzeszutek authored Sep 22, 2020
1 parent 5598544 commit f76fb75
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions smoke-tests/smoke-tests.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dependencies {

implementation deps.slf4j
implementation deps.opentelemetryProto
implementation deps.opentelemetryApi
implementation "org.testcontainers:testcontainers:1.14.3"
implementation 'com.fasterxml.jackson.core:jackson-databind:2.11.2'
implementation 'com.google.protobuf:protobuf-java-util:3.12.4'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@

package io.opentelemetry.smoketest

import static java.util.stream.Collectors.toSet

import com.fasterxml.jackson.databind.ObjectMapper
import com.google.protobuf.util.JsonFormat
import io.opentelemetry.auto.test.utils.OkHttpUtils
import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest
import io.opentelemetry.proto.common.v1.AnyValue
import io.opentelemetry.proto.trace.v1.Span
import java.util.concurrent.TimeUnit
import java.util.regex.Pattern
import java.util.stream.Stream
import okhttp3.OkHttpClient
import okhttp3.Request
Expand All @@ -31,13 +34,15 @@ import org.slf4j.LoggerFactory
import org.testcontainers.containers.GenericContainer
import org.testcontainers.containers.Network
import org.testcontainers.containers.output.Slf4jLogConsumer
import org.testcontainers.containers.output.ToStringConsumer
import org.testcontainers.containers.wait.strategy.Wait
import org.testcontainers.utility.MountableFile
import spock.lang.Shared
import spock.lang.Specification

abstract class SmokeTest extends Specification {
private static final Logger logger = LoggerFactory.getLogger(SmokeTest)
private static final Pattern TRACE_ID_PATTERN = Pattern.compile(".*traceId=(?<traceId>[a-zA-Z0-9]+).*")

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()

Expand Down Expand Up @@ -86,9 +91,11 @@ abstract class SmokeTest extends Specification {
}

def startTarget(int jdk) {
def output = new ToStringConsumer()
target = new GenericContainer<>(getTargetImage(jdk))
.withExposedPorts(8080)
.withNetwork(network)
.withLogConsumer(output)
.withLogConsumer(new Slf4jLogConsumer(logger))
.withCopyFileToContainer(MountableFile.forHostPath(agentPath), "/opentelemetry-javaagent-all.jar")
.withEnv("JAVA_TOOL_OPTIONS", "-javaagent:/opentelemetry-javaagent-all.jar")
Expand All @@ -97,6 +104,7 @@ abstract class SmokeTest extends Specification {
.withEnv("OTEL_OTLP_ENDPOINT", "collector:55680")
.withEnv(extraEnv)
target.start()
output
}

def cleanup() {
Expand Down Expand Up @@ -172,4 +180,15 @@ abstract class SmokeTest extends Specification {

return content
}

protected static Set<String> getLoggedTraceIds(ToStringConsumer output) {
output.toUtf8String().lines()
.flatMap(SmokeTest.&findTraceId)
.collect(toSet())
}

private static Stream<String> findTraceId(String log) {
def m = TRACE_ID_PATTERN.matcher(log)
m.matches() ? Stream.of(m.group("traceId")) : Stream.empty() as Stream<String>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,25 @@

package io.opentelemetry.smoketest

import static java.util.stream.Collectors.toSet

import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest
import io.opentelemetry.trace.TraceId
import java.util.jar.Attributes
import java.util.jar.JarFile
import java.util.stream.Collectors
import okhttp3.Request
import spock.lang.Unroll

class SpringBootSmokeTest extends SmokeTest {

protected String getTargetImage(int jdk) {
"open-telemetry-docker-dev.bintray.io/java/smoke-springboot-jdk$jdk:latest"
}

@Unroll
def "spring boot smoke test on JDK #jdk"(int jdk) {
setup:
startTarget(jdk)
def output = startTarget(jdk)
String url = "http://localhost:${target.getMappedPort(8080)}/greeting"
def request = new Request.Builder().url(url).get().build()

Expand All @@ -49,7 +52,12 @@ class SpringBootSmokeTest extends SmokeTest {

[currentAgentVersion] as Set == findResourceAttribute(traces, "telemetry.auto.version")
.map { it.stringValue }
.collect(Collectors.toSet())
.collect(toSet())

then: "correct traceIds are logged via MDC instrumentation"
getLoggedTraceIds(output) == getSpanStream(traces)
.map({ TraceId.bytesToHex(it.getTraceId().toByteArray()) })
.collect(toSet())

cleanup:
stopTarget()
Expand Down

0 comments on commit f76fb75

Please sign in to comment.