Skip to content
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

Add more OTEL context to GQL resolver errors #1193

Merged
Merged
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
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;
}
}
Loading