-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add request.path if route description is missing. (#24)
* Add path if route description is missing * Add TryGetMeasuredOperationLatency
- Loading branch information
1 parent
026b600
commit 0453d1b
Showing
7 changed files
with
168 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,46 @@ | ||
namespace ServiceLevelIndicators; | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.Features; | ||
|
||
public static class HttpContextExtensions | ||
{ | ||
/// <summary> | ||
/// Gets the MeasuredOperationLatency from the IServiceLevelIndicatorFeature. | ||
/// The method will throw an exception if the route is not configured to emit SLI metrics. | ||
/// </summary> | ||
/// <param name="context"></param> | ||
/// <returns>MeasuredOperationLatency for the current API method.</returns> | ||
/// <exception cref="ArgumentNullException"></exception> | ||
/// <exception cref="InvalidOperationException">If the route does not emit SLI information and therefore MeasuredOperationLatency does not exist.</exception> | ||
public static MeasuredOperationLatency GetMeasuredOperationLatency(this HttpContext context) | ||
{ | ||
if (context == null) | ||
throw new ArgumentNullException(nameof(context)); | ||
|
||
return context.Features.GetRequiredFeature<IServiceLevelIndicatorFeature>().MeasuredOperationLatency; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the MeasuredOperationLatency from the IServiceLevelIndicatorFeature. | ||
/// </summary> | ||
/// <param name="context"></param> | ||
/// <param name="measuredOperationLatency"></param> | ||
/// <returns>true if MeasuredOperationLatency exists.</returns> | ||
/// <exception cref="ArgumentNullException"></exception> | ||
public static bool TryGetMeasuredOperationLatency(this HttpContext context, [MaybeNullWhen(false)] out MeasuredOperationLatency measuredOperationLatency) | ||
{ | ||
if (context == null) | ||
throw new ArgumentNullException(nameof(context)); | ||
|
||
if (context.Features.Get<IServiceLevelIndicatorFeature>() is IServiceLevelIndicatorFeature feature) | ||
{ | ||
measuredOperationLatency = feature.MeasuredOperationLatency; | ||
return true; | ||
} | ||
|
||
measuredOperationLatency = null; | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters