Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OTP request timeout GraphQL instrumentation #5881

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.opentripplanner.apis.transmodel;

import static graphql.execution.instrumentation.SimpleInstrumentationContext.noOp;

import graphql.execution.instrumentation.Instrumentation;
import graphql.execution.instrumentation.InstrumentationContext;
import graphql.execution.instrumentation.InstrumentationState;
import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters;
import java.util.concurrent.atomic.AtomicLong;
import org.opentripplanner.framework.application.OTPRequestTimeoutException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A GraphQL instrumentation that periodically checks the OTP request interruption status while the
* query is being processed.
*/
public class OTPRequestTimeoutInstrumentation implements Instrumentation {

private static final Logger LOG = LoggerFactory.getLogger(OTPRequestTimeoutInstrumentation.class);

private final AtomicLong fieldFetchCounter = new AtomicLong();

@Override
public InstrumentationContext<Object> beginFieldFetch(
InstrumentationFieldFetchParameters parameters,
InstrumentationState state
) {
long fetched = fieldFetchCounter.incrementAndGet();
if (fetched % 100000 == 0) {
LOG.debug("Fetched {} fields", fetched);
OTPRequestTimeoutException.checkForTimeout();
}
return noOp();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ Response executeGraphQL(
}

private static Instrumentation createInstrumentation(int maxResolves, Iterable<Tag> tracingTags) {
Instrumentation instrumentation = new MaxQueryComplexityInstrumentation(maxResolves);
Instrumentation instrumentation = new ChainedInstrumentation(
new MaxQueryComplexityInstrumentation(maxResolves),
new OTPRequestTimeoutInstrumentation()
);

if (OTPFeature.ActuatorAPI.isOn()) {
instrumentation =
Expand Down
Loading