Skip to content

Commit

Permalink
Merge pull request #315 from newrelic/support/graphql-NR-299885
Browse files Browse the repository at this point in the history
[NR-299885] Instrumentation support for GraphQL
  • Loading branch information
IshikaDawda authored Sep 20, 2024
2 parents 08b06c7 + 5cbe093 commit 1b7651c
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The agent version.
agentVersion=1.4.2
jsonVersion=1.2.7
jsonVersion=1.2.9
# Updated exposed NR APM API version.
nrAPIVersion=8.12.0

Expand Down
22 changes: 22 additions & 0 deletions instrumentation-security/graphql-java-16.2/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
dependencies {
implementation(project(":newrelic-security-api"))
implementation("com.newrelic.agent.java:newrelic-api:${nrAPIVersion}")
implementation("com.newrelic.agent.java:newrelic-weaver-api:${nrAPIVersion}")
implementation("com.graphql-java:graphql-java:16.2")
}

jar {
manifest { attributes 'Implementation-Title': 'com.newrelic.instrumentation.security.graphql-java-16.2' }
}

verifyInstrumentation {
passesOnly('com.graphql-java:graphql-java:[16.0,)')
excludeRegex('com.graphql-java:graphql-java:(0.0.0|201|202).*')
excludeRegex('com.graphql-java:graphql-java:.*(vTEST|-beta|-alpha1|-nf-execution|-rc|-TEST).*')
exclude('com.graphql-java:graphql-java:15.0')
}

site {
title 'GraphQL Java'
type 'Framework'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package graphql;

import com.newrelic.api.agent.security.NewRelicSecurity;
import com.newrelic.api.agent.security.schema.HttpRequest;
import com.newrelic.api.agent.security.schema.HttpRequestCustomDataTypeEnum;
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;
import graphql.execution.instrumentation.InstrumentationState;
import graphql.language.Document;
import graphql.schema.GraphQLSchema;

import java.util.concurrent.CompletableFuture;

@Weave(originalName = "graphql.GraphQL", type = MatchType.ExactClass)
public class GraphQL_Instrumentation {

private CompletableFuture<ExecutionResult> execute(ExecutionInput executionInput, Document document, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState) {
try {
if (NewRelicSecurity.isHookProcessingActive()) {
HttpRequest request = NewRelicSecurity.getAgent().getSecurityMetaData().getRequest();
if (executionInput.getQuery() != null && !executionInput.getQuery().isEmpty()) {
request.getCustomDataType().put("*.query", HttpRequestCustomDataTypeEnum.GRAPHQL_QUERY.name());
}
if (executionInput.getVariables() != null && !executionInput.getVariables().isEmpty()) {
request.getCustomDataType().put("*.variables", HttpRequestCustomDataTypeEnum.GRAPHQL_VARIABLE.name());
}
}
} catch (Exception ignored) {}
return Weaver.callOriginal();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package graphql;

import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;

@Weave(originalName = "graphql.ParseAndValidate", type = MatchType.ExactClass)
public class ParseAndValidate_Instrumentation {

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.newrelic.api.agent.security.schema;

import com.newrelic.api.agent.security.schema.annotations.JsonIgnore;

import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -37,9 +36,13 @@ public class HttpRequest {
private Map<String, String> pathParameterMap;

private boolean isRequestParsed;

private boolean isGrpc;

private String route;

private Map<String, String> customDataType;

@JsonIgnore
private List<String> pathParameters;

Expand Down Expand Up @@ -70,6 +73,7 @@ public HttpRequest() {
this.isRequestParsed = false;
this.isGrpc = false;
this.route = StringUtils.EMPTY;
this.customDataType = new HashMap<>();
}

public HttpRequest(HttpRequest servletInfo) {
Expand All @@ -92,6 +96,7 @@ public HttpRequest(HttpRequest servletInfo) {
this.requestHeaderParameters = servletInfo.requestHeaderParameters;
this.requestBodyParameters = servletInfo.requestBodyParameters;
this.isRequestParametersParsed = servletInfo.isRequestParametersParsed;
this.customDataType = servletInfo.customDataType;
}

public String getMethod() {
Expand Down Expand Up @@ -294,6 +299,11 @@ public boolean isRequestParametersParsed() {
public void setRequestParametersParsed(boolean requestParametersParsed) {
isRequestParametersParsed = requestParametersParsed;
}

public Map<String, String> getCustomDataType() {
return customDataType;
}

}


Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.newrelic.api.agent.security.schema;

public enum HttpRequestCustomDataTypeEnum {
GRAPHQL_QUERY,
GRAPHQL_VARIABLE,

NONE;
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,5 @@ include 'instrumentation:solr-5.1.0'
include 'instrumentation:solr-7.0.0'
include 'instrumentation:solr-8.0.0'
include 'instrumentation:solr-9.0.0'
include 'instrumentation:graphql-java-16.2'
include 'instrumentation:websphere-liberty-profile-environment-8.5.5.5'

0 comments on commit 1b7651c

Please sign in to comment.