From 19ad97b22d1d730c851aec5093a3c530799fbbae Mon Sep 17 00:00:00 2001 From: Pallavi Reddy <84752343+pallar-ms@users.noreply.github.com> Date: Thu, 11 Nov 2021 09:03:39 -0800 Subject: [PATCH] refactor: Consolidate creation of error metric (#143) * refactor error metric * addressing PR comments * update existing custom exceptions to use IomtTelemetryFormattableException --- .../MeasurementCollectionToFhir/Processor.cs | 13 +------ src/console/Normalize/Processor.cs | 14 +------ .../IomtTelemetryFormattableException.cs | 21 ++++------- .../Telemetry/MetricExtension.cs | 27 +++++++++++++- .../Exceptions/InvalidEventHubException.cs | 3 ++ .../UnauthorizedAccessEventHubException.cs | 3 ++ .../Telemetry/Metrics/EventMetrics.cs | 12 +----- .../BundleExtensions.cs | 2 +- .../Telemetry/Metrics/FhirClientMetrics.cs | 18 +-------- .../MultipleResourceFoundException.cs | 23 ++++-------- .../TemplateNotFoundException.cs | 25 +++++-------- .../Data/CorrelationIdNotDefinedException.cs | 23 +++++------- .../FhirResourceNotFoundException.cs | 23 ++++-------- .../ResourceIdentityNotDefinedException.cs | 23 ++++-------- .../Service/PatientDeviceMismatchException.cs | 28 ++++++-------- .../Telemetry/Metrics/IomtMetrics.cs | 37 +++---------------- 16 files changed, 102 insertions(+), 193 deletions(-) diff --git a/src/console/MeasurementCollectionToFhir/Processor.cs b/src/console/MeasurementCollectionToFhir/Processor.cs index 08d4b924..df408a33 100644 --- a/src/console/MeasurementCollectionToFhir/Processor.cs +++ b/src/console/MeasurementCollectionToFhir/Processor.cs @@ -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 - { - { 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); } } } diff --git a/src/console/Normalize/Processor.cs b/src/console/Normalize/Processor.cs index fcc42b17..e59f40e1 100644 --- a/src/console/Normalize/Processor.cs +++ b/src/console/Normalize/Processor.cs @@ -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 { @@ -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 - { - { 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); } } } diff --git a/src/lib/Microsoft.Health.Common/Telemetry/Exceptions/IomtTelemetryFormattableException.cs b/src/lib/Microsoft.Health.Common/Telemetry/Exceptions/IomtTelemetryFormattableException.cs index 732fd87b..7e1b4e30 100644 --- a/src/lib/Microsoft.Health.Common/Telemetry/Exceptions/IomtTelemetryFormattableException.cs +++ b/src/lib/Microsoft.Health.Common/Telemetry/Exceptions/IomtTelemetryFormattableException.cs @@ -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() { @@ -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 - { - { 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); } } diff --git a/src/lib/Microsoft.Health.Common/Telemetry/MetricExtension.cs b/src/lib/Microsoft.Health.Common/Telemetry/MetricExtension.cs index 00435906..0f116149 100644 --- a/src/lib/Microsoft.Health.Common/Telemetry/MetricExtension.cs +++ b/src/lib/Microsoft.Health.Common/Telemetry/MetricExtension.cs @@ -4,6 +4,7 @@ // ------------------------------------------------------------------------------------------------- using System.Collections.Generic; +using EnsureThat; namespace Microsoft.Health.Common.Telemetry { @@ -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, @@ -28,7 +33,7 @@ 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; } @@ -36,5 +41,25 @@ public static Metric AddDimension(this Metric metric, string dimensionName, stri 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 + { + { DimensionNames.Name, string.IsNullOrWhiteSpace(errorName) ? metricName : errorName }, + { DimensionNames.Category, Category.Errors }, + { DimensionNames.ErrorType, errorType }, + { DimensionNames.ErrorSeverity, errorSeverity }, + { DimensionNames.Operation, operation }, + }) + .AddDimension(DimensionNames.ErrorSource, errorSource); + } } } diff --git a/src/lib/Microsoft.Health.Events/Telemetry/Exceptions/InvalidEventHubException.cs b/src/lib/Microsoft.Health.Events/Telemetry/Exceptions/InvalidEventHubException.cs index 0c20996e..3eb57e61 100644 --- a/src/lib/Microsoft.Health.Events/Telemetry/Exceptions/InvalidEventHubException.cs +++ b/src/lib/Microsoft.Health.Events/Telemetry/Exceptions/InvalidEventHubException.cs @@ -5,6 +5,7 @@ using System; using Microsoft.Health.Common.Telemetry; +using Microsoft.Health.Common.Telemetry.Exceptions; namespace Microsoft.Health.Events.Telemetry.Exceptions { @@ -26,6 +27,8 @@ public InvalidEventHubException( public override string ErrType => _errorType; + public override string ErrSeverity => ErrorSeverity.Critical; + public override string ErrSource => nameof(ErrorSource.User); } } diff --git a/src/lib/Microsoft.Health.Events/Telemetry/Exceptions/UnauthorizedAccessEventHubException.cs b/src/lib/Microsoft.Health.Events/Telemetry/Exceptions/UnauthorizedAccessEventHubException.cs index 3d60db57..19f8f778 100644 --- a/src/lib/Microsoft.Health.Events/Telemetry/Exceptions/UnauthorizedAccessEventHubException.cs +++ b/src/lib/Microsoft.Health.Events/Telemetry/Exceptions/UnauthorizedAccessEventHubException.cs @@ -5,6 +5,7 @@ using System; using Microsoft.Health.Common.Telemetry; +using Microsoft.Health.Common.Telemetry.Exceptions; namespace Microsoft.Health.Events.Telemetry.Exceptions { @@ -28,6 +29,8 @@ public UnauthorizedAccessEventHubException( public override string ErrType => _errorType; + public override string ErrSeverity => ErrorSeverity.Critical; + public override string ErrSource => nameof(ErrorSource.User); } } diff --git a/src/lib/Microsoft.Health.Events/Telemetry/Metrics/EventMetrics.cs b/src/lib/Microsoft.Health.Events/Telemetry/Metrics/EventMetrics.cs index acbab2b6..426824ff 100644 --- a/src/lib/Microsoft.Health.Events/Telemetry/Metrics/EventMetrics.cs +++ b/src/lib/Microsoft.Health.Events/Telemetry/Metrics/EventMetrics.cs @@ -4,7 +4,6 @@ // ------------------------------------------------------------------------------------------------- using System; -using System.Collections.Generic; using Microsoft.Health.Common.Telemetry; namespace Microsoft.Health.Events.Telemetry @@ -108,16 +107,7 @@ public static Metric EventTimestampLastProcessedPerPartition(string partitionId, /// The stage of the connector public static Metric HandledException(string exceptionName, string connectorStage) { - return new Metric( - exceptionName, - new Dictionary - { - { _nameDimension, exceptionName }, - { _categoryDimension, Category.Errors }, - { _errorTypeDimension, ErrorType.EventHubError }, - { _errorSeverityDimension, ErrorSeverity.Critical }, - { _operationDimension, connectorStage }, - }); + return exceptionName.ToErrorMetric(connectorStage, ErrorType.EventHubError, ErrorSeverity.Critical); } } } diff --git a/src/lib/Microsoft.Health.Extensions.Fhir.R4/BundleExtensions.cs b/src/lib/Microsoft.Health.Extensions.Fhir.R4/BundleExtensions.cs index 5548de47..d41778bf 100644 --- a/src/lib/Microsoft.Health.Extensions.Fhir.R4/BundleExtensions.cs +++ b/src/lib/Microsoft.Health.Extensions.Fhir.R4/BundleExtensions.cs @@ -59,7 +59,7 @@ public static async Task ReadOneFromBundleWithContinuationAsync 1) { - throw new MultipleResourceFoundException(); + throw new MultipleResourceFoundException(resourceCount); } return resources.FirstOrDefault(); diff --git a/src/lib/Microsoft.Health.Extensions.Fhir.R4/Telemetry/Metrics/FhirClientMetrics.cs b/src/lib/Microsoft.Health.Extensions.Fhir.R4/Telemetry/Metrics/FhirClientMetrics.cs index 23e60e3b..8a1ac0f5 100644 --- a/src/lib/Microsoft.Health.Extensions.Fhir.R4/Telemetry/Metrics/FhirClientMetrics.cs +++ b/src/lib/Microsoft.Health.Extensions.Fhir.R4/Telemetry/Metrics/FhirClientMetrics.cs @@ -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; - /// /// A metric recorded when there is an error reading from or connecting with a FHIR server. /// @@ -24,16 +17,7 @@ public class FhirClientMetrics /// The stage of the connector public static Metric HandledException(string exceptionName, string severity, string connectorStage) { - return new Metric( - exceptionName, - new Dictionary - { - { _nameDimension, exceptionName }, - { _categoryDimension, Category.Errors }, - { _errorTypeDimension, ErrorType.GeneralError }, - { _errorSeverityDimension, severity }, - { _operationDimension, connectorStage }, - }); + return exceptionName.ToErrorMetric(connectorStage, ErrorType.GeneralError, severity); } } } diff --git a/src/lib/Microsoft.Health.Extensions.Fhir/MultipleResourceFoundException.cs b/src/lib/Microsoft.Health.Extensions.Fhir/MultipleResourceFoundException.cs index df44f23b..ab86dc91 100644 --- a/src/lib/Microsoft.Health.Extensions.Fhir/MultipleResourceFoundException.cs +++ b/src/lib/Microsoft.Health.Extensions.Fhir/MultipleResourceFoundException.cs @@ -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 : - Exception, - ITelemetryFormattable + public class MultipleResourceFoundException : IomtTelemetryFormattableException { public MultipleResourceFoundException(int resourceCount) : base($"Multiple resources {resourceCount} of type {typeof(T)} found, expected one") @@ -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 - { - { 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; } } diff --git a/src/lib/Microsoft.Health.Fhir.Ingest.Template/TemplateNotFoundException.cs b/src/lib/Microsoft.Health.Fhir.Ingest.Template/TemplateNotFoundException.cs index 9bf921a1..4ee76e84 100644 --- a/src/lib/Microsoft.Health.Fhir.Ingest.Template/TemplateNotFoundException.cs +++ b/src/lib/Microsoft.Health.Fhir.Ingest.Template/TemplateNotFoundException.cs @@ -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 - { - { 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) { @@ -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; } } diff --git a/src/lib/Microsoft.Health.Fhir.Ingest/Data/CorrelationIdNotDefinedException.cs b/src/lib/Microsoft.Health.Fhir.Ingest/Data/CorrelationIdNotDefinedException.cs index 6daecc4f..d7db5dc7 100644 --- a/src/lib/Microsoft.Health.Fhir.Ingest/Data/CorrelationIdNotDefinedException.cs +++ b/src/lib/Microsoft.Health.Fhir.Ingest/Data/CorrelationIdNotDefinedException.cs @@ -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) @@ -27,15 +25,12 @@ public CorrelationIdNotDefinedException() { } - public Metric ToMetric => new Metric( - "CorrelationIdNotDefinedException", - new Dictionary - { - { 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; } } diff --git a/src/lib/Microsoft.Health.Fhir.Ingest/FhirResourceNotFoundException.cs b/src/lib/Microsoft.Health.Fhir.Ingest/FhirResourceNotFoundException.cs index 932c5400..ff7cd8ab 100644 --- a/src/lib/Microsoft.Health.Fhir.Ingest/FhirResourceNotFoundException.cs +++ b/src/lib/Microsoft.Health.Fhir.Ingest/FhirResourceNotFoundException.cs @@ -4,15 +4,13 @@ // ------------------------------------------------------------------------------------------------- using System; -using System.Collections.Generic; using Microsoft.Health.Common.Telemetry; +using Microsoft.Health.Common.Telemetry.Exceptions; using Microsoft.Health.Fhir.Ingest.Data; namespace Microsoft.Health.Fhir.Ingest { - public class FhirResourceNotFoundException : - Exception, - ITelemetryFormattable + public class FhirResourceNotFoundException : IomtTelemetryFormattableException { public FhirResourceNotFoundException(ResourceType resourceType) : base($"Fhir resource of type {resourceType} not found.") @@ -36,17 +34,10 @@ public FhirResourceNotFoundException(string message, Exception innerException) public ResourceType FhirResourceType { get; private set; } - public string EventName => $"{FhirResourceType}NotFoundException"; - - public Metric ToMetric => new Metric( - $"{EventName}", - new Dictionary - { - { DimensionNames.Name, $"{EventName}" }, - { DimensionNames.Category, Category.Errors }, - { DimensionNames.ErrorType, ErrorType.FHIRResourceError }, - { DimensionNames.ErrorSeverity, ErrorSeverity.Warning }, - { DimensionNames.Operation, ConnectorOperation.FHIRConversion }, - }); + public override string ErrName => $"{FhirResourceType}NotFoundException"; + + public override string ErrType => ErrorType.FHIRResourceError; + + public override string Operation => ConnectorOperation.FHIRConversion; } } diff --git a/src/lib/Microsoft.Health.Fhir.Ingest/ResourceIdentityNotDefinedException.cs b/src/lib/Microsoft.Health.Fhir.Ingest/ResourceIdentityNotDefinedException.cs index c462de66..bcc286a0 100644 --- a/src/lib/Microsoft.Health.Fhir.Ingest/ResourceIdentityNotDefinedException.cs +++ b/src/lib/Microsoft.Health.Fhir.Ingest/ResourceIdentityNotDefinedException.cs @@ -4,15 +4,13 @@ // ------------------------------------------------------------------------------------------------- using System; -using System.Collections.Generic; using Microsoft.Health.Common.Telemetry; +using Microsoft.Health.Common.Telemetry.Exceptions; using Microsoft.Health.Fhir.Ingest.Data; namespace Microsoft.Health.Fhir.Ingest.Service { - public class ResourceIdentityNotDefinedException : - Exception, - ITelemetryFormattable + public class ResourceIdentityNotDefinedException : IomtTelemetryFormattableException { public ResourceIdentityNotDefinedException(ResourceType resourceType) : base($"Fhir resource of type {resourceType} not found.") @@ -36,17 +34,10 @@ public ResourceIdentityNotDefinedException(string message, Exception innerExcept public ResourceType FhirResourceType { get; private set; } - public string EventName => $"{FhirResourceType}IdentityNotDefinedException"; - - public Metric ToMetric => new Metric( - $"{EventName}", - new Dictionary - { - { DimensionNames.Name, $"{EventName}" }, - { DimensionNames.Category, Category.Errors }, - { DimensionNames.ErrorType, ErrorType.FHIRResourceError }, - { DimensionNames.ErrorSeverity, ErrorSeverity.Warning }, - { DimensionNames.Operation, ConnectorOperation.FHIRConversion }, - }); + public override string ErrName => $"{FhirResourceType}IdentityNotDefinedException"; + + public override string ErrType => ErrorType.FHIRResourceError; + + public override string Operation => ConnectorOperation.FHIRConversion; } } diff --git a/src/lib/Microsoft.Health.Fhir.Ingest/Service/PatientDeviceMismatchException.cs b/src/lib/Microsoft.Health.Fhir.Ingest/Service/PatientDeviceMismatchException.cs index df2852e1..458e8392 100644 --- a/src/lib/Microsoft.Health.Fhir.Ingest/Service/PatientDeviceMismatchException.cs +++ b/src/lib/Microsoft.Health.Fhir.Ingest/Service/PatientDeviceMismatchException.cs @@ -4,15 +4,18 @@ // ------------------------------------------------------------------------------------------------- using System; -using System.Collections.Generic; using Microsoft.Health.Common.Telemetry; +using Microsoft.Health.Common.Telemetry.Exceptions; namespace Microsoft.Health.Fhir.Ingest.Service { - public class PatientDeviceMismatchException : - Exception, - ITelemetryFormattable + public class PatientDeviceMismatchException : IomtTelemetryFormattableException { + public PatientDeviceMismatchException() + : base() + { + } + public PatientDeviceMismatchException(string message) : base(message) { @@ -23,19 +26,10 @@ public PatientDeviceMismatchException(string message, Exception innerException) { } - public PatientDeviceMismatchException() - { - } + public override string ErrName => nameof(PatientDeviceMismatchException); + + public override string ErrType => ErrorType.FHIRResourceError; - public Metric ToMetric => new Metric( - "PatientDeviceMismatchException", - new Dictionary - { - { DimensionNames.Name, "PatientDeviceMismatchException" }, - { DimensionNames.Category, Category.Errors }, - { DimensionNames.ErrorType, ErrorType.FHIRResourceError }, - { DimensionNames.ErrorSeverity, ErrorSeverity.Warning }, - { DimensionNames.Operation, ConnectorOperation.FHIRConversion }, - }); + public override string Operation => ConnectorOperation.FHIRConversion; } } diff --git a/src/lib/Microsoft.Health.Fhir.Ingest/Telemetry/Metrics/IomtMetrics.cs b/src/lib/Microsoft.Health.Fhir.Ingest/Telemetry/Metrics/IomtMetrics.cs index a0742b5f..defc5c62 100644 --- a/src/lib/Microsoft.Health.Fhir.Ingest/Telemetry/Metrics/IomtMetrics.cs +++ b/src/lib/Microsoft.Health.Fhir.Ingest/Telemetry/Metrics/IomtMetrics.cs @@ -3,6 +3,7 @@ // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. // ------------------------------------------------------------------------------------------------- +using System; using System.Collections.Generic; using EnsureThat; using Microsoft.Health.Common.Telemetry; @@ -24,16 +25,7 @@ public static class IomtMetrics private static Metric _deviceIngressSizeBytes = IomtMetricDefinition.DeviceIngressSizeBytes.CreateBaseMetric(Category.Traffic, ConnectorOperation.Normalization); - private static Metric _notSupported = new Metric( - "NotSupportedException", - new Dictionary - { - { _nameDimension, "NotSupportedException" }, - { _categoryDimension, Category.Errors }, - { _errorTypeDimension, ErrorType.FHIRResourceError }, - { _errorSeverityDimension, ErrorSeverity.Warning }, - { _operationDimension, ConnectorOperation.FHIRConversion }, - }); + private static Metric _notSupported = nameof(NotSupportedException).ToErrorMetric(ConnectorOperation.FHIRConversion, ErrorType.FHIRResourceError, ErrorSeverity.Warning); /// /// The latency between event ingestion and output to FHIR processor. @@ -158,31 +150,14 @@ public static Metric FhirResourceSaved(ResourceType resourceType, ResourceOperat public static Metric UnhandledException(string exceptionName, string connectorStage) { - EnsureArg.IsNotNull(exceptionName); - return new Metric( - "UnhandledException", - new Dictionary - { - { _nameDimension, exceptionName }, - { _categoryDimension, Category.Errors }, - { _errorTypeDimension, ErrorType.GeneralError }, - { _errorSeverityDimension, ErrorSeverity.Critical }, - { _operationDimension, connectorStage }, - }); + EnsureArg.IsNotNullOrWhiteSpace(exceptionName); + + return nameof(UnhandledException).ToErrorMetric(connectorStage, ErrorType.GeneralError, ErrorSeverity.Critical, errorName: exceptionName); } public static Metric HandledException(string exceptionName, string connectorStage) { - return new Metric( - exceptionName, - new Dictionary - { - { _nameDimension, exceptionName }, - { _categoryDimension, Category.Errors }, - { _errorTypeDimension, ErrorType.GeneralError }, - { _errorSeverityDimension, ErrorSeverity.Critical }, - { _operationDimension, connectorStage }, - }); + return exceptionName.ToErrorMetric(connectorStage, ErrorType.GeneralError, ErrorSeverity.Critical); } } }