-
Notifications
You must be signed in to change notification settings - Fork 518
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Metrics] New components to track HTTP failures and exceptions. (#3928)
* New components to track http failures and generic errors. * Emitting metrics signals for every failed request. * Emiting metrics when exceptions are logged. * Cover one more scenario = errorslogger with no exceptions * Fix use of diferrent counters.
- Loading branch information
Showing
14 changed files
with
295 additions
and
28 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/Microsoft.Health.Fhir.Core/Logging/Metrics/DefaultFailureMetricHandler.cs
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System.Collections.Generic; | ||
using System.Diagnostics.Metrics; | ||
using EnsureThat; | ||
|
||
namespace Microsoft.Health.Fhir.Core.Logging.Metrics | ||
{ | ||
public sealed class DefaultFailureMetricHandler : BaseMeterMetricHandler, IFailureMetricHandler | ||
{ | ||
private readonly Counter<long> _counterExceptions; | ||
private readonly Counter<long> _counterHttpFailures; | ||
|
||
public DefaultFailureMetricHandler(IMeterFactory meterFactory) | ||
: base(meterFactory) | ||
{ | ||
_counterExceptions = MetricMeter.CreateCounter<long>("Failures.Exceptions"); | ||
_counterHttpFailures = MetricMeter.CreateCounter<long>("Failures.HttpFailures"); | ||
} | ||
|
||
public void EmitException(IExceptionMetricNotification notification) | ||
{ | ||
EnsureArg.IsNotNull(notification, nameof(notification)); | ||
|
||
_counterExceptions.Add( | ||
1, | ||
KeyValuePair.Create<string, object>("OperationName", notification.OperationName), | ||
KeyValuePair.Create<string, object>("Severity", notification.Severity), | ||
KeyValuePair.Create<string, object>("ExceptionType", notification.ExceptionType)); | ||
} | ||
|
||
public void EmitHttpFailure(IHttpFailureMetricNotification notification) | ||
{ | ||
EnsureArg.IsNotNull(notification, nameof(notification)); | ||
|
||
_counterHttpFailures.Add( | ||
1, | ||
KeyValuePair.Create<string, object>("OperationName", notification.OperationName)); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Microsoft.Health.Fhir.Core/Logging/Metrics/ExceptionMetricNotification.cs
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
namespace Microsoft.Health.Fhir.Core.Logging.Metrics | ||
{ | ||
public sealed class ExceptionMetricNotification : IExceptionMetricNotification | ||
{ | ||
public string OperationName { get; set; } | ||
|
||
public string ExceptionType { get; set; } | ||
|
||
public string Severity { get; set; } | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Microsoft.Health.Fhir.Core/Logging/Metrics/HttpErrorMetricNotification.cs
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
namespace Microsoft.Health.Fhir.Core.Logging.Metrics | ||
{ | ||
public sealed class HttpErrorMetricNotification : IHttpFailureMetricNotification | ||
{ | ||
public string OperationName { get; set; } | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Microsoft.Health.Fhir.Core/Logging/Metrics/IExceptionMetricNotification.cs
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
namespace Microsoft.Health.Fhir.Core.Logging.Metrics | ||
{ | ||
public interface IExceptionMetricNotification | ||
{ | ||
string OperationName { get; set; } | ||
|
||
string ExceptionType { get; set; } | ||
|
||
string Severity { get; set; } | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Microsoft.Health.Fhir.Core/Logging/Metrics/IFailureMetricHandler.cs
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
namespace Microsoft.Health.Fhir.Core.Logging.Metrics | ||
{ | ||
public interface IFailureMetricHandler | ||
{ | ||
void EmitHttpFailure(IHttpFailureMetricNotification notification); | ||
|
||
void EmitException(IExceptionMetricNotification notification); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Microsoft.Health.Fhir.Core/Logging/Metrics/IHttpFailureMetricNotification.cs
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
namespace Microsoft.Health.Fhir.Core.Logging.Metrics | ||
{ | ||
public interface IHttpFailureMetricNotification | ||
{ | ||
string OperationName { get; set; } | ||
} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
src/Microsoft.Health.Fhir.Shared.Api/Extensions/HttpRequestExtension.cs
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Health.Fhir.Core.Features.Telemetry; | ||
|
||
namespace Microsoft.Health.Fhir.Api.Extensions | ||
{ | ||
public static class HttpRequestExtension | ||
{ | ||
public static string GetOperationName(this HttpRequest request, bool includeRouteValues = true) | ||
{ | ||
if (request != null) | ||
{ | ||
var name = request.Path.Value; | ||
if (request.RouteValues != null | ||
&& request.RouteValues.TryGetValue(KnownHttpRequestProperties.RouteValueAction, out var action) | ||
&& request.RouteValues.TryGetValue(KnownHttpRequestProperties.RouteValueController, out var controller)) | ||
{ | ||
name = $"{controller}/{action}"; | ||
|
||
if (includeRouteValues) | ||
{ | ||
var parameterArray = request.RouteValues.Keys?.Where( | ||
k => k.Contains(KnownHttpRequestProperties.RouteValueParameterSuffix, StringComparison.OrdinalIgnoreCase)) | ||
.OrderBy(k => k, StringComparer.OrdinalIgnoreCase) | ||
.ToArray(); | ||
if (parameterArray != null && parameterArray.Any()) | ||
{ | ||
name += $" [{string.Join("/", parameterArray)}]"; | ||
} | ||
} | ||
} | ||
|
||
return $"{request.Method} {name}".Trim(); | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.