Skip to content

Commit

Permalink
switch consent from string to bool
Browse files Browse the repository at this point in the history
  • Loading branch information
andracc committed Dec 10, 2024
1 parent 8307f70 commit 2abf257
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
42 changes: 24 additions & 18 deletions Backend/Otel/OtelKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public static void AddOpenTelemetryInstrumentation(this IServiceCollection servi

internal static void TrackConsent(Activity activity, HttpRequest request)
{
var consent = request.Headers.TryGetValue("otelConsent", out var values) ? values.FirstOrDefault() : "nothing";
activity.SetBaggage("otelConsent", consent);
var consent = request.Headers.TryGetValue("otelConsent", out var values) ? bool.TryParse(values.FirstOrDefault(), out bool _) : false;
activity.SetBaggage("otelConsentBaggage", consent.ToString());
}

internal static void TrackSession(Activity activity, HttpRequest request)
Expand Down Expand Up @@ -105,23 +105,29 @@ internal class LocationEnricher(ILocationProvider locationProvider) : BaseProces
{
public override async void OnEnd(Activity data)
{
data?.SetTag("consent value", data?.GetBaggageItem("otelConsent"));
var uriPath = (string?)data?.GetTagItem("url.full");
var locationUri = LocationProvider.locationGetterUri;
if (uriPath is null || !uriPath.Contains(locationUri))
var consentString = data?.GetBaggageItem("otelConsentBaggage");
data?.AddTag("otelConsent", consentString);
var consent = bool.TryParse(consentString, out bool value) ? value : false;
// Note: A bool value also would have worked for SetTag
if (consent)
{
var location = await locationProvider.GetLocation();
data?.AddTag("country", location?.Country);
data?.AddTag("regionName", location?.RegionName);
data?.AddTag("city", location?.City);
}
data?.SetTag("sessionId", data?.GetBaggageItem("sessionBaggage"));
if (uriPath is not null && uriPath.Contains(locationUri))
{
// When getting location externally, url.full includes site URI and user IP.
// In such cases, only add url without IP information to traces.
data?.SetTag("url.full", "");
data?.SetTag("url.redacted.ip", LocationProvider.locationGetterUri);
var uriPath = (string?)data?.GetTagItem("url.full");
var locationUri = LocationProvider.locationGetterUri;
if (uriPath is null || !uriPath.Contains(locationUri))
{
var location = await locationProvider.GetLocation();
data?.AddTag("country", location?.Country);
data?.AddTag("regionName", location?.RegionName);
data?.AddTag("city", location?.City);
}
data?.SetTag("sessionId", data?.GetBaggageItem("sessionBaggage"));
if (uriPath is not null && uriPath.Contains(locationUri))
{
// When getting location externally, url.full includes site URI and user IP.
// In such cases, only add url without IP information to traces.
data?.SetTag("url.full", "");
data?.SetTag("url.redacted.ip", LocationProvider.locationGetterUri);
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ const axiosInstance = axios.create({ baseURL: apiBaseURL });
axiosInstance.interceptors.request.use((config: InternalAxiosRequestConfig) => {
config.headers.sessionId = getSessionId();
LocalStorage.getCurrentUser()?.otelConsent
? (config.headers.otelConsent = "yay")
: (config.headers.otelConsent = "nay");
? (config.headers.otelConsent = true)
: (config.headers.otelConsent = false);
return config;
});
axiosInstance.interceptors.response.use(undefined, (err: AxiosError) => {
Expand Down

0 comments on commit 2abf257

Please sign in to comment.