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

Add metrics for fhir observations created or updated #102

Merged
merged 2 commits into from
Jun 24, 2021
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
20 changes: 20 additions & 0 deletions src/lib/Microsoft.Health.Fhir.Ingest/Data/ResourceOperation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// -------------------------------------------------------------------------------------------------
// 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.Ingest.Data
{
public enum ResourceOperation
{
/// <summary>
/// FHIR resource created
/// </summary>
Created,

/// <summary>
/// FHIR resource updated
/// </summary>
Updated,
}
}
5 changes: 5 additions & 0 deletions src/lib/Microsoft.Health.Fhir.Ingest/Data/ResourceType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,10 @@ public enum ResourceType
/// Encounter Resource
/// </summary>
Encounter,

/// <summary>
/// Observation Resource
/// </summary>
Observation,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using EnsureThat;
using Microsoft.Health.Common.Telemetry;
using Microsoft.Health.Fhir.Ingest.Data;

namespace Microsoft.Health.Fhir.Ingest.Telemetry
{
Expand Down Expand Up @@ -175,6 +176,23 @@ public static Metric NotSupported()
return _notSupported;
}

/// <summary>
/// A metric for when a FHIR resource has been saved.
/// </summary>
/// <param name="resourceType">The type of FHIR resource that was saved.</param>
/// <param name="resourceOperation">The operation performed on the FHIR resource.</param>
public static Metric FhirResourceSaved(ResourceType resourceType, ResourceOperation resourceOperation)
{
return new Metric(
"FhirResourceSaved",
new Dictionary<string, object>
{
{ _nameDimension, $"{resourceType}{resourceOperation}" },
{ _categoryDimension, Category.Traffic },
{ _operationDimension, ConnectorOperation.FHIRConversion },
});
}

