From b5803ed2f97866770f27cb32fbca0f97fe13567c Mon Sep 17 00:00:00 2001 From: Tim Haasdyk Date: Tue, 5 Nov 2024 10:46:36 +0100 Subject: [PATCH] Add more OTEL context to GQL resolver errors (#1193) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I ran into errors (I can't remember which ones 😬) where I wanted more context, so I think this might help in the future. --- .../ErrorLoggingDiagnosticsEventListener.cs | 44 +++++++++++-------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/backend/LexBoxApi/GraphQL/ErrorLoggingDiagnosticsEventListener.cs b/backend/LexBoxApi/GraphQL/ErrorLoggingDiagnosticsEventListener.cs index 25605668a..2aea08cb0 100644 --- a/backend/LexBoxApi/GraphQL/ErrorLoggingDiagnosticsEventListener.cs +++ b/backend/LexBoxApi/GraphQL/ErrorLoggingDiagnosticsEventListener.cs @@ -7,16 +7,8 @@ namespace LexBoxApi.GraphQL; -public class ErrorLoggingDiagnosticsEventListener : ExecutionDiagnosticEventListener +public class ErrorLoggingDiagnosticsEventListener(ILogger log) : ExecutionDiagnosticEventListener { - private readonly ILogger log; - - public ErrorLoggingDiagnosticsEventListener( - ILogger log) - { - this.log = log; - } - public override void ResolverError( IMiddlewareContext context, IError error) @@ -67,13 +59,17 @@ public override void ValidationErrors(IRequestContext context, IReadOnlyList[] 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 = "") @@ -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[] 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[] 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[] moreTags) + { + foreach (var kvp in moreTags) + { + tags.Add(kvp.Key, kvp.Value); + } + + return tags; + } }