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
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,20 @@ public virtual void EnrichExecuteOperation(
RequestContext context,
Activity activity)
{ }

public virtual void EnrichDocumentNotFoundInStorage(
RequestContext context,
OperationDocumentId documentId,
Activity activity)
{ }

public virtual void EnrichUntrustedDocumentRejected(
RequestContext context,
Activity activity)
{ }

public virtual void EnrichAddedDocumentToCache(
RequestContext context,
Activity activity)
{ }
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,10 @@ public static class Document

// Note: This is not part of the OTEL semantic conventions
public const string Body = "graphql.document.body";

// Note: This is not part of the OTEL semantic conventions
public const string Valid = "graphql.document.valid";
}

public static class Operation
{
// Note: This is not part of the OTEL semantic conventions
public const string Id = "graphql.operation.id";

public const string Name = "graphql.operation.name";
public const string Type = "graphql.operation.type";

Expand All @@ -46,7 +40,7 @@ public static class Step
public const string Id = "graphql.operation.step.id";
public const string Kind = "graphql.operation.step.kind";

// Note: This is not part of the OTEL semantic conventions
// Note: This is specific to Fusion
public static class KindValues
{
public const string Operation = "operation";
Expand Down Expand Up @@ -85,20 +79,11 @@ public static class Selection
public const string Name = "graphql.selection.name";
public const string Path = "graphql.selection.path";

// Note: This is not part of the OTEL semantic conventions
public const string Hierarchy = "graphql.selection.hierarchy";

public static class Field
{
public const string Name = "graphql.selection.field.name";
public const string ParentType = "graphql.selection.field.parent_type";
public const string Coordinate = "graphql.selection.field.coordinate";

// Note: This is not part of the OTEL semantic conventions
public const string IsDeprecated = "graphql.selection.field.isDeprecated";

// Note: This is not part of the OTEL semantic conventions
public const string Type = "graphql.selection.type";
}
}

Expand Down Expand Up @@ -132,6 +117,12 @@ public static class Operation
}
}

// Note: This is not part of the OTEL semantic conventions
public static class Errors
{
public const string Count = "graphql.errors.count";
}

public static class Error
{
public const string Message = "graphql.error.message";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal abstract class ExecuteRequestSpanBase(
Activity activity,
RequestContext context,
InstrumentationOptionsBase options,
ActivityEnricherBase? enricher,
ActivityEnricherBase enricher,
bool shouldDisposeActivity) : SpanBase(activity, shouldDisposeActivity)
{
public RequestContext Context { get; } = context;
Expand Down Expand Up @@ -42,6 +42,12 @@ protected override void OnComplete()
Activity.SetTag(GraphQL.Document.Body, documentInfo.Document.Print());
}

if (Context.Result is OperationResult result)
{
// This was previously also always set to 0, so I just kept that behavior.
Comment thread
tobias-tengler marked this conversation as resolved.
Activity.SetTag(GraphQL.Errors.Count, result.Errors.Count);
}

if (Context.Result is null or OperationResult { Errors: [_, ..] })
{
Activity.SetStatus(ActivityStatusCode.Error);
Expand All @@ -51,6 +57,6 @@ protected override void OnComplete()
Activity.SetStatus(ActivityStatusCode.Ok);
}

enricher?.EnrichExecuteRequest(Context, Activity);
enricher.EnrichExecuteRequest(Context, Activity);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Diagnostics;
using GreenDonut;
using HotChocolate.Execution;
using HotChocolate.Language;
using HotChocolate.Resolvers;

namespace HotChocolate.Diagnostics;
Expand Down Expand Up @@ -46,6 +47,11 @@ public virtual void EnrichBatchDispatchError(
Activity activity)
{ }

public virtual void EnrichAddedOperationToCache(
RequestContext context,
Activity activity)
{ }

public virtual void EnrichOnSubscriptionEvent(
RequestContext context,
ulong subscriptionId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Diagnostics;
using HotChocolate.Execution;
using HotChocolate.Execution.Instrumentation;
using HotChocolate.Language;
using HotChocolate.Resolvers;
using Microsoft.AspNetCore.Http;
using static HotChocolate.Diagnostics.HotChocolateActivitySource;
Expand Down Expand Up @@ -312,11 +313,30 @@ public override void RetrievedDocumentFromStorage(RequestContext context)
}
}

public override void DocumentNotFoundInStorage(RequestContext context, OperationDocumentId documentId)
{
if (context.Features.TryGet<ExecuteRequestSpan>(out var span))
{
span.Activity.AddEvent(new(nameof(DocumentNotFoundInStorage)));
enricher.EnrichDocumentNotFoundInStorage(context, documentId, span.Activity);
}
}

public override void UntrustedDocumentRejected(RequestContext context)
{
if (context.Features.TryGet<ExecuteRequestSpan>(out var span))
{
span.Activity.AddEvent(new(nameof(UntrustedDocumentRejected)));
enricher.EnrichUntrustedDocumentRejected(context, span.Activity);
}
}

public override void AddedDocumentToCache(RequestContext context)
{
if (context.Features.TryGet<ExecuteRequestSpan>(out var span))
{
span.Activity.AddEvent(new(nameof(AddedDocumentToCache)));
enricher.EnrichAddedDocumentToCache(context, span.Activity);
}
}

