Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: LogHttpDependency() should allow status codes within 100-599 range #515

Merged
merged 5 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static class ILoggerHttpDependencyExtensions
/// <param name="context">The context that provides more insights on the dependency that was measured.</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="logger"/>, <paramref name="request"/>, or <paramref name="measurement"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">
/// Thrown when the <paramref name="request"/> doesn't have a request URI or HTTP method, the <paramref name="statusCode"/> is outside the bounds of the enumeration.
/// Thrown when the <paramref name="request"/> doesn't have a request URI or HTTP method, the <paramref name="statusCode"/> is outside 100-599 range inclusively.
/// </exception>
public static void LogHttpDependency(
this ILogger logger,
Expand All @@ -41,8 +41,6 @@ public static void LogHttpDependency(
Guard.NotNull(logger, nameof(logger), "Requires a logger instance to track telemetry");
Guard.NotNull(request, nameof(request), "Requires a HTTP request message to track a HTTP dependency");
Guard.NotNull(measurement, nameof(measurement), "Requires a dependency measurement instance to track the latency of the HTTP communication when tracking a HTTP dependency");
Guard.For(() => !Enum.IsDefined(typeof(HttpStatusCode), statusCode),
new ArgumentException("Requires a response HTTP status code that's within the bound of the enumeration to track a HTTP dependency"));

LogHttpDependency(logger, request, statusCode, measurement.StartTime, measurement.Elapsed, context);
}
Expand All @@ -58,7 +56,7 @@ public static void LogHttpDependency(
/// <param name="context">The context that provides more insights on the dependency that was measured.</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="logger"/>, <paramref name="request"/>, or <paramref name="measurement"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">
/// Thrown when the <paramref name="request"/> doesn't have a request URI or HTTP method, the <paramref name="statusCode"/> is outside the bounds of the enumeration.
/// Thrown when the <paramref name="request"/> doesn't have a request URI or HTTP method, the <paramref name="statusCode"/> is outside 100-599 range inclusively.
/// </exception>
public static void LogHttpDependency(
this ILogger logger,
Expand All @@ -71,8 +69,34 @@ public static void LogHttpDependency(
Guard.NotNull(logger, nameof(logger), "Requires a logger instance to track telemetry");
Guard.NotNull(request, nameof(request), "Requires a HTTP request message to track a HTTP dependency");
Guard.NotNull(measurement, nameof(measurement), "Requires a dependency measurement instance to track the latency of the HTTP communication when tracking a HTTP dependency");
Guard.For(() => !Enum.IsDefined(typeof(HttpStatusCode), statusCode),
new ArgumentException("Requires a response HTTP status code that's within the bound of the enumeration to track a HTTP dependency"));

LogHttpDependency(logger, request, statusCode, measurement.StartTime, measurement.Elapsed, dependencyId, context);
}

/// <summary>
/// Logs an HTTP dependency.
/// </summary>
/// <param name="logger">The logger to track the telemetry.</param>
/// <param name="request">The request that started the HTTP communication.</param>
/// <param name="statusCode">The status code that was returned by the service for this HTTP communication.</param>
/// <param name="measurement">The measurement of the duration to call the dependency.</param>
/// <param name="dependencyId">The ID of the dependency to link as parent ID.</param>
/// <param name="context">The context that provides more insights on the dependency that was measured.</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="logger"/>, <paramref name="request"/>, or <paramref name="measurement"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">
/// Thrown when the <paramref name="request"/> doesn't have a request URI or HTTP method, the <paramref name="statusCode"/> is outside 100-599 range inclusively.
/// </exception>
public static void LogHttpDependency(
this ILogger logger,
HttpRequest request,
int statusCode,
DurationMeasurement measurement,
string dependencyId,
Dictionary<string, object> context = null)
{
Guard.NotNull(logger, nameof(logger), "Requires a logger instance to track telemetry");
Guard.NotNull(request, nameof(request), "Requires a HTTP request message to track a HTTP dependency");
Guard.NotNull(measurement, nameof(measurement), "Requires a dependency measurement instance to track the latency of the HTTP communication when tracking a HTTP dependency");

LogHttpDependency(logger, request, statusCode, measurement.StartTime, measurement.Elapsed, dependencyId, context);
}
Expand All @@ -89,7 +113,7 @@ public static void LogHttpDependency(
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="logger"/> or <paramref name="request"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the <paramref name="duration"/> is a negative time range.</exception>
/// <exception cref="ArgumentException">
/// Thrown when the <paramref name="request"/> doesn't have a request URI or HTTP method, the <paramref name="statusCode"/> is outside the bounds of the enumeration.
/// Thrown when the <paramref name="request"/> doesn't have a request URI or HTTP method, the <paramref name="statusCode"/> is outside 100-599 range inclusively.
/// </exception>
public static void LogHttpDependency(
this ILogger logger,
Expand All @@ -102,8 +126,6 @@ public static void LogHttpDependency(
Guard.NotNull(logger, nameof(logger), "Requires a logger instance to track telemetry");
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");
Guard.For(() => !Enum.IsDefined(typeof(HttpStatusCode), statusCode),
new ArgumentException("Requires a response HTTP status code that's within the bound of the enumeration to track a HTTP dependency"));

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

Expand All @@ -123,7 +145,7 @@ public static void LogHttpDependency(
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="logger"/> or <paramref name="request"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the <paramref name="duration"/> is a negative time range.</exception>
/// <exception cref="ArgumentException">
/// Thrown when the <paramref name="request"/> doesn't have a request URI or HTTP method, the <paramref name="statusCode"/> is outside the bounds of the enumeration.
/// Thrown when the <paramref name="request"/> doesn't have a request URI or HTTP method, the <paramref name="statusCode"/> is outside 100-599 range inclusively.
/// </exception>
public static void LogHttpDependency(
this ILogger logger,
Expand All @@ -137,16 +159,47 @@ public static void LogHttpDependency(
Guard.NotNull(logger, nameof(logger), "Requires a logger instance to track telemetry");
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");
Guard.For(() => !Enum.IsDefined(typeof(HttpStatusCode), statusCode),
new ArgumentException("Requires a response HTTP status code that's within the bound of the enumeration to track a HTTP dependency"));

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

/// <summary>
/// Logs an HTTP dependency
/// </summary>
/// <param name="logger">The logger to track the telemetry.</param>
/// <param name="request">Request that started the HTTP communication</param>
/// <param name="statusCode">Status code that was returned by the service for this HTTP communication</param>
/// <param name="startTime">Point in time when the interaction with the HTTP dependency was started</param>
/// <param name="duration">Duration of the operation</param>
/// <param name="dependencyId">The ID of the dependency to link as parent ID.</param>
/// <param name="context">Context that provides more insights on the dependency that was measured</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="logger"/> or <paramref name="request"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the <paramref name="duration"/> is a negative time range.</exception>
/// <exception cref="ArgumentException">
/// Thrown when the <paramref name="request"/> doesn't have a request URI or HTTP method, the <paramref name="statusCode"/> is outside 100-599 range inclusively.
/// </exception>
public static void LogHttpDependency(
stijnmoreels marked this conversation as resolved.
Show resolved Hide resolved
this ILogger logger,
HttpRequest request,
int statusCode,
DateTimeOffset startTime,
TimeSpan duration,
string dependencyId,
Dictionary<string, object> context = null)
{
Guard.NotNull(logger, nameof(logger), "Requires a logger instance to track telemetry");
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");
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>();

string requestUri = request.Path;
string targetName = request.Host.Host;
string requestMethod = request.Method;
string dependencyName = $"{requestMethod} {requestUri}";
bool isSuccessful = (int)statusCode >= 200 && (int)statusCode < 300;
bool isSuccessful = statusCode >= 200 && statusCode < 300;

logger.LogWarning(MessageFormats.HttpDependencyFormat, new DependencyLogEntry(
dependencyType: "Http",
Expand All @@ -156,7 +209,7 @@ public static void LogHttpDependency(
duration: duration,
startTime: startTime,
dependencyId: dependencyId,
resultCode: (int)statusCode,
resultCode: statusCode,
isSuccessful: isSuccessful,
context: context));
}
Expand Down
Loading