-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathMetricSourceConfiguration.cs
More file actions
96 lines (79 loc) · 3.81 KB
/
MetricSourceConfiguration.cs
File metadata and controls
96 lines (79 loc) · 3.81 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.Diagnostics.NETCore.Client;
using Microsoft.Diagnostics.Tracing.Parsers;
using System;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Globalization;
using System.Linq;
using System.Text;
namespace Microsoft.Diagnostics.Monitoring.EventPipe
{
[Flags]
public enum MetricType
{
EventCounter = 0x1,
Meter = 0x2,
All = 0xFF
}
public sealed class MetricEventPipeProvider
{
public string Provider { get; set; }
public float? IntervalSeconds { get; set; }
public MetricType Type { get; set; } = MetricType.All;
}
public sealed class MetricSourceConfiguration : MonitoringSourceConfiguration
{
private readonly IList<EventPipeProvider> _eventPipeProviders;
public string SessionId { get; private set; }
public MetricSourceConfiguration(float metricIntervalSeconds, IEnumerable<string> eventCounterProviderNames)
: this(metricIntervalSeconds, CreateProviders(eventCounterProviderNames?.Any() == true ? eventCounterProviderNames : DefaultMetricProviders))
{
}
public MetricSourceConfiguration(float metricIntervalSeconds, IEnumerable<MetricEventPipeProvider> providers, int maxHistograms = 20, int maxTimeSeries = 1000)
{
if (providers == null)
{
throw new ArgumentNullException(nameof(providers));
}
RequestRundown = false;
_eventPipeProviders = providers.Where(provider => provider.Type.HasFlag(MetricType.EventCounter))
.Select((MetricEventPipeProvider provider) => new EventPipeProvider(provider.Provider,
EventLevel.Informational,
(long)ClrTraceEventParser.Keywords.None,
new Dictionary<string, string>()
{
{
"EventCounterIntervalSec", (provider.IntervalSeconds ?? metricIntervalSeconds).ToString(CultureInfo.InvariantCulture)
}
})).ToList();
IEnumerable<MetricEventPipeProvider> meterProviders = providers.Where(provider => provider.Type.HasFlag(MetricType.Meter));
if (meterProviders.Any())
{
const long TimeSeriesValues = 0x2;
string metrics = string.Join(',', meterProviders.Select(p => p.Provider));
SessionId = Guid.NewGuid().ToString();
EventPipeProvider metricsEventSourceProvider =
new EventPipeProvider("System.Diagnostics.Metrics", EventLevel.Informational, TimeSeriesValues,
new Dictionary<string, string>()
{
{ "SessionId", SessionId },
{ "Metrics", metrics },
{ "RefreshInterval", metricIntervalSeconds.ToString(CultureInfo.InvariantCulture) },
{ "MaxTimeSeries", maxTimeSeries.ToString() },
{ "MaxHistograms", maxHistograms.ToString() }
}
);
_eventPipeProviders = _eventPipeProviders.Append(metricsEventSourceProvider).ToArray();
}
}
private static IEnumerable<MetricEventPipeProvider> CreateProviders(IEnumerable<string> providers) =>
providers.Select(provider => new MetricEventPipeProvider {
Provider = provider,
Type = MetricType.EventCounter
});
public override IList<EventPipeProvider> GetProviders() => _eventPipeProviders;
}
}