Expand All @@ -325,6 +345,7 @@ public override void AddedOperationToCache(RequestContext context)
if (context.Features.TryGet<ExecuteRequestSpan>(out var span))
{
span.Activity.AddEvent(new(nameof(AddedOperationToCache)));
enricher.EnrichAddedOperationToCache(context, span.Activity);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal sealed class ExecuteRequestSpan(
Activity activity,
RequestContext context,
InstrumentationOptionsBase options,
ActivityEnricherBase? enricher,
ActivityEnricherBase enricher,
bool shouldDisposeActivity)
: ExecuteRequestSpanBase(activity, context, options, enricher, shouldDisposeActivity)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,67 @@ public async Task PersistedOperation_LoadsFromStorage_DefaultScopes()
}
}

[Fact]
public async Task DocumentNotFoundInStorage_RecordsEvent()
{
using (CaptureActivities(out var activities))
{
// arrange
var services = new ServiceCollection()
.AddGraphQL()
.AddInstrumentation(o => o.Scopes = ActivityScopes.All)
.AddQueryType<SimpleQuery>()
.UsePersistedOperationPipeline()
.ConfigureSchemaServices(
s => s.AddSingleton<IOperationDocumentStorage>(new NoopOperationDocumentStorage()))
.Services
.BuildServiceProvider();

var executor = await services.GetRequestExecutorAsync();

var request = OperationRequestBuilder.New()
.SetDocumentId("a8c5e2f1d3b4a6e7c9d0f1a2b3c4d5e6")
.Build();

// act
await executor.ExecuteAsync(request);

// assert
activities.MatchSnapshot();
}
}

[Fact]
public async Task UntrustedDocumentRejected_RecordsEvent()
{
using (CaptureActivities(out var activities))
{
// arrange
var services = new ServiceCollection()
.AddGraphQL()
.AddInstrumentation(o => o.Scopes = ActivityScopes.All)
.AddQueryType<SimpleQuery>()
.ModifyRequestOptions(o => o.PersistedOperations.OnlyAllowPersistedDocuments = true)
.UsePersistedOperationPipeline()
.ConfigureSchemaServices(
s => s.AddSingleton<IOperationDocumentStorage>(new NoopOperationDocumentStorage()))
.Services
.BuildServiceProvider();

var executor = await services.GetRequestExecutorAsync();

var request = OperationRequestBuilder.New()
.SetDocument("{ sayHello }")
.Build();

// act
await executor.ExecuteAsync(request);

// assert
activities.MatchSnapshot();
}
}

[Fact]
public async Task ParsingError_InvalidGraphQLDocument_ReportsErrorStatus()
{
Expand Down Expand Up @@ -493,6 +554,20 @@ public string OnFailingMessage([EventMessage] string message)
=> throw new InvalidOperationException("Subscription event failed.");
}

private sealed class NoopOperationDocumentStorage : IOperationDocumentStorage
{
public ValueTask<IOperationDocument?> TryReadAsync(
OperationDocumentId documentId,
CancellationToken cancellationToken = default)
=> new(default(IOperationDocument));

public ValueTask SaveAsync(
OperationDocumentId documentId,
IOperationDocument document,
CancellationToken cancellationToken = default)
=> default;
}

private sealed class InMemoryOperationDocumentStorage : IOperationDocumentStorage
{
private readonly Dictionary<string, DocumentNode> _cache = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
{
"Key": "graphql.document.hash",
"Value": "md5:f7e9989fbb67af7fa747a9983313c9e5"
},
{
"Key": "graphql.errors.count",
"Value": 0
}
],
"event": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
{
"Key": "graphql.document.body",
"Value": "query SayHelloOperation {\n sayHello\n}"
},
{
"Key": "graphql.errors.count",
"Value": 0
}
],
"event": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
{
"Key": "graphql.document.body",
"Value": "query SayHelloOperation {\n causeFatalError\n}"
},
{
"Key": "graphql.errors.count",
"Value": 1
}
],
"event": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
{
"Key": "graphql.document.hash",
"Value": "md5:f7e9989fbb67af7fa747a9983313c9e5"
},
{
"Key": "graphql.errors.count",
"Value": 0
}
],
"event": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
{
"Key": "graphql.document.hash",
"Value": "md5:9b20745108c8de5afccc35cd56ead9fc"
},
{
"Key": "graphql.errors.count",
"Value": 0
}
],
"event": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
{
"Key": "graphql.document.hash",
"Value": "md5:9b20745108c8de5afccc35cd56ead9fc"
},
{
"Key": "graphql.errors.count",
"Value": 0
}
],
"event": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
{
"Key": "graphql.document.hash",
"Value": "md5:f7e9989fbb67af7fa747a9983313c9e5"
},
{
"Key": "graphql.errors.count",
"Value": 0
}
],
"event": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"activities": [
{
"OperationName": "GraphQL Operation",
"DisplayName": "GraphQL Operation",
"Status": "Error",
"tags": [
{
"Key": "graphql.errors.count",
"Value": 1
}
],
"event": [
{
"Name": "DocumentNotFoundInStorage",
"Tags": []
}
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
{
"Key": "graphql.document.body",
"Value": "query SayHelloOperation {\n sayHello_\n}"
},
{
"Key": "graphql.errors.count",
"Value": 1
}
],
"event": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
{
"Key": "graphql.document.hash",
"Value": "md5:bb507ba78696fc3c6ff3a17fb06784d5"
},
{
"Key": "graphql.errors.count",
"Value": 1
}
],
"event": [
Expand Down
Loading
Loading