-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathOtelKernelTests.cs
111 lines (94 loc) · 3.65 KB
/
OtelKernelTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Backend.Tests.Mocks;
using BackendFramework.Otel;
using Microsoft.AspNetCore.Http;
using NUnit.Framework;
using static BackendFramework.Otel.OtelKernel;
namespace Backend.Tests.Otel
{
public class OtelKernelTests : IDisposable
{
private const string FrontendConsentKey = "otelConsent";
private const string FrontendSessionIdKey = "sessionId";
private const string OtelConsentKey = "otelConsent";
private const string OtelSessionIdKey = "sessionId";
private const string OtelConsentBaggageKey = "otelConsentBaggage";
private const string OtelSessionBaggageKey = "sessionBaggage";
private LocationEnricher _locationEnricher = null!;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_locationEnricher?.Dispose();
}
}
[Test]
public void BuildersSetBaggageFromHeader()
{
// Arrange
var httpContext = new DefaultHttpContext();
httpContext.Request.Headers[FrontendConsentKey] = "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 OnEndSetsTagsFromBaggage()
{
// 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);
// Assert
var testLocation = new Dictionary<string, string>
{
{"country", "test country"},
{"regionName", "test region"},
{"city", "city"}
};
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
_locationEnricher.OnEnd(activity);
// Assert
Assert.That(activity.Tags.Any(_ => _.Key == "url.full" && _.Value == ""));
Assert.That(activity.Tags.Any(_ => _.Key == "url.redacted.ip" && _.Value == LocationProvider.locationGetterUri));
}
}
}