Skip to content

Commit

Permalink
Add new test mode for Java
Browse files Browse the repository at this point in the history
  • Loading branch information
grcevski committed Oct 15, 2024
1 parent a786022 commit fbade2a
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 2 deletions.
6 changes: 6 additions & 0 deletions test/integration/components/javatestserver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
<artifactId>spring-web</artifactId>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.14</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package de.fstab.demo.greeting;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

@Service
public class ApiCallService {
HttpClient httpClient = HttpClients.createDefault();

public String makeApiCall(String apiUrl) throws IOException {
HttpGet httpGet = new HttpGet(apiUrl);

HttpResponse response = httpClient.execute(httpGet);

BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder result = new StringBuilder();
String line;

while ((line = reader.readLine()) != null) {
result.append(line);
}

return result.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
@RestController
public class GreetingController {
private final LookupService lookupService;
private final ApiCallService apiCallService;

public GreetingController(LookupService lookupService) {
RestClient commonClient = RestClient.create();

public GreetingController(LookupService lookupService, ApiCallService apiCallService) {
this.lookupService = lookupService;
this.apiCallService = apiCallService;
}

public static void randomSleep(Duration averageDuration) throws InterruptedException{
Expand Down Expand Up @@ -62,4 +66,29 @@ public ResponseEntity<String> getDist2(
CompletableFuture.allOf(aa).join();
return ResponseEntity.status(response).body(aa.get());
}

@GetMapping("/jtraceA")
public ResponseEntity<String> getDistA(
@RequestParam(required = false, defaultValue = "10", name="delay") Integer delay,
@RequestParam(required = false, defaultValue = "200", name="response") Integer response
) throws Exception {
randomSleep(Duration.ofMillis(delay));
String data = commonClient.get()
.uri("http://ntestserver:3030/traceme")
.accept(MediaType.ALL)
.retrieve()
.body(String.class);
return ResponseEntity.status(response).body(data);
}

@GetMapping("/jtraceB")
public ResponseEntity<String> getDistB(
@RequestParam(required = false, defaultValue = "10", name="delay") Integer delay,
@RequestParam(required = false, defaultValue = "200", name="response") Integer response
) throws Exception {
randomSleep(Duration.ofMillis(delay));
String data = apiCallService.makeApiCall("http://ntestserver:3030/traceme");
return ResponseEntity.status(response).body(data);
}

}
2 changes: 1 addition & 1 deletion test/integration/docker-compose-multiexec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ services:
- 3044:3043

jtestserver:
image: ghcr.io/grafana/beyla-test/greeting-java-jar/0.0.4
image: ghcr.io/grafana/beyla-test/greeting-java-jar/0.0.5
ports:
- "8086:8085"

Expand Down
69 changes: 69 additions & 0 deletions test/integration/traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,75 @@ func testNestedHTTPTracesKProbes(t *testing.T) {
)
assert.Empty(t, sd, sd.String())
}

// test now with a different version of Java thread pool
for i := 0; i < 10; i++ {
doHTTPGet(t, "http://localhost:8086/jtraceB", 200)
}

t.Run("Traces RestClient client /jtraceB", func(t *testing.T) {
ensureTracesMatch(t, "jtraceB")
})
}

func ensureTracesMatch(t *testing.T, urlPath string) {
var multipleTraces []jaeger.Trace
test.Eventually(t, testTimeout, func(t require.TestingT) {
resp, err := http.Get(jaegerQueryURL + "?service=java-service&operation=GET%20%2F" + urlPath)
require.NoError(t, err)
if resp == nil {
return
}
require.Equal(t, http.StatusOK, resp.StatusCode)
var tq jaeger.TracesQuery
require.NoError(t, json.NewDecoder(resp.Body).Decode(&tq))
traces := tq.FindBySpan(jaeger.Tag{Key: "url.path", Type: "string", Value: "/" + urlPath})
require.LessOrEqual(t, 5, len(traces))
multipleTraces = traces
}, test.Interval(500*time.Millisecond))

// Ensure all 5 traces have proper full chain Java -> Node
for _, trace := range multipleTraces {
// Check the information of the java parent span
res := trace.FindByOperationName("GET /" + urlPath)
require.Len(t, res, 1)
parent := res[0]
require.NotEmpty(t, parent.TraceID)
traceID := parent.TraceID
require.NotEmpty(t, parent.SpanID)
// check duration is at least 2us
assert.Less(t, (2 * time.Microsecond).Microseconds(), parent.Duration)
// check span attributes
sd := parent.Diff(
jaeger.Tag{Key: "http.request.method", Type: "string", Value: "GET"},
jaeger.Tag{Key: "http.response.status_code", Type: "int64", Value: float64(200)},
jaeger.Tag{Key: "url.path", Type: "string", Value: "/" + urlPath},
jaeger.Tag{Key: "server.port", Type: "int64", Value: float64(8085)},
jaeger.Tag{Key: "http.route", Type: "string", Value: "/" + urlPath},
jaeger.Tag{Key: "span.kind", Type: "string", Value: "server"},
)
assert.Empty(t, sd, sd.String())

// Check the information of the nodejs parent span
res = trace.FindByOperationName("GET /traceme")
require.Len(t, res, 1)
parent = res[0]
require.NotEmpty(t, parent.TraceID)
require.Equal(t, traceID, parent.TraceID)
require.NotEmpty(t, parent.SpanID)
// check duration is at least 2us
assert.Less(t, (2 * time.Microsecond).Microseconds(), parent.Duration)
// check span attributes
sd = parent.Diff(
jaeger.Tag{Key: "http.request.method", Type: "string", Value: "GET"},
jaeger.Tag{Key: "http.response.status_code", Type: "int64", Value: float64(200)},
jaeger.Tag{Key: "url.path", Type: "string", Value: "/traceme"},
jaeger.Tag{Key: "server.port", Type: "int64", Value: float64(3030)},
jaeger.Tag{Key: "http.route", Type: "string", Value: "/traceme"},
jaeger.Tag{Key: "span.kind", Type: "string", Value: "server"},
)
assert.Empty(t, sd, sd.String())
}
}

func testNestedHTTPSTracesKProbes(t *testing.T) {
Expand Down

0 comments on commit fbade2a

Please sign in to comment.