Skip to content

Commit

Permalink
Add more OTEL context to GQL resolver errors (#1193)
Browse files Browse the repository at this point in the history
I ran into errors (I can't remember which ones 😬) where I wanted more context, so I think this might help in the future.
  • Loading branch information
myieye authored Nov 5, 2024
1 parent ad076a1 commit b5803ed
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions backend/LexBoxApi/GraphQL/ErrorLoggingDiagnosticsEventListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,8 @@

namespace LexBoxApi.GraphQL;

public class ErrorLoggingDiagnosticsEventListener : ExecutionDiagnosticEventListener
public class ErrorLoggingDiagnosticsEventListener(ILogger<ErrorLoggingDiagnosticsEventListener> log) : ExecutionDiagnosticEventListener
{
private readonly ILogger<ErrorLoggingDiagnosticsEventListener> log;

public ErrorLoggingDiagnosticsEventListener(
ILogger<ErrorLoggingDiagnosticsEventListener> log)
{
this.log = log;
}

public override void ResolverError(
IMiddlewareContext context,
IError error)
Expand Down Expand Up @@ -67,13 +59,17 @@ public override void ValidationErrors(IRequestContext context, IReadOnlyList<IEr

public override void ResolverError(IRequestContext context, ISelection selection, IError error)
{
LogError(error);
LogError(error, extraTags: [
new("graphql.selection.field_name", selection.Field.Name),
new("graphql.selection.field_declaring_type", selection.DeclaringType),
new("graphql.selection.field_response_name", selection.ResponseName),
]);
}

private void LogError(IError error, [CallerMemberName] string source = "")
private void LogError(IError error, [CallerMemberName] string source = "", params KeyValuePair<string, object?>[] extraTags)
{
log.LogError(error.Exception, "{Source}: {Message}", source, error.Message);
TraceError(error, source);
TraceError(error, source, extraTags);
}

private void LogException(Exception exception, [CallerMemberName] string source = "")
Expand All @@ -82,35 +78,45 @@ private void LogException(Exception exception, [CallerMemberName] string source
TraceException(exception, source);
}

private void TraceError(IError error, string source)
private void TraceError(IError error, string source, params KeyValuePair<string, object?>[] extraTags)
{
if (error.Exception != null)
{
TraceException(error.Exception, source);
TraceException(error.Exception, source, extraTags);
}
else
{
Activity.Current?.AddEvent(new(source, tags: new()
Activity.Current?.AddEvent(new(source, tags: AddTags(new()
{
["error.message"] = error.Message,
["error.code"] = error.Code,
}));
}, extraTags)));
}
}

private void TraceException(Exception exception, string source)
private void TraceException(Exception exception, string source, params KeyValuePair<string, object?>[] extraTags)
{
Activity.Current?
.SetStatus(ActivityStatusCode.Error)
.AddEvent(new(source, tags: new()
.AddEvent(new(source, tags: AddTags(new()
{
["exception.message"] = exception.Message,
["exception.stacktrace"] = exception.StackTrace,
["exception.source"] = exception.Source,
}));
}, extraTags)));
if (exception.InnerException != null)
{
TraceException(exception.InnerException, $"{source} - Inner");
}
}

private static ActivityTagsCollection AddTags(ActivityTagsCollection tags, KeyValuePair<string, object?>[] moreTags)
{
foreach (var kvp in moreTags)
{
tags.Add(kvp.Key, kvp.Value);
}

return tags;
}
}

0 comments on commit b5803ed

Please sign in to comment.