Skip to content

Commit

Permalink
test: Adds more unit tests for trace correlation
Browse files Browse the repository at this point in the history
  • Loading branch information
amanda-tarafa committed Mar 18, 2021
1 parent 4593a55 commit 0da1f45
Showing 1 changed file with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -289,9 +290,9 @@ public async Task SingleLogEntryWithTraceId()
{
googleTarget.SpanId = "${event-properties:SpanId:Format=x16}";
googleTarget.TraceId = "${activityid}";
googleTarget.TraceSampled = "TRUE";
System.Diagnostics.Trace.CorrelationManager.ActivityId = guidTraceId;
LogManager.GetLogger("testlogger").Info("Hello {SpanId}", 74);
googleTarget.TraceSampled = "${event-properties:Sampled}";
Trace.CorrelationManager.ActivityId = guidTraceId;
LogManager.GetLogger("testlogger").Info("Hello {SpanId}. You are being {Sampled}", 74, true);
return Task.FromResult(0);
});
Assert.Single(uploadedEntries);
Expand All @@ -300,6 +301,35 @@ public async Task SingleLogEntryWithTraceId()
Assert.True(uploadedEntries[0].TraceSampled);
}

[Theory]
[InlineData(true, true)]
[InlineData("true", true)]
[InlineData("TruE", true)]
[InlineData(1, true)] // This is useful when reading directly from X-Cloud-Trace-Context header.
[InlineData("1", true)]
[InlineData(false, false)]
[InlineData("false", false)]
[InlineData("fALse", false)]
[InlineData(0, false)]
[InlineData(5, false)]
[InlineData("something", false)]
public async Task TraceSampledValues(object sampled, bool expectedSampled)
{
var guidTraceId = Guid.NewGuid();

var uploadedEntries = await RunTestWorkingServer(
googleTarget =>
{
googleTarget.TraceId = "${event-properties:TraceId}";
googleTarget.TraceSampled = "${event-properties:Sampled}";
LogManager.GetLogger("testlogger").Info("Hello {TraceId}. You are being {Sampled}", guidTraceId, sampled);
return Task.FromResult(0);
});
Assert.Single(uploadedEntries);
Assert.Equal($"projects/projectId/traces/{guidTraceId}", uploadedEntries[0].Trace);
Assert.Equal(expectedSampled, uploadedEntries[0].TraceSampled);
}

[Fact]
public async Task SingleLogEntryWithLocation()
{
Expand Down

0 comments on commit 0da1f45

Please sign in to comment.