Skip to content

Commit

Permalink
fix: do not alter telemetry ctx pt.1
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnmoreels committed Oct 20, 2023
1 parent 14e17ad commit e92c7ba
Show file tree
Hide file tree
Showing 20 changed files with 373 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static void LogRequest(
Guard.NotNull(request, nameof(request), "Requires a HTTP request instance to track a HTTP request");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the request operation");

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(request.Method,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public static void LogAzureKeyVaultDependency(
Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name for the Azure Key Vault");
Guard.NotNull(measurement, nameof(measurement), "Requires a dependency measurement instance to track the latency of the Azure Key Vault when tracking an Azure Key Vault dependency");

context = context ?? new Dictionary<string, object>();
LogAzureKeyVaultDependency(logger, vaultUri, secretName, isSuccessful, measurement, dependencyId: null, context);
}

Expand Down Expand Up @@ -135,7 +134,6 @@ public static void LogAzureKeyVaultDependency(
Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name for the Azure Key Vault");
Guard.NotNull(measurement, nameof(measurement), "Requires a dependency measurement instance to track the latency of the Azure Key Vault when tracking an Azure Key Vault dependency");

context = context ?? new Dictionary<string, object>();
LogAzureKeyVaultDependency(logger, vaultUri, secretName, isSuccessful, measurement.StartTime, measurement.Elapsed, dependencyId, context);
}

Expand Down Expand Up @@ -168,7 +166,6 @@ public static void LogAzureKeyVaultDependency(
Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name for the Azure Key Vault");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the Azure Key Vault operation");

context = context ?? new Dictionary<string, object>();
LogAzureKeyVaultDependency(logger, vaultUri, secretName, isSuccessful, startTime, duration, dependencyId: null, context);
}

Expand Down Expand Up @@ -203,7 +200,7 @@ public static void LogAzureKeyVaultDependency(
Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name for the Azure Key Vault");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the Azure Key Vault 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 key vault",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ public static void LogBlobStorageDependency(
Guard.NotNullOrWhitespace(containerName, nameof(containerName), "Requires a non-blank container name in the Azure BLob storage resource to track an Azure Blob storage dependency");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the Azure Blob storage operation");

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

LogBlobStorageDependency(logger, accountName, containerName, isSuccessful, startTime, duration, dependencyId: null, context);
}

Expand Down Expand Up @@ -158,7 +156,7 @@ public static void LogBlobStorageDependency(
Guard.NotNullOrWhitespace(containerName, nameof(containerName), "Requires a non-blank container name in the Azure BLob storage resource to track an Azure Blob storage dependency");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the Azure Blob storage operation");

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

string dependencyName = $"{accountName}/{containerName}";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static void LogSecurityEvent(this ILogger logger, string name, Dictionary
Guard.NotNull(logger, nameof(logger), "Requires a logger instance to track telemetry");
Guard.NotNullOrWhitespace(name, nameof(name), "Requires a non-blank name of the event to track an security event");

context = context ?? new Dictionary<string, object>();
context = context is null ? new Dictionary<string, object>() : new Dictionary<string, object>(context);
context["EventType"] = "Security";

LogCustomEvent(logger, name, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ public static void LogHttpDependency(
Guard.For(() => request.RequestUri is null, new ArgumentException("Requires a HTTP request URI to track a HTTP dependency", nameof(request)));
Guard.For(() => request.Method is null, new ArgumentException("Requires a HTTP request method to track a HTTP dependency", nameof(request)));

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

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

Expand Down Expand Up @@ -220,7 +218,7 @@ public static void LogHttpDependency(
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);

Uri requestUri = request.RequestUri;
string targetName = requestUri.Host;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ public static void LogIotHubDependency(
Guard.NotNullOrWhitespace(iotHubName, nameof(iotHubName), "Requires a non-blank resource name of the IoT Hub resource to track a IoT Hub dependency");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the IoT Hub operation");

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

LogIotHubDependency(logger, iotHubName, isSuccessful, startTime, duration, dependencyId: null, context);
}

Expand Down Expand Up @@ -142,8 +140,6 @@ public static void LogIotHubDependencyWithConnectionString(
Guard.NotNullOrWhitespace(iotHubConnectionString, nameof(iotHubConnectionString), "Requires an IoT Hub connection string to retrieve the IoT host name to track the IoT Hub dependency");
Guard.NotNull(measurement, nameof(measurement), "Requires an measurement instance to measure the duration of interaction with the IoT Hub dependency");

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

LogIotHubDependencyWithConnectionString(logger, iotHubConnectionString, isSuccessful, measurement.StartTime, measurement.Elapsed, dependencyId, context);
}

Expand Down Expand Up @@ -172,8 +168,6 @@ public static void LogIotHubDependencyWithConnectionString(
Guard.NotNull(logger, nameof(logger), "Requires an logger instance to track the IoT Hub dependency");
Guard.NotNullOrWhitespace(iotHubConnectionString, nameof(iotHubConnectionString), "Requires an IoT Hub connection string to retrieve the IoT host name to track the IoT Hub dependency");

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

var result = IotHubConnectionStringParser.Parse(iotHubConnectionString);
LogIotHubDependency(logger, iotHubName: result.HostName, isSuccessful, startTime, duration, dependencyId, context);
}
Expand Down Expand Up @@ -204,7 +198,7 @@ public static void LogIotHubDependency(
Guard.NotNullOrWhitespace(iotHubName, nameof(iotHubName), "Requires a non-blank resource name of the IoT Hub resource to track a IoT Hub dependency");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the IoT Hub 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 IoT Hub",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public static void LogCustomMetric(this ILogger logger, string name, double valu
Guard.NotNull(logger, nameof(logger), "Requires a logger instance to track telemetry");
Guard.NotNullOrWhitespace(name, nameof(name), "Requires a non-blank name to track a metric");

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

LogCustomMetric(logger, name, value, DateTimeOffset.UtcNow, context);
}

Expand Down Expand Up @@ -67,7 +65,7 @@ public static void LogCustomMetric(this ILogger logger, string name, double valu
Guard.NotNull(logger, nameof(logger), "Requires a logger instance to track telemetry");
Guard.NotNullOrWhitespace(name, nameof(name), "Requires a non-blank name to track a metric");

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

logger.LogWarning(MessageFormats.MetricFormat, new MetricLogEntry(name, value, timestamp, context));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,6 @@ public static void LogRequest(
Guard.NotNull(request, nameof(request), "Requires a HTTP request instance to track a HTTP request");
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, responseStatusCode, measurement.StartTime, measurement.Elapsed, context);
}

Expand Down Expand Up @@ -340,8 +338,6 @@ public static void LogRequest(
Guard.NotNull(request, nameof(request), "Requires a HTTP request instance to track a HTTP request");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the request operation");

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

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

Expand Down Expand Up @@ -419,8 +415,6 @@ public static void LogRequest(
Guard.NotNull(request, nameof(request), "Requires a HTTP request instance to track a HTTP request");
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, responseStatusCode, operationName, measurement.StartTime, measurement.Elapsed, context);
}

Expand Down Expand Up @@ -457,7 +451,7 @@ public static void LogRequest(
Guard.NotNull(request, nameof(request), "Requires a HTTP request instance to track a HTTP request");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the request operation");

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 @@ -307,8 +307,6 @@ public static void LogSqlDependency(
Guard.NotNullOrWhitespace(databaseName, nameof(databaseName), "Requires a non-blank SQL database name to track a SQL dependency");
Guard.NotNull(measurement, nameof(measurement), "Requires a dependency measurement instance to measure the latency of the SQL storage when tracking an SQL dependency");

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

LogSqlDependency(logger, serverName, databaseName, sqlCommand, operationName, isSuccessful, measurement.StartTime, measurement.Elapsed, dependencyId, context);
}

Expand Down Expand Up @@ -345,7 +343,7 @@ public static void LogSqlDependency(
Guard.NotNullOrWhitespace(databaseName, nameof(databaseName), "Requires a non-blank SQL database name to track a SQL dependency");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the SQL dependency operation");

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

logger.LogWarning(MessageFormats.SqlDependencyFormat, new DependencyLogEntry(
dependencyType: "Sql",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ public static void LogTableStorageDependency(
Guard.NotNullOrWhitespace(tableName, nameof(tableName), "Requires a non-blank table name in the Azure Table storage resource to track an Azure Table storage dependency");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the Azure Table storage operation");

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

LogTableStorageDependency(logger, accountName, tableName, isSuccessful, startTime, duration, dependencyId: null, context);
}

Expand Down Expand Up @@ -158,7 +156,7 @@ public static void LogTableStorageDependency(
Guard.NotNullOrWhitespace(tableName, nameof(tableName), "Requires a non-blank table name in the Azure Table storage resource to track an Azure Table storage dependency");
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration of the Azure Table storage operation");

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

string dependencyName = $"{accountName}/{tableName}";

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 @@ -196,7 +197,7 @@ public void LogBlobStorageDependencyWithDurationMeasurement_WithoutAccountName_F
var measurement = DurationMeasurement.Start();
measurement.Dispose();

// Act /
// Act / Assert
Assert.ThrowsAny<ArgumentException>(
() => logger.LogBlobStorageDependency(accountName, containerName, isSuccessful, measurement));
}
Expand All @@ -214,7 +215,7 @@ public void LogBlobStorageDependencyWithDurationMeasurementWithDependencyId_With
measurement.Dispose();
string dependencyId = _bogusGenerator.Lorem.Word();

// Act /
// Act / Assert
Assert.ThrowsAny<ArgumentException>(
() => logger.LogBlobStorageDependency(accountName, containerName, isSuccessful, measurement, dependencyId));
}
Expand All @@ -231,7 +232,7 @@ public void LogBlobStorageDependencyWithDurationMeasurement_WithoutContainerName
var measurement = DurationMeasurement.Start();
measurement.Dispose();

// Act /
// Act / Assert
Assert.ThrowsAny<ArgumentException>(
() => logger.LogBlobStorageDependency(accountName, containerName, isSuccessful, measurement));
}
Expand All @@ -249,7 +250,7 @@ public void LogBlobStorageDependencyWithDurationMeasurementWithDependencyId_With
measurement.Dispose();
string dependencyId = _bogusGenerator.Lorem.Word();

// Act /
// Act / Assert
Assert.ThrowsAny<ArgumentException>(
() => logger.LogBlobStorageDependency(accountName, containerName, isSuccessful, measurement, dependencyId));
}
Expand All @@ -263,7 +264,7 @@ public void LogBlobStorageDependencyWithDurationMeasurement_WithoutMeasurement_F
string containerName = _bogusGenerator.Finance.AccountName();
bool isSuccessful = _bogusGenerator.Random.Bool();

// Act /
// Act / Assert
Assert.ThrowsAny<ArgumentException>(
() => logger.LogBlobStorageDependency(accountName, containerName, isSuccessful, measurement: (DurationMeasurement)null));
}
Expand All @@ -278,9 +279,48 @@ public void LogBlobStorageDependencyWithDurationMeasurementWithDependencyId_With
bool isSuccessful = _bogusGenerator.Random.Bool();
string dependencyId = _bogusGenerator.Lorem.Word();

// Act /
// Act / Assert
Assert.ThrowsAny<ArgumentException>(
() => logger.LogBlobStorageDependency(accountName, containerName, isSuccessful, measurement: null, dependencyId));
}

[Fact]
public void LogBlobStorageDependencyWithDurationMeasurement_WithContext_DoesNotAlterContext()
{
// Arrange
var logger = new TestLogger();
string accountName = _bogusGenerator.Finance.AccountName();
string containerName = _bogusGenerator.Finance.AccountName();
bool isSuccessful = _bogusGenerator.Random.Bool();
string dependencyId = _bogusGenerator.Lorem.Word();
using var measurement = DurationMeasurement.Start();
var context = new Dictionary<string, object>();

// Act
logger.LogBlobStorageDependency(accountName, containerName, isSuccessful, measurement, dependencyId, context);

// Assert
Assert.Empty(context);
}

[Fact]
public void LogBlobStorageDependencyWithStartDuration_WithContext_DoesNotAlterContext()
{
// Arrange
var logger = new TestLogger();
string accountName = _bogusGenerator.Finance.AccountName();
string containerName = _bogusGenerator.Finance.AccountName();
bool isSuccessful = _bogusGenerator.Random.Bool();
string dependencyId = _bogusGenerator.Lorem.Word();
var startTime = _bogusGenerator.Date.RecentOffset();
var duration = _bogusGenerator.Date.Timespan();
var context = new Dictionary<string, object>();

// Act
logger.LogBlobStorageDependency(accountName, containerName, isSuccessful, startTime, duration, dependencyId, context);

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

0 comments on commit e92c7ba

Please sign in to comment.