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

Pr 1454 #1565

Merged
merged 7 commits into from
Oct 26, 2023
Merged

Pr 1454 #1565

Show file tree
Hide file tree
Changes from 4 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
6 changes: 6 additions & 0 deletions instrumentation/graphql-java-21.0/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ site {

test {
useJUnitPlatform()
// These instrumentation tests only run on Java 11+ regardless of the -PtestN gradle property that is set.
onlyIf {
!project.hasProperty('test8')
}

}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * Copyright 2023 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * Copyright 2023 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * Copyright 2023 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * Copyright 2023 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * Copyright 2023 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package com.nr.instrumentation.graphql;

import graphql.language.*;
import graphql.language.Document;
import graphql.language.Field;
import graphql.language.InlineFragment;
import graphql.language.OperationDefinition;
import graphql.language.Selection;
import graphql.language.SelectionSet;
import graphql.language.SelectionSetContainer;
import graphql.language.TypeName;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -41,9 +48,13 @@ public class GraphQLTransactionName {
* @return a transaction name based on given document
*/
public static String from(final Document document) {
if (document == null) return DEFAULT_TRANSACTION_NAME;
if (document == null) {
return DEFAULT_TRANSACTION_NAME;
}
List<OperationDefinition> operationDefinitions = document.getDefinitionsOfType(OperationDefinition.class);
if (isNullOrEmpty(operationDefinitions)) return DEFAULT_TRANSACTION_NAME;
if (isNullOrEmpty(operationDefinitions)) {
return DEFAULT_TRANSACTION_NAME;
}
if (operationDefinitions.size() == 1) {
return getTransactionNameFor(operationDefinitions.get(0));
}
Expand All @@ -53,7 +64,9 @@ public static String from(final Document document) {
}

private static String getTransactionNameFor(OperationDefinition operationDefinition) {
if (operationDefinition == null) return DEFAULT_TRANSACTION_NAME;
if (operationDefinition == null) {
return DEFAULT_TRANSACTION_NAME;
}
return createBeginningOfTransactionNameFrom(operationDefinition) +
createEndOfTransactionNameFrom(operationDefinition.getSelectionSet());
}
Expand All @@ -66,7 +79,9 @@ private static String createBeginningOfTransactionNameFrom(final OperationDefini

private static String createEndOfTransactionNameFrom(final SelectionSet selectionSet) {
Selection selection = onlyNonFederatedSelectionOrNoneFrom(selectionSet);
if (selection == null) return "";
if (selection == null) {
return "";
}
List<Selection> selections = new ArrayList<>();
while (selection != null) {
selections.add(selection);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
*
* * Copyright 2023 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package com.nr.instrumentation.graphql;

import java.util.Collection;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * Copyright 2023 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/
Expand All @@ -19,8 +19,8 @@

import java.util.concurrent.CompletableFuture;

import static com.nr.instrumentation.graphql.GraphQLSpanUtil.*;
import static com.nr.instrumentation.graphql.GraphQLErrorHandler.*;
import static com.nr.instrumentation.graphql.GraphQLErrorHandler.reportNonNullableExceptionToNR;
import static com.nr.instrumentation.graphql.GraphQLSpanUtil.setResolverAttributes;

@Weave(originalName = "graphql.execution.ExecutionStrategy", type = MatchType.BaseClass)
public class ExecutionStrategy_Instrumentation {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * Copyright 2023 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * Copyright 2023 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/
Expand All @@ -17,9 +17,12 @@
import graphql.validation.ValidationError;

import java.util.List;
import java.util.Locale;
import java.util.function.Predicate;

import static com.nr.instrumentation.graphql.GraphQLSpanUtil.*;
import static com.nr.instrumentation.graphql.GraphQLErrorHandler.*;
import static com.nr.instrumentation.graphql.GraphQLErrorHandler.reportGraphQLError;
import static com.nr.instrumentation.graphql.GraphQLErrorHandler.reportGraphQLException;
import static com.nr.instrumentation.graphql.GraphQLSpanUtil.setOperationAttributes;

@Weave(originalName = "graphql.ParseAndValidate", type = MatchType.ExactClass)
public class ParseAndValidate_Instrumentation {
Expand All @@ -41,7 +44,15 @@ public static ParseAndValidateResult parse(ExecutionInput executionInput) {
return result;
}

public static List<ValidationError> validate(GraphQLSchema graphQLSchema, Document parsedDocument) {
public static List<ValidationError> validate(GraphQLSchema graphQLSchema, Document parsedDocument, Predicate<Class<?>> rulePredicate, Locale locale) {
Copy link
Contributor Author

@jasonjkeller jasonjkeller Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main change made to the customer's PR was instrumenting the correct signatures of the validate method so that error info is properly captured.

List<ValidationError> errors = Weaver.callOriginal();
if (errors != null && !errors.isEmpty()) {
reportGraphQLError(errors.get(0));
}
return errors;
}

public static List<ValidationError> validate(GraphQLSchema graphQLSchema, Document parsedDocument, Predicate<Class<?>> rulePredicate) {
List<ValidationError> errors = Weaver.callOriginal();
if (errors != null && !errors.isEmpty()) {
reportGraphQLError(errors.get(0));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * Copyright 2023 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package com.nr.instrumentation.graphql;

import com.newrelic.test.marker.Java8IncompatibleTest;
import org.junit.experimental.categories.Category;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;

import static com.nr.instrumentation.graphql.helper.GraphQLTestHelper.readText;
import static org.junit.jupiter.api.Assertions.assertEquals;

@Category({ Java8IncompatibleTest.class })
public class GraphQLObfuscatorTest {

private final static String OBFUSCATE_DATA_DIR = "obfuscateQueryTestData";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * Copyright 2023 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/
Expand All @@ -9,6 +9,7 @@

import com.newrelic.agent.bridge.AgentBridge;
import com.newrelic.agent.bridge.PrivateApi;
import com.newrelic.test.marker.Java8IncompatibleTest;
import com.nr.instrumentation.graphql.helper.GraphQLTestHelper;
import com.nr.instrumentation.graphql.helper.PrivateApiStub;
import graphql.execution.ExecutionStepInfo;
Expand All @@ -18,6 +19,7 @@
import graphql.language.Definition;
import graphql.language.Document;
import graphql.schema.GraphQLNonNull;
import org.junit.experimental.categories.Category;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -35,6 +37,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@Category({ Java8IncompatibleTest.class })
public class GraphQLSpanUtilTest {

private static final List<Definition> NO_DEFINITIONS = Collections.emptyList();
Expand Down Expand Up @@ -111,7 +114,8 @@ public void testInvalidClassExceptionFix() {
"'graphql.field.parentType' is not required and should be null here");
}

private static ExecutionStrategyParameters mockExecutionStrategyParametersForInvalidClassCastExceptionTest(String graphqlFieldPath, String graphqlFieldName) {
private static ExecutionStrategyParameters mockExecutionStrategyParametersForInvalidClassCastExceptionTest(String graphqlFieldPath,
String graphqlFieldName) {
ExecutionStrategyParameters parameters = mock(ExecutionStrategyParameters.class);
ResultPath resultPath = mock(ResultPath.class);
MergedField mergedField = mock(MergedField.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * Copyright 2023 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package com.nr.instrumentation.graphql;

import com.newrelic.test.marker.Java8IncompatibleTest;
import graphql.language.Document;
import org.junit.experimental.categories.Category;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;

import static com.nr.instrumentation.graphql.helper.GraphQLTestHelper.parseDocument;
import static org.junit.jupiter.api.Assertions.assertEquals;

@Category({ Java8IncompatibleTest.class })
public class GraphQLTransactionNameTest {

private final static String TEST_DATA_DIR = "transactionNameTestData";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * Copyright 2023 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package com.nr.instrumentation.graphql.helper;

import com.newrelic.test.marker.Java8IncompatibleTest;
import graphql.language.Document;
import graphql.parser.Parser;
import org.junit.experimental.categories.Category;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

@Category({ Java8IncompatibleTest.class })
public class GraphQLTestHelper {
public static Document parseDocument(String testDir, String filename) {
return Parser.parse(readText(testDir, filename));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * Copyright 2023 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package com.nr.instrumentation.graphql.helper;

import com.newrelic.agent.bridge.PrivateApi;
import com.newrelic.test.marker.Java8IncompatibleTest;
import org.junit.experimental.categories.Category;

import javax.management.MBeanServer;
import java.io.Closeable;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Category({ Java8IncompatibleTest.class })
public class PrivateApiStub implements PrivateApi {
private final Map<String, String> tracerParameters = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * Copyright 2023 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/
Expand All @@ -12,6 +12,7 @@
import com.newrelic.agent.introspec.Introspector;
import com.newrelic.agent.introspec.SpanEvent;
import com.newrelic.api.agent.Trace;
import com.newrelic.test.marker.Java8IncompatibleTest;
import graphql.schema.GraphQLSchema;
import graphql.schema.StaticDataFetcher;
import graphql.schema.idl.RuntimeWiring;
Expand All @@ -21,6 +22,7 @@
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.jupiter.api.AfterEach;
import org.junit.runner.RunWith;

Expand All @@ -34,7 +36,8 @@
import static org.junit.Assert.assertTrue;

@RunWith(InstrumentationTestRunner.class)
@InstrumentationTestConfig(includePrefixes = {"graphql", "com.nr.instrumentation"}, configName = "distributed_tracing.yml")
@InstrumentationTestConfig(includePrefixes = { "graphql", "com.nr.instrumentation" }, configName = "distributed_tracing.yml")
@Category({ Java8IncompatibleTest.class })
public class GraphQL_InstrumentationTest {
private static final long DEFAULT_TIMEOUT_IN_MILLIS = 10_000;
private static final String TEST_ARG = "testArg";
Expand Down Expand Up @@ -103,7 +106,7 @@ public void validationException() {
//when
trace(createRunnable(query));
//then
String expectedErrorMessage = "Validation error of type FieldUndefined: Field 'noSuchField' in type 'Query' is undefined @ 'noSuchField'";
String expectedErrorMessage = "Validation error (FieldUndefined@[noSuchField]) : Field 'noSuchField' in type 'Query' is undefined";
assertErrorOperation("QUERY/<anonymous>/noSuchField",
"GraphQL/operation/QUERY/<anonymous>/noSuchField", "graphql.GraphqlErrorException", expectedErrorMessage, false);
}
Expand Down
Loading