Skip to content

Commit

Permalink
Add logging instrumentation in GraphQLResource
Browse files Browse the repository at this point in the history
  • Loading branch information
assadriaz committed Apr 25, 2024
1 parent 30054c1 commit 7660413
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import graphql.GraphQLException;
import graphql.analysis.MaxQueryDepthInstrumentation;
import graphql.execution.AbortExecutionException;
import graphql.execution.instrumentation.ChainedInstrumentation;
import graphql.execution.instrumentation.Instrumentation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.PostConstruct;
import jakarta.ws.rs.Consumes;
Expand All @@ -51,6 +53,7 @@
import org.springframework.transaction.support.TransactionTemplate;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -82,6 +85,10 @@ public class GraphQLResource {
private StopPlaceRegisterGraphQLSchema stopPlaceRegisterGraphQLSchema;


@Autowired
private RequestLoggingInstrumentation requestLoggingInstrumentation;


private final TransactionTemplate transactionTemplate;


Expand All @@ -92,9 +99,16 @@ public GraphQLResource(PlatformTransactionManager transactionManager) {

@PostConstruct
public void init() {
List<Instrumentation> chainedList = new ArrayList<>();
chainedList.add(new MaxQueryDepthInstrumentation(MAX_DEPTH));
chainedList.add(requestLoggingInstrumentation);

final ChainedInstrumentation chainedInstrumentation = new ChainedInstrumentation(chainedList);


logger.info(String.format("max query depth is: %d", MAX_DEPTH));
graphQL = GraphQL.newGraphQL(stopPlaceRegisterGraphQLSchema.stopPlaceRegisterSchema)
.instrumentation(new MaxQueryDepthInstrumentation(MAX_DEPTH))
.instrumentation(chainedInstrumentation)
.build();

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.rutebanken.tiamat.rest.graphql;

import graphql.ExecutionResult;
import graphql.execution.instrumentation.InstrumentationContext;
import graphql.execution.instrumentation.SimpleInstrumentation;
import graphql.execution.instrumentation.SimpleInstrumentationContext;
import graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static org.rutebanken.tiamat.config.JerseyConfig.ET_CLIENT_NAME_HEADER;

@Component
public class RequestLoggingInstrumentation extends SimpleInstrumentation {
public static final Logger logger = LoggerFactory.getLogger(RequestLoggingInstrumentation.class);

@Autowired
private HttpServletRequest httpServletRequest;

@Override
public InstrumentationContext<ExecutionResult> beginExecution(InstrumentationExecutionParameters parameters) {
long startMillis = System.currentTimeMillis();
var executionId = parameters.getExecutionInput().getExecutionId();

final String etClientName = httpServletRequest.getHeader(ET_CLIENT_NAME_HEADER);

logger.debug("[{}] ClientId: {}, Query: {}", executionId,etClientName, parameters.getQuery());
if (parameters.getVariables() != null && !parameters.getVariables().isEmpty()) {
logger.info("[{}] ClientId: {}, variables: {}", executionId,etClientName, parameters.getVariables());
}

return SimpleInstrumentationContext.whenCompleted(((executionResult, throwable) -> {

long endMillis = System.currentTimeMillis();
long duration = endMillis - startMillis;
if (throwable == null) {
logger.debug("[{}] ClientId: {}, completed in {}ms", executionId,etClientName, duration);

} else {
logger.warn("Failed in: {} ", duration, throwable);
}

}));

}

}

0 comments on commit 7660413

Please sign in to comment.