Skip to content

Commit

Permalink
refactor: Consolidate creation of error metric (#143)
Browse files Browse the repository at this point in the history
* refactor error metric

* addressing PR comments

* update existing custom exceptions to use IomtTelemetryFormattableException
  • Loading branch information
pallar-ms authored Nov 11, 2021
1 parent 729e2c3 commit 19ad97b
Show file tree
Hide file tree
Showing 16 changed files with 102 additions and 193 deletions.
13 changes: 2 additions & 11 deletions src/console/MeasurementCollectionToFhir/Processor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,8 @@ bool ExceptionRetryableFilter(Exception ee)
private static void TrackExceptionMetric(Exception exception, ITelemetryLogger logger)
{
var type = exception.GetType().ToString();
var ToMetric = new Metric(
type,
new Dictionary<string, object>
{
{ DimensionNames.Name, type },
{ DimensionNames.Category, Category.Errors },
{ DimensionNames.ErrorType, ErrorType.GeneralError },
{ DimensionNames.ErrorSeverity, ErrorSeverity.Warning },
{ DimensionNames.Operation, ConnectorOperation.FHIRConversion},
});
logger.LogMetric(ToMetric, 1);
var metric = type.ToErrorMetric(ConnectorOperation.FHIRConversion, ErrorType.GeneralError, ErrorSeverity.Warning);
logger.LogMetric(metric, 1);
}
}
}
14 changes: 2 additions & 12 deletions src/console/Normalize/Processor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
using Microsoft.Health.Logging.Telemetry;
using Polly;
using static Microsoft.Azure.EventHubs.EventData;
using AzureMessagingEventHubs = Azure.Messaging.EventHubs;

