Skip to content
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
7 changes: 4 additions & 3 deletions instrumentation/graphql-java/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Settings for the GraphQL instrumentation

| System property | Type | Default | Description |
|--------------------------------------------------------|---------|---------|--------------------------------------------------------------------------------------------|
| `otel.instrumentation.graphql.query-sanitizer.enabled` | Boolean | `true` | Whether to remove sensitive information from query source that is added as span attribute. |
| System property | Type | Default | Description |
|------------------------------------------------------------------------|---------|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `otel.instrumentation.graphql.query-sanitizer.enabled` | Boolean | `true` | Whether to remove sensitive information from query source that is added as span attribute. |
| `otel.instrumentation.graphql.add-operation-name-to-span-name.enabled` | Boolean | `false` | Whether GraphQL operation name is added to the span name. <p>**WARNING**: GraphQL operation name is provided by the client and can have high cardinality. Use only when the server is not exposed to malicious clients. |

# Settings for the GraphQL 20 instrumentation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ dependencies {

latestDepTestLibrary("com.graphql-java:graphql-java:19.+") // see graphql-java-20.0 module
}

tasks.withType<Test>().configureEach {
jvmArgs("-Dotel.instrumentation.graphql.add-operation-name-to-span-name.enabled=true")
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ public final class GraphqlSingletons {
private static final boolean QUERY_SANITIZATION_ENABLED =
AgentInstrumentationConfig.get()
.getBoolean("otel.instrumentation.graphql.query-sanitizer.enabled", true);
private static final boolean ADD_OPERATION_NAME_TO_SPAN_NAME =
AgentInstrumentationConfig.get()
.getBoolean(
"otel.instrumentation.graphql.add-operation-name-to-span-name.enabled", false);

private static final GraphQLTelemetry TELEMETRY =
GraphQLTelemetry.builder(GlobalOpenTelemetry.get())
.setSanitizeQuery(QUERY_SANITIZATION_ENABLED)
.setAddOperationNameToSpanName(ADD_OPERATION_NAME_TO_SPAN_NAME)
.build();

private GraphqlSingletons() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ public static GraphQLTelemetryBuilder builder(OpenTelemetry openTelemetry) {

private final OpenTelemetryInstrumentationHelper helper;

GraphQLTelemetry(OpenTelemetry openTelemetry, boolean sanitizeQuery) {
GraphQLTelemetry(
OpenTelemetry openTelemetry, boolean sanitizeQuery, boolean addOperationNameToSpanName) {
helper =
OpenTelemetryInstrumentationHelper.create(
openTelemetry, INSTRUMENTATION_NAME, sanitizeQuery);
openTelemetry, INSTRUMENTATION_NAME, sanitizeQuery, addOperationNameToSpanName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public final class GraphQLTelemetryBuilder {
private final OpenTelemetry openTelemetry;

private boolean sanitizeQuery = true;
private boolean addOperationNameToSpanName = false;

GraphQLTelemetryBuilder(OpenTelemetry openTelemetry) {
this.openTelemetry = openTelemetry;
Expand All @@ -27,11 +28,23 @@ public GraphQLTelemetryBuilder setSanitizeQuery(boolean sanitizeQuery) {
return this;
}

/**
* Sets whether GraphQL operation name is added to the span name. Default is {@code false}.
*
* <p>WARNING: GraphQL operation name is provided by the client and can have high cardinality. Use
* only when the server is not exposed to malicious clients.
*/
@CanIgnoreReturnValue
public GraphQLTelemetryBuilder setAddOperationNameToSpanName(boolean addOperationNameToSpanName) {
this.addOperationNameToSpanName = addOperationNameToSpanName;
return this;
}

/**
* Returns a new {@link GraphQLTelemetry} with the settings of this {@link
* GraphQLTelemetryBuilder}.
*/
public GraphQLTelemetry build() {
return new GraphQLTelemetry(openTelemetry, sanitizeQuery);
return new GraphQLTelemetry(openTelemetry, sanitizeQuery, addOperationNameToSpanName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ protected InstrumentationExtension getTesting() {

@Override
protected void configure(GraphQL.Builder builder) {
GraphQLTelemetry telemetry = GraphQLTelemetry.builder(testing.getOpenTelemetry()).build();
GraphQLTelemetry telemetry =
GraphQLTelemetry.builder(testing.getOpenTelemetry())
.setAddOperationNameToSpanName(true)
.build();
builder.instrumentation(telemetry.newInstrumentation());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies {

tasks.withType<Test>().configureEach {
jvmArgs("-Dotel.instrumentation.graphql.data-fetcher.enabled=true")
jvmArgs("-Dotel.instrumentation.graphql.add-operation-name-to-span-name.enabled=true")
}

if (findProperty("testLatestDeps") as Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ public final class GraphqlSingletons {
private static final boolean TRIVIAL_DATA_FETCHER_ENABLED =
AgentInstrumentationConfig.get()
.getBoolean("otel.instrumentation.graphql.trivial-data-fetcher.enabled", false);
private static final boolean ADD_OPERATION_NAME_TO_SPAN_NAME =
AgentInstrumentationConfig.get()
.getBoolean(
"otel.instrumentation.graphql.add-operation-name-to-span-name.enabled", false);

private static final GraphQLTelemetry TELEMETRY =
GraphQLTelemetry.builder(GlobalOpenTelemetry.get())
.setSanitizeQuery(QUERY_SANITIZATION_ENABLED)
.setDataFetcherInstrumentationEnabled(DATA_FETCHER_ENABLED)
.setTrivialDataFetcherInstrumentationEnabled(TRIVIAL_DATA_FETCHER_ENABLED)
.setAddOperationNameToSpanName(ADD_OPERATION_NAME_TO_SPAN_NAME)
.build();

private GraphqlSingletons() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ public static GraphQLTelemetryBuilder builder(OpenTelemetry openTelemetry) {
OpenTelemetry openTelemetry,
boolean sanitizeQuery,
Instrumenter<DataFetchingEnvironment, Void> dataFetcherInstrumenter,
boolean createSpansForTrivialDataFetcher) {
helper = GraphqlInstrumenterFactory.createInstrumentationHelper(openTelemetry, sanitizeQuery);
boolean createSpansForTrivialDataFetcher,
boolean addOperationNameToSpanName) {
helper =
GraphqlInstrumenterFactory.createInstrumentationHelper(
openTelemetry, sanitizeQuery, addOperationNameToSpanName);
this.dataFetcherInstrumenter = dataFetcherInstrumenter;
this.createSpansForTrivialDataFetcher = createSpansForTrivialDataFetcher;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ public final class GraphQLTelemetryBuilder {
private final OpenTelemetry openTelemetry;

private boolean sanitizeQuery = true;

private boolean dataFetcherInstrumentationEnabled = false;

private boolean trivialDataFetcherInstrumentationEnabled = false;
private boolean addOperationNameToSpanName = false;

GraphQLTelemetryBuilder(OpenTelemetry openTelemetry) {
this.openTelemetry = openTelemetry;
Expand Down Expand Up @@ -50,6 +49,18 @@ public GraphQLTelemetryBuilder setTrivialDataFetcherInstrumentationEnabled(
return this;
}

/**
* Sets whether GraphQL operation name is added to the span name. Default is {@code false}.
*
* <p>WARNING: GraphQL operation name is provided by the client and can have high cardinality. Use
* only when the server is not exposed to malicious clients.
*/
@CanIgnoreReturnValue
public GraphQLTelemetryBuilder setAddOperationNameToSpanName(boolean addOperationNameToSpanName) {
this.addOperationNameToSpanName = addOperationNameToSpanName;
return this;
}

/**
* Returns a new {@link GraphQLTelemetry} with the settings of this {@link
* GraphQLTelemetryBuilder}.
Expand All @@ -60,6 +71,7 @@ public GraphQLTelemetry build() {
sanitizeQuery,
GraphqlInstrumenterFactory.createDataFetcherInstrumenter(
openTelemetry, dataFetcherInstrumentationEnabled),
trivialDataFetcherInstrumentationEnabled);
trivialDataFetcherInstrumentationEnabled,
addOperationNameToSpanName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ final class GraphqlInstrumenterFactory {
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.graphql-java-20.0";

static OpenTelemetryInstrumentationHelper createInstrumentationHelper(
OpenTelemetry openTelemetry, boolean sanitizeQuery) {
OpenTelemetry openTelemetry, boolean sanitizeQuery, boolean addOperationNameToSpanName) {
return OpenTelemetryInstrumentationHelper.create(
openTelemetry, INSTRUMENTATION_NAME, sanitizeQuery);
openTelemetry, INSTRUMENTATION_NAME, sanitizeQuery, addOperationNameToSpanName);
}

static Instrumenter<DataFetchingEnvironment, Void> createDataFetcherInstrumenter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ protected void configure(GraphQL.Builder builder) {
GraphQLTelemetry telemetry =
GraphQLTelemetry.builder(testing.getOpenTelemetry())
.setDataFetcherInstrumentationEnabled(true)
.setAddOperationNameToSpanName(true)
.build();
builder.instrumentation(telemetry.newInstrumentation());
}
Expand All @@ -56,6 +57,7 @@ void createSpansForDataFetchers() {
GraphQLTelemetry telemetry =
GraphQLTelemetry.builder(testing.getOpenTelemetry())
.setDataFetcherInstrumentationEnabled(true)
.setAddOperationNameToSpanName(true)
.build();

GraphQL graphql =
Expand Down Expand Up @@ -118,6 +120,7 @@ void createSpanForTrivialDataFetchers() {
GraphQLTelemetry.builder(testing.getOpenTelemetry())
.setDataFetcherInstrumentationEnabled(true)
.setTrivialDataFetcherInstrumentationEnabled(true)
.setAddOperationNameToSpanName(true)
.build();

GraphQL graphql =
Expand Down Expand Up @@ -194,6 +197,7 @@ void noDataFetcherSpansCreated() {
GraphQLTelemetry.builder(testing.getOpenTelemetry())
.setDataFetcherInstrumentationEnabled(false)
.setTrivialDataFetcherInstrumentationEnabled(true)
.setAddOperationNameToSpanName(true)
.build();

GraphQL graphql =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,22 @@ public final class OpenTelemetryInstrumentationHelper {

private final Instrumenter<OpenTelemetryInstrumentationState, ExecutionResult> instrumenter;
private final boolean sanitizeQuery;
private final boolean addOperationNameToSpanName;

private OpenTelemetryInstrumentationHelper(
Instrumenter<OpenTelemetryInstrumentationState, ExecutionResult> instrumenter,
boolean sanitizeQuery) {
boolean sanitizeQuery,
boolean addOperationNameToSpanName) {
this.instrumenter = instrumenter;
this.sanitizeQuery = sanitizeQuery;
this.addOperationNameToSpanName = addOperationNameToSpanName;
}

public static OpenTelemetryInstrumentationHelper create(
OpenTelemetry openTelemetry, String instrumentationName, boolean sanitizeQuery) {
OpenTelemetry openTelemetry,
String instrumentationName,
boolean sanitizeQuery,
boolean addOperationNameToSpanName) {
InstrumenterBuilder<OpenTelemetryInstrumentationState, ExecutionResult> builder =
Instrumenter.<OpenTelemetryInstrumentationState, ExecutionResult>builder(
openTelemetry, instrumentationName, ignored -> "GraphQL Operation")
Expand All @@ -76,7 +82,8 @@ public static OpenTelemetryInstrumentationHelper create(
});
builder.addAttributesExtractor(new GraphqlAttributesExtractor());

return new OpenTelemetryInstrumentationHelper(builder.buildInstrumenter(), sanitizeQuery);
return new OpenTelemetryInstrumentationHelper(
builder.buildInstrumenter(), sanitizeQuery, addOperationNameToSpanName);
}

public InstrumentationContext<ExecutionResult> beginExecution(
Expand Down Expand Up @@ -118,7 +125,7 @@ public InstrumentationContext<ExecutionResult> beginExecuteOperation(
String operationName = operationDefinition.getName();

String spanName = operationType;
if (operationName != null && !operationName.isEmpty()) {
if (addOperationNameToSpanName && operationName != null && !operationName.isEmpty()) {
spanName += " " + operationName;
}
span.updateName(spanName);
Expand Down
Loading