Skip to content

Commit

Permalink
Update per PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
theJC committed Oct 5, 2023
1 parent 2e32fd8 commit e7327be
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ exports[`opentelemetry receives spans on fetch failure 1`] = `
{
"name": "gateway.plan",
"attributes": {
"operationName": "GetProduct",
"graphql.operation.name": "GetProduct",
"graphql.document": "#graphql\\n query GetProduct($upc: String!) {\\n product(upc: $upc) {\\n name\\n }\\n }\\n "
"graphql.operation.name": "GetProduct"
},
"children": [],
"status": {
Expand Down Expand Up @@ -88,9 +86,7 @@ exports[`opentelemetry with local data receives spans on plan failure 1`] = `
{
"name": "gateway.plan",
"attributes": {
"operationName": "GetProduct",
"graphql.operation.name": "GetProduct",
"graphql.document": "#graphql\\n subscription GetProduct($upc: String!) {\\n product(upc: $upc) {\\n name\\n }\\n }\\n "
"graphql.operation.name": "GetProduct"
},
"children": [],
"status": {
Expand Down Expand Up @@ -127,9 +123,7 @@ exports[`opentelemetry with local data receives spans on success 1`] = `
{
"name": "gateway.plan",
"attributes": {
"operationName": "GetProduct",
"graphql.operation.name": "GetProduct",
"graphql.document": "#graphql\\n query GetProduct($upc: String!) {\\n product(upc: $upc) {\\n name\\n }\\n }\\n "
"graphql.operation.name": "GetProduct"
},
"children": [],
"status": {
Expand Down
18 changes: 16 additions & 2 deletions gateway-js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ import {
SupergraphManager,
} from './config';
import { SpanStatusCode } from '@opentelemetry/api';
import { OpenTelemetrySpanNames, tracer, requestContextSpanAttributes, operationContextSpanAttributes, recordExceptions } from './utilities/opentelemetry';
import {
OpenTelemetrySpanNames,
tracer,
requestContextSpanAttributes,
operationContextSpanAttributes,
recordExceptions,
OpenTelemetryAttributeNames
} from './utilities/opentelemetry';
import { addExtensions } from './schema-helper/addExtensions';
import {
IntrospectAndCompose,
Expand Down Expand Up @@ -779,7 +786,14 @@ export class ApolloGateway implements GatewayInterface {
if (!queryPlan) {
queryPlan = tracer.startActiveSpan(
OpenTelemetrySpanNames.PLAN,
{ attributes: requestContextSpanAttributes(requestContext, this.config.telemetry) },
requestContext.operationName
? {
attributes: {
[OpenTelemetryAttributeNames.GRAPHQL_OPERATION_NAME]:
requestContext.operationName,
},
}
: {},
(span) => {
try {
const operation = operationFromDocument(
Expand Down
41 changes: 21 additions & 20 deletions gateway-js/src/utilities/opentelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,26 @@ import { OperationContext } from '../operationContext';

export type OpenTelemetryConfig = {
/**
* Whether to include the `graphql.document` attribute in the `gateway.request`
* and `gateway.plan` OpenTelemetry spans. When set to `true`, the attribute
* will contain the entire GraphQL document for the current request.
* Whether to include the `graphql.document` attribute in the `gateway.request` OpenTelemetry spans.
* When set to `true`, the attribute will contain the entire GraphQL document for the current request.
*
* Defaults to `false`, meaning that the GraphQL document will not be added
* as a span attribute.
* Defaults to `false`, meaning that the GraphQL document will not be added as a span attribute.
*/
includeDocument?: boolean;
/**
* Whether to record the GraphQL and internal errors that take place
* while processing a request as exception events in the OpenTelemetry spans
* in which they occur.
* Whether to record the GraphQL and internal errors that take place while processing a request as
* exception events in the OpenTelemetry spans in which they occur.
*
* When a number is given as a value, it represents the maximum number of
* exceptions that will be reported in each OpenTelemetry span.
* When a number is given as a value, it represents the maximum number of exceptions that will be
* reported in each OpenTelemetry span.
*
* Regardless of the value of this setting, the span status code will be set
* to `ERROR` when a GraphQL or internal error occurs.
* Regardless of the value of this setting, the span status code will be set to `ERROR` when a GraphQL
* or internal error occurs.
*
* Defaults to `false`, meaning that no exceptions will be reported in any
* spans.
* Defaults to `false`, meaning that no exceptions will be reported in any spans.
*/
recordExceptions?: boolean | number;
}
};

export enum OpenTelemetrySpanNames {
REQUEST = 'gateway.request',
Expand Down Expand Up @@ -66,16 +62,20 @@ export interface SpanAttributes extends Attributes {

export function requestContextSpanAttributes(
requestContext: GatewayGraphQLRequestContext,
config: OpenTelemetryConfig | undefined
config: OpenTelemetryConfig | undefined,
): SpanAttributes {
const spanAttributes: SpanAttributes = {};

if (requestContext.operationName) {
spanAttributes[OpenTelemetryAttributeNames.GRAPHQL_OPERATION_NAME_DEPRECATED] = requestContext.operationName;
spanAttributes[OpenTelemetryAttributeNames.GRAPHQL_OPERATION_NAME] = requestContext.operationName;
spanAttributes[
OpenTelemetryAttributeNames.GRAPHQL_OPERATION_NAME_DEPRECATED
] = requestContext.operationName;
spanAttributes[OpenTelemetryAttributeNames.GRAPHQL_OPERATION_NAME] =
requestContext.operationName;
}
if (config?.includeDocument && requestContext.source) {
spanAttributes[OpenTelemetryAttributeNames.GRAPHQL_DOCUMENT] = requestContext.source;
spanAttributes[OpenTelemetryAttributeNames.GRAPHQL_DOCUMENT] =
requestContext.source;
}

return spanAttributes;
Expand All @@ -87,7 +87,8 @@ export function operationContextSpanAttributes(
const spanAttributes: SpanAttributes = {};

if (operationContext.operation.operation) {
spanAttributes[OpenTelemetryAttributeNames.GRAPHQL_OPERATION_TYPE] = operationContext.operation.operation;
spanAttributes[OpenTelemetryAttributeNames.GRAPHQL_OPERATION_TYPE] =
operationContext.operation.operation;
}

return spanAttributes;
Expand Down

0 comments on commit e7327be

Please sign in to comment.