-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add function to get sum and total indexed
- Loading branch information
Showing
18 changed files
with
491 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
src/Microsoft.Health.Dicom.Core/Features/Common/IndexedFileProperties.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,22 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// 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.Dicom.Core.Features.Common; | ||
|
||
/// <summary> | ||
/// Metadata on FileProperty table in database | ||
/// </summary> | ||
public class IndexedFileProperties | ||
{ | ||
/// <summary> | ||
/// Total indexed FileProperty in database | ||
/// </summary> | ||
public int TotalIndexed { get; init; } | ||
|
||
/// <summary> | ||
/// Total sum of all ContentLength rows in FileProperty table | ||
/// </summary> | ||
public long TotalSum { get; init; } | ||
} |
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
79 changes: 79 additions & 0 deletions
79
...h.Dicom.Functions.UnitTests/IndexMetricsCollection/IndexMetricsCollectionFunctionTests.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,79 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// 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.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.WebJobs; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.Health.Dicom.Core.Configs; | ||
using Microsoft.Health.Dicom.Core.Features.Common; | ||
using Microsoft.Health.Dicom.Core.Features.Store; | ||
using Microsoft.Health.Dicom.Core.Features.Telemetry; | ||
using Microsoft.Health.Dicom.Functions.IndexMetricsCollection; | ||
using Microsoft.Health.Dicom.Functions.IndexMetricsCollection.Telemetry; | ||
using NSubstitute; | ||
using NSubstitute.ExceptionExtensions; | ||
using OpenTelemetry; | ||
using OpenTelemetry.Metrics; | ||
using Xunit; | ||
|
||
namespace Microsoft.Health.Dicom.Functions.UnitTests.IndexMetricsCollection; | ||
|
||
public class IndexMetricsCollectionFunctionTests | ||
{ | ||
private readonly IndexMetricsCollectionFunction _collectionFunction; | ||
private readonly IIndexDataStore _indexStore; | ||
private readonly IndexMetricsCollectionMeter _meter; | ||
private List<Metric> _exportedItems; | ||
private MeterProvider _meterProvider; | ||
private readonly TimerInfo _timer; | ||
|
||
public IndexMetricsCollectionFunctionTests() | ||
{ | ||
_meter = new IndexMetricsCollectionMeter(); | ||
_indexStore = Substitute.For<IIndexDataStore>(); | ||
_collectionFunction = new IndexMetricsCollectionFunction( | ||
_indexStore, | ||
Options.Create(new FeatureConfiguration { EnableExternalStore = true, }), | ||
_meter); | ||
_timer = Substitute.For<TimerInfo>(default, default, default); | ||
} | ||
|
||
private void InitializeMetricExporter() | ||
{ | ||
_exportedItems = new List<Metric>(); | ||
_meterProvider = Sdk.CreateMeterProviderBuilder() | ||
.AddMeter($"{OpenTelemetryLabels.BaseMeterName}.{IndexMetricsCollectionMeter.MeterName}") | ||
.AddInMemoryExporter(_exportedItems) | ||
.Build(); | ||
} | ||
|
||
[Fact] | ||
public async Task GivenIndexMetricsCollectionFunction_WhenRun_ThenIndexMetricsCollectionsCompletedCounterIsIncremented() | ||
{ | ||
InitializeMetricExporter(); | ||
_indexStore.GetIndexedFilePropertiesAsync().ReturnsForAnyArgs(new IndexedFileProperties()); | ||
|
||
await _collectionFunction.Run(_timer, NullLogger.Instance); | ||
|
||
_meterProvider.ForceFlush(); | ||
Assert.Single(_exportedItems); | ||
Assert.Equal(nameof(_meter.IndexMetricsCollectionsCompletedCounter), _exportedItems[0].Name); | ||
} | ||
|
||
[Fact] | ||
public async Task GivenIndexMetricsCollectionFunction_WhenRunException_ThenIndexMetricsCollectionsCompletedCounterIsNotIncremented() | ||
{ | ||
InitializeMetricExporter(); | ||
_indexStore.GetIndexedFilePropertiesAsync().ThrowsForAnyArgs(new Exception()); | ||
|
||
await Assert.ThrowsAsync<Exception>(async () => await _collectionFunction.Run(_timer, NullLogger.Instance)); | ||
|
||
_meterProvider.ForceFlush(); | ||
Assert.Empty(_exportedItems); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...Microsoft.Health.Dicom.Functions/IndexMetricsCollection/IndexMetricsCollectionFunction.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,80 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
using EnsureThat; | ||
using Microsoft.Azure.WebJobs; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.Health.Core; | ||
using Microsoft.Health.Dicom.Core.Configs; | ||
using Microsoft.Health.Dicom.Core.Features.Common; | ||
using Microsoft.Health.Dicom.Core.Features.Store; | ||
using Microsoft.Health.Dicom.Functions.IndexMetricsCollection.Telemetry; | ||
|
||
namespace Microsoft.Health.Dicom.Functions.IndexMetricsCollection; | ||
|
||
/// <summary> | ||
/// A function for collecting index metrics | ||
/// </summary> | ||
public class IndexMetricsCollectionFunction | ||
{ | ||
private readonly IIndexDataStore _indexDataStore; | ||
private readonly IndexMetricsCollectionMeter _meter; | ||
private readonly bool _externalStoreEnabled; | ||
private readonly bool _enableDataPartitions; | ||
|
||
public IndexMetricsCollectionFunction( | ||
IIndexDataStore indexDataStore, | ||
IOptions<FeatureConfiguration> featureConfiguration, | ||
IndexMetricsCollectionMeter meter) | ||
{ | ||
EnsureArg.IsNotNull(featureConfiguration, nameof(featureConfiguration)); | ||
_indexDataStore = EnsureArg.IsNotNull(indexDataStore, nameof(indexDataStore)); | ||
_meter = EnsureArg.IsNotNull(meter, nameof(meter)); | ||
_externalStoreEnabled = featureConfiguration.Value.EnableExternalStore; | ||
_enableDataPartitions = featureConfiguration.Value.EnableDataPartitions; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Asynchronously collects index metrics. | ||
/// </summary> | ||
/// <param name="invocationTimer">The timer which tracks the invocation schedule.</param> | ||
/// <param name="log">A diagnostic logger.</param> | ||
/// <returns>A task that represents the asynchronous metrics collection operation.</returns> | ||
[FunctionName(nameof(IndexMetricsCollectionFunction))] | ||
public async Task Run( | ||
[TimerTrigger(IndexMetricsCollectionOptions.Frequency)] TimerInfo invocationTimer, | ||
ILogger log) | ||
{ | ||
EnsureArg.IsNotNull(invocationTimer, nameof(invocationTimer)); | ||
EnsureArg.IsNotNull(log, nameof(log)); | ||
|
||
log.LogInformation("Collecting a daily summation starting. At: {Timestamp}", Clock.UtcNow); | ||
if (invocationTimer.IsPastDue) | ||
{ | ||
log.LogWarning("Current function invocation is running late."); | ||
} | ||
|
||
Stopwatch stopwatch = new Stopwatch(); | ||
stopwatch.Start(); | ||
|
||
IndexedFileProperties indexedFileProperties = await _indexDataStore.GetIndexedFilePropertiesAsync(); | ||
|
||
stopwatch.Stop(); | ||
|
||
log.LogInformation("Collecting a daily summation time taken: {ElapsedTime} ms with ExternalStoreEnabled: {ExternalStoreEnabled} and DataPartitionsEnabled: {PartitionsEnabled}", stopwatch.ElapsedMilliseconds, _externalStoreEnabled, _enableDataPartitions); | ||
|
||
log.LogInformation("DICOM telemetry - total files indexed: {0} with ExternalStoreEnabled: {ExternalStoreEnabled} and DataPartitionsEnabled: {PartitionsEnabled}", indexedFileProperties.TotalIndexed, _externalStoreEnabled, _enableDataPartitions); | ||
|
||
log.LogInformation("DICOM telemetry - total content length indexed: {0} with ExternalStoreEnabled: {ExternalStoreEnabled} and DataPartitionsEnabled: {PartitionsEnabled}", indexedFileProperties.TotalSum, _externalStoreEnabled, _enableDataPartitions); | ||
|
||
log.LogInformation("Collecting a daily summation completed. with ExternalStoreEnabled: {ExternalStoreEnabled} and DataPartitionsEnabled: {PartitionsEnabled}", _externalStoreEnabled, _enableDataPartitions); | ||
|
||
_meter.IndexMetricsCollectionsCompletedCounter.Add(1, IndexMetricsCollectionMeter.CreateTelemetryDimension(_externalStoreEnabled, _enableDataPartitions)); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Microsoft.Health.Dicom.Functions/IndexMetricsCollection/IndexMetricsCollectionOptions.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,28 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
|
||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace Microsoft.Health.Dicom.Functions.IndexMetricsCollection; | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
public static class IndexMetricsCollectionOptions | ||
{ | ||
/// <summary> | ||
/// The default section name for <see cref="IndexMetricsCollectionOptions"/> in a configuration. | ||
/// </summary> | ||
public const string SectionName = "IndexMetricsCollection"; | ||
|
||
/// <summary> | ||
/// Gets or sets the cron expression that indicates how frequently to run the index metrics collection function. | ||
/// </summary> | ||
/// <value>A value cron expression</value> | ||
[Required] | ||
public const string Frequency = "* * * * *"; // Every day at midnight | ||
// public const string Frequency = "0 0 * * *"; // Every day at midnight | ||
} |
43 changes: 43 additions & 0 deletions
43
...ft.Health.Dicom.Functions/IndexMetricsCollection/Telemetry/IndexMetricsCollectionMeter.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,43 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// 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.Collections.Generic; | ||
using System.Diagnostics.Metrics; | ||
using Microsoft.Health.Dicom.Core.Features.Telemetry; | ||
|
||
namespace Microsoft.Health.Dicom.Functions.IndexMetricsCollection.Telemetry; | ||
|
||
public sealed class IndexMetricsCollectionMeter : IDisposable | ||
{ | ||
private readonly Meter _meter; | ||
internal const string MeterName = "IndexMetricsCollection"; | ||
|
||
public IndexMetricsCollectionMeter() | ||
{ | ||
_meter = new Meter($"{OpenTelemetryLabels.BaseMeterName}.{MeterName}", "1.0"); | ||
|
||
IndexMetricsCollectionsCompletedCounter = | ||
_meter.CreateCounter<long>( | ||
nameof(IndexMetricsCollectionsCompletedCounter), | ||
description: "Represents a successful run of the index metrics collection function."); | ||
} | ||
|
||
|
||
public static KeyValuePair<string, object>[] CreateTelemetryDimension(bool externalStoreEnabled, bool dataPartitionsEnabled) => | ||
new[] | ||
{ | ||
new KeyValuePair<string, object>("ExternalStoreEnabled", externalStoreEnabled), | ||
new KeyValuePair<string, object>("DataPartitionsEnabled", dataPartitionsEnabled), | ||
}; | ||
|
||
/// <summary> | ||
/// Represents a successful run of the index metrics collection function | ||
/// </summary> | ||
public Counter<long> IndexMetricsCollectionsCompletedCounter { get; } | ||
|
||
public void Dispose() | ||
=> _meter.Dispose(); | ||
} |
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
15 changes: 15 additions & 0 deletions
15
src/Microsoft.Health.Dicom.SqlServer/Features/Schema/Migrations/57.diff.sql
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
19 changes: 19 additions & 0 deletions
19
...Dicom.SqlServer/Features/Schema/Sql/Sprocs/GetTotalAndSumContentLengthIndexedAsyncV57.sql
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,19 @@ | ||
/***************************************************************************************/ | ||
-- STORED PROCEDURE | ||
-- GetTotalAndSumContentLengthIndexedAsyncV57 | ||
-- | ||
-- FIRST SCHEMA VERSION | ||
-- 57 | ||
-- | ||
-- DESCRIPTION | ||
-- Retrieves total sum of content length across all FileProperty rows | ||
-- | ||
/***************************************************************************************/ | ||
CREATE OR ALTER PROCEDURE dbo.GetTotalAndSumContentLengthIndexedAsyncV57 | ||
AS | ||
BEGIN | ||
SET NOCOUNT ON | ||
SET XACT_ABORT ON | ||
|
||
SELECT count(*), SUM(ContentLength) FROM dbo.FileProperty | ||
END |
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.