Skip to content

Commit

Permalink
fix: do not alter telemetry ctx pt.2 (#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnmoreels committed Oct 30, 2023
1 parent 415964f commit 06ba21d
Show file tree
Hide file tree
Showing 22 changed files with 321 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ public static class ILoggerHttpDependencyExtensions
Guard.NotNull(request, nameof(request), "Requires a HTTP request message to track a HTTP dependency");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the HTTP dependency operation");

context = context ?? new Dictionary<string, object>();

LogHttpDependency(logger, request, statusCode, startTime, duration, dependencyId: null, context);
}

Expand Down Expand Up @@ -193,7 +191,7 @@ public static class ILoggerHttpDependencyExtensions
Guard.NotLessThan(statusCode, 100, nameof(statusCode), "Requires a valid HTTP response status code that's within the range of 100 to 599, inclusive");
Guard.NotGreaterThan(statusCode, 599, nameof(statusCode), "Requires a valid HTTP response status code that's within the range of 100 to 599, inclusive");

context = context ?? new Dictionary<string, object>();
context = context is null ? new Dictionary<string, object>() : new Dictionary<string, object>(context);

string requestUri = request.Path;
string targetName = request.Host.Host;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ public static void LogRequest(this ILogger logger, HttpRequest request, HttpResp
Guard.NotNull(response, nameof(response), "Requires a HTTP response instance to track a HTTP request operation");
Guard.NotNull(measurement, nameof(measurement), "Requires an measurement instance to time the duration of the HTTP request");

context = context ?? new Dictionary<string, object>();

LogRequest(logger, request, response, measurement.StartTime, measurement.Elapsed, context);
}

Expand Down Expand Up @@ -99,8 +97,6 @@ public static void LogRequest(this ILogger logger, HttpRequest request, HttpResp
Guard.NotNull(response, nameof(response), "Requires a HTTP response instance to track a HTTP request operation");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the HTTP request");

context = context ?? new Dictionary<string, object>();

LogRequest(logger, request, response.StatusCode, operationName: null, startTime, duration, context);
}

Expand Down Expand Up @@ -160,8 +156,6 @@ public static void LogRequest(this ILogger logger, HttpRequest request, HttpResp
Guard.NotNull(response, nameof(response), "Requires a HTTP response instance to track a HTTP request operation");
Guard.NotNull(measurement, nameof(measurement), "Requires an measurement instance to time the duration of the HTTP request");

context = context ?? new Dictionary<string, object>();

LogRequest(logger, request, response, operationName, measurement.StartTime, measurement.Elapsed, context);
}

Expand Down Expand Up @@ -191,8 +185,6 @@ public static void LogRequest(this ILogger logger, HttpRequest request, HttpResp
Guard.NotNull(response, nameof(response), "Requires a HTTP response instance to track a HTTP request operation");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the HTTP request");

context = context ?? new Dictionary<string, object>();

LogRequest(logger, request, response.StatusCode, operationName, startTime, duration, context);
}

Expand Down Expand Up @@ -254,8 +246,6 @@ public static void LogRequest(this ILogger logger, HttpRequest request, int resp
Guard.NotLessThan(responseStatusCode, 0, nameof(responseStatusCode), "Requires a HTTP response status code that's within the 0-999 range to track a HTTP request");
Guard.NotGreaterThan(responseStatusCode, 999, nameof(responseStatusCode), "Requires a HTTP response status code that's within the 0-999 range to track a HTTP request");

context = context ?? new Dictionary<string, object>();

LogRequest(logger, request, responseStatusCode, measurement.StartTime, measurement.Elapsed, context);
}

Expand Down Expand Up @@ -287,8 +277,6 @@ public static void LogRequest(this ILogger logger, HttpRequest request, int resp
Guard.NotGreaterThan(responseStatusCode, 999, nameof(responseStatusCode), "Requires a HTTP response status code that's within the 0-999 range to track a HTTP request");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the HTTP request");

context = context ?? new Dictionary<string, object>();

LogRequest(logger, request, responseStatusCode, operationName: null, startTime, duration, context);
}