namespace Microsoft.Health.Fhir.Ingest.Console.Normalize
{
Expand Down Expand Up @@ -125,17 +124,8 @@ bool ExceptionRetryableFilter(Exception ee)
private static void TrackExceptionMetric(Exception exception, ITelemetryLogger logger)
{
var type = exception.GetType().ToString();
var ToMetric = new Metric(
type,
new Dictionary<string, object>
{
{ DimensionNames.Name, type },
{ DimensionNames.Category, Category.Errors },
{ DimensionNames.ErrorType, ErrorType.DeviceMessageError },
{ DimensionNames.ErrorSeverity, ErrorSeverity.Warning },
{ DimensionNames.Operation, ConnectorOperation.Normalization},
});
logger.LogMetric(ToMetric, 1);
var metric = type.ToErrorMetric(ConnectorOperation.Normalization, ErrorType.DeviceMessageError, ErrorSeverity.Warning);
logger.LogMetric(metric, 1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
// -------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using EnsureThat;

namespace Microsoft.Health.Common.Telemetry
namespace Microsoft.Health.Common.Telemetry.Exceptions
{
public class IomtTelemetryFormattableException :
Exception,
ITelemetryFormattable
{
private readonly string _name;
private readonly string _operation;
private readonly string _operation = ConnectorOperation.Unknown;

public IomtTelemetryFormattableException()
{
Expand Down Expand Up @@ -47,16 +46,10 @@ public IomtTelemetryFormattableException(

public virtual string ErrSource => nameof(ErrorSource.Undefined);

public Metric ToMetric => new Metric(
EnsureArg.IsNotNullOrWhiteSpace(_name, nameof(_name)),
new Dictionary<string, object>
{
{ DimensionNames.Category, Category.Errors },
})
.AddDimension(DimensionNames.Name, _name)
.AddDimension(DimensionNames.Operation, _operation)
.AddDimension(DimensionNames.ErrorType, ErrType)
.AddDimension(DimensionNames.ErrorSeverity, ErrSeverity)
.AddDimension(DimensionNames.ErrorSource, ErrSource);
public virtual string ErrName => _name;

public virtual string Operation => _operation;

public Metric ToMetric => ErrName.ToErrorMetric(Operation, ErrType, ErrSeverity, ErrSource);
}
}
27 changes: 26 additions & 1 deletion src/lib/Microsoft.Health.Common/Telemetry/MetricExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// -------------------------------------------------------------------------------------------------

using System.Collections.Generic;
using EnsureThat;

namespace Microsoft.Health.Common.Telemetry
{
Expand All @@ -15,6 +16,10 @@ public static class MetricExtension

public static Metric CreateBaseMetric(this MetricDefinition iomtMetric, string category, string operation)
{
EnsureArg.IsNotNull(iomtMetric);
EnsureArg.IsNotNullOrWhiteSpace(category, nameof(category));
EnsureArg.IsNotNullOrWhiteSpace(operation, nameof(operation));

var metricName = iomtMetric.ToString();
return new Metric(
metricName,
Expand All @@ -28,13 +33,33 @@ public static Metric CreateBaseMetric(this MetricDefinition iomtMetric, string c

public static Metric AddDimension(this Metric metric, string dimensionName, string dimensionValue)
{
if (string.IsNullOrEmpty(dimensionValue))
if (string.IsNullOrWhiteSpace(dimensionValue))
{
return metric;
}

metric.Dimensions.Add(dimensionName, dimensionValue);
return metric;
}

public static Metric ToErrorMetric(this string metricName, string operation, string errorType, string errorSeverity, string errorSource = "", string errorName = "")
{
EnsureArg.IsNotNullOrWhiteSpace(metricName, nameof(metricName));
EnsureArg.IsNotNullOrWhiteSpace(operation, nameof(operation));
EnsureArg.IsNotNullOrWhiteSpace(errorType, nameof(errorType));
EnsureArg.IsNotNullOrWhiteSpace(errorSeverity, nameof(errorSeverity));

return new Metric(
metricName,
new Dictionary<string, object>
{
{ DimensionNames.Name, string.IsNullOrWhiteSpace(errorName) ? metricName : errorName },
{ DimensionNames.Category, Category.Errors },
{ DimensionNames.ErrorType, errorType },
{ DimensionNames.ErrorSeverity, errorSeverity },
{ DimensionNames.Operation, operation },
})
.AddDimension(DimensionNames.ErrorSource, errorSource);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System;
using Microsoft.Health.Common.Telemetry;
using Microsoft.Health.Common.Telemetry.Exceptions;

namespace Microsoft.Health.Events.Telemetry.Exceptions
{
Expand All @@ -26,6 +27,8 @@ public InvalidEventHubException(

public override string ErrType => _errorType;

public override string ErrSeverity => ErrorSeverity.Critical;

public override string ErrSource => nameof(ErrorSource.User);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System;
using Microsoft.Health.Common.Telemetry;
using Microsoft.Health.Common.Telemetry.Exceptions;

namespace Microsoft.Health.Events.Telemetry.Exceptions
{
Expand All @@ -28,6 +29,8 @@ public UnauthorizedAccessEventHubException(

public override string ErrType => _errorType;

public override string ErrSeverity => ErrorSeverity.Critical;

public override string ErrSource => nameof(ErrorSource.User);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// -------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using Microsoft.Health.Common.Telemetry;

namespace Microsoft.Health.Events.Telemetry
Expand Down Expand Up @@ -108,16 +107,7 @@ public static Metric EventTimestampLastProcessedPerPartition(string partitionId,
/// <param name="connectorStage">The stage of the connector</param>
public static Metric HandledException(string exceptionName, string connectorStage)
{
return new Metric(
exceptionName,
new Dictionary<string, object>
{
{ _nameDimension, exceptionName },
{ _categoryDimension, Category.Errors },
{ _errorTypeDimension, ErrorType.EventHubError },
{ _errorSeverityDimension, ErrorSeverity.Critical },
{ _operationDimension, connectorStage },
});
return exceptionName.ToErrorMetric(connectorStage, ErrorType.EventHubError, ErrorSeverity.Critical);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static async Task<TResource> ReadOneFromBundleWithContinuationAsync<TReso

if (throwOnMultipleFound && resourceCount > 1)
{
throw new MultipleResourceFoundException<TResource>();
throw new MultipleResourceFoundException<TResource>(resourceCount);
}

return resources.FirstOrDefault();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,12 @@
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// -------------------------------------------------------------------------------------------------

using System.Collections.Generic;
using Microsoft.Health.Common.Telemetry;

namespace Microsoft.Health.Extensions.Fhir.Telemetry.Metrics
{
public class FhirClientMetrics
{
private static string _nameDimension = DimensionNames.Name;
private static string _categoryDimension = DimensionNames.Category;
private static string _errorTypeDimension = DimensionNames.ErrorType;
private static string _errorSeverityDimension = DimensionNames.ErrorSeverity;
private static string _operationDimension = DimensionNames.Operation;

/// <summary>
/// A metric recorded when there is an error reading from or connecting with a FHIR server.
/// </summary>
Expand All @@ -24,16 +17,7 @@ public class FhirClientMetrics
/// <param name="connectorStage">The stage of the connector</param>
public static Metric HandledException(string exceptionName, string severity, string connectorStage)
{
return new Metric(
exceptionName,
new Dictionary<string, object>
{
{ _nameDimension, exceptionName },
{ _categoryDimension, Category.Errors },
{ _errorTypeDimension, ErrorType.GeneralError },
{ _errorSeverityDimension, severity },
{ _operationDimension, connectorStage },
});
return exceptionName.ToErrorMetric(connectorStage, ErrorType.GeneralError, severity);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
// -------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using Microsoft.Health.Common.Telemetry;
using Microsoft.Health.Common.Telemetry.Exceptions;

namespace Microsoft.Health.Extensions.Fhir
{
public class MultipleResourceFoundException<T> :
Exception,
ITelemetryFormattable
public class MultipleResourceFoundException<T> : IomtTelemetryFormattableException
{
public MultipleResourceFoundException(int resourceCount)
: base($"Multiple resources {resourceCount} of type {typeof(T)} found, expected one")
Expand All @@ -32,17 +30,10 @@ public MultipleResourceFoundException(string message, Exception innerException)
{
}

public string EventName => $"Multiple{typeof(T).Name}FoundException";

public Metric ToMetric => new Metric(
$"{EventName}",
new Dictionary<string, object>
{
{ DimensionNames.Name, $"{EventName}" },
{ DimensionNames.Category, Category.Errors },
{ DimensionNames.ErrorType, ErrorType.FHIRResourceError },
{ DimensionNames.ErrorSeverity, ErrorSeverity.Warning },
{ DimensionNames.Operation, ConnectorOperation.FHIRConversion },
});
public override string ErrName => $"Multiple{typeof(T).Name}FoundException";

public override string ErrType => ErrorType.FHIRResourceError;

public override string Operation => ConnectorOperation.FHIRConversion;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,13 @@
// -------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using Microsoft.Health.Common.Telemetry;
using Microsoft.Health.Common.Telemetry.Exceptions;

namespace Microsoft.Health.Fhir.Ingest.Template
{
public class TemplateNotFoundException :
Exception,
ITelemetryFormattable
public class TemplateNotFoundException : IomtTelemetryFormattableException
{
private static Metric _templateNotFound = new Metric(
"TemplateNotFoundException",
new Dictionary<string, object>
{
{ DimensionNames.Name, "TemplateNotFoundException" },
{ DimensionNames.Category, Category.Errors },
{ DimensionNames.ErrorType, ErrorType.GeneralError },
{ DimensionNames.ErrorSeverity, ErrorSeverity.Critical },
{ DimensionNames.Operation, ConnectorOperation.Unknown },
});

public TemplateNotFoundException(string message)
: base(message)
{
Expand All @@ -38,6 +25,12 @@ public TemplateNotFoundException()
{
}

public Metric ToMetric => _templateNotFound;
public override string ErrName => nameof(TemplateNotFoundException);

public override string ErrType => ErrorType.GeneralError;

public override string ErrSeverity => ErrorSeverity.Critical;

public override string Operation => ConnectorOperation.FHIRConversion;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
// -------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using Microsoft.Health.Common.Telemetry;
using Microsoft.Health.Common.Telemetry.Exceptions;

namespace Microsoft.Health.Fhir.Ingest.Data
{
public class CorrelationIdNotDefinedException :
Exception,
ITelemetryFormattable
public class CorrelationIdNotDefinedException : IomtTelemetryFormattableException
{
public CorrelationIdNotDefinedException(string message)
: base(message)
Expand All @@ -27,15 +25,12 @@ public CorrelationIdNotDefinedException()
{
}

public Metric ToMetric => new Metric(
"CorrelationIdNotDefinedException",
new Dictionary<string, object>
{
{ DimensionNames.Name, "CorrelationIdNotDefinedException" },
{ DimensionNames.Category, Category.Errors },
{ DimensionNames.ErrorType, ErrorType.DeviceMessageError },
{ DimensionNames.ErrorSeverity, ErrorSeverity.Critical },
{ DimensionNames.Operation, ConnectorOperation.Grouping },
});
public override string ErrName => nameof(CorrelationIdNotDefinedException);

public override string ErrType => ErrorType.DeviceMessageError;

public override string ErrSeverity => ErrorSeverity.Critical;

public override string Operation => ConnectorOperation.Grouping;
}
}
Loading

0 comments on commit 19ad97b

Please sign in to comment.