prometheus_model.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #pragma once
  2. #include <util/generic/strbuf.h>
  3. namespace NMonitoring {
  4. namespace NPrometheus {
  5. //
  6. // Prometheus specific names and validation rules.
  7. //
  8. // See https://github.com/prometheus/docs/blob/master/content/docs/instrumenting/exposition_formats.md
  9. // and https://github.com/prometheus/common/blob/master/expfmt/text_parse.go
  10. //
  11. inline constexpr TStringBuf BUCKET_SUFFIX = "_bucket";
  12. inline constexpr TStringBuf COUNT_SUFFIX = "_count";
  13. inline constexpr TStringBuf SUM_SUFFIX = "_sum";
  14. inline constexpr TStringBuf MIN_SUFFIX = "_min";
  15. inline constexpr TStringBuf MAX_SUFFIX = "_max";
  16. inline constexpr TStringBuf LAST_SUFFIX = "_last";
  17. // Used for the label that defines the upper bound of a bucket of a
  18. // histogram ("le" -> "less or equal").
  19. inline constexpr TStringBuf BUCKET_LABEL = "le";
  20. inline bool IsValidLabelNameStart(char ch) {
  21. return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_';
  22. }
  23. inline bool IsValidLabelNameContinuation(char ch) {
  24. return IsValidLabelNameStart(ch) || (ch >= '0' && ch <= '9');
  25. }
  26. inline bool IsValidMetricNameStart(char ch) {
  27. return IsValidLabelNameStart(ch) || ch == ':';
  28. }
  29. inline bool IsValidMetricNameContinuation(char ch) {
  30. return IsValidLabelNameContinuation(ch) || ch == ':';
  31. }
  32. inline bool IsSum(TStringBuf name) {
  33. return name.EndsWith(SUM_SUFFIX);
  34. }
  35. inline bool IsCount(TStringBuf name) {
  36. return name.EndsWith(COUNT_SUFFIX);
  37. }
  38. inline bool IsBucket(TStringBuf name) {
  39. return name.EndsWith(BUCKET_SUFFIX);
  40. }
  41. inline TStringBuf ToBaseName(TStringBuf name) {
  42. if (IsBucket(name)) {
  43. return name.SubString(0, name.length() - BUCKET_SUFFIX.length());
  44. }
  45. if (IsCount(name)) {
  46. return name.SubString(0, name.length() - COUNT_SUFFIX.length());
  47. }
  48. if (IsSum(name)) {
  49. return name.SubString(0, name.length() - SUM_SUFFIX.length());
  50. }
  51. return name;
  52. }
  53. } // namespace NPrometheus
  54. } // namespace NMonitoring