|
| 1 | +/* |
| 2 | + * |
| 3 | + * |
| 4 | + * * Licensed under the EUPL, Version 1.2 or – as soon they will be approved by |
| 5 | + * * the European Commission - subsequent versions of the EUPL (the "Licence"); |
| 6 | + * * You may not use this work except in compliance with the Licence. |
| 7 | + * * You may obtain a copy of the Licence at: |
| 8 | + * * |
| 9 | + * * https://joinup.ec.europa.eu/software/page/eupl |
| 10 | + * * |
| 11 | + * * Unless required by applicable law or agreed to in writing, software |
| 12 | + * * distributed under the Licence is distributed on an "AS IS" basis, |
| 13 | + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * * See the Licence for the specific language governing permissions and |
| 15 | + * * limitations under the Licence. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package org.entur.lamassu.config.graphql; |
| 20 | + |
| 21 | +import graphql.ExecutionResult; |
| 22 | +import graphql.execution.instrumentation.InstrumentationContext; |
| 23 | +import graphql.execution.instrumentation.SimpleInstrumentationContext; |
| 24 | +import graphql.execution.instrumentation.SimplePerformantInstrumentation; |
| 25 | +import graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters; |
| 26 | +import jakarta.servlet.http.HttpServletRequest; |
| 27 | +import org.jetbrains.annotations.NotNull; |
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
| 30 | +import org.springframework.beans.factory.annotation.Autowired; |
| 31 | +import org.springframework.beans.factory.annotation.Value; |
| 32 | +import org.springframework.stereotype.Component; |
| 33 | + |
| 34 | +@Component |
| 35 | +public class RequestLoggingInstrumentation extends SimplePerformantInstrumentation { |
| 36 | + |
| 37 | + public static final Logger logger = LoggerFactory.getLogger( |
| 38 | + RequestLoggingInstrumentation.class |
| 39 | + ); |
| 40 | + |
| 41 | + @Autowired |
| 42 | + private HttpServletRequest httpServletRequest; |
| 43 | + |
| 44 | + @Value("org.entur.lamassu.graphql.instrumentation.extract-header-name") |
| 45 | + private String extractHeaderName; |
| 46 | + |
| 47 | + @Override |
| 48 | + public @NotNull InstrumentationContext<ExecutionResult> beginExecution( |
| 49 | + InstrumentationExecutionParameters parameters |
| 50 | + ) { |
| 51 | + long startMillis = System.currentTimeMillis(); |
| 52 | + var executionId = parameters.getExecutionInput().getExecutionId(); |
| 53 | + |
| 54 | + final String extractHeaderValue = httpServletRequest.getHeader(extractHeaderName); |
| 55 | + |
| 56 | + logger.debug( |
| 57 | + "[{}] {}: {}, Query: {}", |
| 58 | + executionId, |
| 59 | + extractHeaderName, |
| 60 | + extractHeaderValue, |
| 61 | + parameters.getQuery() |
| 62 | + ); |
| 63 | + if (parameters.getVariables() != null && !parameters.getVariables().isEmpty()) { |
| 64 | + logger.info( |
| 65 | + "[{}] {}: {}, variables: {}", |
| 66 | + executionId, |
| 67 | + extractHeaderName, |
| 68 | + extractHeaderValue, |
| 69 | + parameters.getVariables() |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + return SimpleInstrumentationContext.whenCompleted( |
| 74 | + ( |
| 75 | + (executionResult, throwable) -> { |
| 76 | + long endMillis = System.currentTimeMillis(); |
| 77 | + long duration = endMillis - startMillis; |
| 78 | + if (throwable == null) { |
| 79 | + logger.debug( |
| 80 | + "[{}] {}: {}, completed in {}ms", |
| 81 | + executionId, |
| 82 | + extractHeaderName, |
| 83 | + extractHeaderValue, |
| 84 | + duration |
| 85 | + ); |
| 86 | + } else { |
| 87 | + logger.warn("Failed in: {} ", duration, throwable); |
| 88 | + } |
| 89 | + } |
| 90 | + ) |
| 91 | + ); |
| 92 | + } |
| 93 | +} |
0 commit comments