Skip to content

Commit b6f9d16

Browse files
committed
improve test
1 parent 82ca4cd commit b6f9d16

File tree

1 file changed

+88
-3
lines changed
  • instrumentation/graphql-java/graphql-java-20.0/library/src/test/java/io/opentelemetry/instrumentation/graphql/v20_0

1 file changed

+88
-3
lines changed

instrumentation/graphql-java/graphql-java-20.0/library/src/test/java/io/opentelemetry/instrumentation/graphql/v20_0/GraphqlTest.java

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,9 @@ void noDataFetcherSpansCreated() {
241241
.hasParent(spanWithName("query findBookById"))));
242242
}
243243

244+
// test data fetcher throwing an exception
244245
@Test
245-
void dataFetcherError() {
246+
void dataFetcherException() {
246247
// Arrange
247248
GraphQLTelemetry telemetry =
248249
GraphQLTelemetry.builder(testing.getOpenTelemetry())
@@ -254,11 +255,12 @@ void dataFetcherError() {
254255
GraphQL.newGraphQL(graphqlSchema).instrumentation(telemetry.newInstrumentation()).build();
255256

256257
// Act
258+
// book-exception triggers exception in data fetcher
257259
ExecutionResult result =
258260
graphql.execute(
259261
""
260262
+ " query findBookById {\n"
261-
+ " bookById(id: \"book-failure\") {\n"
263+
+ " bookById(id: \"book-exception\") {\n"
262264
+ " name\n"
263265
+ " author {\n"
264266
+ " name\n"
@@ -301,7 +303,9 @@ void dataFetcherError() {
301303
.hasParent(spanWithName("query findBookById"))
302304
.hasAttributesSatisfyingExactly(
303305
equalTo(GRAPHQL_FIELD_NAME, "bookById"),
304-
equalTo(GRAPHQL_FIELD_PATH, "/bookById")),
306+
equalTo(GRAPHQL_FIELD_PATH, "/bookById"))
307+
.hasStatus(StatusData.error())
308+
.hasException(new IllegalStateException("fetching book failed")),
305309
span ->
306310
span.hasName("fetchBookById")
307311
.hasKind(SpanKind.INTERNAL)
@@ -310,6 +314,87 @@ void dataFetcherError() {
310314
.hasException(new IllegalStateException("fetching book failed"))));
311315
}
312316

317+
// test data fetcher returning an error
318+
@Test
319+
void dataFetcherError() {
320+
// Arrange
321+
GraphQLTelemetry telemetry =
322+
GraphQLTelemetry.builder(testing.getOpenTelemetry())
323+
.setDataFetcherInstrumentationEnabled(true)
324+
.setAddOperationNameToSpanName(true)
325+
.build();
326+
327+
GraphQL graphql =
328+
GraphQL.newGraphQL(graphqlSchema).instrumentation(telemetry.newInstrumentation()).build();
329+
330+
// Act
331+
// book-graphql-error triggers returning an error from data fetcher
332+
ExecutionResult result =
333+
graphql.execute(
334+
""
335+
+ " query findBookById {\n"
336+
+ " bookById(id: \"book-graphql-error\") {\n"
337+
+ " name\n"
338+
+ " author {\n"
339+
+ " name\n"
340+
+ " }\n"
341+
+ " }\n"
342+
+ " }");
343+
344+
// Assert
345+
assertThat(result.getErrors()).isNotEmpty();
346+
347+
testing.waitAndAssertTraces(
348+
trace ->
349+
trace.hasSpansSatisfyingExactly(
350+
span ->
351+
span.hasName("query findBookById")
352+
.hasKind(SpanKind.INTERNAL)
353+
.hasNoParent()
354+
.hasAttributesSatisfyingExactly(
355+
equalTo(
356+
GraphqlIncubatingAttributes.GRAPHQL_OPERATION_NAME, "findBookById"),
357+
equalTo(GraphqlIncubatingAttributes.GRAPHQL_OPERATION_TYPE, "query"),
358+
normalizedQueryEqualsTo(
359+
GraphqlIncubatingAttributes.GRAPHQL_DOCUMENT,
360+
"query findBookById { bookById(id: ?) { name author { name } } }"))
361+
.hasStatus(StatusData.error())
362+
.hasEventsSatisfyingExactly(
363+
event ->
364+
event
365+
.hasName("exception")
366+
.hasAttributesSatisfyingExactly(
367+
equalTo(
368+
ExceptionAttributes.EXCEPTION_TYPE,
369+
"DataFetchingException"),
370+
equalTo(
371+
ExceptionAttributes.EXCEPTION_MESSAGE,
372+
"failed to fetch book"))),
373+
span ->
374+
span.hasName("bookById")
375+
.hasKind(SpanKind.INTERNAL)
376+
.hasParent(spanWithName("query findBookById"))
377+
.hasAttributesSatisfyingExactly(
378+
equalTo(GRAPHQL_FIELD_NAME, "bookById"),
379+
equalTo(GRAPHQL_FIELD_PATH, "/bookById"))
380+
.hasStatus(StatusData.error())
381+
.hasEventsSatisfyingExactly(
382+
event ->
383+
event
384+
.hasName("exception")
385+
.hasAttributesSatisfyingExactly(
386+
equalTo(
387+
ExceptionAttributes.EXCEPTION_TYPE,
388+
"DataFetchingException"),
389+
equalTo(
390+
ExceptionAttributes.EXCEPTION_MESSAGE,
391+
"failed to fetch book"))),
392+
span ->
393+
span.hasName("fetchBookById")
394+
.hasKind(SpanKind.INTERNAL)
395+
.hasParent(spanWithName("bookById"))));
396+
}
397+
313398
private static SpanData spanWithName(String name) {
314399
return testing.spans().stream()
315400
.filter(span -> span.getName().equals(name))

0 commit comments

Comments
 (0)