Skip to content

Commit

Permalink
Add Copyright statements
Browse files Browse the repository at this point in the history
  • Loading branch information
twcrone committed Sep 1, 2021
1 parent cc6eae4 commit 495c2ea
Show file tree
Hide file tree
Showing 16 changed files with 125 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package com.nr.instrumentation.graphql;

import com.newrelic.api.agent.NewRelic;
Expand All @@ -12,13 +19,9 @@
import java.util.logging.Level;

public class GraphQLErrorHandler {
public static void reportResolverThrowableToNR(Throwable e) {
NewRelic.noticeError(e);
}

public static void reportNonNullableExceptionToNR(FieldValueInfo result) {
CompletableFuture<ExecutionResult> exceptionResult = result.getFieldValue();
if (ifResultHasException(exceptionResult)) {
if (resultHasException(exceptionResult)) {
reportExceptionFromCompletedExceptionally(exceptionResult);
}
}
Expand All @@ -31,7 +34,7 @@ public static void reportGraphQLError(GraphQLError error) {
NewRelic.noticeError(throwableFromGraphQLError(error));
}

private static boolean ifResultHasException(CompletableFuture<ExecutionResult> exceptionResult) {
private static boolean resultHasException(CompletableFuture<ExecutionResult> exceptionResult) {
return exceptionResult != null && exceptionResult.isCompletedExceptionally();
}

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

package com.nr.instrumentation.graphql;

import graphql.com.google.common.base.Joiner;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package com.nr.instrumentation.graphql;

import graphql.language.Document;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package com.nr.instrumentation.graphql;

import com.newrelic.agent.bridge.AgentBridge;
import com.newrelic.api.agent.NewRelic;
import graphql.ExecutionResult;
import graphql.GraphQLError;
import graphql.GraphQLException;
import graphql.GraphqlErrorException;
import graphql.execution.ExecutionStrategyParameters;
import graphql.execution.FieldValueInfo;
import graphql.language.Document;
import graphql.language.OperationDefinition;
import graphql.schema.GraphQLObjectType;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;

import static com.nr.instrumentation.graphql.GraphQLObfuscator.obfuscate;
import static com.nr.instrumentation.graphql.GraphQLOperationDefinition.getOperationTypeFrom;

import static com.nr.instrumentation.graphql.Utils.getValueOrDefault;

public class GraphQLSpanUtil {

Expand Down Expand Up @@ -57,8 +54,4 @@ private static void setDefaultOperationAttributes(String query) {
AgentBridge.privateApi.addTracerParameter("graphql.operation.name", DEFAULT_OPERATION_NAME);
AgentBridge.privateApi.addTracerParameter("graphql.operation.query", obfuscate(query));
}

private static <T> T getValueOrDefault(T value, T defaultValue) {
return value == null ? defaultValue : value;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package com.nr.instrumentation.graphql;

import graphql.language.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import static com.nr.instrumentation.graphql.Utils.isNullOrEmpty;

/**
* Generates GraphQL transaction names based on details referenced in Node instrumentation.
*
Expand Down Expand Up @@ -95,7 +103,7 @@ private static Selection onlyNonFederatedSelectionOrNoneFrom(final SelectionSet
return null;
}
List<Selection> selections = selectionSet.getSelections();
if (selections == null || selections.isEmpty()) {
if (isNullOrEmpty(selections)) {
return null;
}
List<Selection> selection = selections.stream()
Expand Down Expand Up @@ -139,8 +147,4 @@ private static Selection nextNonFederatedSelectionChildFrom(final Selection sele
private static boolean notFederatedFieldName(final String fieldName) {
return !(TYPENAME.equals(fieldName) || ID.equals(fieldName));
}

private static boolean isNullOrEmpty(final Collection<?> c) {
return c == null || c.isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.nr.instrumentation.graphql;

import java.util.Collection;

// instead of adding dependencies, just add some utility methods
public class Utils {
public static <T> T getValueOrDefault(T value, T defaultValue) {
return value == null ? defaultValue : value;
}

public static boolean isNullOrEmpty(final Collection<?> c) {
return c == null || c.isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package graphql;

import com.newrelic.api.agent.NewRelic;
Expand Down Expand Up @@ -27,7 +34,7 @@ protected CompletableFuture<FieldValueInfo> resolveFieldWithInfo(ExecutionContex
}

protected void handleFetchingException(ExecutionContext executionContext, DataFetchingEnvironment environment, Throwable e) {
reportResolverThrowableToNR(e);
NewRelic.noticeError(e);
Weaver.callOriginal();
}

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

package graphql;

import com.newrelic.api.agent.Trace;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package graphql;

import com.newrelic.api.agent.NewRelic;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package com.nr.instrumentation.graphql;

import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -12,14 +19,14 @@ public class GraphQLObfuscatorTest {

@ParameterizedTest
@CsvFileSource(resources = "/obfuscateQueryTestData/obfuscate-query-test-data.csv", delimiter = '|', numLinesToSkip = 2)
public void testObfuscateQuery(String queryToObfuscateFile, String expectedObfuscatedQueryFile) {
public void testObfuscateQuery(String queryToObfuscateFilename, String expectedObfuscatedQueryFilename) {
//setup
queryToObfuscateFile = queryToObfuscateFile.trim();
expectedObfuscatedQueryFile = expectedObfuscatedQueryFile.trim();
String expectedObfuscatedResult = readText(OBFUSCATE_DATA_DIR, expectedObfuscatedQueryFile);//readText(expectedObfuscatedQueryFile);
queryToObfuscateFilename = queryToObfuscateFilename.trim();
expectedObfuscatedQueryFilename = expectedObfuscatedQueryFilename.trim();
String expectedObfuscatedResult = readText(OBFUSCATE_DATA_DIR, expectedObfuscatedQueryFilename);

//given
String query = readText(OBFUSCATE_DATA_DIR, queryToObfuscateFile);
String query = readText(OBFUSCATE_DATA_DIR, queryToObfuscateFilename);

//when
String obfuscatedQuery = GraphQLObfuscator.obfuscate(query);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package com.nr.instrumentation.graphql;

import com.newrelic.agent.bridge.AgentBridge;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package com.nr.instrumentation.graphql;

import graphql.language.Document;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package com.nr.instrumentation.graphql.helper;

import graphql.language.Document;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package com.nr.instrumentation.graphql.helper;

import com.newrelic.agent.bridge.PrivateApi;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package graphql;

import com.newrelic.agent.introspec.InstrumentationTestConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public SpanEvent createSpanEvent(Tracer tracer, TransactionData transactionData,
private SpanEventFactory maybeSetGraphQLAttributes(Tracer tracer, SpanEventFactory builder) {
Map<String, Object> agentAttributes = tracer.getAgentAttributes();
boolean containsGraphQLAttributes = agentAttributes.keySet().stream().anyMatch(key -> key.contains("graphql"));
if(containsGraphQLAttributes){
if (containsGraphQLAttributes){
agentAttributes.entrySet().stream()
.filter(e -> e.getKey().contains("graphql"))
.forEach(e -> builder.putAgentAttribute(e.getKey(), e.getValue()));
Expand Down

0 comments on commit 495c2ea

Please sign in to comment.