Previously, GraphQlTagsContributor was unable to produce tags base on GQL data fetcher result as DataFetcher<?> does not really allow to access its result. Sometimes accessing the result (POJO) is necessary as the requester may opt out from returning errors in their GQL query, but you still want to record that operation as a failure.

For example, assume your mutations can return a standardised type of a response:

interface Payload {
   success: Boolean!
   errors: [Error!]
}
type Error {
  code: Int!
}

Each data fetcher call can return 0* errors. A requester can opt out from returning them in the request response, hence it's impossible to add tags based on those errors to the metric graphql.datafetcher, unless I am missing something.

With this change a developer can do something like that:

class MyGraphQlTagsContributor implements GraphQlTagsContributor {
    (...)
    @Override
    public Iterable<Tag> getDataFetchingTags(
        DataFetcher<?> dataFetcher,
        Object dataFetcherResult,
        InstrumentationFieldFetchParameters parameters,
        Throwable exception
    ) {
        return Optional.ofNullable(dataFetcherResult)
            .filter(Payload.class::isInstance)
            .map(Payload.class::cast)
            .map(Payload::errors)
            .map(errors -> errors.stream()
                .map(Error::code)
                .reduce(0, Integer::max))
            .map(code -> Tags.of("code", String.valueOf(code)))
            .orElseGet(Tags::empty);            
    }
}

interface Payload {
    boolean success();
    List<Error> errors();
}

interface Error {
    int code();
}

Comment From: pivotal-cla

@gzgudka Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

Comment From: pivotal-cla

@gzgudka Thank you for signing the Contributor License Agreement!

Comment From: bclozel

Thanks for your contribution @gzgudka , but the instrumentation is moving entirely out of Spring Boot. Hopefully the new implementation will cover those needs. Please follow spring-projects/spring-graphql#501 for more information about that.