Expand Down Expand Up @@ -353,8 +341,6 @@ public static void LogRequest(this ILogger logger, HttpRequest request, int resp
Guard.NotLessThan(responseStatusCode, 0, nameof(responseStatusCode), "Requires a HTTP response status code that's within the 0-999 range to track a HTTP request");
Guard.NotGreaterThan(responseStatusCode, 999, nameof(responseStatusCode), "Requires a HTTP response status code that's within the 0-999 range to track a HTTP request");

context = context ?? new Dictionary<string, object>();

LogRequest(logger, request, responseStatusCode, operationName, measurement.StartTime, measurement.Elapsed, context);
}

Expand Down Expand Up @@ -388,7 +374,7 @@ public static void LogRequest(this ILogger logger, HttpRequest request, int resp
Guard.NotGreaterThan(responseStatusCode, 999, nameof(responseStatusCode), "Requires a HTTP response status code that's within the 0-999 range to track a HTTP request");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the HTTP request");

context = context ?? new Dictionary<string, object>();
context = context is null ? new Dictionary<string, object>() : new Dictionary<string, object>(context);

logger.LogWarning(MessageFormats.RequestFormat,
RequestLogEntry.CreateForHttpRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ public static partial class ILoggerExtensions
Guard.NotNullOrWhitespace(operationName, nameof(operationName), "Requires a non-blank name for the Azure Search service to track the Azure Service dependency");
Guard.NotNull(measurement, nameof(measurement), "Requires a dependency measurement instance to track the latency of the Azure Search resource when tracking the Azure Search dependency");

context = context ?? new Dictionary<string, object>();

LogAzureSearchDependency(logger, searchServiceName, operationName, isSuccessful, measurement.StartTime, measurement.Elapsed, context);
}

Expand Down Expand Up @@ -98,8 +96,6 @@ public static partial class ILoggerExtensions
Guard.NotNullOrWhitespace(operationName, nameof(operationName), "Requires a non-blank name for the Azure Search service to track the Azure Service dependency");
Guard.NotNull(measurement, nameof(measurement), "Requires a dependency measurement instance to track the latency of the Azure Search resource when tracking the Azure Search dependency");

context = context ?? new Dictionary<string, object>();

LogAzureSearchDependency(logger, searchServiceName, operationName, isSuccessful, measurement.StartTime, measurement.Elapsed, dependencyId, context);
}

Expand Down Expand Up @@ -129,8 +125,6 @@ public static partial class ILoggerExtensions
Guard.NotNullOrWhitespace(operationName, nameof(operationName), "Requires a non-blank name for the Azure Search service to track the Azure Service dependency");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the Azure Search operation");

context = context ?? new Dictionary<string, object>();

LogAzureSearchDependency(logger, searchServiceName, operationName, isSuccessful, startTime, duration, dependencyId: null, context);
}

Expand Down Expand Up @@ -162,7 +156,7 @@ public static partial class ILoggerExtensions
Guard.NotNullOrWhitespace(operationName, nameof(operationName), "Requires a non-blank name for the Azure Search service to track the Azure Service dependency");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the Azure Search operation");

context = context ?? new Dictionary<string, object>();
context = context is null ? new Dictionary<string, object>() : new Dictionary<string, object>(context);

