Skip to content

Commit

Permalink
fix tests to account for conditioning Otel tags on consent
Browse files Browse the repository at this point in the history
  • Loading branch information
andracc committed Dec 11, 2024
1 parent 9ada92d commit 30f4418
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions Backend.Tests/Otel/OtelKernelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ namespace Backend.Tests.Otel
public class OtelKernelTests : IDisposable
{
private const string FrontendSessionIdKey = "sessionId";
private const string FrontendOtelConsentKey = "otelConsent";
private const string OtelSessionIdKey = "sessionId";
private const string OtelConsentKey = "otelConsent";
private const string OtelSessionBaggageKey = "sessionBaggage";

private const string OtelConsentBaggageKey = "otelConsentBaggage";
private LocationEnricher _locationEnricher = null!;

public void Dispose()
Expand All @@ -32,41 +36,47 @@ protected virtual void Dispose(bool disposing)
}

[Test]
public void BuildersSetSessionBaggageFromHeader()
public void BuildersSetConsentAndSessionBaggageFromHeader()
{
// Arrange
var httpContext = new DefaultHttpContext();
httpContext.Request.Headers[FrontendOtelConsentKey] = "true";
httpContext.Request.Headers[FrontendSessionIdKey] = "123";
var activity = new Activity("testActivity").Start();

// Act

TrackConsent(activity, httpContext.Request);
TrackSession(activity, httpContext.Request);

// Assert
Assert.That(activity.Baggage.Any(_ => _.Key == OtelConsentBaggageKey));
Assert.That(activity.Baggage.Any(_ => _.Key == OtelSessionBaggageKey));
}

[Test]
public void OnEndSetsSessionTagFromBaggage()
public void OnEndSetsConsentAndSessionTagFromBaggage()
{
// Arrange
var activity = new Activity("testActivity").Start();
activity.SetBaggage(OtelConsentBaggageKey, "true");
activity.SetBaggage(OtelSessionBaggageKey, "test session id");

// Act
_locationEnricher.OnEnd(activity);

// Assert
Assert.That(activity.Tags.Any(_ => _.Key == OtelConsentKey));
Assert.That(activity.Tags.Any(_ => _.Key == OtelSessionIdKey));
}


[Test]
public void OnEndSetsLocationTags()
{
// Arrange
_locationEnricher = new LocationEnricher(new LocationProviderMock());
var activity = new Activity("testActivity").Start();
activity.SetBaggage(OtelConsentBaggageKey, "true");

// Act
_locationEnricher.OnEnd(activity);
Expand All @@ -81,11 +91,13 @@ public void OnEndSetsLocationTags()
Assert.That(activity.Tags, Is.SupersetOf(testLocation));
}

[Test]
public void OnEndRedactsIp()
{
// Arrange
_locationEnricher = new LocationEnricher(new LocationProviderMock());
var activity = new Activity("testActivity").Start();
activity.SetBaggage(OtelConsentBaggageKey, "true");
activity.SetTag("url.full", $"{LocationProvider.locationGetterUri}100.0.0.0");

// Act
Expand Down

0 comments on commit 30f4418

Please sign in to comment.