aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/encode/prometheus/prometheus_model.h
blob: cb7f2cb15b7bc9eb7285416932cc88ccc7607cea (plain) (blame)
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
#pragma once

#include <util/generic/strbuf.h>


namespace NMonitoring {
namespace NPrometheus {

    //
    // Prometheus specific names and validation rules.
    //
    // See https://github.com/prometheus/docs/blob/master/content/docs/instrumenting/exposition_formats.md
    // and https://github.com/prometheus/common/blob/master/expfmt/text_parse.go
    //

    inline constexpr TStringBuf BUCKET_SUFFIX = "_bucket";
    inline constexpr TStringBuf COUNT_SUFFIX = "_count";
    inline constexpr TStringBuf SUM_SUFFIX = "_sum";
    inline constexpr TStringBuf MIN_SUFFIX = "_min";
    inline constexpr TStringBuf MAX_SUFFIX = "_max";
    inline constexpr TStringBuf LAST_SUFFIX = "_last";

    // Used for the label that defines the upper bound of a bucket of a
    // histogram ("le" -> "less or equal").
    inline constexpr TStringBuf BUCKET_LABEL = "le";


    inline bool IsValidLabelNameStart(char ch) {
        return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_';
    }

    inline bool IsValidLabelNameContinuation(char ch) {
        return IsValidLabelNameStart(ch) || (ch >= '0' && ch <= '9');
    }

    inline bool IsValidMetricNameStart(char ch) {
        return IsValidLabelNameStart(ch) || ch == ':';
    }

    inline bool IsValidMetricNameContinuation(char ch) {
        return IsValidLabelNameContinuation(ch) || ch == ':';
    }

    inline bool IsSum(TStringBuf name) {
        return name.EndsWith(SUM_SUFFIX);
    }

    inline bool IsCount(TStringBuf name) {
        return name.EndsWith(COUNT_SUFFIX);
    }

    inline bool IsBucket(TStringBuf name) {
        return name.EndsWith(BUCKET_SUFFIX);
    }

    inline TStringBuf ToBaseName(TStringBuf name) {
        if (IsBucket(name)) {
            return name.SubString(0, name.length() - BUCKET_SUFFIX.length());
        }
        if (IsCount(name)) {
            return name.SubString(0, name.length() - COUNT_SUFFIX.length());
        }
        if (IsSum(name)) {
            return name.SubString(0, name.length() - SUM_SUFFIX.length());
        }
        return name;
    }

} // namespace NPrometheus
} // namespace NMonitoring