Skip to content

Enhance GraphQL OTEL instrumentation with custom metrics and traces #2673

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

Open
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

RubenCerna2079
Copy link
Contributor

Why make this change?

This closes #2642

What is this change?

This PR enhances the OTEL instrumentation for the GraphQL APIs by adding custom traces and metrics.
Metrics can be filtered for status_code, api_type, endpoint and method.

How was this tested?

  • Local Testing
  • Integration Tests
  • Unit Tests

Sample Request(s)

image
image
image

@RubenCerna2079 RubenCerna2079 added this to the 1.5 milestone May 2, 2025
@RubenCerna2079 RubenCerna2079 self-assigned this May 2, 2025
@Copilot Copilot AI review requested due to automatic review settings May 2, 2025 16:59
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR enhances the OTEL instrumentation for GraphQL APIs by incorporating custom traces and metrics. Key changes include adding new telemetry using directives and namespaces, integrating activity tracking in query execution, and updating the BuildRequestStateMiddleware to support custom telemetry for GraphQL operations.

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Service/Startup.cs Added telemetry using directive.
src/Service/Controllers/RestController.cs Updated telemetry namespace usage to align with telemetry enhancements.
src/Core/Telemetry/TelemetryTracesHelper.cs Updated namespace and provided clarified telemetry trace comments.
src/Core/Telemetry/TelemetryMetricsHelper.cs Updated namespace and removed unneeded using statements.
src/Core/Services/ExecutionHelper.cs Added telemetry activity tracking for both query and mutation execution.
src/Core/Services/BuildRequestStateMiddleware.cs Integrated telemetry, runtime config provider, and error tracking.

@RubenCerna2079
Copy link
Contributor Author

/azp run

Copy link

Azure Pipelines successfully started running 6 pipeline(s).

@RubenCerna2079
Copy link
Contributor Author

/azp run

Copy link

Azure Pipelines successfully started running 6 pipeline(s).

@RubenCerna2079 RubenCerna2079 changed the title Jerry/otel ruben Enhance GraphQL OTEL instrumentation with custom metrics and traces May 2, 2025
@RubenCerna2079
Copy link
Contributor Author

/azp run

Copy link

Azure Pipelines successfully started running 6 pipeline(s).

statusCode = HttpStatusCode.InternalServerError;
}

string errorMessage = context.Result.Errors![0].Message;
Copy link
Contributor

Choose a reason for hiding this comment

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

We should verify Errors is not null here before dereferencing it

Exception ex = new(errorMessage);

// Activity will track error
activity?.TrackRestControllerActivityFinishedWithException(ex, statusCode);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: the name TrackRestControllerActivityFinishedWithException is misleading since this is now being used for GraphQL too.. Consider using a generic TrackController.. name

@@ -91,6 +96,8 @@ public async ValueTask ExecuteQueryAsync(IMiddlewareContext context)
/// </param>
public async ValueTask ExecuteMutateAsync(IMiddlewareContext context)
{
using Activity? activity = StartQueryActivity(context);
Copy link
Contributor

Choose a reason for hiding this comment

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

When is the end of this activity marked?

// We want to ignore introspection queries DAB uses to check access to GraphQL since they are not sent by the user.
if (!isIntrospectionQuery)
{
TelemetryMetricsHelper.IncrementActiveRequests(apiType);
Copy link
Contributor

Choose a reason for hiding this comment

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

How did you test this? Could you please add it to the description?
Can we add some integration tests as well?

@sourabh1007
Copy link
Contributor

You should follow open telemetry semantic conventions for otel metrics and trace.
For HTTP related information, this should be the semantic convention: https://opentelemetry.io/docs/specs/semconv/http/http-spans/

There are few HTTP metrics already available, https://opentelemetry.io/docs/specs/semconv/http/http-metrics/.

Similar exercise, we did for Cosmos DB SDK. ref. https://opentelemetry.io/docs/specs/semconv/database/cosmosdb/. Right now, it is under preview.

@Aniruddh25 what do you think about it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OTEL Implementation for GraphQL
4 participants