prometheus-net

Форк
0
/
LabelEnrichingAutoLeasingMetric.cs 
82 строки · 3.1 Кб
1
using System.Buffers;
2

3
namespace Prometheus;
4

5
internal sealed class LabelEnrichingAutoLeasingMetric<TMetric> : ICollector<TMetric>
6
    where TMetric : ICollectorChild
7
{
8
    public LabelEnrichingAutoLeasingMetric(ICollector<TMetric> inner, string[] enrichWithLabelValues)
9
    {
10
        _inner = inner;
11
        _enrichWithLabelValues = enrichWithLabelValues;
12
    }
13

14
    private readonly ICollector<TMetric> _inner;
15
    private readonly string[] _enrichWithLabelValues;
16

17
    public TMetric Unlabelled
18
    {
19
        get
20
        {
21
            // If we are not provided any custom label values, we can be pretty sure the label values are not going to change
22
            // between calls, so reuse a buffer to avoid allocations when passing the data to the inner instance.
23
            var buffer = ArrayPool<string>.Shared.Rent(_enrichWithLabelValues.Length);
24

25
            try
26
            {
27
                _enrichWithLabelValues.CopyTo(buffer, 0);
28
                var finalLabelValues = buffer.AsSpan(0, _enrichWithLabelValues.Length);
29

30
                return _inner.WithLabels(finalLabelValues);
31
            }
32
            finally
33
            {
34
                ArrayPool<string>.Shared.Return(buffer);
35
            }
36
        }
37
    }
38

39
    public string Name => _inner.Name;
40
    public string Help => _inner.Help;
41

42
    // We do not display the enriched labels, they are transparent - this is only the instance-specific label names.
43
    public string[] LabelNames => _inner.LabelNames;
44

45
    public TMetric WithLabels(params string[] labelValues)
46
    {
47
        // The caller passing us string[] does not signal that the allocation is not needed - in all likelihood it is.
48
        // However, we do not want to allocate two arrays here (because we need to concatenate as well) so instead we
49
        // use the reusable-buffer overload to avoid at least one of the allocations.
50

51
        return WithLabels(labelValues.AsSpan());
52
    }
53

54
    public TMetric WithLabels(ReadOnlyMemory<string> labelValues)
55
    {
56
        // The caller passing us ReadOnlyMemory does not signal that the allocation is not needed - in all likelihood it is.
57
        // However, we do not want to allocate two arrays here (because we need to concatenate as well) so instead we
58
        // use the reusable-buffer overload to avoid at least one of the allocations.
59

60
        return WithLabels(labelValues.Span);
61
    }
62

63
    public TMetric WithLabels(ReadOnlySpan<string> labelValues)
64
    {
65
        // The ReadOnlySpan overload suggests that the label values may already be known to the metric,
66
        // so we should strongly avoid allocating memory here. Thus we copy everything to a reusable buffer.
67
        var buffer = ArrayPool<string>.Shared.Rent(_enrichWithLabelValues.Length + labelValues.Length);
68

69
        try
70
        {
71
            _enrichWithLabelValues.CopyTo(buffer, 0);
72
            labelValues.CopyTo(buffer.AsSpan(_enrichWithLabelValues.Length));
73
            var finalLabelValues = buffer.AsSpan(0, _enrichWithLabelValues.Length + labelValues.Length);
74

75
            return _inner.WithLabels(finalLabelValues);
76
        }
77
        finally
78
        {
79
            ArrayPool<string>.Shared.Return(buffer);
80
        }
81
    }
82
}
83

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.