From da6efd254fb1c8b71c25c9f86928fe00efb38b74 Mon Sep 17 00:00:00 2001 From: PascalSenn Date: Mon, 25 Sep 2023 13:51:00 +0200 Subject: [PATCH] Cache printed GraphQL Query for Tracing (#6543) --- .../Diagnostics/src/Diagnostics/ActivityEnricher.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/HotChocolate/Diagnostics/src/Diagnostics/ActivityEnricher.cs b/src/HotChocolate/Diagnostics/src/Diagnostics/ActivityEnricher.cs index daec6c69560..788a963fa7f 100644 --- a/src/HotChocolate/Diagnostics/src/Diagnostics/ActivityEnricher.cs +++ b/src/HotChocolate/Diagnostics/src/Diagnostics/ActivityEnricher.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; +using System.Runtime.CompilerServices; using System.Text; using System.Text.Json; using Microsoft.AspNetCore.Http; @@ -26,6 +27,7 @@ namespace HotChocolate.Diagnostics; public class ActivityEnricher { private readonly InstrumentationOptions _options; + private readonly ConditionalWeakTable _queryCache = new(); /// /// Initializes a new instance of . @@ -108,7 +110,13 @@ public virtual void EnrichSingleRequest( if (request.Query is not null && (_options.RequestDetails & RequestDetails.Query) == RequestDetails.Query) { - activity.SetTag("graphql.http.request.query.body", request.Query.Print()); + if (!_queryCache.TryGetValue(request.Query, out var query)) + { + query = request.Query.Print(); + _queryCache.Add(request.Query, query); + } + + activity.SetTag("graphql.http.request.query.body", query); } if (request.OperationName is not null &&