Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge 'Metrics/master' to 'master'. Only Metrics sub-project is touch…
Browse files Browse the repository at this point in the history
…ed. No need for CR.

Merge 'Metrics/master' to 'master'. Only Metrics sub-project is touched. No need for CR.
  • Loading branch information
macrogreg committed Mar 16, 2018
2 parents 6a42a0e + 6694233 commit ef72e14
Show file tree
Hide file tree
Showing 65 changed files with 3,696 additions and 1,584 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,9 @@ private static string BuildPointMoniker(string[] coordinates)
private MultidimensionalPointResult<TPoint> TryCreatePoint(string[] coordinates, string pointMoniker)
{
#pragma warning disable SA1509 // Opening braces must not be preceded by blank line

// We already have tried getting the existng point and failed.
// We also checked that _totalPointsCountLimit was not reached.
// We also checked that _totalPointsCountLimit was not reached (outside the lock).
// Lastly, we took a lock.
// Now we can begin the slow path.

Expand Down Expand Up @@ -379,7 +380,9 @@ private MultidimensionalPointResult<TPoint> TryCreatePoint(string[] coordinates,
bool added = _points.TryAdd(pointMoniker, point);
if (false == added)
{
throw new InvalidOperationException($"Internal Metrics SDK bug. Please report this! (pointMoniker: {pointMoniker})");
throw new InvalidOperationException($"Internal SDK bug. Please report this! (pointMoniker: {pointMoniker})"
+ $" Info: Failed to add a point to the {nameof(_points)}-collection in"
+ $" class {nameof(MultidimensionalCube2<TPoint>)} despite passing all the cerfification checks.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ namespace Microsoft.ApplicationInsights.Metrics
/// <summary />
internal class MeasurementAggregateToApplicationInsightsPipelineConverter : MetricAggregateToApplicationInsightsPipelineConverterBase
{
public override string AggregationKindMoniker { get { return MetricConfigurations.Common.AggregateKinds().Measurement().Moniker; } }
public override string AggregationKindMoniker { get { return MetricSeriesConfigurationForMeasurement.Constants.AggregateKindMoniker; } }

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062", Justification = "telemetryItem and aggregate are validated by base")]
protected override void PopulateDataValues(MetricTelemetry telemetryItem, MetricAggregate aggregate)
{
telemetryItem.Count = aggregate.GetDataValue<int>(MetricConfigurations.Common.AggregateKinds().Measurement().DataKeys.Count, 0);
telemetryItem.Sum = aggregate.GetDataValue<double>(MetricConfigurations.Common.AggregateKinds().Measurement().DataKeys.Sum, 0.0);
telemetryItem.Min = aggregate.GetDataValue<double>(MetricConfigurations.Common.AggregateKinds().Measurement().DataKeys.Min, 0.0);
telemetryItem.Max = aggregate.GetDataValue<double>(MetricConfigurations.Common.AggregateKinds().Measurement().DataKeys.Max, 0.0);
telemetryItem.StandardDeviation = aggregate.GetDataValue<double>(MetricConfigurations.Common.AggregateKinds().Measurement().DataKeys.StdDev, 0.0);
telemetryItem.Count = aggregate.GetDataValue<int>(MetricSeriesConfigurationForMeasurement.Constants.AggregateKindDataKeys.Count, 0);
telemetryItem.Sum = aggregate.GetDataValue<double>(MetricSeriesConfigurationForMeasurement.Constants.AggregateKindDataKeys.Sum, 0.0);
telemetryItem.Min = aggregate.GetDataValue<double>(MetricSeriesConfigurationForMeasurement.Constants.AggregateKindDataKeys.Min, 0.0);
telemetryItem.Max = aggregate.GetDataValue<double>(MetricSeriesConfigurationForMeasurement.Constants.AggregateKindDataKeys.Max, 0.0);
telemetryItem.StandardDeviation = aggregate.GetDataValue<double>(MetricSeriesConfigurationForMeasurement.Constants.AggregateKindDataKeys.StdDev, 0.0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,15 @@ protected override MetricAggregate CreateAggregate(DateTimeOffset periodEnd)
}

MetricAggregate aggregate = new MetricAggregate(
DataSeries?.MetricId ?? Util.NullString,
MetricConfigurations.Common.AggregateKinds().Measurement().Moniker);

aggregate.Data[MetricConfigurations.Common.AggregateKinds().Measurement().DataKeys.Count] = count;
aggregate.Data[MetricConfigurations.Common.AggregateKinds().Measurement().DataKeys.Sum] = sum;
aggregate.Data[MetricConfigurations.Common.AggregateKinds().Measurement().DataKeys.Min] = min;
aggregate.Data[MetricConfigurations.Common.AggregateKinds().Measurement().DataKeys.Max] = max;
aggregate.Data[MetricConfigurations.Common.AggregateKinds().Measurement().DataKeys.StdDev] = stdDev;
DataSeries?.MetricIdentifier.MetricNamespace ?? Util.NullString,
DataSeries?.MetricIdentifier.MetricId ?? Util.NullString,
MetricSeriesConfigurationForMeasurement.Constants.AggregateKindMoniker);

aggregate.Data[MetricSeriesConfigurationForMeasurement.Constants.AggregateKindDataKeys.Count] = count;
aggregate.Data[MetricSeriesConfigurationForMeasurement.Constants.AggregateKindDataKeys.Sum] = sum;
aggregate.Data[MetricSeriesConfigurationForMeasurement.Constants.AggregateKindDataKeys.Min] = min;
aggregate.Data[MetricSeriesConfigurationForMeasurement.Constants.AggregateKindDataKeys.Max] = max;
aggregate.Data[MetricSeriesConfigurationForMeasurement.Constants.AggregateKindDataKeys.StdDev] = stdDev;

AddInfo_Timing_Dimensions_Context(aggregate, periodEnd);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,13 @@ internal bool IsCycleActive(MetricAggregationCycleKind aggregationCycleKind, out
return true;

case CycleKind.QuickPulse:
AggregatorCollection qpAggs = _aggregatorsForQuickPulse;
filter = qpAggs?.Filter;
return (qpAggs != null);
case CycleKind.Custom:
AggregatorCollection aggs = (aggregationCycleKind == CycleKind.QuickPulse) ? _aggregatorsForQuickPulse : _aggregatorsForCustom;
filter = aggs?.Filter;
return (aggs != null);
AggregatorCollection cAggs = _aggregatorsForCustom;
filter = cAggs?.Filter;
return (cAggs != null);

default:
throw new ArgumentException($"Unexpected value of {nameof(aggregationCycleKind)}: {aggregationCycleKind}.");
Expand Down Expand Up @@ -120,8 +123,8 @@ private static bool AddAggregator(IMetricSeriesAggregator aggregator, Aggregator
}

IMetricSeriesFilter seriesFilter = aggregatorCollection.Filter;
IMetricValueFilter valueFilter = null;
if (seriesFilter != null && !seriesFilter.WillConsume(aggregator.DataSeries, out valueFilter))
IMetricValueFilter valueFilter;
if (! Util.FilterWillConsume(seriesFilter, aggregator.DataSeries, out valueFilter))
{
return false;
}
Expand All @@ -140,7 +143,7 @@ private AggregationPeriodSummary CycleAggregators(
{
if (aggregators == _aggregatorsForPersistent)
{
throw new InvalidOperationException("Invernal SDK bug. Please report. Cannot cycle persistent aggregators.");
throw new InvalidOperationException("Internal SDK bug. Please report. Cannot cycle persistent aggregators.");
}

tactTimestamp = Util.RoundDownToSecond(tactTimestamp);
Expand All @@ -157,7 +160,10 @@ private AggregationPeriodSummary CycleAggregators(
prevAggregators = Interlocked.Exchange(ref aggregators, nextAggregators);
}

// Get persistent aggregations. We do this for any cycle kind, i.e. for whatever the aggregators collection was:
List<MetricAggregate> persistentValsAggregations = GetPersistentAggregations(tactTimestamp, prevAggregators?.Filter);

// Get non-persistent aggregations:
List<MetricAggregate> nonpersistentAggregations = GetNonpersistentAggregations(tactTimestamp, prevAggregators);

var summary = new AggregationPeriodSummary(persistentValsAggregations, nonpersistentAggregations);
Expand All @@ -184,9 +190,7 @@ private List<MetricAggregate> GetPersistentAggregations(DateTimeOffset tactTimes
// But we can apply the cycle's filters to determine whether or not to pull the aggregator
// for a aggregate at this time. Of course, only series filters, not value filters, can be considered.
IMetricValueFilter unusedValueFilter;
bool satisfiesFilter = (previousFilter == null)
||
(previousFilter.WillConsume(aggregator.DataSeries, out unusedValueFilter));
bool satisfiesFilter = Util.FilterWillConsume(previousFilter, aggregator.DataSeries, out unusedValueFilter);
if (satisfiesFilter)
{
MetricAggregate aggregate = aggregator.CompleteAggregation(tactTimestamp);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.Extensibility.Implementation;
using Microsoft.ApplicationInsights.Metrics.Extensibility;

namespace Microsoft.ApplicationInsights.Metrics
{
Expand Down Expand Up @@ -212,6 +213,81 @@ public static DateTimeOffset RoundDownToSecond(DateTimeOffset dto)
return new DateTimeOffset(dto.Year, dto.Month, dto.Day, dto.Hour, dto.Minute, dto.Second, 0, dto.Offset);
}

public static bool FilterWillConsume(IMetricSeriesFilter seriesFilter, MetricSeries series, out IMetricValueFilter valueFilter)
{
valueFilter = null;
try
{
return (seriesFilter == null) || (seriesFilter.WillConsume(series, out valueFilter));
}
catch
{
// Protect against errors in user's implemenmtation of IMetricSeriesFilter.WillConsume(..).
// If it throws, assume that the filter is not functional and accept all values.
return true;
}
}

public static bool FilterWillConsume(IMetricValueFilter valueFilter, MetricSeries series, double metricValue)
{
try
{
return (valueFilter == null) || (valueFilter.WillConsume(series, metricValue));
}
catch
{
// If user code in IMetricValueFilter.WillConsume(..) throws, assume that the filter is not functional and accept all values.
return true;
}
}

public static bool FilterWillConsume(IMetricValueFilter valueFilter, MetricSeries series, object metricValue)
{
try
{
return (valueFilter == null) || (valueFilter.WillConsume(series, metricValue));
}
catch
{
// If user code in IMetricValueFilter.WillConsume(..) throws, assume that the filter is not functional and accept all values.
return true;
}
}

public static int CombineHashCodes(int hash1)
{
int hash = 17;
unchecked
{
hash = hash * 23 + hash1;
}
return hash;
}

public static int CombineHashCodes(int hash1, int hash2)
{
int hash = 17;
unchecked
{
hash = hash * 23 + hash1;
hash = hash * 23 + hash2;
}
return hash;
}

public static int CombineHashCodes(int hash1, int hash2, int hash3, int hash4)
{
int hash = 17;
unchecked
{
hash = hash * 23 + hash1;
hash = hash * 23 + hash2;
hash = hash * 23 + hash3;
hash = hash * 23 + hash4;
}
return hash;
}

/// <summary>
/// We are working on adding a publically exposed method to a future version of the Core SDK so that the reflection employed here is not necesary.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.ComponentModel;

namespace Microsoft.ApplicationInsights.Metrics.Extensibility
{
/// <summary>
/// Provides discoverable access to constants used by metric aggregates.
/// Do not use directly. Instead, use: <c>MetricConfigurations.Common.Xxxx().Constants()</c>.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class MetricConfigurationExtensions
{
/// <summary>
/// Groups constants used my metric aggregates produced by aggregators that are configured by metric configurations represented
/// through instances of <see cref="MetricSeriesConfigurationForMeasurement"/>. See also <c>MetricConfigurations.Common.Measurement()</c>./>
/// </summary>
/// <param name="measurementConfig"></param>
/// <returns></returns>
public static MetricSeriesConfigurationForMeasurement.AggregateKindConstants Constants(this MetricSeriesConfigurationForMeasurement measurementConfig)
{
return MetricSeriesConfigurationForMeasurement.AggregateKindConstants.Instance;
}

/// <summary>
/// Groups constants used my metric aggregates produced by aggregators that are configured by metric configurations represented
/// through instances of <see cref="MetricConfigurationForMeasurement"/>. See also <c>MetricConfigurations.Common.Measurement()</c>./>
/// </summary>
/// <param name="measurementConfig"></param>
/// <returns></returns>
public static MetricSeriesConfigurationForMeasurement.AggregateKindConstants Constants(this MetricConfigurationForMeasurement measurementConfig)
{
return MetricSeriesConfigurationForMeasurement.AggregateKindConstants.Instance;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static class MetricExtensions
/// </summary>
/// <param name="metric"></param>
/// <returns></returns>
public static IMetricConfiguration GetConfiguration(this Metric metric)
public static MetricConfiguration GetConfiguration(this Metric metric)
{
return metric._configuration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,36 +67,5 @@ public static Task StopDefaultAggregationCycleAsync(this MetricManager metricMan
metricManager.Flush();
return metricManager.AggregationCycle.StopAsync();
}

/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="metricManager"></param>
/// <param name="newExtensionStateInstanceFactory"></param>
/// <returns></returns>
public static T GetOrCreateExtensionState<T>(this MetricManager metricManager, Func<MetricManager, T> newExtensionStateInstanceFactory)
where T : class
{
Util.ValidateNotNull(metricManager, nameof(metricManager));

object cache = metricManager.GetOrCreateExtensionStateUnsafe(newExtensionStateInstanceFactory);

if (cache == null)
{
return null;
}

T typedCache = cache as T;
if (typedCache == null)
{
throw new InvalidOperationException($"{nameof(MetricManagerExtensions)}.{nameof(GetOrCreateExtensionState)}<T>(..) expected to find a"
+ $" cache of type {typeof(T).FullName}, but the present cache was of"
+ $" type {cache.GetType().FullName}. This indicates that multiple extensions attempt to use"
+ $" this extension point of the {nameof(MetricManager)} in a conflicting manner.");
}

return typedCache;
}
}
}
Loading

0 comments on commit ef72e14

Please sign in to comment.