Skip to content

Commit

Permalink
Add smoke test for telemetry app-started
Browse files Browse the repository at this point in the history
  • Loading branch information
smola committed Nov 14, 2024
1 parent 7185aac commit bf4901e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import datadog.trace.agent.test.utils.OkHttpUtils
import datadog.trace.agent.test.utils.PortUtils
import okhttp3.OkHttpClient
import spock.lang.Shared
import static org.junit.Assume.assumeTrue

import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
Expand Down Expand Up @@ -116,4 +117,23 @@ abstract class AbstractServerSmokeTest extends AbstractSmokeTest {
}
return remaining
}

boolean testTelemetry() {
return true
}

void 'receive telemetry app-started'() {
when:
assumeTrue(testTelemetry())
waitForTelemetryCount(1)

then:
telemetryMessages.size() >= 1
Object msg = telemetryMessages.get(0)
msg['request_type'] == 'app-started'
}

List<String> expectedTelemetryDependencies() {
[]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,23 +129,22 @@ abstract class AbstractSmokeTest extends ProcessManager {
response.status(200).send(remoteConfigResponse)
}
prefix("/telemetry/proxy/api/v2/apmtelemetry") {
def body = request.getBody()
if (body != null) {
Map<String, Object> msg = null
try {
msg = new JsonSlurper().parseText(new String(body, StandardCharsets.UTF_8)) as Map<String, Object>
} catch (Throwable t) {
println("=== Failure during telemetry decoding ===")
t.printStackTrace(System.out)
telemetryDecodingFailure = t
throw t
}
telemetryMessages.add(msg)
if (msg.get("request_type") == "message-batch") {
msg.get("payload")?.each { telemetryFlatMessages.add(it as Map<String, Object>) }
} else {
telemetryFlatMessages.add(msg)
try {
byte[] body = request.getBody()
if (body != null) {
Map<String, Object> msg = new JsonSlurper().parseText(new String(body, StandardCharsets.UTF_8)) as Map<String, Object>
telemetryMessages.add(msg)
if (msg.get("request_type") == "message-batch") {
msg.get("payload")?.each { telemetryFlatMessages.add(it as Map<String, Object>) }
} else {
telemetryFlatMessages.add(msg)
}
}
} catch (Throwable t) {
println("=== Failure during telemetry decoding ===")
t.printStackTrace(System.out)
telemetryDecodingFailure = t
throw t
}
response.status(202).send()
}
Expand Down Expand Up @@ -300,6 +299,31 @@ abstract class AbstractSmokeTest extends ProcessManager {
}
}

void waitForTelemetryCount(final int count) {
def conditions = new PollingConditions(timeout: 30, initialDelay: 0, delay: 1, factor: 1)
waitForTelemetryCount(conditions, count)
}

void waitForTelemetryCount(final PollingConditions poll, final int count) {
poll.eventually {
telemetryMessages.size() >= count
}
}

void waitForTelemetryFlat(final Function<Map<String, Object>, Boolean> predicate) {
def conditions = new PollingConditions(timeout: 30, initialDelay: 0, delay: 1, factor: 1)
waitForTelemetryFlat(conditions, predicate)
}

void waitForTelemetryFlat(final PollingConditions poll, final Function<Map<String, Object>, Boolean> predicate) {
poll.eventually {
if (telemetryDecodingFailure != null) {
throw telemetryDecodingFailure
}
assert telemetryFlatMessages.find { predicate.apply(it) } != null
}
}

List<DecodedTrace> getTraces() {
decodeTraces
}
Expand Down

0 comments on commit bf4901e

Please sign in to comment.