Skip to content

Commit 531a0db

Browse files
committed
Add request logging instrumentation
1 parent 22b568b commit 531a0db

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
/*
20+
*
21+
*
22+
* * Licensed under the EUPL, Version 1.2 or – as soon they will be approved by
23+
* * the European Commission - subsequent versions of the EUPL (the "Licence");
24+
* * You may not use this work except in compliance with the Licence.
25+
* * You may obtain a copy of the Licence at:
26+
* *
27+
* * https://joinup.ec.europa.eu/software/page/eupl
28+
* *
29+
* * Unless required by applicable law or agreed to in writing, software
30+
* * distributed under the Licence is distributed on an "AS IS" basis,
31+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32+
* * See the Licence for the specific language governing permissions and
33+
* * limitations under the Licence.
34+
*
35+
*/
36+
37+
package org.entur.lamassu.config.graphql;
38+
39+
import graphql.execution.instrumentation.Instrumentation;
40+
import java.util.ArrayList;
41+
import java.util.List;
42+
import org.springframework.context.annotation.Bean;
43+
import org.springframework.context.annotation.Configuration;
44+
45+
@Configuration
46+
public class InstrumentationConfiguration {
47+
48+
/**
49+
* Return all instrumentations as a bean.
50+
*/
51+
@Bean
52+
List<Instrumentation> instrumentations() {
53+
// Note: Due to a bug in GraphQLWebAutoConfiguration, the returned list has to be modifiable (it will be sorted)
54+
return new ArrayList<>(List.of(new RequestLoggingInstrumentation()));
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

Comments
 (0)