forked from googleapis/google-cloud-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Corrects Span ID format for adding to Log Entries.
Fixes googleapis#5358.
- Loading branch information
1 parent
f9db567
commit f46b1c6
Showing
3 changed files
with
97 additions
and
3 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
...NetCore/Google.Cloud.Diagnostics.AspNetCore.Tests/Logging/TraceContextForLogEntryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<IManagedTracer> tracerMock = new Mock<IManagedTracer>(MockBehavior.Strict); | ||
tracerMock.Setup(t => t.GetCurrentTraceId()).Returns(traceId); | ||
tracerMock.Setup(t => t.GetCurrentSpanId()).Returns(spanId); | ||
|
||
return tracerMock.Object; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters