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

feat(monitoring): track graphql errors in metrics #6087

Merged
merged 9 commits into from
Sep 29, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ private Map<String, Object> buildExtensions(DataHubGraphQLErrorCode errorCode) {
return extensions;
}

public int getErrorCode() { return errorCode.getCode(); }

@Override
public String getMessage() {
return message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.linkedin.datahub.graphql.GraphQLEngine;
import com.linkedin.datahub.graphql.exception.DataHubGraphQLError;
import com.linkedin.metadata.utils.metrics.MetricUtils;
import graphql.ExecutionResult;
import java.util.Collections;
Expand Down Expand Up @@ -126,9 +127,25 @@ void getGraphQL(HttpServletRequest request, HttpServletResponse response) {
throw new UnsupportedOperationException("GraphQL gets not supported.");
}

private void observeErrors(ExecutionResult executionResult) {
szalai1 marked this conversation as resolved.
Show resolved Hide resolved
executionResult.getErrors().forEach(graphQLError -> {
if (graphQLError instanceof DataHubGraphQLError) {
DataHubGraphQLError DHGraphQLError = (DataHubGraphQLError) graphQLError;
int errorCode = DHGraphQLError.getErrorCode();
MetricUtils.get().counter(MetricRegistry.name(this.getClass(), "errorCode", Integer.toString(errorCode))).inc();
} else {
MetricUtils.get().counter(MetricRegistry.name(this.getClass(), "errorType", graphQLError.getErrorType().toString())).inc();
}
});
if(executionResult.getErrors().size() != 0) {
MetricUtils.get().counter(MetricRegistry.name(this.getClass(), "error")).inc();
}
}

@SuppressWarnings("unchecked")
private void submitMetrics(ExecutionResult executionResult) {
try {
observeErrors(executionResult);
Object tracingInstrumentation = executionResult.getExtensions().get("tracing");
if (tracingInstrumentation instanceof Map) {
Map<String, Object> tracingMap = (Map<String, Object>) tracingInstrumentation;
Expand All @@ -137,11 +154,12 @@ private void submitMetrics(ExecutionResult executionResult) {
// Extract top level resolver, parent is top level query. Assumes single query per call.
List<Map<String, Object>> resolvers = (List<Map<String, Object>>) executionData.get("resolvers");
Optional<Map<String, Object>>
parentResolver = resolvers.stream().filter(resolver -> resolver.get("parentType").equals("Query")).findFirst();
parentResolver = resolvers.stream().filter(resolver -> resolver.get("parentType").equals("Query")).findFirst();
String fieldName = parentResolver.isPresent() ? (String) parentResolver.get().get("fieldName") : "UNKNOWN";
MetricUtils.get().histogram(MetricRegistry.name(this.getClass(), fieldName)).update(totalDuration);
}
} catch (Exception e) {
MetricUtils.get().counter(MetricRegistry.name(this.getClass(),"submitMetrics", "exception")).inc();
log.error("Unable to submit metrics for GraphQL call.", e);
}
}
Expand Down