-
Notifications
You must be signed in to change notification settings - Fork 730
/
MeasurementExtensions.cs
103 lines (92 loc) · 5.16 KB
/
MeasurementExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Linq;
namespace Microsoft.Extensions.Diagnostics.Metrics.Testing;
/// <summary>
/// Extensions to simplify working with lists of measurements.
/// </summary>
public static class MeasurementExtensions
{
/// <summary>
/// Filters a list of measurements based on subset tags matching.
/// </summary>
/// <typeparam name="T">The type of measurement value.</typeparam>
/// <param name="measurements">The original full list of measurements.</param>
/// <param name="tags">The set of tags to match against. Only measurements that have at least these matching tags are returned.</param>
/// <returns>A list of matching measurements.</returns>
public static IEnumerable<CollectedMeasurement<T>> ContainsTags<T>(this IEnumerable<CollectedMeasurement<T>> measurements, params KeyValuePair<string, object?>[] tags)
where T : struct
=> measurements.Where(m => m.ContainsTags(tags));
/// <summary>
/// Filters a list of measurements based on subset tag matching.
/// </summary>
/// <typeparam name="T">The type of measurement value.</typeparam>
/// <param name="measurements">The original full list of measurements.</param>
/// <param name="tags">The set of tags to match against. Only measurements that have at least these matching tag names are returned.</param>
/// <returns>A list of matching measurements.</returns>
public static IEnumerable<CollectedMeasurement<T>> ContainsTags<T>(this IEnumerable<CollectedMeasurement<T>> measurements, params string[] tags)
where T : struct
=> measurements.Where(m => m.ContainsTags(tags));
/// <summary>
/// Filters a list of measurements based on exact tag matching.
/// </summary>
/// <typeparam name="T">The type of measurement value.</typeparam>
/// <param name="measurements">The original full list of measurements.</param>
/// <param name="tags">The set of tags to match against. Only measurements that have exactly those matching tags are returned.</param>
/// <returns>A list of matching measurements.</returns>
public static IEnumerable<CollectedMeasurement<T>> MatchesTags<T>(this IEnumerable<CollectedMeasurement<T>> measurements, params KeyValuePair<string, object?>[] tags)
where T : struct
=> measurements.Where(m => m.MatchesTags(tags));
/// <summary>
/// Filters a list of measurements based on exact tag name matching.
/// </summary>
/// <typeparam name="T">The type of measurement value.</typeparam>
/// <param name="measurements">The original full list of measurements.</param>
/// <param name="tags">The set of tags to match against. Only measurements that have exactly those matching tag names are returned.</param>
/// <returns>A list of matching measurements.</returns>
public static IEnumerable<CollectedMeasurement<T>> MatchesTags<T>(this IEnumerable<CollectedMeasurement<T>> measurements, params string[] tags)
where T : struct
=> measurements.Where(m => m.MatchesTags(tags));
/// <summary>
/// Process the series of measurements adding all values together to produce a final count, identical to what a <see cref="Counter{T}" /> instrument would produce.
/// </summary>
/// <typeparam name="T">The type of measurement value.</typeparam>
/// <param name="measurements">The list of measurements to process.</param>
/// <returns>The resulting count.</returns>
public static T EvaluateAsCounter<T>(this IEnumerable<CollectedMeasurement<T>> measurements)
where T : struct
{
#pragma warning disable CS8509 // The switch expression does not handle all possible values of its input type (it is not exhaustive).
return measurements switch
{
IEnumerable<CollectedMeasurement<byte>> l => (T)(object)ByteSum(l),
IEnumerable<CollectedMeasurement<short>> l => (T)(object)ShortSum(l),
IEnumerable<CollectedMeasurement<int>> l => (T)(object)l.Sum(m => m.Value),
IEnumerable<CollectedMeasurement<long>> l => (T)(object)l.Sum(m => m.Value),
IEnumerable<CollectedMeasurement<float>> l => (T)(object)l.Sum(m => m.Value),
IEnumerable<CollectedMeasurement<double>> l => (T)(object)l.Sum(m => m.Value),
IEnumerable<CollectedMeasurement<decimal>> l => (T)(object)l.Sum(m => m.Value),
};
#pragma warning restore CS8509 // The switch expression does not handle all possible values of its input type (it is not exhaustive).
static byte ByteSum(IEnumerable<CollectedMeasurement<byte>> measurements)
{
byte sum = 0;
foreach (var measurement in measurements)
{
sum += measurement.Value;
}
return sum;
}
static short ShortSum(IEnumerable<CollectedMeasurement<short>> measurements)
{
short sum = 0;
foreach (var measurement in measurements)
{
sum += measurement.Value;
}
return sum;
}
}
}