logger.LogWarning(MessageFormats.DependencyFormat, new DependencyLogEntry(
dependencyType: "Azure Search",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ public static partial class ILoggerExtensions
Guard.NotNullOrWhitespace(container, nameof(container), "Requires a non-blank container name of the Cosmos SQL storage to track a Cosmos SQL dependency");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the Cosmos SQL operation");

context = context ?? new Dictionary<string, object>();

LogCosmosSqlDependency(logger, accountName, database, container, isSuccessful, startTime, duration, dependencyId: null, context);
}

Expand Down Expand Up @@ -173,7 +171,7 @@ public static partial class ILoggerExtensions
Guard.NotNullOrWhitespace(container, nameof(container), "Requires a non-blank container name of the Cosmos SQL storage to track a Cosmos SQL dependency");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the Cosmos SQL operation");

context = context ?? new Dictionary<string, object>();
context = context is null ? new Dictionary<string, object>() : new Dictionary<string, object>(context);
string data = $"{database}/{container}";

logger.LogWarning(MessageFormats.DependencyFormat, new DependencyLogEntry(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,8 +636,6 @@ public static partial class ILoggerExtensions
Guard.NotNull(dependencyData, nameof(dependencyData), "Requires custom dependency data when tracking the custom dependency");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the dependency operation");

context = context ?? new Dictionary<string, object>();

LogDependency(logger, dependencyType, dependencyData, targetName, isSuccessful, dependencyName, startTime, duration, dependencyId: null, context);
}

Expand Down Expand Up @@ -676,7 +674,7 @@ public static partial class ILoggerExtensions
Guard.NotNull(dependencyData, nameof(dependencyData), "Requires custom dependency data when tracking the custom dependency");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the dependency operation");

context = context ?? new Dictionary<string, object>();
context = context is null ? new Dictionary<string, object>() : new Dictionary<string, object>(context);

logger.LogWarning(MessageFormats.DependencyFormat, new DependencyLogEntry(
dependencyType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static void LogCustomEvent(this ILogger logger, string name, Dictionary<s
Guard.NotNull(logger, nameof(logger), "Requires a logger instance to track telemetry");
Guard.NotNullOrWhitespace(name, nameof(name), "Requires a non-blank event name to track an custom event");

context = context ?? new Dictionary<string, object>();
context = context is null ? new Dictionary<string, object>() : new Dictionary<string, object>(context);

logger.LogWarning(MessageFormats.EventFormat, new EventLogEntry(name, context));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ public static partial class ILoggerExtensions
Guard.NotNullOrWhitespace(eventHubName, nameof(eventHubName), "Requires a non-blank Azure Event Hub name to track an Azure Event Hub dependency");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the Azure Events Hubs operation");

context = context ?? new Dictionary<string, object>();

LogEventHubsDependency(logger, namespaceName, eventHubName, isSuccessful, startTime, duration, dependencyId: null, context);
}

Expand Down Expand Up @@ -158,7 +156,7 @@ public static partial class ILoggerExtensions
Guard.NotNullOrWhitespace(eventHubName, nameof(eventHubName), "Requires a non-blank Azure Event Hub name to track an Azure Event Hub dependency");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the Azure Events Hubs operation");

context = context ?? new Dictionary<string, object>();
context = context is null ? new Dictionary<string, object>() : new Dictionary<string, object>(context);

logger.LogWarning(MessageFormats.DependencyFormat, new DependencyLogEntry(
dependencyType: "Azure Event Hubs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public static partial class ILoggerExtensions
operationName = ContextProperties.RequestTracking.EventHubs.DefaultOperationName;
}

context = context ?? new Dictionary<string, object>();
context = context is null ? new Dictionary<string, object>() : new Dictionary<string, object>(context);
context[ContextProperties.RequestTracking.EventHubs.Namespace] = eventHubsNamespace;
context[ContextProperties.RequestTracking.EventHubs.ConsumerGroup] = consumerGroup;
context[ContextProperties.RequestTracking.EventHubs.Name] = eventHubsName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static partial class ILoggerExtensions
operationName = ContextProperties.RequestTracking.DefaultOperationName;
}

context = context ?? new Dictionary<string, object>();
context = context is null ? new Dictionary<string, object>() : new Dictionary<string, object>(context);

logger.LogWarning(MessageFormats.RequestFormat, RequestLogEntry.CreateForCustomRequest(requestSource, operationName, isSuccessful, duration, startTime, context));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ public static partial class ILoggerExtensions
Guard.NotNullOrWhitespace(entityName, nameof(entityName), "Requires a non-blank Azure Service Bus entity name to track an Azure Service Bus dependency");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the Azure Service Bus operation");

context = context ?? new Dictionary<string, object>();
context = context is null ? new Dictionary<string, object>() : new Dictionary<string, object>(context);
context[ContextProperties.DependencyTracking.ServiceBus.EntityType] = entityType;
context[ContextProperties.DependencyTracking.ServiceBus.Endpoint] = serviceBusNamespaceEndpoint;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static partial class ILoggerExtensions
Guard.NotNullOrWhitespace(subscriptionName, nameof(subscriptionName), "Requires an Azure Service Bus subscription name on the to track the topic request");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the Azure Service Bus topic request operation");

context = context ?? new Dictionary<string, object>();
context = context is null ? new Dictionary<string, object>() : new Dictionary<string, object>(context);
context[ContextProperties.RequestTracking.ServiceBus.Topic.SubscriptionName] = subscriptionName;

LogServiceBusRequestWithSuffix(logger, serviceBusNamespace, serviceBusNamespaceSuffix, topicName, operationName, isSuccessful, duration, startTime, ServiceBusEntityType.Topic, context);
Expand Down Expand Up @@ -159,7 +159,7 @@ public static partial class ILoggerExtensions
Guard.NotNullOrWhitespace(subscriptionName, nameof(subscriptionName), "Requires an Azure Service Bus subscription name on the to track the topic request");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the Azure Service Bus topic request operation");

context = context ?? new Dictionary<string, object>();
context = context is null ? new Dictionary<string, object>() : new Dictionary<string, object>(context);
context[ContextProperties.RequestTracking.ServiceBus.Topic.SubscriptionName] = subscriptionName;

LogServiceBusRequest(logger, serviceBusNamespace, topicName, operationName, isSuccessful, duration, startTime, ServiceBusEntityType.Topic, context);
Expand Down Expand Up @@ -431,7 +431,7 @@ public static partial class ILoggerExtensions
operationName = ContextProperties.RequestTracking.ServiceBus.DefaultOperationName;
}

context = context ?? new Dictionary<string, object>();
context = context is null ? new Dictionary<string, object>() : new Dictionary<string, object>(context);
context[ContextProperties.RequestTracking.ServiceBus.Endpoint] = serviceBusNamespace;
context[ContextProperties.RequestTracking.ServiceBus.EntityName] = entityName;
context[ContextProperties.RequestTracking.ServiceBus.EntityType] = entityType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Arcus.Observability.Telemetry.Core;
using Arcus.Observability.Telemetry.Core.Logging;
using Bogus;
Expand Down Expand Up @@ -307,5 +308,25 @@ public void LogAzureSearchDependencyWithDependencyIdWithDurationMeasurement_With
Assert.ThrowsAny<ArgumentException>(
() => logger.LogAzureSearchDependency(searchServiceName, operationName, isSuccessful, measurement: (DurationMeasurement)null, dependencyId));
}

[Fact]
public void LogAzureSearchDependency_WithContext_DoesNotAlterContext()
{
// Arrange
var logger = new TestLogger();
string searchServiceName = _bogusGenerator.Commerce.ProductName();
string operationName = _bogusGenerator.Commerce.ProductName();
bool isSuccessful = _bogusGenerator.PickRandom(true, false);
string dependencyId = _bogusGenerator.Lorem.Word();
var startTime = _bogusGenerator.Date.RecentOffset();
var duration = _bogusGenerator.Date.Timespan();
var context = new Dictionary<string, object>();

// Act
logger.LogAzureSearchDependency(searchServiceName, operationName, isSuccessful, startTime, duration, dependencyId, context);

// Assert
Assert.Empty(context);
}
}
}
Loading

0 comments on commit 06ba21d

Please sign in to comment.