LokiQueryModeller.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { LokiAndPromQueryModellerBase } from '../../prometheus/querybuilder/shared/LokiAndPromQueryModellerBase';
  2. import { QueryBuilderLabelFilter } from '../../prometheus/querybuilder/shared/types';
  3. import { getOperationDefinitions } from './operations';
  4. import { LokiOperationId, LokiQueryPattern, LokiVisualQueryOperationCategory } from './types';
  5. export class LokiQueryModeller extends LokiAndPromQueryModellerBase {
  6. constructor() {
  7. super(getOperationDefinitions);
  8. this.setOperationCategories([
  9. LokiVisualQueryOperationCategory.Aggregations,
  10. LokiVisualQueryOperationCategory.RangeFunctions,
  11. LokiVisualQueryOperationCategory.Formats,
  12. LokiVisualQueryOperationCategory.BinaryOps,
  13. LokiVisualQueryOperationCategory.LabelFilters,
  14. LokiVisualQueryOperationCategory.LineFilters,
  15. ]);
  16. }
  17. renderLabels(labels: QueryBuilderLabelFilter[]) {
  18. if (labels.length === 0) {
  19. return '{}';
  20. }
  21. return super.renderLabels(labels);
  22. }
  23. getQueryPatterns(): LokiQueryPattern[] {
  24. return [
  25. {
  26. name: 'Log query with parsing',
  27. // {} | logfmt | __error__=``
  28. operations: [
  29. { id: LokiOperationId.Logfmt, params: [] },
  30. { id: LokiOperationId.LabelFilterNoErrors, params: [] },
  31. ],
  32. },
  33. {
  34. name: 'Log query with filtering and parsing',
  35. // {} |= `` | logfmt | __error__=``
  36. operations: [
  37. { id: LokiOperationId.LineContains, params: [''] },
  38. { id: LokiOperationId.Logfmt, params: [] },
  39. { id: LokiOperationId.LabelFilterNoErrors, params: [] },
  40. ],
  41. },
  42. {
  43. name: 'Log query with parsing and label filter',
  44. // {} |= `` | logfmt | __error__=`` | label=`value`
  45. operations: [
  46. { id: LokiOperationId.LineContains, params: [''] },
  47. { id: LokiOperationId.Logfmt, params: [] },
  48. { id: LokiOperationId.LabelFilterNoErrors, params: [] },
  49. { id: LokiOperationId.LabelFilter, params: ['label', '=', 'value'] },
  50. ],
  51. },
  52. {
  53. name: 'Log query with parsing of nested json',
  54. // {} |= `` | json | line_format `{{ .message}}` | json
  55. operations: [
  56. { id: LokiOperationId.LineContains, params: [''] },
  57. { id: LokiOperationId.Json, params: [] },
  58. { id: LokiOperationId.LabelFilterNoErrors, params: [] },
  59. { id: LokiOperationId.LineFormat, params: ['{{.message}}'] },
  60. { id: LokiOperationId.Json, params: [] },
  61. { id: LokiOperationId.LabelFilterNoErrors, params: [] },
  62. ],
  63. },
  64. {
  65. name: 'Log query with reformatted log line',
  66. // {} |= `` | logfmt | line_format `{{.message}}`
  67. operations: [
  68. { id: LokiOperationId.LineContains, params: [''] },
  69. { id: LokiOperationId.Logfmt, params: [] },
  70. { id: LokiOperationId.LabelFilterNoErrors, params: [] },
  71. { id: LokiOperationId.LineFormat, params: ['{{.message}}'] },
  72. ],
  73. },
  74. {
  75. name: 'Log query with mapped log level',
  76. // {} |= `` | logfmt | label_format level=lvl
  77. operations: [
  78. { id: LokiOperationId.LineContains, params: [''] },
  79. { id: LokiOperationId.Logfmt, params: [] },
  80. { id: LokiOperationId.LabelFilterNoErrors, params: [] },
  81. { id: LokiOperationId.LabelFormat, params: ['lvl', 'level'] },
  82. ],
  83. },
  84. {
  85. name: 'Metrics query on value inside log line',
  86. // sum(sum_over_time({ | logfmt | __error__=`` | unwrap | __error__=`` [$__interval]))
  87. operations: [
  88. { id: LokiOperationId.LineContains, params: [''] },
  89. { id: LokiOperationId.Logfmt, params: [] },
  90. { id: LokiOperationId.LabelFilterNoErrors, params: [] },
  91. { id: LokiOperationId.Unwrap, params: [''] },
  92. { id: LokiOperationId.LabelFilterNoErrors, params: [] },
  93. { id: LokiOperationId.SumOverTime, params: ['$__interval'] },
  94. { id: LokiOperationId.Sum, params: [] },
  95. ],
  96. },
  97. {
  98. name: 'Metrics query for total requests per label of streams',
  99. // sum by() (count_over_time({}[$__interval)
  100. operations: [
  101. { id: LokiOperationId.LineContains, params: [''] },
  102. { id: LokiOperationId.CountOverTime, params: ['$__interval'] },
  103. { id: LokiOperationId.Sum, params: [] },
  104. ],
  105. },
  106. {
  107. name: 'Metrics query for total requests per parsed label or label of streams',
  108. // sum by() (count_over_time({}| logfmt | __error__=`` [$__interval))
  109. operations: [
  110. { id: LokiOperationId.LineContains, params: [''] },
  111. { id: LokiOperationId.Logfmt, params: [] },
  112. { id: LokiOperationId.LabelFilterNoErrors, params: [] },
  113. { id: LokiOperationId.CountOverTime, params: ['$__interval'] },
  114. { id: LokiOperationId.Sum, params: [] },
  115. ],
  116. },
  117. {
  118. name: 'Metrics query for bytes used by log stream',
  119. // bytes_over_time({}[$__interval])
  120. operations: [
  121. { id: LokiOperationId.LineContains, params: [''] },
  122. { id: LokiOperationId.BytesOverTime, params: ['$__interval'] },
  123. ],
  124. },
  125. {
  126. name: 'Metrics query for count of log lines per stream',
  127. // count_over_time({}[$__interval])
  128. operations: [
  129. { id: LokiOperationId.LineContains, params: [''] },
  130. { id: LokiOperationId.CountOverTime, params: ['$__interval'] },
  131. ],
  132. },
  133. {
  134. name: 'Metrics query for top n results by label or parsed label',
  135. // topk(10, sum by () (count_over_time({} | logfmt | __error__=`` [$__interval])))
  136. operations: [
  137. { id: LokiOperationId.Logfmt, params: [] },
  138. { id: LokiOperationId.LabelFilterNoErrors, params: [] },
  139. { id: LokiOperationId.CountOverTime, params: ['$__interval'] },
  140. { id: LokiOperationId.Sum, params: [] },
  141. { id: LokiOperationId.TopK, params: [10] },
  142. ],
  143. },
  144. {
  145. name: 'Metrics query for extracted quantile',
  146. // quantile_over_time(0.5,{} | logfmt | unwrap latency[$__interval]) by ()
  147. operations: [
  148. { id: LokiOperationId.Logfmt, params: [] },
  149. { id: LokiOperationId.LabelFilterNoErrors, params: [] },
  150. { id: LokiOperationId.Unwrap, params: ['latency'] },
  151. { id: LokiOperationId.LabelFilterNoErrors, params: [] },
  152. { id: LokiOperationId.QuantileOverTime, params: [0.5, '$__interval'] },
  153. { id: LokiOperationId.Sum, params: [] },
  154. ],
  155. },
  156. ];
  157. }
  158. }
  159. export const lokiQueryModeller = new LokiQueryModeller();