From f46b1c671e0508d8babbc75433b317413fd66780 Mon Sep 17 00:00:00 2001 From: Amanda Tarafa Mas Date: Fri, 25 Sep 2020 15:17:40 +0100 Subject: [PATCH] Corrects Span ID format for adding to Log Entries. Fixes #5358. --- .../Logging/TraceContextForLogEntryTests.cs | 95 +++++++++++++++++++ .../Logging/TraceContextForLogEntry.cs | 3 +- .../EntryData.cs | 2 +- 3 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore.Tests/Logging/TraceContextForLogEntryTests.cs diff --git a/apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore.Tests/Logging/TraceContextForLogEntryTests.cs b/apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore.Tests/Logging/TraceContextForLogEntryTests.cs new file mode 100644 index 000000000000..eb4d0061d4b1 --- /dev/null +++ b/apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore.Tests/Logging/TraceContextForLogEntryTests.cs @@ -0,0 +1,95 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using Google.Cloud.Diagnostics.Common; +using Moq; +using Xunit; + +namespace Google.Cloud.Diagnostics.AspNetCore.Tests +{ + public class TraceContextForLogEntryTests + { + [Fact] + public void FromGoogleTrace() + { + var oldTracer = ContextTracerManager.GetCurrentTracer(); + try + { + string traceId = "dummy_trace_id"; + ulong spanId = 0x12D687; + // The spanId set on the log entry should confirm to x16 + // format so that the backend can really associate the log entry + // to the span. + string expectedSpanId = "000000000012d687"; + + ContextTracerManager.SetCurrentTracer(MockTracer(traceId, spanId)); + + var traceContext = TraceContextForLogEntry.FromGoogleTrace(); + + Assert.Equal(traceId, traceContext.TraceId); + Assert.Equal(expectedSpanId, traceContext.SpanId); + } + finally + { + ContextTracerManager.SetCurrentTracer(oldTracer); + } + } + + [Fact] + public void FromGoogleTrace_NoTrace() + { + var oldTracer = ContextTracerManager.GetCurrentTracer(); + try + { + ContextTracerManager.SetCurrentTracer(MockTracer()); + + Assert.Null(TraceContextForLogEntry.FromGoogleTrace()); + } + finally + { + ContextTracerManager.SetCurrentTracer(oldTracer); + } + } + + [Fact] + public void FromGoogleTrace_NoSpan() + { + var oldTracer = ContextTracerManager.GetCurrentTracer(); + try + { + string traceId = "dummy_trace_id"; + + ContextTracerManager.SetCurrentTracer(MockTracer(traceId)); + + var traceContext = TraceContextForLogEntry.FromGoogleTrace(); + + Assert.Equal(traceId, traceContext.TraceId); + Assert.Null(traceContext.SpanId); + } + finally + { + ContextTracerManager.SetCurrentTracer(oldTracer); + } + } + + private IManagedTracer MockTracer(string traceId = null, ulong? spanId = null) + { + Mock tracerMock = new Mock(MockBehavior.Strict); + tracerMock.Setup(t => t.GetCurrentTraceId()).Returns(traceId); + tracerMock.Setup(t => t.GetCurrentSpanId()).Returns(spanId); + + return tracerMock.Object; + } + } +} diff --git a/apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore/Logging/TraceContextForLogEntry.cs b/apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore/Logging/TraceContextForLogEntry.cs index 54330d5bec39..cacc3cc5cf73 100644 --- a/apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore/Logging/TraceContextForLogEntry.cs +++ b/apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore/Logging/TraceContextForLogEntry.cs @@ -62,7 +62,6 @@ internal static TraceContextForLogEntry FromGoogleTrace() => internal static TraceContextForLogEntry FromExternalTrace(IServiceProvider serviceProvider) => serviceProvider?.GetService()?.GetCurrentTraceContext(serviceProvider); - private static string SpanIdToHex(ulong? spanId) => spanId is null ? null : string.Format("0x{0:X}", spanId); - + private static string SpanIdToHex(ulong? spanId) => spanId is null ? null : $"{spanId:x16}"; } } diff --git a/apis/Google.Cloud.Diagnostics.Common/Google.Cloud.Diagnostics.Common.IntegrationTests/EntryData.cs b/apis/Google.Cloud.Diagnostics.Common/Google.Cloud.Diagnostics.Common.IntegrationTests/EntryData.cs index 101d4cf5eaa7..05583407ab0d 100644 --- a/apis/Google.Cloud.Diagnostics.Common/Google.Cloud.Diagnostics.Common.IntegrationTests/EntryData.cs +++ b/apis/Google.Cloud.Diagnostics.Common/Google.Cloud.Diagnostics.Common.IntegrationTests/EntryData.cs @@ -28,6 +28,6 @@ public static class EntryData /// public static string GetMessage(string message, string id) => $"{message} - {id}"; - public static string SpanIdToHex(ulong? spanId) => spanId is null ? null : string.Format("0x{0:X}", spanId); + public static string SpanIdToHex(ulong? spanId) => spanId is null ? null : $"{spanId:x16}"; } }