public static Metric UnhandledException(string exceptionName, string connectorStage)
{
EnsureArg.IsNotNull(exceptionName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
using Microsoft.Health.Extensions.Fhir;
using Microsoft.Health.Extensions.Fhir.Search;
using Microsoft.Health.Fhir.Ingest.Data;
using Microsoft.Health.Fhir.Ingest.Telemetry;
using Microsoft.Health.Fhir.Ingest.Template;
using Microsoft.Health.Logging.Telemetry;
using Polly;
using Model = Hl7.Fhir.Model;

Expand All @@ -24,12 +26,14 @@ public class R4FhirImportService :
private readonly FhirClient _client;
private readonly IFhirTemplateProcessor<ILookupTemplate<IFhirTemplate>, Model.Observation> _fhirTemplateProcessor;
private readonly IMemoryCache _observationCache;
private readonly ITelemetryLogger _logger;

public R4FhirImportService(IResourceIdentityService resourceIdentityService, FhirClient fhirClient, IFhirTemplateProcessor<ILookupTemplate<IFhirTemplate>, Model.Observation> fhirTemplateProcessor, IMemoryCache observationCache)
public R4FhirImportService(IResourceIdentityService resourceIdentityService, FhirClient fhirClient, IFhirTemplateProcessor<ILookupTemplate<IFhirTemplate>, Model.Observation> fhirTemplateProcessor, IMemoryCache observationCache, ITelemetryLogger logger)
{
_fhirTemplateProcessor = EnsureArg.IsNotNull(fhirTemplateProcessor, nameof(fhirTemplateProcessor));
_client = EnsureArg.IsNotNull(fhirClient, nameof(fhirClient));
_observationCache = EnsureArg.IsNotNull(observationCache, nameof(observationCache));
_logger = EnsureArg.IsNotNull(logger, nameof(logger));

ResourceIdentityService = EnsureArg.IsNotNull(resourceIdentityService, nameof(resourceIdentityService));
}
Expand Down Expand Up @@ -64,6 +68,7 @@ public virtual async Task<string> SaveObservationAsync(ILookupTemplate<IFhirTemp
{
var newObservation = GenerateObservation(config, observationGroup, identifier, ids);
result = await _client.CreateAsync(newObservation).ConfigureAwait(false);
_logger.LogMetric(IomtMetrics.FhirResourceSaved(ResourceType.Observation, ResourceOperation.Created), 1);
}
else
{
Expand All @@ -87,6 +92,7 @@ public virtual async Task<string> SaveObservationAsync(ILookupTemplate<IFhirTemp
}

result = policyResult.Result;
_logger.LogMetric(IomtMetrics.FhirResourceSaved(ResourceType.Observation, ResourceOperation.Updated), 1);
}

_observationCache.CreateEntry(cacheKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Health.Fhir.Ingest.Data;
using Microsoft.Health.Fhir.Ingest.Template;
using Microsoft.Health.Logging.Telemetry;
using Microsoft.Health.Tests.Common;
using NSubstitute;
using Xunit;
Expand Down Expand Up @@ -43,7 +44,9 @@ public async void GivenValidData_WhenProcessAsync_ThenSaveObservationInvokedForE
var measurementGroup = Substitute.For<IMeasurementGroup>();
var config = Substitute.For<ILookupTemplate<IFhirTemplate>>();

var service = Substitute.ForPartsOf<R4FhirImportService>(identityService, fhirClient, templateProcessor, cache)
var logger = Substitute.For<ITelemetryLogger>();

var service = Substitute.ForPartsOf<R4FhirImportService>(identityService, fhirClient, templateProcessor, cache, logger)
.Mock(m => m.SaveObservationAsync(default, default, default).ReturnsForAnyArgs(string.Empty));

await service.ProcessAsync(config, measurementGroup);
Expand Down Expand Up @@ -76,7 +79,9 @@ public async void GivenNotFoundObservation_WhenSaveObservationAsync_ThenCreateIn
var cache = Substitute.For<IMemoryCache>();
var config = Substitute.For<ILookupTemplate<IFhirTemplate>>();

var service = new R4FhirImportService(identityService, fhirClient, templateProcessor, cache);
var logger = Substitute.For<ITelemetryLogger>();

var service = new R4FhirImportService(identityService, fhirClient, templateProcessor, cache, logger);

var result = await service.SaveObservationAsync(config, observationGroup, ids);

Expand Down Expand Up @@ -121,7 +126,9 @@ public async void GivenFoundObservation_WhenSaveObservationAsync_ThenUpdateInvok
var cache = Substitute.For<IMemoryCache>();
var config = Substitute.For<ILookupTemplate<IFhirTemplate>>();

var service = new R4FhirImportService(identityService, fhirClient, templateProcessor, cache);
var logger = Substitute.For<ITelemetryLogger>();

var service = new R4FhirImportService(identityService, fhirClient, templateProcessor, cache, logger);

var result = await service.SaveObservationAsync(config, observationGroup, ids);

Expand Down Expand Up @@ -179,7 +186,9 @@ public async void GivenFoundObservationAndConflictOnSave_WhenSaveObservationAsyn
var cache = Substitute.For<IMemoryCache>();
var config = Substitute.For<ILookupTemplate<IFhirTemplate>>();

var service = new R4FhirImportService(identityService, fhirClient, templateProcessor, cache);
var logger = Substitute.For<ITelemetryLogger>();

var service = new R4FhirImportService(identityService, fhirClient, templateProcessor, cache, logger);

var result = await service.SaveObservationAsync(config, observationGroup, ids);

Expand All @@ -205,7 +214,9 @@ public void GivenValidTemplate_WhenGenerateObservation_ExpectedReferencesSet_Tes
var identifer = new Model.Identifier();
var config = Substitute.For<ILookupTemplate<IFhirTemplate>>();

var service = new R4FhirImportService(identityService, fhirClient, templateProcessor, cache);
var logger = Substitute.For<ITelemetryLogger>();

var service = new R4FhirImportService(identityService, fhirClient, templateProcessor, cache, logger);

var result = service.GenerateObservation(config, observationGroup, identifer, ids);

Expand Down