-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathBreakdownMetricsProvider.cs
112 lines (95 loc) · 3.04 KB
/
BreakdownMetricsProvider.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
104
105
106
107
108
109
110
111
112
// Licensed to Elasticsearch B.V under
// one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information
using System.Collections.Generic;
using System.Linq;
using Elastic.Apm.Api;
using Elastic.Apm.Helpers;
using Elastic.Apm.Logging;
using Elastic.Apm.Model;
namespace Elastic.Apm.Metrics.MetricsProvider
{
internal class BreakdownMetricsProvider : IMetricsProvider
{
internal const string SpanSelfTime = "span.self_time";
private readonly List<MetricSet> _itemsToSend = new();
private readonly IApmLogger _logger;
private readonly object _lock = new();
private int _transactionCount;
public int ConsecutiveNumberOfFailedReads { get; set; }
public string DbgName => nameof(BreakdownMetricsProvider);
public BreakdownMetricsProvider(IApmLogger logger) => _logger = logger.Scoped(nameof(BreakdownMetricsProvider));
public bool IsMetricAlreadyCaptured
{
get
{
lock (_lock)
return _itemsToSend.Count > 0;
}
}
public bool IsEnabled(IReadOnlyList<WildcardMatcher> matchers) => !WildcardMatcher.IsAnyMatch(matchers, SpanSelfTime);
public void CaptureTransaction(Transaction transaction)
{
lock (_lock)
{
_transactionCount++;
var timestampNow = TimeUtils.TimestampNow();
var loggedWarning = false;
foreach (var item in transaction.SpanTimings)
{
var metricSet =
new MetricSet(timestampNow,
new List<MetricSample>
{
new($"{SpanSelfTime}.count", item.Value.Count), new($"{SpanSelfTime}.sum.us", item.Value.TotalDuration * 1000)
})
{
Span = new SpanInfo { Type = item.Key.Type, SubType = item.Key.SubType },
Transaction = new TransactionInfo { Name = transaction.Name, Type = transaction.Type }
};
if (_itemsToSend.Count < 1000)
_itemsToSend.Add(metricSet);
else
{
if (loggedWarning) continue;
_logger.Warning()
?.Log(
"The limit of 1000 metricsets has been reached, no new metricsets will be created.");
loggedWarning = true;
}
}
var transactionMetric =
new MetricSet(timestampNow,
new List<MetricSample>
{
new("transaction.duration.count", _transactionCount),
new("transaction.duration.sum.us", transaction.Duration!.Value * 1000),
new("transaction.breakdown.count", _transactionCount),
}) { Transaction = new TransactionInfo { Name = transaction.Name, Type = transaction.Type } };
if (_itemsToSend.Count < 1000)
_itemsToSend.Add(transactionMetric);
else
{
if (!loggedWarning)
{
_logger.Warning()
?.Log(
"The limit of 1000 metricsets has been reached, no new metricsets will be created.");
}
}
}
}
public IEnumerable<MetricSet> GetSamples()
{
var retVal = new List<MetricSet>(_itemsToSend.Count < 1000 ? _itemsToSend.Count : 1000);
lock (_lock)
{
retVal.AddRange(_itemsToSend);
_itemsToSend.Clear();
_transactionCount = 0;
}
return retVal;
}
}
}