From 2d2e5955551cf53518d342a9821c8920e07dd631 Mon Sep 17 00:00:00 2001 From: Paulo Dias <44772900+paulojmdias@users.noreply.github.com> Date: Fri, 21 Feb 2025 17:11:16 +0000 Subject: [PATCH 01/26] Alerting: Add multiple threshold operators (#99516) The following operators are being added: - Equal - Not Equal - Greater or Equal - Less or Equal - Within Range Inclusive - Outside Range Inclusive --- .betterer.results | 7 +- pkg/expr/classic/evaluator.go | 16 +- pkg/expr/classic/evaluator_test.go | 42 ++ pkg/expr/query.panel.schema.json | 18 +- pkg/expr/query.request.schema.json | 18 +- pkg/expr/query.types.json | 18 +- pkg/expr/threshold.go | 100 ++++- pkg/expr/threshold_test.go | 90 ++++ .../alerting/state/ThresholdMapper.ts | 47 +++ .../app/features/alerting/state/alertDef.ts | 12 + .../unified/GrafanaRuleQueryViewer.tsx | 7 +- .../unified/components/rule-editor/util.ts | 52 ++- .../expressions/components/Condition.tsx | 5 +- .../expressions/components/Threshold.tsx | 384 +++++++++++++----- .../components/thresholdReducer.ts | 61 ++- public/app/features/expressions/types.ts | 6 + public/locales/en-US/grafana.json | 13 + public/locales/pseudo-LOCALE/grafana.json | 13 + 18 files changed, 781 insertions(+), 128 deletions(-) diff --git a/.betterer.results b/.betterer.results index 89f038771ec..4197643351c 100644 --- a/.betterer.results +++ b/.betterer.results @@ -4991,12 +4991,7 @@ exports[`better eslint`] = { [0, 0, 0, "No untranslated strings in text props. Wrap text with or use t()", "4"] ], "public/app/features/expressions/components/Threshold.tsx:5381": [ - [0, 0, 0, "No untranslated strings in text props. Wrap text with or use t()", "0"], - [0, 0, 0, "No untranslated strings in text props. Wrap text with or use t()", "1"], - [0, 0, 0, "No untranslated strings in text props. Wrap text with or use t()", "2"], - [0, 0, 0, "No untranslated strings in text props. Wrap text with or use t()", "3"], - [0, 0, 0, "No untranslated strings in text props. Wrap text with or use t()", "4"], - [0, 0, 0, "No untranslated strings in text props. Wrap text with or use t()", "5"] + [0, 0, 0, "No untranslated strings in text props. Wrap text with or use t()", "0"] ], "public/app/features/expressions/guards.ts:5381": [ [0, 0, 0, "\'@grafana/runtime/src/utils/DataSourceWithBackend\' import is restricted from being used by a pattern. Import from the public export instead.", "0"], diff --git a/pkg/expr/classic/evaluator.go b/pkg/expr/classic/evaluator.go index 9c1117f057f..9f38bf2136e 100644 --- a/pkg/expr/classic/evaluator.go +++ b/pkg/expr/classic/evaluator.go @@ -48,9 +48,9 @@ func (rangedEvaluator) Kind() EvaluatorKind { // an AlertEvaluator depending on evaluation operator. func newAlertEvaluator(model ConditionEvalJSON) (evaluator, error) { switch model.Type { - case "gt", "lt": + case "gt", "lt", "eq", "ne", "gte", "lte": return newThresholdEvaluator(model) - case "within_range", "outside_range": + case "within_range", "outside_range", "within_range_included", "outside_range_included": return newRangedEvaluator(model) case "no_value": return &noValueEvaluator{}, nil @@ -70,6 +70,14 @@ func (e *thresholdEvaluator) Eval(reducedValue mathexp.Number) bool { return *fv > e.Threshold case "lt": return *fv < e.Threshold + case "eq": + return *fv == e.Threshold + case "ne": + return *fv != e.Threshold + case "gte": + return *fv >= e.Threshold + case "lte": + return *fv <= e.Threshold } return false @@ -113,6 +121,10 @@ func (e *rangedEvaluator) Eval(reducedValue mathexp.Number) bool { return (e.Lower < *fv && e.Upper > *fv) || (e.Upper < *fv && e.Lower > *fv) case "outside_range": return (e.Upper < *fv && e.Lower < *fv) || (e.Upper > *fv && e.Lower > *fv) + case "within_range_included": + return (e.Lower <= *fv && e.Upper >= *fv) || (e.Upper <= *fv && e.Lower >= *fv) + case "outside_range_included": + return (e.Upper <= *fv && e.Lower <= *fv) || (e.Upper >= *fv && e.Lower >= *fv) } return false diff --git a/pkg/expr/classic/evaluator_test.go b/pkg/expr/classic/evaluator_test.go index 92aa7ad2a40..4a82b1c920c 100644 --- a/pkg/expr/classic/evaluator_test.go +++ b/pkg/expr/classic/evaluator_test.go @@ -40,6 +40,48 @@ func TestThresholdEvaluator(t *testing.T) { inputNumber: newNumber(util.Pointer(1.0)), expected: true, }, + { + name: "value 1 is eq 1: false", + evaluator: &thresholdEvaluator{"eq", 1}, + inputNumber: newNumber(util.Pointer(1.0)), + expected: true, + }, + { + name: "value 0 is eq 0: false", + evaluator: &thresholdEvaluator{"eq", 0}, + inputNumber: newNumber(util.Pointer(0.0)), + expected: true, + }, + { + name: "value 1 is eq 0: false", + evaluator: &thresholdEvaluator{"eq", 0}, + inputNumber: newNumber(util.Pointer(1.0)), + expected: false, + }, + { + name: "value 0 is eq 1: false", + evaluator: &thresholdEvaluator{"eq", 1}, + inputNumber: newNumber(util.Pointer(0.0)), + expected: false, + }, + { + name: "value 1 is ne 1: false", + evaluator: &thresholdEvaluator{"ne", 1}, + inputNumber: newNumber(util.Pointer(1.0)), + expected: false, + }, + { + name: "value 3 is gte 3: false", + evaluator: &thresholdEvaluator{"gte", 3}, + inputNumber: newNumber(util.Pointer(3.0)), + expected: true, + }, + { + name: "value 5 is lte 4: false", + evaluator: &thresholdEvaluator{"lte", 4}, + inputNumber: newNumber(util.Pointer(5.0)), + expected: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/pkg/expr/query.panel.schema.json b/pkg/expr/query.panel.schema.json index 388781100ee..4437e10b82e 100644 --- a/pkg/expr/query.panel.schema.json +++ b/pkg/expr/query.panel.schema.json @@ -712,8 +712,14 @@ "enum": [ "gt", "lt", + "eq", + "ne", + "gte", + "lte", "within_range", - "outside_range" + "outside_range", + "within_range_included", + "outside_range_included" ], "x-enum-description": {} } @@ -744,8 +750,14 @@ "enum": [ "gt", "lt", + "eq", + "ne", + "gte", + "lte", "within_range", - "outside_range" + "outside_range", + "within_range_included", + "outside_range_included" ], "x-enum-description": {} } @@ -1013,4 +1025,4 @@ }, "additionalProperties": true, "$schema": "https://json-schema.org/draft-04/schema#" -} \ No newline at end of file +} diff --git a/pkg/expr/query.request.schema.json b/pkg/expr/query.request.schema.json index aa08911fc3c..5e8c4a60371 100644 --- a/pkg/expr/query.request.schema.json +++ b/pkg/expr/query.request.schema.json @@ -754,8 +754,14 @@ "enum": [ "gt", "lt", + "eq", + "ne", + "gte", + "lte", "within_range", - "outside_range" + "outside_range", + "within_range_included", + "outside_range_included" ], "x-enum-description": {} } @@ -786,8 +792,14 @@ "enum": [ "gt", "lt", + "eq", + "ne", + "gte", + "lte", "within_range", - "outside_range" + "outside_range", + "within_range_included", + "outside_range_included" ], "x-enum-description": {} } @@ -1071,4 +1083,4 @@ }, "additionalProperties": false, "$schema": "https://json-schema.org/draft-04/schema#" -} \ No newline at end of file +} diff --git a/pkg/expr/query.types.json b/pkg/expr/query.types.json index 092abaa7393..b24de7a4319 100644 --- a/pkg/expr/query.types.json +++ b/pkg/expr/query.types.json @@ -395,8 +395,14 @@ "enum": [ "gt", "lt", + "eq", + "ne", + "gte", + "lte", "within_range", - "outside_range" + "outside_range", + "within_range_included", + "outside_range_included" ], "type": "string", "x-enum-description": {} @@ -427,8 +433,14 @@ "enum": [ "gt", "lt", + "eq", + "ne", + "gte", + "lte", "within_range", - "outside_range" + "outside_range", + "within_range_included", + "outside_range_included" ], "type": "string", "x-enum-description": {} @@ -579,4 +591,4 @@ } } ] -} \ No newline at end of file +} diff --git a/pkg/expr/threshold.go b/pkg/expr/threshold.go index 127de9ce8a7..d07a5d7676e 100644 --- a/pkg/expr/threshold.go +++ b/pkg/expr/threshold.go @@ -32,18 +32,30 @@ type ThresholdCommand struct { type ThresholdType string const ( - ThresholdIsAbove ThresholdType = "gt" - ThresholdIsBelow ThresholdType = "lt" - ThresholdIsWithinRange ThresholdType = "within_range" - ThresholdIsOutsideRange ThresholdType = "outside_range" + ThresholdIsAbove ThresholdType = "gt" + ThresholdIsBelow ThresholdType = "lt" + ThresholdIsEqual ThresholdType = "eq" + ThresholdIsNotEqual ThresholdType = "ne" + ThresholdIsGreaterThanEqual ThresholdType = "gte" + ThresholdIsLessThanEqual ThresholdType = "lte" + ThresholdIsWithinRange ThresholdType = "within_range" + ThresholdIsOutsideRange ThresholdType = "outside_range" + ThresholdIsWithinRangeIncluded ThresholdType = "within_range_included" + ThresholdIsOutsideRangeIncluded ThresholdType = "outside_range_included" ) var ( supportedThresholdFuncs = []string{ string(ThresholdIsAbove), string(ThresholdIsBelow), + string(ThresholdIsEqual), + string(ThresholdIsNotEqual), + string(ThresholdIsGreaterThanEqual), + string(ThresholdIsLessThanEqual), string(ThresholdIsWithinRange), string(ThresholdIsOutsideRange), + string(ThresholdIsWithinRangeIncluded), + string(ThresholdIsOutsideRangeIncluded), } ) @@ -60,6 +72,16 @@ func NewThresholdCommand(refID, referenceVar string, thresholdFunc ThresholdType return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 2", thresholdFunc, len(conditions)) } predicate = withinRangePredicate{left: conditions[0], right: conditions[1]} + case ThresholdIsWithinRangeIncluded: + if len(conditions) < 2 { + return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 2", thresholdFunc, len(conditions)) + } + predicate = withinRangeIncludedPredicate{left: conditions[0], right: conditions[1]} + case ThresholdIsOutsideRangeIncluded: + if len(conditions) < 2 { + return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 2", thresholdFunc, len(conditions)) + } + predicate = outsideRangeIncludedPredicate{left: conditions[0], right: conditions[1]} case ThresholdIsAbove: if len(conditions) < 1 { return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 1", thresholdFunc, len(conditions)) @@ -70,6 +92,26 @@ func NewThresholdCommand(refID, referenceVar string, thresholdFunc ThresholdType return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 1", thresholdFunc, len(conditions)) } predicate = lessThanPredicate{value: conditions[0]} + case ThresholdIsEqual: + if len(conditions) < 1 { + return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 1", thresholdFunc, len(conditions)) + } + predicate = equalPredicate{value: conditions[0]} + case ThresholdIsNotEqual: + if len(conditions) < 1 { + return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 1", thresholdFunc, len(conditions)) + } + predicate = notEqualPredicate{value: conditions[0]} + case ThresholdIsGreaterThanEqual: + if len(conditions) < 1 { + return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 1", thresholdFunc, len(conditions)) + } + predicate = greaterThanEqualPredicate{value: conditions[0]} + case ThresholdIsLessThanEqual: + if len(conditions) < 1 { + return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 1", thresholdFunc, len(conditions)) + } + predicate = lessThanEqualPredicate{value: conditions[0]} default: return nil, fmt.Errorf("expected threshold function to be one of [%s], got %s", strings.Join(supportedThresholdFuncs, ", "), thresholdFunc) } @@ -279,6 +321,24 @@ func (r outsideRangePredicate) Eval(f float64) bool { return f < r.left || f > r.right } +type withinRangeIncludedPredicate struct { + left float64 + right float64 +} + +func (r withinRangeIncludedPredicate) Eval(f float64) bool { + return f >= r.left && f <= r.right +} + +type outsideRangeIncludedPredicate struct { + left float64 + right float64 +} + +func (r outsideRangeIncludedPredicate) Eval(f float64) bool { + return f <= r.left || f >= r.right +} + type lessThanPredicate struct { value float64 } @@ -294,3 +354,35 @@ type greaterThanPredicate struct { func (r greaterThanPredicate) Eval(f float64) bool { return f > r.value } + +type equalPredicate struct { + value float64 +} + +func (r equalPredicate) Eval(f float64) bool { + return f == r.value +} + +type notEqualPredicate struct { + value float64 +} + +func (r notEqualPredicate) Eval(f float64) bool { + return f != r.value +} + +type greaterThanEqualPredicate struct { + value float64 +} + +func (r greaterThanEqualPredicate) Eval(f float64) bool { + return f >= r.value +} + +type lessThanEqualPredicate struct { + value float64 +} + +func (r lessThanEqualPredicate) Eval(f float64) bool { + return f <= r.value +} diff --git a/pkg/expr/threshold_test.go b/pkg/expr/threshold_test.go index d458304ae08..2cfcadab18f 100644 --- a/pkg/expr/threshold_test.go +++ b/pkg/expr/threshold_test.go @@ -38,6 +38,26 @@ func TestNewThresholdCommand(t *testing.T) { args: []float64{0}, shouldError: false, }, + { + fn: "eq", + args: []float64{0}, + shouldError: false, + }, + { + fn: "ne", + args: []float64{0}, + shouldError: false, + }, + { + fn: "gte", + args: []float64{0}, + shouldError: false, + }, + { + fn: "lte", + args: []float64{0}, + shouldError: false, + }, { fn: "within_range", args: []float64{0, 1}, @@ -48,6 +68,16 @@ func TestNewThresholdCommand(t *testing.T) { args: []float64{0, 1}, shouldError: false, }, + { + fn: "within_range_included", + args: []float64{0, 1}, + shouldError: false, + }, + { + fn: "outside_range_included", + args: []float64{0, 1}, + shouldError: false, + }, { fn: "gt", args: []float64{}, @@ -60,6 +90,30 @@ func TestNewThresholdCommand(t *testing.T) { shouldError: true, expectedError: "incorrect number of arguments", }, + { + fn: "eq", + args: []float64{}, + shouldError: true, + expectedError: "incorrect number of arguments", + }, + { + fn: "ne", + args: []float64{}, + shouldError: true, + expectedError: "incorrect number of arguments", + }, + { + fn: "gte", + args: []float64{}, + shouldError: true, + expectedError: "incorrect number of arguments", + }, + { + fn: "lte", + args: []float64{}, + shouldError: true, + expectedError: "incorrect number of arguments", + }, { fn: "within_range", args: []float64{0}, @@ -72,6 +126,18 @@ func TestNewThresholdCommand(t *testing.T) { shouldError: true, expectedError: "incorrect number of arguments", }, + { + fn: "within_range_included", + args: []float64{0}, + shouldError: true, + expectedError: "incorrect number of arguments", + }, + { + fn: "outside_range_included", + args: []float64{0}, + shouldError: true, + expectedError: "incorrect number of arguments", + }, } for _, tc := range cases { @@ -249,6 +315,22 @@ func TestIsSupportedThresholdFunc(t *testing.T) { function: ThresholdIsBelow, supported: true, }, + { + function: ThresholdIsEqual, + supported: true, + }, + { + function: ThresholdIsNotEqual, + supported: true, + }, + { + function: ThresholdIsGreaterThanEqual, + supported: true, + }, + { + function: ThresholdIsLessThanEqual, + supported: true, + }, { function: ThresholdIsWithinRange, supported: true, @@ -257,6 +339,14 @@ func TestIsSupportedThresholdFunc(t *testing.T) { function: ThresholdIsOutsideRange, supported: true, }, + { + function: ThresholdIsWithinRangeIncluded, + supported: true, + }, + { + function: ThresholdIsOutsideRangeIncluded, + supported: true, + }, { function: "foo", supported: false, diff --git a/public/app/features/alerting/state/ThresholdMapper.ts b/public/app/features/alerting/state/ThresholdMapper.ts index 265934a61fd..48b9b7c656a 100644 --- a/public/app/features/alerting/state/ThresholdMapper.ts +++ b/public/app/features/alerting/state/ThresholdMapper.ts @@ -29,6 +29,26 @@ export class ThresholdMapper { thresholds.push({ value: value, op: 'lt', visible }); break; } + case 'eq': { + const value = evaluator.params[0]; + thresholds.push({ value: value, op: 'eq', visible }); + break; + } + case 'ne': { + const value = evaluator.params[0]; + thresholds.push({ value: value, op: 'ne', visible }); + break; + } + case 'gte': { + const value = evaluator.params[0]; + thresholds.push({ value: value, op: 'ge', visible }); + break; + } + case 'lte': { + const value = evaluator.params[0]; + thresholds.push({ value: value, op: 'le', visible }); + break; + } case 'outside_range': { const value1 = evaluator.params[0]; const value2 = evaluator.params[1]; @@ -56,6 +76,33 @@ export class ThresholdMapper { } break; } + case 'outside_range_included': { + const value1 = evaluator.params[0]; + const value2 = evaluator.params[1]; + + if (value1 >= value2) { + thresholds.push({ value: value1, op: 'ge', visible }); + thresholds.push({ value: value2, op: 'le', visible }); + } else { + thresholds.push({ value: value1, op: 'le', visible }); + thresholds.push({ value: value2, op: 'ge', visible }); + } + + break; + } + case 'within_range_included': { + const value1 = evaluator.params[0]; + const value2 = evaluator.params[1]; + + if (value1 >= value2) { + thresholds.push({ value: value1, op: 'le', visible }); + thresholds.push({ value: value2, op: 'ge', visible }); + } else { + thresholds.push({ value: value1, op: 'ge', visible }); + thresholds.push({ value: value2, op: 'le', visible }); + } + break; + } } break; } diff --git a/public/app/features/alerting/state/alertDef.ts b/public/app/features/alerting/state/alertDef.ts index 96884ce8ae1..cdcdcbda7c1 100644 --- a/public/app/features/alerting/state/alertDef.ts +++ b/public/app/features/alerting/state/alertDef.ts @@ -32,16 +32,28 @@ const alertStateSortScore = { export enum EvalFunction { 'IsAbove' = 'gt', 'IsBelow' = 'lt', + 'IsEqual' = 'eq', + 'IsNotEqual' = 'ne', + 'IsGreaterThanEqual' = 'gte', + 'IsLessThanEqual' = 'lte', 'IsOutsideRange' = 'outside_range', 'IsWithinRange' = 'within_range', + 'IsWithinRangeIncluded' = 'within_range_included', + 'IsOutsideRangeIncluded' = 'outside_range_included', 'HasNoValue' = 'no_value', } const evalFunctions = [ { value: EvalFunction.IsAbove, text: 'IS ABOVE' }, { value: EvalFunction.IsBelow, text: 'IS BELOW' }, + { value: EvalFunction.IsEqual, text: 'IS EQUAL TO' }, + { value: EvalFunction.IsNotEqual, text: 'IS NOT EQUAL TO' }, + { value: EvalFunction.IsGreaterThanEqual, text: 'IS ABOVE OR EQUAL TO' }, + { value: EvalFunction.IsLessThanEqual, text: 'IS BELOW OR EQUAL TO' }, { value: EvalFunction.IsOutsideRange, text: 'IS OUTSIDE RANGE' }, { value: EvalFunction.IsWithinRange, text: 'IS WITHIN RANGE' }, + { value: EvalFunction.IsOutsideRangeIncluded, text: 'IS OUTSIDE RANGE INCLUDED' }, + { value: EvalFunction.IsWithinRangeIncluded, text: 'IS WITHIN RANGE INCLUDED' }, { value: EvalFunction.HasNoValue, text: 'HAS NO VALUE' }, ]; diff --git a/public/app/features/alerting/unified/GrafanaRuleQueryViewer.tsx b/public/app/features/alerting/unified/GrafanaRuleQueryViewer.tsx index 1839ea4c8eb..10126744321 100644 --- a/public/app/features/alerting/unified/GrafanaRuleQueryViewer.tsx +++ b/public/app/features/alerting/unified/GrafanaRuleQueryViewer.tsx @@ -538,5 +538,10 @@ const getCommonQueryStyles = (theme: GrafanaTheme2) => ({ }); function isRangeEvaluator(evaluator: { params: number[]; type: EvalFunction }) { - return evaluator.type === EvalFunction.IsWithinRange || evaluator.type === EvalFunction.IsOutsideRange; + return ( + evaluator.type === EvalFunction.IsWithinRange || + evaluator.type === EvalFunction.IsOutsideRange || + evaluator.type === EvalFunction.IsOutsideRangeIncluded || + evaluator.type === EvalFunction.IsWithinRangeIncluded + ); } diff --git a/public/app/features/alerting/unified/components/rule-editor/util.ts b/public/app/features/alerting/unified/components/rule-editor/util.ts index 3933de4f48e..b8e20cba6b4 100644 --- a/public/app/features/alerting/unified/components/rule-editor/util.ts +++ b/public/app/features/alerting/unified/components/rule-editor/util.ts @@ -279,6 +279,53 @@ export function getThresholdsForQueries(queries: AlertQuery[], condition: string ); } + if (type === EvalFunction.IsWithinRangeIncluded) { + thresholds[refId].config.steps.push( + ...[ + { + value: -Infinity, + color: 'transparent', + }, + { + value: values[0], + color: config.theme2.colors.error.main, + }, + { + value: values[1], + color: config.theme2.colors.error.main, + }, + { + value: values[1], + color: 'transparent', + }, + ] + ); + } + + if (type === EvalFunction.IsOutsideRangeIncluded) { + thresholds[refId].config.steps.push( + ...[ + { + value: -Infinity, + color: config.theme2.colors.error.main, + }, + // we have to duplicate this value, or the graph will not display the handle in the right color + { + value: values[0], + color: config.theme2.colors.error.main, + }, + { + value: values[0], + color: 'transparent', + }, + { + value: values[1], + color: config.theme2.colors.error.main, + }, + ] + ); + } + // now also sort the threshold values, if we don't then they will look weird in the time series panel // TODO this doesn't work for negative values for now, those need to be sorted inverse thresholds[refId].config.steps.sort((a, b) => a.value - b.value); @@ -292,7 +339,10 @@ export function getThresholdsForQueries(queries: AlertQuery[], condition: string function isRangeCondition(condition: ClassicCondition) { return ( - condition.evaluator.type === EvalFunction.IsWithinRange || condition.evaluator.type === EvalFunction.IsOutsideRange + condition.evaluator.type === EvalFunction.IsWithinRange || + condition.evaluator.type === EvalFunction.IsOutsideRange || + condition.evaluator.type === EvalFunction.IsOutsideRangeIncluded || + condition.evaluator.type === EvalFunction.IsWithinRangeIncluded ); } diff --git a/public/app/features/expressions/components/Condition.tsx b/public/app/features/expressions/components/Condition.tsx index fc6455fd540..778869c5a60 100644 --- a/public/app/features/expressions/components/Condition.tsx +++ b/public/app/features/expressions/components/Condition.tsx @@ -69,7 +69,10 @@ export const Condition = ({ condition, index, onChange, onRemoveCondition, refId }); const isRange = - condition.evaluator.type === EvalFunction.IsWithinRange || condition.evaluator.type === EvalFunction.IsOutsideRange; + condition.evaluator.type === EvalFunction.IsWithinRange || + condition.evaluator.type === EvalFunction.IsOutsideRange || + condition.evaluator.type === EvalFunction.IsOutsideRangeIncluded || + condition.evaluator.type === EvalFunction.IsWithinRangeIncluded; return ( diff --git a/public/app/features/expressions/components/Threshold.tsx b/public/app/features/expressions/components/Threshold.tsx index 6474c497e16..6d0d43eb7e9 100644 --- a/public/app/features/expressions/components/Threshold.tsx +++ b/public/app/features/expressions/components/Threshold.tsx @@ -5,8 +5,9 @@ import * as React from 'react'; import { FormEvent, useEffect, useReducer } from 'react'; import { GrafanaTheme2, SelectableValue } from '@grafana/data'; -import { InlineField, InlineFieldRow, InlineSwitch, Input, Select, useStyles2, Stack } from '@grafana/ui'; +import { InlineField, InlineFieldRow, InlineSwitch, Input, Select, Stack, useStyles2 } from '@grafana/ui'; import { config } from 'app/core/config'; +import { t } from 'app/core/internationalization'; import { EvalFunction } from 'app/features/alerting/state/alertDef'; import { ClassicCondition, ExpressionQuery, thresholdFunctions } from '../types'; @@ -81,7 +82,9 @@ export const Threshold = ({ labelWidth, onChange, refIds, query, onError, useHys const isRange = conditionInState.evaluator.type === EvalFunction.IsWithinRange || - conditionInState.evaluator.type === EvalFunction.IsOutsideRange; + conditionInState.evaluator.type === EvalFunction.IsOutsideRange || + conditionInState.evaluator.type === EvalFunction.IsOutsideRangeIncluded || + conditionInState.evaluator.type === EvalFunction.IsWithinRangeIncluded; const hysteresisEnabled = Boolean(config.featureToggles?.recoveryThreshold) && useHysteresis; @@ -155,7 +158,7 @@ export const Threshold = ({ labelWidth, onChange, refIds, query, onError, useHys
- - -
- - allowOnblur.current && onUnloadValueChange(event, 0)} - defaultValue={condition.unloadEvaluator?.params[0]} - /> - -
- -
- - allowOnblur.current && onUnloadValueChange(event, 1)} - defaultValue={condition.unloadEvaluator?.params[1]} - /> - -
-
-
- - ); - } else { - return ( - - - -
- - allowOnblur.current && onUnloadValueChange(event, 0)} - defaultValue={condition.unloadEvaluator?.params[0]} - /> - -
+ switch (condition.evaluator.type) { + case EvalFunction.IsWithinRange: + if (condition.evaluator.type === EvalFunction.IsWithinRange) { + return ( + + + +
+ + allowOnblur.current && onUnloadValueChange(event, 0)} + defaultValue={condition.unloadEvaluator?.params[0]} + /> + +
+ +
+ + allowOnblur.current && onUnloadValueChange(event, 1)} + defaultValue={condition.unloadEvaluator?.params[1]} + /> + +
+
+
+
+ ); + } + case EvalFunction.IsOutsideRange: + return ( + + + +
+ + allowOnblur.current && onUnloadValueChange(event, 0)} + defaultValue={condition.unloadEvaluator?.params[0]} + /> + +
- -
- - allowOnblur.current && onUnloadValueChange(event, 1)} - defaultValue={condition.unloadEvaluator?.params[1]} - /> - -
-
-
-
- ); + +
+ + allowOnblur.current && onUnloadValueChange(event, 1)} + defaultValue={condition.unloadEvaluator?.params[1]} + /> + +
+
+
+
+ ); + case EvalFunction.IsOutsideRangeIncluded: + return ( + + + +
+ + allowOnblur.current && onUnloadValueChange(event, 0)} + defaultValue={condition.unloadEvaluator?.params[0]} + /> + +
+ +
+ + allowOnblur.current && onUnloadValueChange(event, 1)} + defaultValue={condition.unloadEvaluator?.params[1]} + /> + +
+
+
+
+ ); + case EvalFunction.IsWithinRangeIncluded: + return ( + + + +
+ + allowOnblur.current && onUnloadValueChange(event, 0)} + defaultValue={condition.unloadEvaluator?.params[0]} + /> + +
+ +
+ + allowOnblur.current && onUnloadValueChange(event, 1)} + defaultValue={condition.unloadEvaluator?.params[1]} + /> + +
+
+
+
+ ); + default: + return null; } } function RecoveryForSingleValue({ allowOnblur }: RecoveryProps) { - if (condition.evaluator.type === EvalFunction.IsAbove) { - return ( - - - { - allowOnblur.current && onUnloadValueChange(event, 0); - }} - defaultValue={condition.unloadEvaluator?.params[0]} - /> - - - ); - } else { - return ( - - - { - allowOnblur.current && onUnloadValueChange(event, 0); - }} - defaultValue={condition.unloadEvaluator?.params[0]} - /> - - - ); + switch (condition.evaluator.type) { + case EvalFunction.IsAbove: + return ( + + + { + allowOnblur.current && onUnloadValueChange(event, 0); + }} + defaultValue={condition.unloadEvaluator?.params[0]} + /> + + + ); + case EvalFunction.IsBelow: + return ( + + + { + allowOnblur.current && onUnloadValueChange(event, 0); + }} + defaultValue={condition.unloadEvaluator?.params[0]} + /> + + + ); + case EvalFunction.IsEqual: + return ( + + + { + allowOnblur.current && onUnloadValueChange(event, 0); + }} + defaultValue={condition.unloadEvaluator?.params[0]} + /> + + + ); + case EvalFunction.IsNotEqual: + return ( + + + { + allowOnblur.current && onUnloadValueChange(event, 0); + }} + defaultValue={condition.unloadEvaluator?.params[0]} + /> + + + ); + case EvalFunction.IsGreaterThanEqual: + return ( + + + { + allowOnblur.current && onUnloadValueChange(event, 0); + }} + defaultValue={condition.unloadEvaluator?.params[0]} + /> + + + ); + case EvalFunction.IsLessThanEqual: + return ( + + + { + allowOnblur.current && onUnloadValueChange(event, 0); + }} + defaultValue={condition.unloadEvaluator?.params[0]} + /> + + + ); + default: + return null; } } } diff --git a/public/app/features/expressions/components/thresholdReducer.ts b/public/app/features/expressions/components/thresholdReducer.ts index 7fa1e428a98..503baa3e727 100644 --- a/public/app/features/expressions/components/thresholdReducer.ts +++ b/public/app/features/expressions/components/thresholdReducer.ts @@ -104,12 +104,30 @@ function getUnloadEvaluatorTypeFromEvaluatorType(type: EvalFunction) { if (type === EvalFunction.IsBelow) { return EvalFunction.IsAbove; } + if (type === EvalFunction.IsEqual) { + return EvalFunction.IsNotEqual; + } + if (type === EvalFunction.IsNotEqual) { + return EvalFunction.IsEqual; + } + if (type === EvalFunction.IsGreaterThanEqual) { + return EvalFunction.IsLessThanEqual; + } + if (type === EvalFunction.IsLessThanEqual) { + return EvalFunction.IsGreaterThanEqual; + } if (type === EvalFunction.IsWithinRange) { return EvalFunction.IsOutsideRange; } if (type === EvalFunction.IsOutsideRange) { return EvalFunction.IsWithinRange; } + if (type === EvalFunction.IsWithinRangeIncluded) { + return EvalFunction.IsOutsideRangeIncluded; + } + if (type === EvalFunction.IsOutsideRangeIncluded) { + return EvalFunction.IsWithinRangeIncluded; + } return EvalFunction.IsBelow; } @@ -126,7 +144,12 @@ export function isInvalid(condition: ClassicCondition) { const { type, params: loadParams } = evaluator; const { params: unloadParams } = unloadEvaluator; - if (type === EvalFunction.IsWithinRange || type === EvalFunction.IsOutsideRange) { + if ( + type === EvalFunction.IsWithinRange || + type === EvalFunction.IsOutsideRange || + type === EvalFunction.IsWithinRangeIncluded || + type === EvalFunction.IsOutsideRangeIncluded + ) { if (unloadParams[0] === undefined || Number.isNaN(unloadParams[0])) { return { errorMsgFrom: 'This value cannot be empty' }; } @@ -149,6 +172,26 @@ export function isInvalid(condition: ClassicCondition) { return { errorMsg: `Enter a number more than or equal to ${firstParamInEvaluator}` }; } break; + case EvalFunction.IsEqual: + if (firstParamInUnloadEvaluator === firstParamInEvaluator) { + return { errorMsg: `Enter a different number than ${firstParamInEvaluator}` }; + } + break; + case EvalFunction.IsNotEqual: + if (firstParamInUnloadEvaluator !== firstParamInEvaluator) { + return { errorMsg: `Enter the same number as ${firstParamInEvaluator}` }; + } + break; + case EvalFunction.IsGreaterThanEqual: + if (firstParamInUnloadEvaluator >= firstParamInEvaluator) { + return { errorMsg: `Enter a number less than ${firstParamInEvaluator}` }; + } + break; + case EvalFunction.IsLessThanEqual: + if (firstParamInUnloadEvaluator <= firstParamInEvaluator) { + return { errorMsg: `Enter a number more than ${firstParamInEvaluator}` }; + } + break; case EvalFunction.IsOutsideRange: if (firstParamInUnloadEvaluator < firstParamInEvaluator) { return { errorMsgFrom: `Enter a number more than or equal to ${firstParamInEvaluator}` }; @@ -165,6 +208,22 @@ export function isInvalid(condition: ClassicCondition) { return { errorMsgTo: `Enter a number be more than or equal to ${secondParamInEvaluator}` }; } break; + case EvalFunction.IsOutsideRangeIncluded: + if (firstParamInUnloadEvaluator <= firstParamInEvaluator) { + return { errorMsgFrom: `Enter a number more than ${firstParamInEvaluator}` }; + } + if (secondParamInUnloadEvaluator >= secondParamInEvaluator) { + return { errorMsgTo: `Enter a number less than ${secondParamInEvaluator}` }; + } + break; + case EvalFunction.IsWithinRangeIncluded: + if (firstParamInUnloadEvaluator >= firstParamInEvaluator) { + return { errorMsgFrom: `Enter a number less than ${firstParamInEvaluator}` }; + } + if (secondParamInUnloadEvaluator <= secondParamInEvaluator) { + return { errorMsgTo: `Enter a number be more than ${secondParamInEvaluator}` }; + } + break; default: throw new Error(`evaluator function type ${type} not supported.`); } diff --git a/public/app/features/expressions/types.ts b/public/app/features/expressions/types.ts index a1f88729c1e..a132670f00f 100644 --- a/public/app/features/expressions/types.ts +++ b/public/app/features/expressions/types.ts @@ -126,8 +126,14 @@ export const upsamplingTypes: Array> = [ export const thresholdFunctions: Array> = [ { value: EvalFunction.IsAbove, label: 'Is above' }, { value: EvalFunction.IsBelow, label: 'Is below' }, + { value: EvalFunction.IsEqual, label: 'Is equal to' }, + { value: EvalFunction.IsNotEqual, label: 'Is not equal to' }, + { value: EvalFunction.IsGreaterThanEqual, label: 'Is above or equal to' }, + { value: EvalFunction.IsLessThanEqual, label: 'Is below or equal to' }, { value: EvalFunction.IsWithinRange, label: 'Is within range' }, { value: EvalFunction.IsOutsideRange, label: 'Is outside range' }, + { value: EvalFunction.IsWithinRangeIncluded, label: 'Is within range included' }, + { value: EvalFunction.IsOutsideRangeIncluded, label: 'Is outside range included' }, ]; /** diff --git a/public/locales/en-US/grafana.json b/public/locales/en-US/grafana.json index 88c3d0aa34b..0e93bc677a7 100644 --- a/public/locales/en-US/grafana.json +++ b/public/locales/en-US/grafana.json @@ -528,6 +528,19 @@ }, "pause": { "label": "Pause evaluation" + }, + "threshold": { + "recovery": { + "stop-alerting-above": "Stop alerting when above", + "stop-alerting-bellow": "Stop alerting when below", + "stop-alerting-equal": "Stop alerting when equal to", + "stop-alerting-inside-range": "Stop alerting when inside range", + "stop-alerting-less": "Stop alerting when less than", + "stop-alerting-more": "Stop alerting when more than", + "stop-alerting-not-equal": "Stop alerting when not equal to", + "stop-alerting-outside-range": "Stop alerting when outside range", + "title": "Custom recovery threshold" + } } }, "rule-groups": { diff --git a/public/locales/pseudo-LOCALE/grafana.json b/public/locales/pseudo-LOCALE/grafana.json index f4b2de7e6dc..583a6e6832f 100644 --- a/public/locales/pseudo-LOCALE/grafana.json +++ b/public/locales/pseudo-LOCALE/grafana.json @@ -528,6 +528,19 @@ }, "pause": { "label": "Päūşę ęväľūäŧįőʼn" + }, + "threshold": { + "recovery": { + "stop-alerting-above": "Ŝŧőp äľęřŧįʼnģ ŵĥęʼn äþővę", + "stop-alerting-bellow": "Ŝŧőp äľęřŧįʼnģ ŵĥęʼn þęľőŵ", + "stop-alerting-equal": "Ŝŧőp äľęřŧįʼnģ ŵĥęʼn ęqūäľ ŧő", + "stop-alerting-inside-range": "Ŝŧőp äľęřŧįʼnģ ŵĥęʼn įʼnşįđę řäʼnģę", + "stop-alerting-less": "Ŝŧőp äľęřŧįʼnģ ŵĥęʼn ľęşş ŧĥäʼn", + "stop-alerting-more": "Ŝŧőp äľęřŧįʼnģ ŵĥęʼn mőřę ŧĥäʼn", + "stop-alerting-not-equal": "Ŝŧőp äľęřŧįʼnģ ŵĥęʼn ʼnőŧ ęqūäľ ŧő", + "stop-alerting-outside-range": "Ŝŧőp äľęřŧįʼnģ ŵĥęʼn őūŧşįđę řäʼnģę", + "title": "Cūşŧőm řęčővęřy ŧĥřęşĥőľđ" + } } }, "rule-groups": { From d1dfa0576b49f94c62f1d023abd9687291a31c7e Mon Sep 17 00:00:00 2001 From: Yuri Tseretyan Date: Fri, 21 Feb 2025 12:51:38 -0500 Subject: [PATCH 02/26] Alerting: Support Jira Integration (#100480) --- go.mod | 2 +- go.sum | 4 +- .../ngalert/api/compat_contact_points.go | 12 ++ .../api/tooling/definitions/contact_points.go | 24 +++ .../channels_config/available_channels.go | 151 +++++++++++++++++- .../available_channels_test.go | 22 ++- .../notifier/channels_config/plugin.go | 2 + .../provisioning/contactpoints_test.go | 3 + pkg/storage/unified/apistore/go.mod | 2 +- pkg/storage/unified/apistore/go.sum | 4 +- pkg/storage/unified/resource/go.mod | 2 +- pkg/storage/unified/resource/go.sum | 4 +- .../notifications/receivers/receiver_test.go | 6 + 13 files changed, 226 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index 8e2e1792445..c9a91001aec 100644 --- a/go.mod +++ b/go.mod @@ -71,7 +71,7 @@ require ( github.com/googleapis/gax-go/v2 v2.14.1 // @grafana/grafana-backend-group github.com/gorilla/mux v1.8.1 // @grafana/grafana-backend-group github.com/gorilla/websocket v1.5.3 // @grafana/grafana-app-platform-squad - github.com/grafana/alerting v0.0.0-20250219153626-c475b1a572b0 // @grafana/alerting-backend + github.com/grafana/alerting v0.0.0-20250220212119-4baca04e46bb // @grafana/alerting-backend github.com/grafana/authlib v0.0.0-20250219100139-6a3b1bbb50e7 // @grafana/identity-access-team github.com/grafana/authlib/types v0.0.0-20250219092154-21ce22b49f31 // @grafana/identity-access-team github.com/grafana/dataplane/examples v0.0.1 // @grafana/observability-metrics diff --git a/go.sum b/go.sum index 88780f81fc9..a467f87cba7 100644 --- a/go.sum +++ b/go.sum @@ -1511,8 +1511,8 @@ github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7Fsg github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/grafana/alerting v0.0.0-20250219153626-c475b1a572b0 h1:LGH+tVzHCDrR9hsltmkP4jmNRg5IreQw5CNFbJKlnts= -github.com/grafana/alerting v0.0.0-20250219153626-c475b1a572b0/go.mod h1:hdGB3dSl8Ma9Rjo2YiAEAjMkZ5HiNJbNDqRKDefRZrM= +github.com/grafana/alerting v0.0.0-20250220212119-4baca04e46bb h1:WfCsiuZXhGXIdzImQ9/Kjfn9M4e6f7z5mddcSKCRxmI= +github.com/grafana/alerting v0.0.0-20250220212119-4baca04e46bb/go.mod h1:hdGB3dSl8Ma9Rjo2YiAEAjMkZ5HiNJbNDqRKDefRZrM= github.com/grafana/authlib v0.0.0-20250219100139-6a3b1bbb50e7 h1:NTMmow+74I3Jb033xhbRgWQS7A//5TDhiM4tl7bsVP4= github.com/grafana/authlib v0.0.0-20250219100139-6a3b1bbb50e7/go.mod h1:T3X4z0ejGfJOiOmZLFeKCRT/yxWJq/RtclAc/PHj/w4= github.com/grafana/authlib/types v0.0.0-20250219092154-21ce22b49f31 h1:EokLC5grHwLPs4tXW8T6E8187H1e5G9AP0QQ5B60HbA= diff --git a/pkg/services/ngalert/api/compat_contact_points.go b/pkg/services/ngalert/api/compat_contact_points.go index d10c3859598..1299af0bb64 100644 --- a/pkg/services/ngalert/api/compat_contact_points.go +++ b/pkg/services/ngalert/api/compat_contact_points.go @@ -91,6 +91,13 @@ func ContactPointToContactPointExport(cp definitions.ContactPoint) (notify.APIRe } integration = append(integration, el) } + for _, i := range cp.Jira { + el, err := marshallIntegration(j, "jira", i, i.DisableResolveMessage) + if err != nil { + errs = append(errs, err) + } + integration = append(integration, el) + } for _, i := range cp.Kafka { el, err := marshallIntegration(j, "kafka", i, i.DisableResolveMessage) if err != nil { @@ -271,6 +278,11 @@ func parseIntegration(json jsoniter.API, result *definitions.ContactPoint, recei if err = json.Unmarshal(data, &integration); err == nil { result.Googlechat = append(result.Googlechat, integration) } + case "jira": + integration := definitions.JiraIntegration{DisableResolveMessage: disable} + if err = json.Unmarshal(data, &integration); err == nil { + result.Jira = append(result.Jira, integration) + } case "kafka": integration := definitions.KafkaIntegration{DisableResolveMessage: disable} if err = json.Unmarshal(data, &integration); err == nil { diff --git a/pkg/services/ngalert/api/tooling/definitions/contact_points.go b/pkg/services/ngalert/api/tooling/definitions/contact_points.go index 9df9b8ee444..498ac6f0d37 100644 --- a/pkg/services/ngalert/api/tooling/definitions/contact_points.go +++ b/pkg/services/ngalert/api/tooling/definitions/contact_points.go @@ -62,6 +62,29 @@ type GooglechatIntegration struct { Message *string `json:"message,omitempty" yaml:"message,omitempty" hcl:"message"` } +type JiraIntegration struct { + DisableResolveMessage *bool `json:"-" yaml:"-" hcl:"disable_resolve_message"` + + URL string `yaml:"api_url,omitempty" json:"api_url,omitempty" hcl:"api_url"` + Project string `yaml:"project,omitempty" json:"project,omitempty" hcl:"project"` + IssueType string `yaml:"issue_type,omitempty" json:"issue_type,omitempty" hcl:"issue_type"` + + Summary *string `yaml:"summary,omitempty" json:"summary,omitempty" hcl:"summary"` + Description *string `yaml:"description,omitempty" json:"description,omitempty" hcl:"description"` + Labels *[]string `yaml:"labels,omitempty" json:"labels,omitempty" hcl:"labels"` + Priority *string `yaml:"priority,omitempty" json:"priority,omitempty" hcl:"priority"` + ReopenTransition *string `yaml:"reopen_transition,omitempty" json:"reopen_transition,omitempty" hcl:"reopen_transition"` + ResolveTransition *string `yaml:"resolve_transition,omitempty" json:"resolve_transition,omitempty" hcl:"resolve_transition"` + WontFixResolution *string `yaml:"wont_fix_resolution,omitempty" json:"wont_fix_resolution,omitempty" hcl:"wont_fix_resolution"` + ReopenDuration *string `yaml:"reopen_duration,omitempty" json:"reopen_duration,omitempty" hcl:"reopen_duration"` + DedupKeyFieldName *string `yaml:"dedup_key_field,omitempty" json:"dedup_key_field,omitempty" hcl:"dedup_key_field"` + Fields *map[string]any `yaml:"fields,omitempty" json:"fields,omitempty" hcl:"fields"` + + User *Secret `yaml:"user,omitempty" json:"user,omitempty" hcl:"user"` + Password *Secret `yaml:"password,omitempty" json:"password,omitempty" hcl:"password"` + Token *Secret `yaml:"api_token,omitempty" json:"api_token,omitempty" hcl:"api_token"` +} + type KafkaIntegration struct { DisableResolveMessage *bool `json:"-" yaml:"-" hcl:"disable_resolve_message"` @@ -321,6 +344,7 @@ type ContactPoint struct { Discord []DiscordIntegration `json:"discord" yaml:"discord" hcl:"discord,block"` Email []EmailIntegration `json:"email" yaml:"email" hcl:"email,block"` Googlechat []GooglechatIntegration `json:"googlechat" yaml:"googlechat" hcl:"googlechat,block"` + Jira []JiraIntegration `json:"jira" yaml:"jira" hcl:"jira,block"` Kafka []KafkaIntegration `json:"kafka" yaml:"kafka" hcl:"kafka,block"` Line []LineIntegration `json:"line" yaml:"line" hcl:"line,block"` Mqtt []MqttIntegration `json:"mqtt" yaml:"mqtt" hcl:"mqtt,block"` diff --git a/pkg/services/ngalert/notifier/channels_config/available_channels.go b/pkg/services/ngalert/notifier/channels_config/available_channels.go index e1748a7550b..a64c641a40e 100644 --- a/pkg/services/ngalert/notifier/channels_config/available_channels.go +++ b/pkg/services/ngalert/notifier/channels_config/available_channels.go @@ -5,6 +5,7 @@ import ( "os" "strings" + "github.com/grafana/alerting/receivers/jira" alertingMqtt "github.com/grafana/alerting/receivers/mqtt" alertingOpsgenie "github.com/grafana/alerting/receivers/opsgenie" alertingPagerduty "github.com/grafana/alerting/receivers/pagerduty" @@ -365,7 +366,7 @@ func GetAvailableNotifiers() []*NotifierPlugin { InputType: InputTypeText, PropertyName: "details", }, - { //New in 11.1 + { // New in 11.1 Label: "URL", Description: "The URL to send API requests to", Element: ElementTypeInput, @@ -1708,6 +1709,154 @@ func GetAvailableNotifiers() []*NotifierPlugin { }, }, }, + { // Since Grafana 11.6 + Type: "jira", + Name: "Jira", + Description: "Creates Jira issues from alerts", + Heading: "Jira settings", + Options: []NotifierOption{ + { + Label: "API URL of Jira instance, including version of API", + Element: ElementTypeInput, + InputType: InputTypeText, + Placeholder: "https://grafana.atlassian.net/rest/api/3", + PropertyName: "api_url", + Description: "Supported v2 or v3 APIs", + Required: true, + }, + { + Label: "HTTP Basic Authentication - Username", + Element: ElementTypeInput, + InputType: InputTypeText, + PropertyName: "user", + Description: "Username to use for Jira authentication.", + Secure: true, + Required: false, + }, + { + Label: "HTTP Basic Authentication - Password", + Element: ElementTypeInput, + InputType: InputTypePassword, + PropertyName: "password", + // Go to https://id.atlassian.com/manage-profile/security/api-tokens to obtain a token. + Description: "Password to use for Jira authentication.", + Secure: true, + Required: false, + }, + { + Label: "Authorization Header - Personal Access Token", + Element: ElementTypeInput, + InputType: InputTypePassword, + PropertyName: "api_token", + // Go to https://confluence.atlassian.com/enterprise/using-personal-access-tokens-1026032365.html for how to obtain a token. + Description: "Personal Access Token that is used as a bearer authorization header.", + Secure: true, + Required: false, + }, + { + Label: "Project Key", + Description: "The project key associated with the relevant Jira project", + Element: ElementTypeInput, + InputType: InputTypeText, + Placeholder: "Grafana", + PropertyName: "project", + Required: true, + }, + { + Label: "Issue Type", + Description: "The type of the Jira issue (e.g., Bug, Task, Story). You can use templates to customize this field.", + Element: ElementTypeInput, + InputType: InputTypeText, + Placeholder: "Task", + Required: true, + PropertyName: "issue_type", + }, + { + Label: "Summary", + Description: fmt.Sprintf("The summary of the Jira issue. You can use templates to customize this field. Maximum length is %d characters.", jira.MaxSummaryLenRunes), + Element: ElementTypeInput, + InputType: InputTypeText, + Placeholder: jira.DefaultSummary, + PropertyName: "summary", + }, + { + Label: "Description", + Description: fmt.Sprintf("The description of the Jira issue. You can use templates to customize this field. Maximum length is %d characters.", jira.MaxDescriptionLenRunes), + Element: ElementTypeTextArea, + InputType: InputTypeText, + Placeholder: jira.DefaultDescription, + PropertyName: "description", + }, + { + Label: "Labels", + Description: "Labels to assign to the Jira issue. You can use templates to customize this field.", + Element: ElementStringArray, + Placeholder: "", + PropertyName: "labels", + }, + { + Label: "Priority", + Description: "The priority of the Jira issue (e.g., High, Medium, Low). You can use templates to customize this field.", + Element: ElementTypeInput, + InputType: InputTypeText, + Placeholder: jira.DefaultPriority, + PropertyName: "priority", + Required: false, + }, + { + Label: "Resolve Transition", + Description: `Name of the workflow transition to resolve an issue. The target status must have the category "done". If not set, the issue will not be resolved.`, + Element: ElementTypeInput, + InputType: InputTypeText, + Placeholder: "", + PropertyName: "resolve_transition", + Required: false, + }, + { + Label: "Reopen Transition", + Description: `Name of the workflow transition to resolve an issue. The target status must not have the category "done". If not set, the issue will not be reopened.`, + Element: ElementTypeInput, + InputType: InputTypeText, + Placeholder: "", + PropertyName: "reopen_transition", + Required: false, + }, + { + Label: "Reopen Duration", + Description: "Reopen the issue when it is not older than this value in minutes. Otherwise, create a new issue.", + Element: ElementTypeInput, + InputType: InputTypeText, + Placeholder: "10m", + PropertyName: "reopen_duration", + }, + { + Label: "\"Won't fix\" Transition", + Description: `If reopen transition is defined, ignore issues with that resolution.`, + Element: ElementTypeInput, + InputType: InputTypeText, + Placeholder: "", + PropertyName: "wont_fix_resolution", + Required: false, + }, + { + Label: "Custom field ID for deduplication", + Description: "Id of the custom field where the deduplication key should be stored. Otherwise, it is added to labels in format 'ALERT($KEY).'", + Element: ElementTypeInput, + InputType: InputTypeText, + Placeholder: "10000", + ValidationRule: "^[0-9]+$", + PropertyName: "dedup_key_field", + }, + { + Label: "Custom Field Data", + Description: "Custom field data to set on the Jira issue.", + Element: ElementTypeKeyValueMap, + InputType: InputTypeText, + Placeholder: "", + PropertyName: "fields", + }, + }, + }, } } diff --git a/pkg/services/ngalert/notifier/channels_config/available_channels_test.go b/pkg/services/ngalert/notifier/channels_config/available_channels_test.go index 5e305cb1224..cc2defa2c80 100644 --- a/pkg/services/ngalert/notifier/channels_config/available_channels_test.go +++ b/pkg/services/ngalert/notifier/channels_config/available_channels_test.go @@ -27,20 +27,38 @@ func TestGetSecretKeysForContactPointType(t *testing.T) { {receiverType: "prometheus-alertmanager", expectedSecretFields: []string{"basicAuthPassword"}}, {receiverType: "discord", expectedSecretFields: []string{"url"}}, {receiverType: "googlechat", expectedSecretFields: []string{"url"}}, - {receiverType: "line", expectedSecretFields: []string{"token"}}, + {receiverType: "LINE", expectedSecretFields: []string{"token"}}, {receiverType: "threema", expectedSecretFields: []string{"api_secret"}}, {receiverType: "opsgenie", expectedSecretFields: []string{"apiKey"}}, {receiverType: "webex", expectedSecretFields: []string{"bot_token"}}, {receiverType: "sns", expectedSecretFields: []string{"sigv4.access_key", "sigv4.secret_key"}}, + {receiverType: "mqtt", expectedSecretFields: []string{"password", "tlsConfig.caCertificate", "tlsConfig.clientCertificate", "tlsConfig.clientKey"}}, + {receiverType: "jira", expectedSecretFields: []string{"user", "password", "api_token"}}, } + n := GetAvailableNotifiers() + allTypes := make(map[string]struct{}, len(n)) + for _, plugin := range n { + allTypes[plugin.Type] = struct{}{} + } + for _, testCase := range testCases { + delete(allTypes, testCase.receiverType) t.Run(testCase.receiverType, func(t *testing.T) { got, err := GetSecretKeysForContactPointType(testCase.receiverType) require.NoError(t, err) - t.Logf("got secret fields: %#v", got) require.ElementsMatch(t, testCase.expectedSecretFields, got) }) } + + for integrationType := range allTypes { + t.Run(integrationType, func(t *testing.T) { + got, err := GetSecretKeysForContactPointType(integrationType) + require.NoError(t, err) + require.Emptyf(t, got, "secret keys for %s should be empty", integrationType) + }) + } + + require.Emptyf(t, allTypes, "not all types are covered: %s", allTypes) } func Test_getSecretFields(t *testing.T) { diff --git a/pkg/services/ngalert/notifier/channels_config/plugin.go b/pkg/services/ngalert/notifier/channels_config/plugin.go index f390ee1d9fc..5a8b7527604 100644 --- a/pkg/services/ngalert/notifier/channels_config/plugin.go +++ b/pkg/services/ngalert/notifier/channels_config/plugin.go @@ -45,6 +45,8 @@ const ( ElementTypeSubform = "subform" // ElementSubformArray will render a multiple sub-forms with schema defined in SubformOptions ElementSubformArray = "subform_array" + // ElementStringArray will render a set of fields to manage an array of strings. + ElementStringArray = "string_array" ) // InputType is the type of input that can be rendered in the frontend. diff --git a/pkg/services/ngalert/provisioning/contactpoints_test.go b/pkg/services/ngalert/provisioning/contactpoints_test.go index 7a8c5e5bd10..cab8dd42400 100644 --- a/pkg/services/ngalert/provisioning/contactpoints_test.go +++ b/pkg/services/ngalert/provisioning/contactpoints_test.go @@ -423,6 +423,9 @@ func TestRemoveSecretsForContactPoint(t *testing.T) { "webhook": func(settings map[string]any) { // add additional field to the settings because valid config does not allow it to be specified along with password settings["authorization_credentials"] = "test-authz-creds" }, + "jira": func(settings map[string]any) { // add additional field to the settings because valid config does not allow it to be specified along with password + settings["api_token"] = "test-token" + }, } configs := notify.AllKnownConfigsForTesting diff --git a/pkg/storage/unified/apistore/go.mod b/pkg/storage/unified/apistore/go.mod index d99962d0d35..16380887a73 100644 --- a/pkg/storage/unified/apistore/go.mod +++ b/pkg/storage/unified/apistore/go.mod @@ -192,7 +192,7 @@ require ( github.com/googleapis/gax-go/v2 v2.14.1 // indirect github.com/gorilla/mux v1.8.1 // indirect github.com/gorilla/websocket v1.5.3 // indirect - github.com/grafana/alerting v0.0.0-20250219153626-c475b1a572b0 // indirect + github.com/grafana/alerting v0.0.0-20250220212119-4baca04e46bb // indirect github.com/grafana/authlib v0.0.0-20250219100139-6a3b1bbb50e7 // indirect github.com/grafana/dataplane/sdata v0.0.9 // indirect github.com/grafana/dskit v0.0.0-20241105154643-a6b453a88040 // indirect diff --git a/pkg/storage/unified/apistore/go.sum b/pkg/storage/unified/apistore/go.sum index 4d78ed949da..40c89cf0360 100644 --- a/pkg/storage/unified/apistore/go.sum +++ b/pkg/storage/unified/apistore/go.sum @@ -566,8 +566,8 @@ github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/grafana/alerting v0.0.0-20250219153626-c475b1a572b0 h1:LGH+tVzHCDrR9hsltmkP4jmNRg5IreQw5CNFbJKlnts= -github.com/grafana/alerting v0.0.0-20250219153626-c475b1a572b0/go.mod h1:hdGB3dSl8Ma9Rjo2YiAEAjMkZ5HiNJbNDqRKDefRZrM= +github.com/grafana/alerting v0.0.0-20250220212119-4baca04e46bb h1:WfCsiuZXhGXIdzImQ9/Kjfn9M4e6f7z5mddcSKCRxmI= +github.com/grafana/alerting v0.0.0-20250220212119-4baca04e46bb/go.mod h1:hdGB3dSl8Ma9Rjo2YiAEAjMkZ5HiNJbNDqRKDefRZrM= github.com/grafana/authlib v0.0.0-20250219100139-6a3b1bbb50e7 h1:NTMmow+74I3Jb033xhbRgWQS7A//5TDhiM4tl7bsVP4= github.com/grafana/authlib v0.0.0-20250219100139-6a3b1bbb50e7/go.mod h1:T3X4z0ejGfJOiOmZLFeKCRT/yxWJq/RtclAc/PHj/w4= github.com/grafana/authlib/types v0.0.0-20250219092154-21ce22b49f31 h1:EokLC5grHwLPs4tXW8T6E8187H1e5G9AP0QQ5B60HbA= diff --git a/pkg/storage/unified/resource/go.mod b/pkg/storage/unified/resource/go.mod index 57c54262114..f2ab921a4f6 100644 --- a/pkg/storage/unified/resource/go.mod +++ b/pkg/storage/unified/resource/go.mod @@ -117,7 +117,7 @@ require ( github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect github.com/googleapis/gax-go/v2 v2.14.1 // indirect github.com/gorilla/mux v1.8.1 // indirect - github.com/grafana/alerting v0.0.0-20250219153626-c475b1a572b0 // indirect + github.com/grafana/alerting v0.0.0-20250220212119-4baca04e46bb // indirect github.com/grafana/dataplane/sdata v0.0.9 // indirect github.com/grafana/grafana-app-sdk/logging v0.30.0 // indirect github.com/grafana/grafana-aws-sdk v0.31.5 // indirect diff --git a/pkg/storage/unified/resource/go.sum b/pkg/storage/unified/resource/go.sum index 9ee7f749839..40fcf6f458a 100644 --- a/pkg/storage/unified/resource/go.sum +++ b/pkg/storage/unified/resource/go.sum @@ -397,8 +397,8 @@ github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2z github.com/gorilla/mux v1.7.1/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= -github.com/grafana/alerting v0.0.0-20250219153626-c475b1a572b0 h1:LGH+tVzHCDrR9hsltmkP4jmNRg5IreQw5CNFbJKlnts= -github.com/grafana/alerting v0.0.0-20250219153626-c475b1a572b0/go.mod h1:hdGB3dSl8Ma9Rjo2YiAEAjMkZ5HiNJbNDqRKDefRZrM= +github.com/grafana/alerting v0.0.0-20250220212119-4baca04e46bb h1:WfCsiuZXhGXIdzImQ9/Kjfn9M4e6f7z5mddcSKCRxmI= +github.com/grafana/alerting v0.0.0-20250220212119-4baca04e46bb/go.mod h1:hdGB3dSl8Ma9Rjo2YiAEAjMkZ5HiNJbNDqRKDefRZrM= github.com/grafana/authlib v0.0.0-20250219100139-6a3b1bbb50e7 h1:NTMmow+74I3Jb033xhbRgWQS7A//5TDhiM4tl7bsVP4= github.com/grafana/authlib v0.0.0-20250219100139-6a3b1bbb50e7/go.mod h1:T3X4z0ejGfJOiOmZLFeKCRT/yxWJq/RtclAc/PHj/w4= github.com/grafana/authlib/types v0.0.0-20250219092154-21ce22b49f31 h1:EokLC5grHwLPs4tXW8T6E8187H1e5G9AP0QQ5B60HbA= diff --git a/pkg/tests/apis/alerting/notifications/receivers/receiver_test.go b/pkg/tests/apis/alerting/notifications/receivers/receiver_test.go index 283d996f177..c483b6938ae 100644 --- a/pkg/tests/apis/alerting/notifications/receivers/receiver_test.go +++ b/pkg/tests/apis/alerting/notifications/receivers/receiver_test.go @@ -1392,9 +1392,15 @@ func TestIntegrationCRUD(t *testing.T) { t.Run("should return secrets in secureFields but not settings", func(t *testing.T) { for _, integration := range get.Spec.Integrations { t.Run(integration.Type, func(t *testing.T) { + expected := notify.AllKnownConfigsForTesting[strings.ToLower(integration.Type)] + var fields map[string]any + require.NoError(t, json.Unmarshal([]byte(expected.Config), &fields)) secretFields, err := channels_config.GetSecretKeysForContactPointType(integration.Type) require.NoError(t, err) for _, field := range secretFields { + if _, ok := fields[field]; !ok { // skip field that is not in the original setting + continue + } assert.Contains(t, integration.SecureFields, field) assert.Truef(t, integration.SecureFields[field], "secure field should be always true") From bfc234779930af778bf7c2effd7c9a3381d4dc13 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 21 Feb 2025 13:04:21 -0500 Subject: [PATCH 03/26] Bump github.com/go-sourcemap/sourcemap from 2.1.3+incompatible to 2.1.4+incompatible (#98639) Bump github.com/go-sourcemap/sourcemap Bumps [github.com/go-sourcemap/sourcemap](https://github.com/go-sourcemap/sourcemap) from 2.1.3+incompatible to 2.1.4+incompatible. - [Commits](https://github.com/go-sourcemap/sourcemap/compare/v2.1.3...v2.1.4) --- updated-dependencies: - dependency-name: github.com/go-sourcemap/sourcemap dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index c9a91001aec..744aa4d529e 100644 --- a/go.mod +++ b/go.mod @@ -54,7 +54,7 @@ require ( github.com/go-openapi/runtime v0.28.0 // @grafana/alerting-backend github.com/go-openapi/strfmt v0.23.0 // @grafana/alerting-backend github.com/go-redis/redis/v8 v8.11.5 // @grafana/grafana-backend-group - github.com/go-sourcemap/sourcemap v2.1.3+incompatible // @grafana/grafana-backend-group + github.com/go-sourcemap/sourcemap v2.1.4+incompatible // @grafana/grafana-backend-group github.com/go-sql-driver/mysql v1.8.1 // @grafana/grafana-search-and-storage github.com/go-stack/stack v1.8.1 // @grafana/grafana-backend-group github.com/gobwas/glob v0.2.3 // @grafana/grafana-backend-group diff --git a/go.sum b/go.sum index a467f87cba7..85c8855cce6 100644 --- a/go.sum +++ b/go.sum @@ -1281,8 +1281,8 @@ github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo= github.com/go-resty/resty/v2 v2.15.3 h1:bqff+hcqAflpiF591hhJzNdkRsFhlB96CYfBwSFvql8= github.com/go-resty/resty/v2 v2.15.3/go.mod h1:0fHAoK7JoBy/Ch36N8VFeMsK7xQOHhvWaC3iOktwmIU= -github.com/go-sourcemap/sourcemap v2.1.3+incompatible h1:W1iEw64niKVGogNgBN3ePyLFfuisuzeidWPMPWmECqU= -github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg= +github.com/go-sourcemap/sourcemap v2.1.4+incompatible h1:a+iTbH5auLKxaNwQFg0B+TCYl6lbukKPc7b5x0n1s6Q= +github.com/go-sourcemap/sourcemap v2.1.4+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg= github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= From 9780a9c49fd312da2d9df6210c66ae0bac0caf7a Mon Sep 17 00:00:00 2001 From: Larissa Wandzura <126723338+lwandz13@users.noreply.github.com> Date: Fri, 21 Feb 2025 13:09:04 -0600 Subject: [PATCH 04/26] Docs: Overhaul of PostgreSQL data source documenation (#99908) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * created new topic docs * added info to config doc * updates to config doc * updates to config doc * finished config doc * updated Query editor doc * final edits * rename, ref URI work * a few more updates prior to PR issue * fixed the double Macros heading issue * final edits and cleanup * edits based on feedback * ran prettier * added updates * updates based on feedback * vale linter issues * more vale linting issues addressed * small addition on main page * ran prettier again * changed title * Update docs/sources/datasources/postgres/query-editor/_index.md Co-authored-by: Jack Baldry * Update docs/sources/datasources/postgres/_index.md Co-authored-by: Jack Baldry * Update docs/sources/datasources/postgres/configure/_index.md Co-authored-by: Jack Baldry * Update docs/sources/datasources/postgres/configure/_index.md Co-authored-by: Jack Baldry * Update docs/sources/datasources/postgres/query-editor/_index.md Co-authored-by: Jack Baldry * Update docs/sources/datasources/postgres/configure/_index.md Co-authored-by: Jack Baldry * Update docs/sources/datasources/postgres/configure/_index.md Co-authored-by: Jack Baldry * Update docs/sources/datasources/postgres/configure/_index.md Co-authored-by: Jack Baldry * changed Grafana's * added changes * Update docs/sources/datasources/postgres/query-editor/_index.md Co-authored-by: Jack Baldry * Update docs/sources/datasources/postgres/query-editor/_index.md Co-authored-by: Jack Baldry * Update docs/sources/datasources/postgres/query-editor/_index.md Co-authored-by: Jack Baldry * Update docs/sources/datasources/postgres/query-editor/_index.md Co-authored-by: Jack Baldry * Update docs/sources/datasources/postgres/query-editor/_index.md Co-authored-by: Jack Baldry * ran prettier again * Remove aliases Signed-off-by: Jack Baldry * Fix link Signed-off-by: Jack Baldry * Put code in `code` Signed-off-by: Jack Baldry * Avoid bold for emphasis Signed-off-by: Jack Baldry * Fix link Signed-off-by: Jack Baldry --------- Signed-off-by: Jack Baldry Co-authored-by: Irene Rodríguez Co-authored-by: Jack Baldry --- docs/sources/datasources/postgres/_index.md | 524 ++---------------- .../datasources/postgres/configure/_index.md | 194 +++++++ .../postgres/query-editor/_index.md | 410 ++++++++++++++ 3 files changed, 642 insertions(+), 486 deletions(-) create mode 100644 docs/sources/datasources/postgres/configure/_index.md create mode 100644 docs/sources/datasources/postgres/query-editor/_index.md diff --git a/docs/sources/datasources/postgres/_index.md b/docs/sources/datasources/postgres/_index.md index 3489b760f70..868a2bb7d9e 100644 --- a/docs/sources/datasources/postgres/_index.md +++ b/docs/sources/datasources/postgres/_index.md @@ -2,7 +2,7 @@ aliases: - ../data-sources/postgres/ - ../features/datasources/postgres/ -description: Guide for using PostgreSQL in Grafana +description: Introduction to the PostgreSQL data source in Grafana. keywords: - grafana - postgresql @@ -16,506 +16,58 @@ menuTitle: PostgreSQL title: PostgreSQL data source weight: 1200 refs: - provisioning-data-sources: - - pattern: /docs/grafana/ - destination: /docs/grafana//administration/provisioning/#datasources - - pattern: /docs/grafana-cloud/ - destination: /docs/grafana//administration/provisioning/#datasources - variables: - - pattern: /docs/grafana/ - destination: /docs/grafana//dashboards/variables/ - - pattern: /docs/grafana-cloud/ - destination: /docs/grafana//dashboards/variables/ - add-template-variables-interval-ms: - - pattern: /docs/grafana/ - destination: /docs/grafana//dashboards/variables/add-template-variables/#__interval_ms - - pattern: /docs/grafana-cloud/ - destination: /docs/grafana//dashboards/variables/add-template-variables/#__interval_ms - add-template-variables-interval: - - pattern: /docs/grafana/ - destination: /docs/grafana//dashboards/variables/add-template-variables/#__interval - - pattern: /docs/grafana-cloud/ - destination: /docs/grafana//dashboards/variables/add-template-variables/#__interval annotate-visualizations: - pattern: /docs/grafana/ destination: /docs/grafana//dashboards/build-dashboards/annotate-visualizations/ - pattern: /docs/grafana-cloud/ destination: /docs/grafana//dashboards/build-dashboards/annotate-visualizations/ - configure-standard-options-display-name: + configure-postgres-data-source: - pattern: /docs/grafana/ - destination: /docs/grafana//panels-visualizations/configure-standard-options/#display-name + destination: /docs/grafana//datasources/postgres/configure/ - pattern: /docs/grafana-cloud/ - destination: /docs/grafana//panels-visualizations/configure-standard-options/#display-name - data-source-management: + destination: /docs/grafana//datasources/postgres/configure/ + postgres-query-editor: - pattern: /docs/grafana/ - destination: /docs/grafana//administration/data-source-management/ + destination: /docs/grafana//datasources/postgres/query-editor/ - pattern: /docs/grafana-cloud/ - destination: /docs/grafana//administration/data-source-management/ - variable-syntax-advanced-variable-format-options: + destination: /docs/grafana//datasources/postgres/query-editor/ + alerting: - pattern: /docs/grafana/ - destination: /docs/grafana//dashboards/variables/variable-syntax/#advanced-variable-format-options + destination: /docs/grafana//alerting/ - pattern: /docs/grafana-cloud/ - destination: /docs/grafana//dashboards/variables/variable-syntax/#advanced-variable-format-options + destination: /docs/grafana-cloud/alerting-and-irm/alerting/ + transformations: + - pattern: /docs/grafana/ + destination: /docs/grafana//panels-visualizations/query-transform-data/transform-data/ + - pattern: /docs/grafana-cloud/ + destination: /docs/grafana-cloud/visualizations/panels-visualizations/query-transform-data/transform-data/ + visualizations: + - pattern: /docs/grafana/ + destination: /docs/grafana//panels-visualizations/visualizations/ + - pattern: /docs/grafana-cloud/ + destination: /docs/grafana-cloud/visualizations/panels-visualizations/visualizations/ --- # PostgreSQL data source -Grafana ships with a built-in PostgreSQL data source plugin that allows you to query and visualize data from a PostgreSQL compatible database. +Grafana includes a built-in PostgreSQL data source plugin, enabling you to query and visualize data from any PostgreSQL-compatible database. You don't need to install a plugin to add the PostgreSQL data source to your Grafana instance. -For instructions on how to add a data source to Grafana, refer to the [administration documentation](ref:data-source-management). -Only users with the organization administrator role can add data sources. -Administrators can also [configure the data source via YAML](#provision-the-data-source) with Grafana's provisioning system. +Grafana offers several configuration options for this data source as well as a visual and code-based query editor. + +## Get started with the PostgreSQL data source + +The following documents will help you get started with the PostgreSQL data source in Grafana: + +- [Configure the PostgreSQL data source](ref:configure-postgres-data-source) +- [PostgreSQL query editor](ref:postgres-query-editor) + +After you have configured the data source you can: + +- Create a variety of [visualizations](ref:visualizations) +- Add [annotations](ref:annotate-visualizations) +- Set up [alerting](ref:alerting) +- Add [transformations](ref:transformations) + +View a PostgreSQL overview on Grafana Play: {{< docs/play title="PostgreSQL Overview" url="https://play.grafana.org/d/ddvpgdhiwjvuod/postgresql-overview" >}} - -## PostgreSQL settings - -To configure basic settings for the data source, complete the following steps: - -1. Click **Connections** in the left-side menu. -1. Under Your connections, click **Data sources**. -1. Enter `PostgreSQL` in the search bar. -1. Select **PostgreSQL**. - - The **Settings** tab of the data source is displayed. - -1. Set the data source's basic configuration options: - -| Name | Description | -| --------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| **Name** | The data source name. This is how you refer to the data source in panels and queries. | -| **Default** | Default data source means that it will be pre-selected for new panels. | -| **Host** | The IP address/hostname and optional port of your PostgreSQL instance. _Do not_ include the database name. The connection string for connecting to Postgres will not be correct and it may cause errors. | -| **Database** | Name of your PostgreSQL database. | -| **User** | Database user's login/username | -| **Password** | Database user's password | -| **SSL Mode** | Determines whether or with what priority a secure SSL TCP/IP connection will be negotiated with the server. When SSL Mode is disabled, SSL Method and Auth Details would not be visible. | -| **SSL Auth Details Method** | Determines whether the SSL Auth details will be configured as a file path or file content. | -| **SSL Auth Details Value** | File path or file content of SSL root certificate, client certificate and client key | -| **Max open** | The maximum number of open connections to the database, default `100`. | -| **Max idle** | The maximum number of connections in the idle connection pool, default `100`. | -| **Auto (max idle)** | If set will set the maximum number of idle connections to the number of maximum open connections. Default is `true`. | -| **Max lifetime** | The maximum amount of time in seconds a connection may be reused, default `14400`/4 hours. | -| **Version** | Determines which functions are available in the query builder. | -| **TimescaleDB** | A time-series database built as a PostgreSQL extension. When enabled, Grafana uses `time_bucket` in the `$__timeGroup` macro to display TimescaleDB specific aggregate functions in the query builder. For more information, see [TimescaleDB documentation](https://docs.timescale.com/timescaledb/latest/tutorials/grafana/grafana-timescalecloud/#connect-timescaledb-and-grafana). | - -### Min time interval - -A lower limit for the [`$__interval`](ref:add-template-variables-interval) and [`$__interval_ms`](ref:add-template-variables-interval-ms) variables. -Recommended to be set to write frequency, for example `1m` if your data is written every minute. -This option can also be overridden/configured in a dashboard panel under data source options. It's important to note that this value **needs** to be formatted as a -number followed by a valid time identifier, e.g. `1m` (1 minute) or `30s` (30 seconds). The following time identifiers are supported: - -| Identifier | Description | -| ---------- | ----------- | -| `y` | year | -| `M` | month | -| `w` | week | -| `d` | day | -| `h` | hour | -| `m` | minute | -| `s` | second | -| `ms` | millisecond | - -### Database user permissions (Important!) - -The database user you specify when you add the data source should only be granted SELECT permissions on -the specified database and tables you want to query. Grafana does not validate that the query is safe. The query -could include any SQL statement. For example, statements like `DELETE FROM user;` and `DROP TABLE user;` would be -executed. To protect against this we **highly** recommend you create a specific PostgreSQL user with restricted permissions. - -Example: - -```sql - CREATE USER grafanareader WITH PASSWORD 'password'; - GRANT USAGE ON SCHEMA schema TO grafanareader; - GRANT SELECT ON schema.table TO grafanareader; -``` - -Make sure the user does not get any unwanted privileges from the public role. - -## Query builder - -{{< figure src="/static/img/docs/screenshot-postgres-query-editor.png" class="docs-image--no-shadow" caption="PostgreSQL query builder" >}} - -The PostgreSQL query builder is available when editing a panel using a PostgreSQL data source. The built query can be run by pressing the `Run query` button in the top right corner of the editor. - -### Format - -The response from PostgreSQL can be formatted as either a table or as a time series. To use the time series format one of the columns must be named `time`. - -### Dataset and table selection - -The dataset dropdown will be populated with the configured database to which the user has access. -The table dropdown is populated with the tables that are available within that database. - -### Columns and Aggregation functions (SELECT) - -Using the dropdown, select a column to include in the data. You can also specify an optional aggregation function. - -Add further value columns by clicking the plus button and another column dropdown appears. - -{{< docs/shared source="grafana" lookup="datasources/sql-query-builder-macros.md" version="" >}} - -### Filter data (WHERE) - -To add a filter, toggle the **Filter** switch at the top of the editor. -This reveals a **Filter by column value** section with two dropdown selectors. - -Use the first dropdown to choose whether all of the filters need to match (`AND`), or if only one of the filters needs to match (`OR`). -Use the second dropdown to choose a filter. - -To filter on more columns, click the plus (`+`) button to the right of the condition dropdown. - -To remove a filter, click the `x` button next to that filter's dropdown. - -After selecting a date type column, you can choose Macros from the operators list and select timeFilter which will add the $\_\_timeFilter macro to the query with the selected date column. - -### Group By - -To group the results by column, flip the group switch at the top of the editor. You can then choose which column to group the results by. The group by clause can be removed by pressing the X button. - -### Preview - -By flipping the preview switch at the top of the editor, you can get a preview of the SQL query generated by the query builder. - -### Provision the data source - -It's now possible to configure data sources using config files with Grafana's provisioning system. You can read more about how it works and all the settings you can set for data sources on the [provisioning docs page](ref:provisioning-data-sources). - -#### Provisioning example - -```yaml -apiVersion: 1 - -datasources: - - name: Postgres - type: postgres - url: localhost:5432 - user: grafana - secureJsonData: - password: 'Password!' - jsonData: - database: grafana - sslmode: 'disable' # disable/require/verify-ca/verify-full - maxOpenConns: 100 - maxIdleConns: 100 - maxIdleConnsAuto: true - connMaxLifetime: 14400 - postgresVersion: 903 # 903=9.3, 904=9.4, 905=9.5, 906=9.6, 1000=10 - timescaledb: false -``` - -{{% admonition type="note" %}} -In the above code, the `postgresVersion` value of `10` refers to version PostgreSQL 10 and above. -{{% /admonition %}} - -#### Troubleshoot provisioning - -If you encounter metric request errors or other issues: - -- Make sure your data source YAML file parameters exactly match the example. This includes parameter names and use of quotation marks. -- Make sure the `database` name is not included in the `url`. - -## Code editor - -{{< figure src="/static/img/docs/v92/sql_code_editor.png" class="docs-image--no-shadow" >}} - -To make advanced queries, switch to the code editor by clicking `code` in the top right corner of the editor. The code editor support autocompletion of tables, columns, SQL keywords, standard sql functions, Grafana template variables and Grafana macros. Columns cannot be completed before a table has been specified. - -You can expand the code editor by pressing the `chevron` pointing downwards in the lower right corner of the code editor. - -`CTRL/CMD + Return` works as a keyboard shortcut to run the query. - -## Macros - -Macros can be used within a query to simplify syntax and allow for dynamic parts. - -| Macro example | Description | -| ----------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -| `$__time(dateColumn)` | Will be replaced by an expression to convert to a UNIX timestamp and rename the column to `time_sec`. For example, _UNIX_TIMESTAMP(dateColumn) as time_sec_ | -| `$__timeEpoch(dateColumn)` | Will be replaced by an expression to convert to a UNIX timestamp and rename the column to `time_sec`. For example, _UNIX_TIMESTAMP(dateColumn) as time_sec_ | -| `$__timeFilter(dateColumn)` | Will be replaced by a time range filter using the specified column name. For example, _dateColumn BETWEEN FROM_UNIXTIME(1494410783) AND FROM_UNIXTIME(1494410983)_ | -| `$__timeFrom()` | Will be replaced by the start of the currently active time selection. For example, _FROM_UNIXTIME(1494410783)_ | -| `$__timeTo()` | Will be replaced by the end of the currently active time selection. For example, _FROM_UNIXTIME(1494410983)_ | -| `$__timeGroup(dateColumn,'5m')` | Will be replaced by an expression usable in GROUP BY clause. For example, *cast(cast(UNIX_TIMESTAMP(dateColumn)/(300) as signed)*300 as signed),\* | -| `$__timeGroup(dateColumn,'5m', 0)` | Same as above but with a fill parameter so missing points in that series will be added by grafana and 0 will be used as value (only works with time series queries). | -| `$__timeGroup(dateColumn,'5m', NULL)` | Same as above but NULL will be used as value for missing points (only works with time series queries). | -| `$__timeGroup(dateColumn,'5m', previous)` | Same as above but the previous value in that series will be used as fill value if no value has been seen yet NULL will be used (only works with time series queries). | -| `$__timeGroupAlias(dateColumn,'5m')` | Will be replaced identical to $\_\_timeGroup but with an added column alias. | -| `$__unixEpochFilter(dateColumn)` | Will be replaced by a time range filter using the specified column name with times represented as Unix timestamp. For example, _dateColumn > 1494410783 AND dateColumn < 1494497183_ | -| `$__unixEpochFrom()` | Will be replaced by the start of the currently active time selection as Unix timestamp. For example, _1494410783_ | -| `$__unixEpochTo()` | Will be replaced by the end of the currently active time selection as Unix timestamp. For example, _1494497183_ | -| `$__unixEpochNanoFilter(dateColumn)` | Will be replaced by a time range filter using the specified column name with times represented as nanosecond timestamp. For example, _dateColumn > 1494410783152415214 AND dateColumn < 1494497183142514872_ | -| `$__unixEpochNanoFrom()` | Will be replaced by the start of the currently active time selection as nanosecond timestamp. For example, _1494410783152415214_ | -| `$__unixEpochNanoTo()` | Will be replaced by the end of the currently active time selection as nanosecond timestamp. For example, _1494497183142514872_ | -| `$__unixEpochGroup(dateColumn,'5m', [fillmode])` | Same as $\_\_timeGroup but for times stored as Unix timestamp (`fillMode` only works with time series queries). | -| `$__unixEpochGroupAlias(dateColumn,'5m', [fillmode])` | Same as above but also adds a column alias (`fillMode` only works with time series queries). | - -## Table queries - -If the `Format as` query option is set to `Table` then you can basically do any type of SQL query. The table panel will automatically show the results of whatever columns and rows your query returns. - -Query editor with example query: - -![](/static/img/docs/v46/postgres_table_query.png) - -The query: - -```sql -SELECT - title as "Title", - "user".login as "Created By", - dashboard.created as "Created On" -FROM dashboard -INNER JOIN "user" on "user".id = dashboard.created_by -WHERE $__timeFilter(dashboard.created) -``` - -You can control the name of the Table panel columns by using regular `as ` SQL column selection syntax. - -The resulting table panel: - -![postgres table](/static/img/docs/v46/postgres_table.png) - -## Time series queries - -If you set Format as to _Time series_, then the query must have a column named time that returns either a SQL datetime or any numeric datatype representing Unix epoch in seconds. In addition, result sets of time series queries must be sorted by time for panels to properly visualize the result. - -A time series query result is returned in a [wide data frame format](https://grafana.com/developers/plugin-tools/key-concepts/data-frames#wide-format). Any column except time or of type string transforms into value fields in the data frame query result. Any string column transforms into field labels in the data frame query result. - -> For backward compatibility, there's an exception to the above rule for queries that return three columns including a string column named metric. Instead of transforming the metric column into field labels, it becomes the field name, and then the series name is formatted as the value of the metric column. See the example with the metric column below. - -To optionally customize the default series name formatting, refer to [Standard options definitions](ref:configure-standard-options-display-name). - -**Example with `metric` column:** - -```sql -SELECT - $__timeGroupAlias("time_date_time",'5m'), - min("value_double"), - 'min' as metric -FROM test_data -WHERE $__timeFilter("time_date_time") -GROUP BY time -ORDER BY time -``` - -Data frame result: - -```text -+---------------------+-----------------+ -| Name: time | Name: min | -| Labels: | Labels: | -| Type: []time.Time | Type: []float64 | -+---------------------+-----------------+ -| 2020-01-02 03:05:00 | 3 | -| 2020-01-02 03:10:00 | 6 | -+---------------------+-----------------+ -``` - -**Example using the fill parameter in the $\_\_timeGroupAlias macro to convert null values to be zero instead:** - -```sql -SELECT - $__timeGroupAlias("createdAt",'5m',0), - sum(value) as value, - hostname -FROM test_data -WHERE - $__timeFilter("createdAt") -GROUP BY time, hostname -ORDER BY time -``` - -Given the data frame result in the following example and using the graph panel, you will get two series named _value 10.0.1.1_ and _value 10.0.1.2_. To render the series with a name of _10.0.1.1_ and _10.0.1.2_ , use a [Standard options definitions](ref:configure-standard-options-display-name) display value of `${__field.labels.hostname}`. - -Data frame result: - -```text -+---------------------+---------------------------+---------------------------+ -| Name: time | Name: value | Name: value | -| Labels: | Labels: hostname=10.0.1.1 | Labels: hostname=10.0.1.2 | -| Type: []time.Time | Type: []float64 | Type: []float64 | -+---------------------+---------------------------+---------------------------+ -| 2020-01-02 03:05:00 | 3 | 4 | -| 2020-01-02 03:10:00 | 6 | 7 | -+---------------------+---------------------------+---------------------------+ -``` - -**Example with multiple columns:** - -```sql -SELECT - $__timeGroupAlias("time_date_time",'5m'), - min("value_double") as "min_value", - max("value_double") as "max_value" -FROM test_data -WHERE $__timeFilter("time_date_time") -GROUP BY time -ORDER BY time -``` - -Data frame result: - -```text -+---------------------+-----------------+-----------------+ -| Name: time | Name: min_value | Name: max_value | -| Labels: | Labels: | Labels: | -| Type: []time.Time | Type: []float64 | Type: []float64 | -+---------------------+-----------------+-----------------+ -| 2020-01-02 03:04:00 | 3 | 4 | -| 2020-01-02 03:05:00 | 6 | 7 | -+---------------------+-----------------+-----------------+ -``` - -## Templating - -Instead of hard-coding things like server, application and sensor name in your metric queries you can use variables in their place. Variables are shown as dropdown select boxes at the top of the dashboard. These dropdowns make it easy to change the data being displayed in your dashboard. - -Refer to [Templates and variables](ref:variables) for an introduction to the templating feature and the different types of template variables. - -### Query variable - -If you add a template variable of the type `Query`, you can write a PostgreSQL query that can -return things like measurement names, key names or key values that are shown as a dropdown select box. - -For example, you can have a variable that contains all values for the `hostname` column in a table if you specify a query like this in the templating variable _Query_ setting. - -```sql -SELECT hostname FROM host -``` - -A query can return multiple columns and Grafana will automatically create a list from them. For example, the query below will return a list with values from `hostname` and `hostname2`. - -```sql -SELECT host.hostname, other_host.hostname2 FROM host JOIN other_host ON host.city = other_host.city -``` - -To use time range dependent macros like `$__timeFilter(column)` in your query the refresh mode of the template variable needs to be set to _On Time Range Change_. - -```sql -SELECT event_name FROM event_log WHERE $__timeFilter(time_column) -``` - -Another option is a query that can create a key/value variable. The query should return two columns that are named `__text` and `__value`. The `__text` column value should be unique (if it is not unique then the first value is used). The options in the dropdown will have a text and value that allows you to have a friendly name as text and an id as the value. An example query with `hostname` as the text and `id` as the value: - -```sql -SELECT hostname AS __text, id AS __value FROM host -``` - -You can also create nested variables. Using a variable named `region`, you could have -the hosts variable only show hosts from the current selected region with a query like this (if `region` is a multi-value variable then use the `IN` comparison operator rather than `=` to match against multiple values): - -```sql -SELECT hostname FROM host WHERE region IN($region) -``` - -#### Using `__searchFilter` to filter results in Query Variable - -Using `__searchFilter` in the query field will filter the query result based on what the user types in the dropdown select box. -When nothing has been entered by the user the default value for `__searchFilter` is `%`. - -> Important that you surround the `__searchFilter` expression with quotes as Grafana does not do this for you. - -The example below shows how to use `__searchFilter` as part of the query field to enable searching for `hostname` while the user types in the dropdown select box. - -Query - -```sql -SELECT hostname FROM my_host WHERE hostname LIKE '$__searchFilter' -``` - -### Using Variables in Queries - -Template variable values are only quoted when the template variable is a `multi-value`. - -If the variable is a multi-value variable then use the `IN` comparison operator rather than `=` to match against multiple values. - -There are two syntaxes: - -`$` Example with a template variable named `hostname`: - -```sql -SELECT - atimestamp as time, - aint as value -FROM table -WHERE $__timeFilter(atimestamp) and hostname in($hostname) -ORDER BY atimestamp ASC -``` - -`[[varname]]` Example with a template variable named `hostname`: - -```sql -SELECT - atimestamp as time, - aint as value -FROM table -WHERE $__timeFilter(atimestamp) and hostname in([[hostname]]) -ORDER BY atimestamp ASC -``` - -#### Disabling quoting for multi-value variables - -Grafana automatically creates a quoted, comma-separated string for multi-value variables. For example: if `server01` and `server02` are selected then it will be formatted as: `'server01', 'server02'`. To disable quoting, use the csv formatting option for variables: - -`${servers:csv}` - -Read more about variable formatting options in the [Variables](ref:variable-syntax-advanced-variable-format-options) documentation. - -## Annotations - -[Annotations](ref:annotate-visualizations) allow you to overlay rich event information on top of graphs. You add annotation queries via the Dashboard menu / Annotations view. - -**Example query using time column with epoch values:** - -```sql -SELECT - epoch_time as time, - metric1 as text, - concat_ws(', ', metric1::text, metric2::text) as tags -FROM - public.test_data -WHERE - $__unixEpochFilter(epoch_time) -``` - -**Example region query using time and timeend columns with epoch values:** - -```sql -SELECT - epoch_time as time, - epoch_time_end as timeend, - metric1 as text, - concat_ws(', ', metric1::text, metric2::text) as tags -FROM - public.test_data -WHERE - $__unixEpochFilter(epoch_time) -``` - -**Example query using time column of native SQL date/time data type:** - -```sql -SELECT - native_date_time as time, - metric1 as text, - concat_ws(', ', metric1::text, metric2::text) as tags -FROM - public.test_data -WHERE - $__timeFilter(native_date_time) -``` - -| Name | Description | -| --------- | ----------------------------------------------------------------------------------------------------------------- | -| `time` | The name of the date/time field. Could be a column with a native SQL date/time data type or epoch value. | -| `timeend` | Optional name of the end date/time field. Could be a column with a native SQL date/time data type or epoch value. | -| `text` | Event description field. | -| `tags` | Optional field name to use for event tags as a comma separated string. | - -## Alerting - -Time series queries should work in alerting conditions. Table formatted queries are not yet supported in alert rule -conditions. diff --git a/docs/sources/datasources/postgres/configure/_index.md b/docs/sources/datasources/postgres/configure/_index.md new file mode 100644 index 00000000000..7b6a66c0a70 --- /dev/null +++ b/docs/sources/datasources/postgres/configure/_index.md @@ -0,0 +1,194 @@ +--- +description: This document provides instructions for configuring the PostgreSQL data source. +keywords: + - grafana + - postgresql + - guide +labels: + products: + - cloud + - enterprise + - oss +menuTitle: Configure the PostgreSQL data source +title: Configure the PostgreSQL data source +weight: 10 +refs: + provisioning-data-sources: + - pattern: /docs/grafana/ + destination: /docs/grafana//administration/provisioning/#datasources + - pattern: /docs/grafana-cloud/ + destination: /docs/grafana//administration/provisioning/#datasources + variables: + - pattern: /docs/grafana/ + destination: /docs/grafana//dashboards/variables/ + - pattern: /docs/grafana-cloud/ + destination: /docs/grafana//dashboards/variables/ + add-template-variables-interval-ms: + - pattern: /docs/grafana/ + destination: /docs/grafana//dashboards/variables/add-template-variables/#__interval_ms + - pattern: /docs/grafana-cloud/ + destination: /docs/grafana//dashboards/variables/add-template-variables/#__interval_ms + add-template-variables-interval: + - pattern: /docs/grafana/ + destination: /docs/grafana//dashboards/variables/add-template-variables/#__interval + - pattern: /docs/grafana-cloud/ + destination: /docs/grafana//dashboards/variables/add-template-variables/#__interval + data-source-management: + - pattern: /docs/grafana/ + destination: /docs/grafana//administration/data-source-management/ + - pattern: /docs/grafana-cloud/ + destination: /docs/grafana//administration/data-source-management/ + variable-syntax-advanced-variable-format-options: + - pattern: /docs/grafana/ + destination: /docs/grafana//dashboards/variables/variable-syntax/#advanced-variable-format-options + - pattern: /docs/grafana-cloud/ + destination: /docs/grafana//dashboards/variables/variable-syntax/#advanced-variable-format-options +--- + +# Configure the PostgreSQL data source + +This document provides instructions for configuring the PostgreSQL data source and explains available configuration options. For general information on managing data sources refer to [Data source management](ref:data-source-management). + +## Before you begin + +You must have the `Organization administrator` role to configure the Postgres data source. +Organization administrators can also [configure the data source via YAML](#provision-the-data-source) with the Grafana provisioning system. + +Grafana comes with a built-in PostgreSQL data source plugin, eliminating the need to install a plugin. + +{{< admonition type="note" >}} +When adding a data source, the database user you specify should have only `SELECT` permissions on the relevant database and tables. Grafana does not validate the safety of queries, which means they can include potentially harmful SQL statements, such as `USE otherdb;` or `DROP TABLE user;`, that could be executed. To mitigate this risk, Grafana strongly recommends creating a dedicated PostgreSQL user with restricted permissions. +{{< /admonition >}} + +Example: + +```sql + CREATE USER grafanareader WITH PASSWORD 'password'; + GRANT USAGE ON SCHEMA schema TO grafanareader; + GRANT SELECT ON schema.table TO grafanareader; +``` + +## Add the PostgreSQL data source + +Complete the following steps to set up a new PostgreSQL data source: + +1. Click **Connections** in the left-side menu. +1. Click **Add new connection** +1. Type `PostgreSQL` in the search bar. +1. Select the **PostgreSQL data source**. +1. Click **Add new data source** in the upper right. + +You are taken to the **Settings** tab where you will configure the data source. + +## PostgreSQL configuration options + +Following is a list of PostgreSQL configuration options: + +- **Name** - Sets the name you use to refer to the data source in panels and queries. Examples: `PostgreSQL-DB-1`. +- **Default** - Toggle to set this specific PostgreSQL data source as the default pre-selected data source in panels and visualizations. + +**Connection section:** + +- **Host URL** - The IP address/hostname and optional port of your PostgreSQL instance. +- **Database name** - The name of your PostgreSQL database. + +**Authentication section:** + +- **Username** - Enter the username used to connect to your PostgreSQL database. +- **Password** - Enter the password used to connect to the PostgreSQL database. +- **TLS/SSL Mode** - Determines whether or with what priority a secure SSL TCP/IP connection will be negotiated with the server. When **TLS/SSL Mode** is disabled, **TLS/SSL Method** and **TLS/SSL Auth Details** aren't visible options. +- **TLS/SSL Method** - Determines how TLS/SSL certificates are configured. + - **File system path** - This option allows you to configure certificates by specifying paths to existing certificates on the local file system where Grafana is running. Ensure this file is readable by the user executing the Grafana process. + - **Certificate content** - This option allows you to configure certificate by specifying their content. The content is stored and encrypted in the Grafana database. When connecting to the database, the certificates are saved as files, on the local filesystem, in the Grafana data path. + +**TLS/SSL Auth Details** + +If you select the TLS/SSL Mode options **require**, **verify-ca** or **verify-full** and **file system path** the following are required: + +- **TLS/SSL Root Certificate** - Specify the path to the root certificate file. +- **TLS/SSL Client Certificate** - Specify the path to the client certificate and ensure the file is accessible to the user running the Grafana process. +- **TLS/SSL Client Key** - Specify the path to the client key file and ensure the file is accessible to the user running the Grafana process. + +If you select the TLS/SSL Mode option **require** and TLS/SSL Method certificate content the following are required: + +- **TLS/SSL Client Certificate** - Provide the client certificate. +- **TLS/SSL Client Key** - Provide the client key. + +If you select the TLS/SSL Mode options **verify-ca** or **verify-full** with the TLS/SSL Method certificate content the following are required: + +- **TLS/SSL Client Certificate** - Provide the client certificate. +- **TLS/SSL Root Certificate** - Provide the root certificate. +- **TLS/SSL Client Key** - Provide the client key. + +**PostgreSQL Options:** + +- **Version** - Determines which functions are available in the query builder. The default is the current version. +- **Min time interval** - Defines a lower limit for the auto group by by time interval. Grafana recommends aligning this setting with the data write frequency. For example, set it to `1m` if your data is written every minute. Refer to [Min time interval](#min-time-interval) for format examples. +- **TimescaleDB** - A time-series database built as a PostgreSQL extension. When enabled, Grafana uses `time_bucket` in the `$__timeGroup` macro to display TimescaleDB specific aggregate functions in the query builder. For more information, refer to [TimescaleDB documentation](https://docs.timescale.com/timescaledb/latest/tutorials/grafana/grafana-timescalecloud/#connect-timescaledb-and-grafana). + +**Connection limits:** + +- **Max open** - The maximum number of open connections to the database. The default `100`. +- **Auto max idle** - Toggle to set the maximum number of idle connections to the number of maximum open connections. This setting is toggled on by default. +- **Max idle** - The maximum number of connections in the idle connection pool. The default `100`. +- **Max lifetime** - The maximum amount of time in seconds a connection may be reused. The default is `14400`, or 4 hours. + +**Private data source connect** - _Only for Grafana Cloud users._ Private data source connect, or PDC, allows you to establish a private, secured connection between a Grafana Cloud instance, or stack, and data sources secured within a private network. Click the drop-down to locate the URL for PDC. For more information regarding Grafana PDC refer to [Private data source connect (PDC)](https://grafana.com/docs/grafana-cloud/connect-externally-hosted/private-data-source-connect/). + +Click **Manage private data source connect** to be taken to your PDC connection page, where you’ll find your PDC configuration details. + +After you have added your PostgreSQL connection settings, click **Save & test** to test and save the data source connection. + +### Min time interval + +The **Min time interval** setting defines a lower limit for the [`$__interval`](ref:add-template-variables-interval) and [`$__interval_ms`](ref:add-template-variables-interval-ms) variables. + +This option can also be configured or overridden in the dashboard panel under the data source settings. + +This value must be formatted as a number followed by a valid time identifier: + +| Identifier | Description | +| ---------- | ----------- | +| `y` | year | +| `M` | month | +| `w` | week | +| `d` | day | +| `h` | hour | +| `m` | minute | +| `s` | second | +| `ms` | millisecond | + +## Provision the data source + +You can define and configure the data source in YAML files with [provisioning](/docs/grafana//administration/provisioning/#data-sources). +For more information about provisioning, and available configuration options, refer to [Provision Grafana](ref:provisioning-data-sources). + +### PostgreSQL provisioning example + +```yaml +apiVersion: 1 + +datasources: + - name: Postgres + type: postgres + url: localhost:5432 + user: grafana + secureJsonData: + password: 'Password!' + jsonData: + database: grafana + sslmode: 'disable' # disable/require/verify-ca/verify-full + maxOpenConns: 100 + maxIdleConns: 100 + maxIdleConnsAuto: true + connMaxLifetime: 14400 + postgresVersion: 903 # 903=9.3, 904=9.4, 905=9.5, 906=9.6, 1000=10 + timescaledb: false +``` + +#### Troubleshoot provisioning issues + +If you encounter metric request errors or other issues: + +- Ensure that the parameters in your data source YAML file precisely match the example provided, including parameter names and the correct use of quotation marks. +- Verify that the database name _isn't_ included in the URL. diff --git a/docs/sources/datasources/postgres/query-editor/_index.md b/docs/sources/datasources/postgres/query-editor/_index.md new file mode 100644 index 00000000000..5c37f7047e7 --- /dev/null +++ b/docs/sources/datasources/postgres/query-editor/_index.md @@ -0,0 +1,410 @@ +--- +description: This document describes the PostgreSQL query editor in Grafana. +keywords: + - grafana + - postgresql + - guide +labels: + products: + - cloud + - enterprise + - oss +menuTitle: PostgreSQL query editor +title: PostgreSQL query editor +weight: 20 +refs: + variables: + - pattern: /docs/grafana/ + destination: /docs/grafana//dashboards/variables/ + - pattern: /docs/grafana-cloud/ + destination: /docs/grafana//dashboards/variables/ + add-template-variables-interval: + - pattern: /docs/grafana/ + destination: /docs/grafana//dashboards/variables/add-template-variables/#__interval + - pattern: /docs/grafana-cloud/ + destination: /docs/grafana//dashboards/variables/add-template-variables/#__interval + explore: + - pattern: /docs/grafana/ + destination: /docs/grafana//explore/ + - pattern: /docs/grafana-cloud/ + destination: /docs/grafana//explore/ + query-transform-data: + - pattern: /docs/grafana/ + destination: /docs/grafana//panels-visualizations/query-transform-data/ + - pattern: /docs/grafana-cloud/ + destination: /docs/grafana-cloud/visualizations/panels-visualizations/query-transform-data/ + query-editor: + - pattern: /docs/grafana/ + destination: /docs/grafana//panels-visualizations/query-transform-data/#query-editors + - pattern: /docs/grafana-cloud/ + destination: /docs/grafana-cloud/visualizations/panels-visualizations/query-transform-data/#query-editors + alert-rules: + - pattern: /docs/grafana/ + destination: /docs/grafana//alerting/fundamentals/alert-rules/ + - pattern: /docs/grafana-cloud/ + destination: /docs/grafana-cloud/alerting-and-irm/alerting/alerting-rules/ + template-annotations-and-labels: + - pattern: /docs/grafana/ + destination: /docs/grafana//alerting/alerting-rules/templates/ + - pattern: /docs/grafana-cloud/ + destination: /docs/grafana-cloud/alerting-and-irm/alerting/alerting-rules/templates/ + templates: + - pattern: /docs/grafana/ + destination: /docs/grafana//dashboards/variables/#templates + - pattern: /docs/grafana-cloud/ + destination: /docs/grafana-cloud/visualizations/dashboards/variables/#templates +--- + +# PostgreSQL query editor + +Grafana query editors are unique for each data source. + +For general information on Grafana query editors, refer to [Query editors](ref:query-editor). + +For general information on querying data sources in Grafana, refer to [Query and transform data](ref:query-transform-data). + +The PostgreSQL query editor is located on the [Explore page](ref:explore). You can also access the PostgreSQL query editor from a dashboard panel. Click the ellipsis in the upper right of the panel and select **Edit**. + +{{< figure src="/static/img/docs/screenshot-postgres-query-editor.png" class="docs-image--no-shadow" caption="PostgreSQL query builder" >}} + +## PostgreSQL query editor components + +The PostgreSQL query editor has two modes: **Builder** and **Code**. + +Builder mode helps you build a query using a visual interface. Code mode allows for advanced querying and offers support for complex SQL query writing. + +### PostgreSQL Builder mode + +The following components will help you build a PostgreSQL query: + +- **Format** - Select a format response from the drop-down for the PostgreSQL query. The default is **Table**. If you use the **Time series** format option, one of the columns must be `time`. Refer to [Time series queries](#time-series-queries) for more information. +- **Table** - Select a table from the drop-down. Tables correspond to the chosen database. +- **Data operations** - _Optional_ Select an aggregation from the drop-down. You can add multiple data operations by clicking the **+ sign**. Click the **garbage can icon** to remove data operations. +- **Column** - Select a column on which to run the aggregation. +- **Alias** - _Optional_ Add an alias from the drop-down. You can also add your own alias by typing it in the box and clicking **Enter**. Remove an alias by clicking the **X**. +- **Filter** - Toggle to add filters. +- **Filter by column value** - _Optional_ If you toggle **Filter** you can add a column to filter by from the drop-down. To filter on more columns, click the **+ sign** to the right of the condition drop-down. You can choose a variety of operators from the drop-down next to the condition. When multiple filters are added you can add an `AND` operator to display all true conditions or an `OR` operator to display any true conditions. Use the second drop-down to choose a filter. To remove a filter, click the `X` button next to that filter's drop-down. After selecting a date type column, you can choose **Macros** from the operators list and select `timeFilter` which will add the `$\_\_timeFilter` macro to the query with the selected date column. +- **Group** - Toggle to add **Group by column**. +- **Group by column** - Select a column to filter by from the drop-down. Click the **+sign** to filter by multiple columns. Click the **X** to remove a filter. +- **Order** - Toggle to add an `ORDER BY` statement. +- **Order by** - Select a column to order by from the drop-down. Select ascending (`ASC`) or descending (`DESC`) order. +- **Limit** - You can add an optional limit on the number of retrieved results. Default is 50. +- **Preview** - Toggle for a preview of the SQL query generated by the query builder. Preview is toggled on by default. + +## PostgreSQL Code mode + +To create advanced queries, switch to **Code mode** by clicking **Code** in the upper right of the editor window. Code mode supports the auto-completion of tables, columns, SQL keywords, standard SQL functions, Grafana template variables, and Grafana macros. Columns cannot be completed before a table has been specified. + +{{< figure src="/static/img/docs/v92/sql_code_editor.png" class="docs-image--no-shadow" >}} + +Select **Table** or **Time Series** as the format. Click the **{}** in the bottom right to format the query. Click the **downward caret** to expand the Code mode editor. **CTRL/CMD + Return** serves as a keyboard shortcut to execute the query. + +{{< admonition type="warning" >}} +Changes made to a query in Code mode will not transfer to Builder mode and will be discarded. You will be prompted to copy your code to the clipboard to save any changes. +{{< /admonition >}} + +## Macros + +You can add macros to your queries to simplify the syntax and enable dynamic elements, such as date range filters. + +| Macro example | Description | +| ----------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `$__time(dateColumn)` | Replaces the value with an expression to convert to a UNIX timestamp and renames the column to `time_sec`. Example: `UNIX_TIMESTAMP(dateColumn) AS time_sec`. | +| `$__timeEpoch(dateColumn)` | Replaces the value with an expression to convert to a UNIX Epoch timestamp and renames the column to `time_sec`. Example: `UNIX_TIMESTAMP(dateColumn) AS time_sec`. | +| `$__timeFilter(dateColumn)` | Replaces the value a time range filter using the specified column name. Example: `dateColumn BETWEEN FROM_UNIXTIME(1494410783) AND FROM_UNIXTIME(1494410983)` | +| `$__timeFrom()` | Replaces the value with the start of the currently active time selection. Example: `FROM_UNIXTIME(1494410783)` | +| `$__timeTo()` | Replaces the value with the end of the currently active time selection. Example: `FROM_UNIXTIME(1494410983)` | +| `$__timeGroup(dateColumn,'5m')` | Replaces the value with an expression suitable for use in a `GROUP BY` clause. Example: `cast(cast(UNIX_TIMESTAMP(dateColumn)/(300) AS signed)*300 AS signed)` | +| `$__timeGroup(dateColumn,'5m', 0)` | Same as the `$__timeGroup(dateColumn,'5m')` macro, but includes a fill parameter to ensure missing points in the series are added by Grafana, using 0 as the default value. **This applies only to time series queries.** | +| `$__timeGroup(dateColumn,'5m', NULL)` | Same as the `$__timeGroup(dateColumn,'5m', 0)` but `NULL` is used as the value for missing points. _This applies only to time series queries._ | +| `$__timeGroup(dateColumn,'5m', previous)` | Same as the `$__timeGroup(dateColumn,'5m', previous)` macro, but uses the previous value in the series as the fill value. If no previous value exists, it uses `NULL`. _This applies only to time series queries._ | +| `$__timeGroupAlias(dateColumn,'5m')` | Replaces the value identical to `$__timeGroup` but with an added column alias. | +| `$__unixEpochFilter(dateColumn)` | Replaces the value by a time range filter using the specified column name with times represented as a UNIX timestamp. Example: `dateColumn > 1494410783 AND dateColumn < 1494497183` | +| `$__unixEpochFrom()` | Replaces the value with the start of the currently active time selection as a UNIX timestamp. Example: `1494410783` | +| `$__unixEpochTo()` | Replaces the value with the end of the currently active time selection as a UNIX timestamp. Example: `1494497183` | +| `$__unixEpochNanoFilter(dateColumn)` | Replaces the value with a time range filter using the specified column name with time represented as a nanosecond timestamp. Example: `dateColumn > 1494410783152415214 AND dateColumn < 1494497183142514872` | +| `$__unixEpochNanoFrom()` | Replaces the value with the start of the currently active time selection as a nanosecond timestamp. Example: `1494410783152415214` | +| `$__unixEpochNanoTo()` | Replaces the value with the end of the currently active time selection as nanosecond timestamp. Example: `1494497183142514872` | +| `$__unixEpochGroup(dateColumn,'5m', [fillmode])` | Same as `$__timeGroup` but for times stored as Unix timestamp. `fillMode` only works with time series queries. | +| `$__unixEpochGroupAlias(dateColumn,'5m', [fillmode])` | Same as `$__timeGroup` but also adds a column alias. `fillMode` only works with time series queries. | + +## Table SQL queries + +If the **Format** option is set to **Table**, you can execute virtually any type of SQL query. The Table panel will automatically display the resulting columns and rows from your query. + +![Table query](/media/docs/postgres/PostgreSQL-query-editor-v11.4.png) + +You can change or customize the name of a Table panel column by using the SQL keyword `AS` syntax. + +```sql +SELECT + title as "Title", + "user".login as "Created By", + dashboard.created as "Created On" +FROM dashboard +INNER JOIN "user" on "user".id = dashboard.created_by +WHERE $__timeFilter(dashboard.created) +``` + +## Time series queries + +Set the **Format** option to **Time series** to create and run time series queries. + +{{< admonition type="note" >}} +To run a time series query you must include a column named `time` that returns either a SQL `datetime` value or a numeric datatype representing the UNIX epoch time in seconds. Additionally, the query results must be sorted by the `time` column for proper visualization in panels. +{{< /admonition >}} + +The examples in this section refer to the data in the following table: + +```text ++---------------------+--------------+---------------------+----------+ +| time_date_time | value_double | CreatedAt | hostname | ++---------------------+--------------+---------------------+----------+ +| 2020-01-02 03:05:00 | 3.0 | 2020-01-02 03:05:00 | 10.0.1.1 | +| 2020-01-02 03:06:00 | 4.0 | 2020-01-02 03:06:00 | 10.0.1.2 | +| 2020-01-02 03:10:00 | 6.0 | 2020-01-02 03:10:00 | 10.0.1.1 | +| 2020-01-02 03:11:00 | 7.0 | 2020-01-02 03:11:00 | 10.0.1.2 | +| 2020-01-02 03:20:00 | 5.0 | 2020-01-02 03:20:00 | 10.0.1.2 | ++---------------------+--------------+---------------------+----------+ +``` + +Time series query results are returned in [wide data frame format](https://grafana.com/developers/plugin-tools/key-concepts/data-frames#wide-format). In the data frame query result, any column, except for time or string-type columns, transforms into value fields. String columns, on the other hand, become field labels. + +{{< admonition type="note" >}} +For backward compatibility, an exception to this rule applies to queries that return three columns, one of which is a string column named `metric`. Instead of converting the metric column into field labels, it is used as the field name, while the series name is set to its value. See the following example for reference. +{{< /admonition >}} + +**Example with `metric` column:** + +```sql +SELECT + $__timeGroupAlias("time_date_time",'5m'), + min("value_double"), + 'min' as metric +FROM test_data +WHERE $__timeFilter("time_date_time") +GROUP BY time +ORDER BY time +``` + +Data frame result: + +```text ++---------------------+-----------------+ +| Name: time | Name: min | +| Labels: | Labels: | +| Type: []time.Time | Type: []float64 | ++---------------------+-----------------+ +| 2020-01-02 03:05:00 | 3 | +| 2020-01-02 03:10:00 | 6 | ++---------------------+-----------------+ +``` + +To customize default series name formatting, refer to [Standard options definitions](ref:configure-standard-options-display-name). + +Following are time series query examples. + +**Example using the fill parameter in the $\_\_timeGroupAlias macro to convert null values to be zero instead:** + +```sql +SELECT + $__timeGroupAlias("createdAt",'5m',0), + sum(value) as value, + hostname +FROM test_data +WHERE + $__timeFilter("createdAt") +GROUP BY time, hostname +ORDER BY time +``` + +Based on the data frame result in the following example, the time series panel will generate two series named _value 10.0.1.1_ and _value 10.0.1.2_. To display the series names as _10.0.1.1_ and _10.0.1.2_, use the [Standard options definitions](ref:configure-standard-options-display-name) display value `${__field.labels.hostname}`. + +Data frame result: + +```text ++---------------------+---------------------------+---------------------------+ +| Name: time | Name: value | Name: value | +| Labels: | Labels: hostname=10.0.1.1 | Labels: hostname=10.0.1.2 | +| Type: []time.Time | Type: []float64 | Type: []float64 | ++---------------------+---------------------------+---------------------------+ +| 2020-01-02 03:05:00 | 3 | 4 | +| 2020-01-02 03:10:00 | 6 | 7 | ++---------------------+---------------------------+---------------------------+ +``` + +**Example with multiple columns:** + +```sql +SELECT + $__timeGroupAlias("time_date_time",'5m'), + min("value_double") as "min_value", + max("value_double") as "max_value" +FROM test_data +WHERE $__timeFilter("time_date_time") +GROUP BY time +ORDER BY time +``` + +Data frame result: + +```text ++---------------------+-----------------+-----------------+ +| Name: time | Name: min_value | Name: max_value | +| Labels: | Labels: | Labels: | +| Type: []time.Time | Type: []float64 | Type: []float64 | ++---------------------+-----------------+-----------------+ +| 2020-01-02 03:04:00 | 3 | 4 | +| 2020-01-02 03:05:00 | 6 | 7 | ++---------------------+-----------------+-----------------+ +``` + +## Templating + +Instead of hard coding values like server, application, or sensor names in your metric queries, you can use variables. Variables appear as drop-down select boxes at the top of the dashboard. These drop-downs make it easy to change the data being displayed in your dashboard. + +Refer to [Templates](ref:templates) for an introduction to creating template variables as well as the different types. + +### Query variable + +If you add a `Query` template variable you can write a PostgreSQL query to retrieve items such as measurement names, key names, or key values, which will be displayed in the drop-down menu. + +For example, you can use a variable to retrieve all the values from the `hostname` column in a table by creating the following query in the templating variable _Query_ setting. + +```sql +SELECT hostname FROM host +``` + +A query can return multiple columns, and Grafana will automatically generate a list based on the query results. For example, the following query returns a list with values from `hostname` and `hostname2`. + +```sql +SELECT host.hostname, other_host.hostname2 FROM host JOIN other_host ON host.city = other_host.city +``` + +To use time range dependent macros like `$__timeFilter(column)` in your query, you must set the template variable's refresh mode to _On Time Range Change_. + +```sql +SELECT event_name FROM event_log WHERE $__timeFilter(time_column) +``` + +Another option is a query that can create a key/value variable. The query should return two columns that are named `__text` and `__value`. The `__text` column must contain unique values (if not, only the first value is used). This allows the drop-down options to display a text-friendly name as the text while using an ID as the value. For example, a query could use `hostname` as the text and `id` as the value: + +```sql +SELECT hostname AS __text, id AS __value FROM host +``` + +You can also create nested variables. For example, if you have a variable named `region`, you can configure the `hosts` variable to display only the hosts within the currently selected region as shown in the following example. If `region` is a multi-value variable, use the `IN` operator instead of `=` to match multiple values. + +```sql +SELECT hostname FROM host WHERE region IN($region) +``` + +#### Using `__searchFilter` to filter results in Query Variable + +Using `__searchFilter` in the query field allows the query results to be filtered based on the user’s input in the drop-down selection box. If you do not enter anything, the default value for `__searchFilter` is `%`. + +Note that you must enclose the `__searchFilter` expression in quotes as Grafana does not add them automatically. + +The following example demonstrates how to use `__searchFilter` in the query field to enable real-time searching for `hostname` as the user type in the drop-down selection box. + +```sql +SELECT hostname FROM my_host WHERE hostname LIKE '$__searchFilter' +``` + +### Using Variables in Queries + +Template variable values are only quoted when the template variable is a `multi-value`. + +If the variable is a multi-value variable, use the `IN` comparison operator instead of `=` to match against multiple values. + +You can use two different syntaxes: + +`$` Example with a template variable named `hostname`: + +```sql +SELECT + atimestamp as time, + aint as value +FROM table +WHERE $__timeFilter(atimestamp) and hostname in($hostname) +ORDER BY atimestamp ASC +``` + +`[[varname]]` Example with a template variable named `hostname`: + +```sql +SELECT + atimestamp as time, + aint as value +FROM table +WHERE $__timeFilter(atimestamp) and hostname in([[hostname]]) +ORDER BY atimestamp ASC +``` + +#### Disabling quoting for multi-value variables + +Grafana automatically formats multi-value variables as a quoted, comma-separated string. For example, if `server01` and `server02` are selected, they are formatted as `'server01'`, `'server02'`. To remove the quotes, enable the CSV formatting option for the variables: + +`${servers:csv}` + +Read more about variable formatting options in the [Variables](ref:variable-syntax-advanced-variable-format-options) documentation. + +## Annotations + +[Annotations](ref:annotate-visualizations) allow you to overlay rich event information on top of graphs. Add annotation queries via the **Dashboard settings > Annotations view**. + +**Example query using a `time` column with epoch values:** + +```sql +SELECT + epoch_time as time, + metric1 as text, + concat_ws(', ', metric1::text, metric2::text) as tags +FROM + public.test_data +WHERE + $__unixEpochFilter(epoch_time) +``` + +**Example region query using `time` and `timeend` columns with epoch values:** + +```sql +SELECT + epoch_time as time, + epoch_time_end as timeend, + metric1 as text, + concat_ws(', ', metric1::text, metric2::text) as tags +FROM + public.test_data +WHERE + $__unixEpochFilter(epoch_time) +``` + +**Example query using a `time` column with a native SQL date/time data type:** + +```sql +SELECT + native_date_time as time, + metric1 as text, + concat_ws(', ', metric1::text, metric2::text) as tags +FROM + public.test_data +WHERE + $__timeFilter(native_date_time) +``` + +| Name | Description | +| --------- | --------------------------------------------------------------------------------------------------------------------- | +| `time` | The name of the date/time field, which can be a column with a native SQL date/time data type or epoch value. | +| `timeend` | Optional name of the end date/time field, which can be a column with a native SQL date/time data type or epoch value. | +| `text` | Event description field. | +| `tags` | Optional field name to use for event tags as a comma-separated string. | + +## Alerting + +Use time series queries to create alerts. Table formatted queries aren't yet supported in alert rule conditions. + +For more information regarding alerting refer to the following: + +- [Alert rules](ref:alert-rules) +- [Template annotations and labels](ref:template-annotations-and-labels) From 9f9c248766f8999cdd12613769c5f715dd204a13 Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Fri, 21 Feb 2025 14:10:38 -0500 Subject: [PATCH 05/26] chore(deps): Remove unreferenced goavro dependency (#101171) Signed-off-by: Dave Henderson --- go.mod | 1 - go.sum | 2 -- pkg/extensions/main.go | 1 - 3 files changed, 4 deletions(-) diff --git a/go.mod b/go.mod index 744aa4d529e..2347415b45a 100644 --- a/go.mod +++ b/go.mod @@ -111,7 +111,6 @@ require ( github.com/jmoiron/sqlx v1.3.5 // @grafana/grafana-backend-group github.com/json-iterator/go v1.1.12 // @grafana/grafana-backend-group github.com/lib/pq v1.10.9 // @grafana/grafana-backend-group - github.com/linkedin/goavro/v2 v2.10.0 // @grafana/grafana-backend-group github.com/m3db/prometheus_remote_client_golang v0.4.4 // @grafana/grafana-backend-group github.com/madflojo/testcerts v1.1.1 // @grafana/alerting-backend github.com/magefile/mage v1.15.0 // @grafana/grafana-developer-enablement-squad diff --git a/go.sum b/go.sum index 85c8855cce6..e4bd6172c9a 100644 --- a/go.sum +++ b/go.sum @@ -1845,8 +1845,6 @@ github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= -github.com/linkedin/goavro/v2 v2.10.0 h1:eTBIRoInBM88gITGXYtUSqqxLTFXfOsJBiX8ZMW0o4U= -github.com/linkedin/goavro/v2 v2.10.0/go.mod h1:UgQUb2N/pmueQYH9bfqFioWxzYCZXSfF8Jw03O5sjqA= github.com/linode/linodego v1.43.0 h1:sGeBB3caZt7vKBoPS5p4AVzmlG4JoqQOdigIibx3egk= github.com/linode/linodego v1.43.0/go.mod h1:n4TMFu1UVNala+icHqrTEFFaicYSF74cSAUG5zkTwfA= github.com/lyft/protoc-gen-star v0.6.0/go.mod h1:TGAoBVkt8w7MPG72TrKIu85MIdXwDuzJYeZuUPFPNwA= diff --git a/pkg/extensions/main.go b/pkg/extensions/main.go index 98e324f8e5e..b4aace2cfad 100644 --- a/pkg/extensions/main.go +++ b/pkg/extensions/main.go @@ -19,7 +19,6 @@ import ( _ "github.com/grpc-ecosystem/go-grpc-middleware/v2" _ "github.com/hashicorp/go-multierror" _ "github.com/hashicorp/golang-lru/v2" - _ "github.com/linkedin/goavro/v2" _ "github.com/m3db/prometheus_remote_client_golang/promremote" _ "github.com/phpdave11/gofpdi" _ "github.com/robfig/cron/v3" From c33e908baff25768d86e7ffb7e80a58940730acf Mon Sep 17 00:00:00 2001 From: William Wernert Date: Fri, 21 Feb 2025 16:02:39 -0500 Subject: [PATCH 06/26] Alerting: Update alerting package to include SNS fix (#101177) * Update alerting package to include sns fix * Update workspace --- go.mod | 2 +- go.sum | 4 +-- go.work.sum | 38 ++++++++++++++++++++++++++++- pkg/storage/unified/apistore/go.mod | 2 +- pkg/storage/unified/apistore/go.sum | 4 +-- pkg/storage/unified/resource/go.mod | 2 +- pkg/storage/unified/resource/go.sum | 4 +-- 7 files changed, 46 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 2347415b45a..d512579c149 100644 --- a/go.mod +++ b/go.mod @@ -71,7 +71,7 @@ require ( github.com/googleapis/gax-go/v2 v2.14.1 // @grafana/grafana-backend-group github.com/gorilla/mux v1.8.1 // @grafana/grafana-backend-group github.com/gorilla/websocket v1.5.3 // @grafana/grafana-app-platform-squad - github.com/grafana/alerting v0.0.0-20250220212119-4baca04e46bb // @grafana/alerting-backend + github.com/grafana/alerting v0.0.0-20250221202230-9d7e00921e44 // @grafana/alerting-backend github.com/grafana/authlib v0.0.0-20250219100139-6a3b1bbb50e7 // @grafana/identity-access-team github.com/grafana/authlib/types v0.0.0-20250219092154-21ce22b49f31 // @grafana/identity-access-team github.com/grafana/dataplane/examples v0.0.1 // @grafana/observability-metrics diff --git a/go.sum b/go.sum index e4bd6172c9a..2b409ed61b5 100644 --- a/go.sum +++ b/go.sum @@ -1511,8 +1511,8 @@ github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7Fsg github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/grafana/alerting v0.0.0-20250220212119-4baca04e46bb h1:WfCsiuZXhGXIdzImQ9/Kjfn9M4e6f7z5mddcSKCRxmI= -github.com/grafana/alerting v0.0.0-20250220212119-4baca04e46bb/go.mod h1:hdGB3dSl8Ma9Rjo2YiAEAjMkZ5HiNJbNDqRKDefRZrM= +github.com/grafana/alerting v0.0.0-20250221202230-9d7e00921e44 h1:vboqvbAO0s0CTALHnqfmNvhCP1ziBcZNpYDbORqvOgg= +github.com/grafana/alerting v0.0.0-20250221202230-9d7e00921e44/go.mod h1:hdGB3dSl8Ma9Rjo2YiAEAjMkZ5HiNJbNDqRKDefRZrM= github.com/grafana/authlib v0.0.0-20250219100139-6a3b1bbb50e7 h1:NTMmow+74I3Jb033xhbRgWQS7A//5TDhiM4tl7bsVP4= github.com/grafana/authlib v0.0.0-20250219100139-6a3b1bbb50e7/go.mod h1:T3X4z0ejGfJOiOmZLFeKCRT/yxWJq/RtclAc/PHj/w4= github.com/grafana/authlib/types v0.0.0-20250219092154-21ce22b49f31 h1:EokLC5grHwLPs4tXW8T6E8187H1e5G9AP0QQ5B60HbA= diff --git a/go.work.sum b/go.work.sum index 7dc97c7b7bb..6d37067853e 100644 --- a/go.work.sum +++ b/go.work.sum @@ -392,6 +392,7 @@ github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA= github.com/chenzhuoyu/iasm v0.9.0 h1:9fhXjVzq5hUy2gkhhgHl95zG2cEAhw9OSGs8toWWAwo= github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= +github.com/chromedp/cdproto v0.0.0-20220208224320-6efb837e6bc2/go.mod h1:At5TxYYdxkbQL0TSefRjhLE3Q0lgvqKKMSFUglJ7i1U= github.com/chromedp/sysutil v1.0.0 h1:+ZxhTpfpZlmchB58ih/LBHX52ky7w2VhQVKQMucy3Ic= github.com/chromedp/sysutil v1.0.0/go.mod h1:kgWmDdq8fTzXYcKIBqIYvRRTnYb9aNS9moAV0xufSww= github.com/chzyer/logex v1.2.1 h1:XHDu3E6q+gdHgsdTPH6ImJMIp436vR6MPtH8gP05QzM= @@ -508,7 +509,9 @@ github.com/elastic/go-sysinfo v1.11.2 h1:mcm4OSYVMyws6+n2HIVMGkln5HOpo5Ie1ZmbbNn github.com/elastic/go-sysinfo v1.11.2/go.mod h1:GKqR8bbMK/1ITnez9NIsIfXQr25aLhRJa7AfT8HpBFQ= github.com/elastic/go-windows v1.0.1 h1:AlYZOldA+UJ0/2nBuqWdo90GFCgG9xuyw9SYzGUtJm0= github.com/elastic/go-windows v1.0.1/go.mod h1:FoVvqWSun28vaDQPbj2Elfc0JahhPB7WQEGa3c814Ss= +github.com/elazarl/goproxy v1.3.0/go.mod h1:X/5W/t+gzDyLfHW4DrMdpjqYjpXsURlBt9lpBDxZZZQ= github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633 h1:H2pdYOb3KQ1/YsqVWoWNLQO+fusocsw354rqGTZtAgw= +github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo= github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= @@ -546,10 +549,12 @@ github.com/go-kit/kit v0.13.0 h1:OoneCcHKHQ03LfBpoQCUfCluwd2Vt3ohz+kvbJneZAU= github.com/go-kit/kit v0.13.0/go.mod h1:phqEHMMUbyrCFCTgH48JueqrM3md2HcAZ8N3XE4FKDg= github.com/go-latex/latex v0.0.0-20231108140139-5c1ce85aa4ea h1:DfZQkvEbdmOe+JK2TMtBM+0I9GSdzE2y/L1/AmD8xKc= github.com/go-latex/latex v0.0.0-20231108140139-5c1ce85aa4ea/go.mod h1:Y7Vld91/HRbTBm7JwoI7HejdDB0u+e9AUBO9MB7yuZk= +github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/go-openapi/jsonpointer v0.19.6/go.mod h1:osyAmYz/mB/C3I+WsTTSgw1ONzaLJoLCyoi6/zppojs= +github.com/go-openapi/jsonreference v0.20.1/go.mod h1:Bl1zwGIM8/wsvqjsOQLJ/SH+En5Ap4rVB5KVcIDZG2k= github.com/go-openapi/jsonreference v0.20.2/go.mod h1:Bl1zwGIM8/wsvqjsOQLJ/SH+En5Ap4rVB5KVcIDZG2k= github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14= github.com/go-pdf/fpdf v0.9.0 h1:PPvSaUuo1iMi9KkaAn90NuKi+P4gwMedWPHhj8YlJQw= @@ -561,6 +566,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= github.com/go-playground/validator/v10 v10.19.0 h1:ol+5Fu+cSq9JD7SoSqe04GMI92cbn0+wvQ3bZ8b/AU4= github.com/go-playground/validator/v10 v10.19.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= +github.com/go-sourcemap/sourcemap v2.1.3+incompatible h1:W1iEw64niKVGogNgBN3ePyLFfuisuzeidWPMPWmECqU= +github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls= github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 h1:TQcrn6Wq+sKGkpyPvppOz99zsMBaUOKXq6HSv655U1c= github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= @@ -579,7 +586,6 @@ github.com/golang-jwt/jwt v3.2.1+incompatible h1:73Z+4BJcrTC+KczS6WvTPvRGOp1WmfE github.com/golang-jwt/jwt v3.2.1+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= github.com/golang/glog v1.2.3/go.mod h1:6AhwSGph0fcJtXVM/PEHPqZlFeoLxhs7/t5UDAwmO+w= -github.com/golang/glog v1.2.4/go.mod h1:6AhwSGph0fcJtXVM/PEHPqZlFeoLxhs7/t5UDAwmO+w= github.com/gomarkdown/markdown v0.0.0-20230716120725-531d2d74bc12 h1:uK3X/2mt4tbSGoHvbLBHUny7CKiuwUip3MArtukol4E= github.com/gomarkdown/markdown v0.0.0-20230716120725-531d2d74bc12/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA= github.com/gomodule/redigo v1.8.9 h1:Sl3u+2BI/kk+VEatbj0scLdrFhjPmbxOc1myhDP41ws= @@ -609,17 +615,29 @@ github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY= github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c= github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4= github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q= +github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/grafana/alerting v0.0.0-20250129195454-3e5b80036b7a/go.mod h1:QsnoKX/iYZxA4Cv+H+wC7uxutBD8qi8ZW5UJvD2TYmU= +github.com/grafana/authlib v0.0.0-20250123104008-e99947858901/go.mod h1:/gYfphsNu9v1qYWXxpv1NSvMEMSwvdf8qb8YlgwIRl8= github.com/grafana/authlib/types v0.0.0-20250120144156-d6737a7dc8f5/go.mod h1:qYjSd1tmJiuVoSICp7Py9/zD54O9uQQA3wuM6Gg4DFM= +github.com/grafana/authlib/types v0.0.0-20250120145936-5f0e28e7a87c/go.mod h1:qYjSd1tmJiuVoSICp7Py9/zD54O9uQQA3wuM6Gg4DFM= github.com/grafana/cloudflare-go v0.0.0-20230110200409-c627cf6792f2 h1:qhugDMdQ4Vp68H0tp/0iN17DM2ehRo1rLEdOFe/gB8I= github.com/grafana/cloudflare-go v0.0.0-20230110200409-c627cf6792f2/go.mod h1:w/aiO1POVIeXUQyl0VQSZjl5OAGDTL5aX+4v0RA1tcw= github.com/grafana/cog v0.0.23 h1:/0CCJ24Z8XXM2DnboSd2FzoIswUroqIZzVr8oJWmMQs= github.com/grafana/cog v0.0.23/go.mod h1:jrS9indvWuDs60RHEZpLaAkmZdgyoLKMOEUT0jiB1t0= github.com/grafana/go-gelf/v2 v2.0.1 h1:BOChP0h/jLeD+7F9mL7tq10xVkDG15he3T1zHuQaWak= github.com/grafana/go-gelf/v2 v2.0.1/go.mod h1:lexHie0xzYGwCgiRGcvZ723bSNyNI8ZRD4s0CLobh90= +github.com/grafana/grafana-plugin-sdk-go v0.263.0/go.mod h1:U43Cnrj/9DNYyvFcNdeUWNjMXTKNB0jcTcQGpWKd2gw= +github.com/grafana/grafana/apps/advisor v0.0.0-20250123151950-b066a6313173/go.mod h1:goSDiy3jtC2cp8wjpPZdUHRENcoSUHae1/Px/MDfddA= github.com/grafana/grafana/apps/advisor v0.0.0-20250220154326-6e5de80ef295/go.mod h1:9I1dKV3Dqr0NPR9Af0WJGxOytp5/6W3JLiNChOz8r+c= +github.com/grafana/grafana/apps/alerting/notifications v0.0.0-20250121113133-e747350fee2d/go.mod h1:AvleS6icyPmcBjihtx5jYEvdzLmHGBp66NuE0AMR57A= +github.com/grafana/grafana/apps/investigation v0.0.0-20250121113133-e747350fee2d/go.mod h1:HQprw3MmiYj5OUV9CZnkwA1FKDZBmYACuAB3oDvUOmI= +github.com/grafana/grafana/apps/playlist v0.0.0-20250121113133-e747350fee2d/go.mod h1:DjJe5osrW/BKrzN9hAAOSElNWutj1bcriExa7iDP7kA= +github.com/grafana/grafana/pkg/aggregator v0.0.0-20250121113133-e747350fee2d/go.mod h1:1sq0guad+G4SUTlBgx7SXfhnzy7D86K/LcVOtiQCiMA= github.com/grafana/grafana/pkg/build v0.0.0-20250220114259-be81314e2118/go.mod h1:STVpVboMYeBAfyn6Zw6XHhTHqUxzMy7pzRiVgk1l0W0= +github.com/grafana/grafana/pkg/semconv v0.0.0-20250121113133-e747350fee2d/go.mod h1:tfLnBpPYgwrBMRz4EXqPCZJyCjEG4Ev37FSlXnocJ2c= +github.com/grafana/grafana/pkg/storage/unified/apistore v0.0.0-20250121113133-e747350fee2d/go.mod h1:CXpwZ3Mkw6xVlGKc0SqUxqXCP3Uv182q6qAQnLaLxRg= github.com/grafana/tail v0.0.0-20230510142333-77b18831edf0 h1:bjh0PVYSVVFxzINqPFYJmAmJNrWPgnVjuSdYJGHmtFU= github.com/grafana/tail v0.0.0-20230510142333-77b18831edf0/go.mod h1:7t5XR+2IA8P2qggOAHTj/GCZfoLBle3OvNSYh1VkRBU= github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 h1:+ngKgrYPPJrOjhax5N+uePQ0Fh1Z7PheYoUI/0nzkPA= @@ -630,8 +648,10 @@ github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645 h1 github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed h1:5upAirOpQc1Q53c0bnx2ufif5kANL7bfZWcc6VJWJd8= github.com/hamba/avro/v2 v2.27.0 h1:IAM4lQ0VzUIKBuo4qlAiLKfqALSrFC+zi1iseTtbBKU= github.com/hamba/avro/v2 v2.27.0/go.mod h1:jN209lopfllfrz7IGoZErlDz+AyUJ3vrBePQFZwYf5I= +github.com/hashicorp/go-hclog v0.14.1/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ= github.com/hashicorp/go-msgpack/v2 v2.1.1 h1:xQEY9yB2wnHitoSzk/B9UjXWRQ67QKu5AOm8aFp8N3I= github.com/hashicorp/go-msgpack/v2 v2.1.1/go.mod h1:upybraOAblm4S7rx0+jeNy+CWWhzywQsSRV5033mMu4= +github.com/hashicorp/go-plugin v1.6.2/go.mod h1:CkgLQ5CZqNmdL9U9JzM532t8ZiYQ35+pj3b1FD37R0Q= github.com/hashicorp/go-syslog v1.0.0 h1:KaodqZuhUoZereWVIYmpUgZysurB1kBLX2j0MwMrUAE= github.com/hashicorp/go.net v0.0.1 h1:sNCoNyDEvN1xa+X0baata4RdcpKwcMS6DH+xwfqPgjw= github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y= @@ -654,6 +674,7 @@ github.com/influxdata/tdigest v0.0.2-0.20210216194612-fc98d27c9e8b h1:i44CesU68Z github.com/influxdata/tdigest v0.0.2-0.20210216194612-fc98d27c9e8b/go.mod h1:Z0kXnxzbTC2qrx4NaIzYkE1k66+6oEDQTvL95hQFh5Y= github.com/influxdata/telegraf v1.16.3 h1:x0qeuSGGMg5y+YqP/5ZHwXZu3bcBrO8AAQOTNlYEb1c= github.com/influxdata/telegraf v1.16.3/go.mod h1:fX/6k7qpIqzVPWyeIamb0wN5hbwc0ANUaTS80lPYFB8= +github.com/invopop/jsonschema v0.12.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0= github.com/iris-contrib/schema v0.0.6 h1:CPSBLyx2e91H2yJzPuhGuifVRnZBBJ3pCOMbOvPZaTw= github.com/iris-contrib/schema v0.0.6/go.mod h1:iYszG0IOsuIsfzjymw1kMzTL8YQcCWlm65f3wX8J5iA= github.com/jackc/fake v0.0.0-20150926172116-812a484cc733 h1:vr3AYkKovP8uR8AvSGGUK1IDqRa5lAAvEkZG1LKaCRc= @@ -726,6 +747,7 @@ github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= github.com/leodido/ragel-machinery v0.0.0-20190525184631-5f46317e436b h1:11UHH39z1RhZ5dc4y4r/4koJo6IYFgTRMe/LlwRTEw0= github.com/leodido/ragel-machinery v0.0.0-20190525184631-5f46317e436b/go.mod h1:WZxr2/6a/Ar9bMDc2rN/LJrE/hF6bXE4LPyDSIxwAfg= +github.com/linkedin/goavro/v2 v2.10.0/go.mod h1:UgQUb2N/pmueQYH9bfqFioWxzYCZXSfF8Jw03O5sjqA= github.com/logrusorgru/aurora/v3 v3.0.0 h1:R6zcoZZbvVcGMvDCKo45A9U/lzYyzl5NfYIvznmDfE4= github.com/logrusorgru/aurora/v3 v3.0.0/go.mod h1:vsR12bk5grlLvLXAYrBsb5Oc/N+LxAlxggSjiwMnCUc= github.com/lufia/plan9stats v0.0.0-20220913051719-115f729f3c8c h1:VtwQ41oftZwlMnOEbMWQtSEUgU64U4s+GHk7hZK+jtY= @@ -737,6 +759,8 @@ github.com/mailgun/raymond/v2 v2.0.48 h1:5dmlB680ZkFG2RN/0lvTAghrSxIESeu9/2aeDqA github.com/mailgun/raymond/v2 v2.0.48/go.mod h1:lsgvL50kgt1ylcFJYZiULi5fjPBkkhNfj4KA0W54Z18= github.com/matryer/moq v0.3.3 h1:pScMH9VyrdT4S93yiLpVyU8rCDqGQr24uOyBxmktG5Q= github.com/matryer/moq v0.3.3/go.mod h1:RJ75ZZZD71hejp39j4crZLsEDszGk6iH4v4YsWFKH4s= +github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg= github.com/mfridman/xflag v0.1.0 h1:TWZrZwG1QklFX5S4j1vxfF1sZbZeZSGofMwPMLAF29M= github.com/mfridman/xflag v0.1.0/go.mod h1:/483ywM5ZO5SuMVjrIGquYNE5CzLrj5Ux/LxWWnjRaE= @@ -883,6 +907,7 @@ github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad h1:fiWzISvDn0Csy5H0iwgAuJ github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8= github.com/stoewer/parquet-cli v0.0.7 h1:rhdZODIbyMS3twr4OM3am8BPPT5pbfMcHLH93whDM5o= github.com/stoewer/parquet-cli v0.0.7/go.mod h1:bskxHdj8q3H1EmfuCqjViFoeO3NEvs5lzZAQvI8Nfjk= +github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/substrait-io/substrait v0.57.1 h1:GW8nnYfSowMseHR8Os82/X6lNtQGIK7p4p+lr6r+auw= github.com/substrait-io/substrait v0.57.1/go.mod h1:q9s+tjo+gK0lsA+SqYB0lhojNuxvdPdfYlGUP0hjbrA= github.com/substrait-io/substrait-go v1.2.0 h1:3ZNRkc8FYD7ifCagKEOZQtUcgMceMQfwo2N1NGaK4Q4= @@ -1035,9 +1060,14 @@ go.opentelemetry.io/contrib/detectors/gcp v1.32.0/go.mod h1:TVqo0Sda4Cv8gCIixd7L go.opentelemetry.io/contrib/exporters/autoexport v0.53.0 h1:13K+tY7E8GJInkrvRiPAhC0gi/7vKjzDNhtmCf+QXG8= go.opentelemetry.io/contrib/exporters/autoexport v0.53.0/go.mod h1:lyQF6xQ4iDnMg4sccNdFs1zf62xd79YI8vZqKjOTwMs= go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.53.0/go.mod h1:azvtTADFQJA8mX80jIH/akaE7h+dbm/sVuaHqN13w74= +go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.58.0/go.mod h1:HDBUsEjOuRC0EzKZ1bSaRGZWUBAzo+MhAcUUORSr4D0= +go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.58.0/go.mod h1:uosvgpqTcTXtcPQORTbEkZNDQTCDOgTz1fe6aLSyqrQ= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0/go.mod h1:jjdQuTGVsXV4vSs+CJ2qYDeDPf9yIJV23qlIzBm73Vg= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.58.0/go.mod h1:umTcuxiv1n/s/S6/c2AT/g2CQ7u5C59sHDNmfSwgz7Q= go.opentelemetry.io/contrib/propagators/b3 v1.27.0 h1:IjgxbomVrV9za6bRi8fWCNXENs0co37SZedQilP2hm0= go.opentelemetry.io/contrib/propagators/b3 v1.27.0/go.mod h1:Dv9obQz25lCisDvvs4dy28UPh974CxkahRDUPsY7y9E= +go.opentelemetry.io/contrib/propagators/jaeger v1.33.0/go.mod h1:ku/EpGk44S5lyVMbtJRK2KFOnXEehxf6SDnhu1eZmjA= +go.opentelemetry.io/contrib/samplers/jaegerremote v0.27.0/go.mod h1:IohbtCIY5Erb6wKnDddXOMNlG7GwyZnkrgcqjPmhpaA= go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo= go.opentelemetry.io/otel v1.28.0/go.mod h1:q68ijF8Fc8CnMHKyzqL6akLO46ePnjkgfIMIjUIX9z4= go.opentelemetry.io/otel v1.32.0/go.mod h1:00DCVSB0RQcnzlwyTfqtxSm+DRr9hpYrHjNGiBHVQIg= @@ -1131,6 +1161,8 @@ golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxb golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c= golang.org/x/tools v0.25.0/go.mod h1:/vtpO8WL1N9cQC3FN5zPqb//fRXskFHbLKk4OW1Q7rg= golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0= +golang.org/x/tools v0.28.0/go.mod h1:dcIOrVd3mfQKTgrDVQHqCPMWy6lnhfhtX3hLXYVLfRw= +golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90= gonum.org/v1/netlib v0.0.0-20190313105609-8cb42192e0e0 h1:OE9mWmgKkjJyEmDAAtGMPjXu+YNeGvK9VTSHY6+Qihc= gonum.org/v1/plot v0.14.0 h1:+LBDVFYwFe4LHhdP8coW6296MBEY4nQ+Y4vuUpJopcE= gonum.org/v1/plot v0.14.0/go.mod h1:MLdR9424SJed+5VqC6MsouEpig9pZX2VZ57H9ko2bXU= @@ -1144,12 +1176,14 @@ google.golang.org/genproto/googleapis/api v0.0.0-20241202173237-19429a94021a/go. google.golang.org/genproto/googleapis/api v0.0.0-20241219192143-6b3ec007d9bb/go.mod h1:E5//3O5ZIG2l71Xnt+P/CYUY8Bxs8E7WMoZ9tlcMbAY= google.golang.org/genproto/googleapis/bytestream v0.0.0-20250102185135-69823020774d h1:NZBSeFsuFS5YrgHMW/8xfTbzNXMshQPNgq2Yb7xipEs= google.golang.org/genproto/googleapis/bytestream v0.0.0-20250102185135-69823020774d/go.mod h1:s4mHJ3FfG8P6A3O+gZ8TVqB3ufjOl9UG3ANCMMwCHmo= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98/go.mod h1:TUfxEVdsvPg18p6AslUXFoLdpED4oBnGwyqk3dV1XzM= google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY= google.golang.org/genproto/googleapis/rpc v0.0.0-20240826202546-f6391c0de4c7/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU= google.golang.org/genproto/googleapis/rpc v0.0.0-20241015192408-796eee8c2d53/go.mod h1:GX3210XPVPUjJbTUbvwI8f2IpZDMZuPJWDzDuebbviI= google.golang.org/genproto/googleapis/rpc v0.0.0-20241202173237-19429a94021a/go.mod h1:5uTbfoYQed2U9p3KIj2/Zzm02PYhndfdmML0qC3q3FU= google.golang.org/genproto/googleapis/rpc v0.0.0-20241219192143-6b3ec007d9bb/go.mod h1:lcTa1sDdWEIHMWlITnIczmw5w60CF9ffkb8Z+DVmmjA= google.golang.org/genproto/googleapis/rpc v0.0.0-20250106144421-5f5ef82da422/go.mod h1:3ENsm/5D1mzDyhpzeRi1NR784I0BcofWBoSc5QqqMK4= +google.golang.org/grpc v1.58.3/go.mod h1:tgX3ZQDlNJGU96V6yHh1T/JeoBQ2TXdr43YbYSsCJk0= google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ= google.golang.org/grpc v1.67.1/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA= google.golang.org/grpc v1.69.2/go.mod h1:vyjdE6jLBI76dgpDojsFGNaHlxdjXN9ghpnd2o7JGZ4= @@ -1180,6 +1214,8 @@ k8s.io/gengo/v2 v2.0.0-20240911193312-2b36238f13e9 h1:si3PfKm8dDYxgfbeA6orqrtLkv k8s.io/gengo/v2 v2.0.0-20240911193312-2b36238f13e9/go.mod h1:EJykeLsmFC60UQbYJezXkEsG2FLrt0GPNkU5iK5GWxU= k8s.io/klog v1.0.0 h1:Pt+yjF5aB1xDSVbau4VsWe+dQNzA0qv1LlXdC2dF6Q8= k8s.io/klog v1.0.0/go.mod h1:4Bi6QPql/J/LkTDqv7R/cd3hPo4k2DG6Ptcz060Ez5I= +k8s.io/klog/v2 v2.80.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0= +k8s.io/utils v0.0.0-20240711033017-18e509b52bc8/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= lukechampine.com/uint128 v1.2.0 h1:mBi/5l91vocEN8otkC5bDLhi2KdCticRiwbdB0O+rjI= modernc.org/cc/v3 v3.36.3 h1:uISP3F66UlixxWEcKuIWERa4TwrZENHSL8tWxZz8bHg= modernc.org/ccgo/v3 v3.16.9 h1:AXquSwg7GuMk11pIdw7fmO1Y/ybgazVkMhsZWCV0mHM= diff --git a/pkg/storage/unified/apistore/go.mod b/pkg/storage/unified/apistore/go.mod index 16380887a73..b7abdb3bb91 100644 --- a/pkg/storage/unified/apistore/go.mod +++ b/pkg/storage/unified/apistore/go.mod @@ -192,7 +192,7 @@ require ( github.com/googleapis/gax-go/v2 v2.14.1 // indirect github.com/gorilla/mux v1.8.1 // indirect github.com/gorilla/websocket v1.5.3 // indirect - github.com/grafana/alerting v0.0.0-20250220212119-4baca04e46bb // indirect + github.com/grafana/alerting v0.0.0-20250221202230-9d7e00921e44 // indirect github.com/grafana/authlib v0.0.0-20250219100139-6a3b1bbb50e7 // indirect github.com/grafana/dataplane/sdata v0.0.9 // indirect github.com/grafana/dskit v0.0.0-20241105154643-a6b453a88040 // indirect diff --git a/pkg/storage/unified/apistore/go.sum b/pkg/storage/unified/apistore/go.sum index 40c89cf0360..87e7727af96 100644 --- a/pkg/storage/unified/apistore/go.sum +++ b/pkg/storage/unified/apistore/go.sum @@ -566,8 +566,8 @@ github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/grafana/alerting v0.0.0-20250220212119-4baca04e46bb h1:WfCsiuZXhGXIdzImQ9/Kjfn9M4e6f7z5mddcSKCRxmI= -github.com/grafana/alerting v0.0.0-20250220212119-4baca04e46bb/go.mod h1:hdGB3dSl8Ma9Rjo2YiAEAjMkZ5HiNJbNDqRKDefRZrM= +github.com/grafana/alerting v0.0.0-20250221202230-9d7e00921e44 h1:vboqvbAO0s0CTALHnqfmNvhCP1ziBcZNpYDbORqvOgg= +github.com/grafana/alerting v0.0.0-20250221202230-9d7e00921e44/go.mod h1:hdGB3dSl8Ma9Rjo2YiAEAjMkZ5HiNJbNDqRKDefRZrM= github.com/grafana/authlib v0.0.0-20250219100139-6a3b1bbb50e7 h1:NTMmow+74I3Jb033xhbRgWQS7A//5TDhiM4tl7bsVP4= github.com/grafana/authlib v0.0.0-20250219100139-6a3b1bbb50e7/go.mod h1:T3X4z0ejGfJOiOmZLFeKCRT/yxWJq/RtclAc/PHj/w4= github.com/grafana/authlib/types v0.0.0-20250219092154-21ce22b49f31 h1:EokLC5grHwLPs4tXW8T6E8187H1e5G9AP0QQ5B60HbA= diff --git a/pkg/storage/unified/resource/go.mod b/pkg/storage/unified/resource/go.mod index f2ab921a4f6..833dd96a890 100644 --- a/pkg/storage/unified/resource/go.mod +++ b/pkg/storage/unified/resource/go.mod @@ -117,7 +117,7 @@ require ( github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect github.com/googleapis/gax-go/v2 v2.14.1 // indirect github.com/gorilla/mux v1.8.1 // indirect - github.com/grafana/alerting v0.0.0-20250220212119-4baca04e46bb // indirect + github.com/grafana/alerting v0.0.0-20250221202230-9d7e00921e44 // indirect github.com/grafana/dataplane/sdata v0.0.9 // indirect github.com/grafana/grafana-app-sdk/logging v0.30.0 // indirect github.com/grafana/grafana-aws-sdk v0.31.5 // indirect diff --git a/pkg/storage/unified/resource/go.sum b/pkg/storage/unified/resource/go.sum index 40fcf6f458a..7d566e2e658 100644 --- a/pkg/storage/unified/resource/go.sum +++ b/pkg/storage/unified/resource/go.sum @@ -397,8 +397,8 @@ github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2z github.com/gorilla/mux v1.7.1/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= -github.com/grafana/alerting v0.0.0-20250220212119-4baca04e46bb h1:WfCsiuZXhGXIdzImQ9/Kjfn9M4e6f7z5mddcSKCRxmI= -github.com/grafana/alerting v0.0.0-20250220212119-4baca04e46bb/go.mod h1:hdGB3dSl8Ma9Rjo2YiAEAjMkZ5HiNJbNDqRKDefRZrM= +github.com/grafana/alerting v0.0.0-20250221202230-9d7e00921e44 h1:vboqvbAO0s0CTALHnqfmNvhCP1ziBcZNpYDbORqvOgg= +github.com/grafana/alerting v0.0.0-20250221202230-9d7e00921e44/go.mod h1:hdGB3dSl8Ma9Rjo2YiAEAjMkZ5HiNJbNDqRKDefRZrM= github.com/grafana/authlib v0.0.0-20250219100139-6a3b1bbb50e7 h1:NTMmow+74I3Jb033xhbRgWQS7A//5TDhiM4tl7bsVP4= github.com/grafana/authlib v0.0.0-20250219100139-6a3b1bbb50e7/go.mod h1:T3X4z0ejGfJOiOmZLFeKCRT/yxWJq/RtclAc/PHj/w4= github.com/grafana/authlib/types v0.0.0-20250219092154-21ce22b49f31 h1:EokLC5grHwLPs4tXW8T6E8187H1e5G9AP0QQ5B60HbA= From bbeae4610502bc2b6375eac60c354b915637f6a7 Mon Sep 17 00:00:00 2001 From: Yuri Tseretyan Date: Fri, 21 Feb 2025 16:08:40 -0500 Subject: [PATCH 07/26] Alerting: Fix rule state history with annotations backend (#101174) * add alertUID to annotations API query parameter * update state history UI to fetch rule by UID --------- Signed-off-by: Yuri Tseretyan --- pkg/api/annotations.go | 8 ++- .../annotationsimpl/loki/historian_store.go | 12 +++-- .../loki/historian_store_test.go | 50 ++++++++++++++++++- .../annotations/annotationsimpl/xorm_store.go | 7 ++- pkg/services/annotations/models.go | 1 + public/api-merged.json | 8 ++- .../alerting/unified/api/annotations.test.ts | 2 +- .../alerting/unified/api/annotations.ts | 4 +- .../components/rule-viewer/tabs/History.tsx | 3 +- .../rules/state-history/StateHistory.tsx | 6 +-- .../hooks/useManagedAlertStateHistory.ts | 6 +-- .../unified/hooks/useStateHistoryModal.tsx | 2 +- .../alerting/unified/state/actions.ts | 2 +- public/openapi3.json | 10 +++- 14 files changed, 97 insertions(+), 24 deletions(-) diff --git a/pkg/api/annotations.go b/pkg/api/annotations.go index 5612fd4a639..4720e25e91f 100644 --- a/pkg/api/annotations.go +++ b/pkg/api/annotations.go @@ -41,6 +41,7 @@ func (hs *HTTPServer) GetAnnotations(c *contextmodel.ReqContext) response.Respon OrgID: c.SignedInUser.GetOrgID(), UserID: c.QueryInt64("userId"), AlertID: c.QueryInt64("alertId"), + AlertUID: c.Query("alertUID"), DashboardID: c.QueryInt64("dashboardId"), DashboardUID: c.Query("dashboardUID"), PanelID: c.QueryInt64("panelId"), @@ -744,10 +745,15 @@ type GetAnnotationsParams struct { // in:query // required:false UserID int64 `json:"userId"` - // Find annotations for a specified alert. + // Find annotations for a specified alert rule by its ID. + // deprecated: AlertID is deprecated and will be removed in future versions. Please use AlertUID instead. // in:query // required:false AlertID int64 `json:"alertId"` + // Find annotations for a specified alert rule by its UID. + // in:query + // required:false + AlertUID string `json:"alertUID"` // Find annotations that are scoped to a specific dashboard // in:query // required:false diff --git a/pkg/services/annotations/annotationsimpl/loki/historian_store.go b/pkg/services/annotations/annotationsimpl/loki/historian_store.go index 563d4435be7..5bc4e02d303 100644 --- a/pkg/services/annotations/annotationsimpl/loki/historian_store.go +++ b/pkg/services/annotations/annotationsimpl/loki/historian_store.go @@ -91,20 +91,22 @@ func (r *LokiHistorianStore) Get(ctx context.Context, query annotations.ItemQuer return make([]*annotations.ItemDTO, 0), nil } - rule := &ngmodels.AlertRule{} - if query.AlertID != 0 { - var err error - rule, err = r.ruleStore.GetRuleByID(ctx, ngmodels.GetAlertRuleByIDQuery{OrgID: query.OrgID, ID: query.AlertID}) + var ruleUID string + if query.AlertUID != "" { + ruleUID = query.AlertUID + } else if query.AlertID != 0 { + rule, err := r.ruleStore.GetRuleByID(ctx, ngmodels.GetAlertRuleByIDQuery{OrgID: query.OrgID, ID: query.AlertID}) if err != nil { if errors.Is(err, ngmodels.ErrAlertRuleNotFound) { return make([]*annotations.ItemDTO, 0), ErrLokiStoreNotFound.Errorf("rule with ID %d does not exist", query.AlertID) } return make([]*annotations.ItemDTO, 0), ErrLokiStoreInternal.Errorf("failed to query rule: %w", err) } + ruleUID = rule.UID } // No folders in the filter because it filter by Dashboard UID, and the request is already authorized. - logQL, err := historian.BuildLogQuery(buildHistoryQuery(&query, accessResources.Dashboards, rule.UID), nil, r.client.MaxQuerySize()) + logQL, err := historian.BuildLogQuery(buildHistoryQuery(&query, accessResources.Dashboards, ruleUID), nil, r.client.MaxQuerySize()) if err != nil { grafanaErr := errutil.Error{} if errors.As(err, &grafanaErr) { diff --git a/pkg/services/annotations/annotationsimpl/loki/historian_store_test.go b/pkg/services/annotations/annotationsimpl/loki/historian_store_test.go index f4e4bf6630a..67ebca32918 100644 --- a/pkg/services/annotations/annotationsimpl/loki/historian_store_test.go +++ b/pkg/services/annotations/annotationsimpl/loki/historian_store_test.go @@ -108,7 +108,34 @@ func TestIntegrationAlertStateHistoryStore(t *testing.T) { require.Len(t, res, numTransitions) }) - t.Run("should return ErrLokiStoreNotFound if rule is not found", func(t *testing.T) { + t.Run("can query history by alert uid", func(t *testing.T) { + rule := dashboardRules[dashboard1.UID][0] + + fakeLokiClient.rangeQueryRes = []historian.Stream{ + historian.StatesToStream(ruleMetaFromRule(t, rule), transitions, map[string]string{}, log.NewNopLogger()), + } + + query := annotations.ItemQuery{ + OrgID: 1, + AlertUID: rule.UID, + From: start.UnixMilli(), + To: start.Add(time.Second * time.Duration(numTransitions+1)).UnixMilli(), + } + res, err := store.Get( + context.Background(), + query, + &annotation_ac.AccessResources{ + Dashboards: map[string]int64{ + dashboard1.UID: dashboard1.ID, + }, + CanAccessDashAnnotations: true, + }, + ) + require.NoError(t, err) + require.Len(t, res, numTransitions) + }) + + t.Run("should return ErrLokiStoreNotFound if rule is not found by ID", func(t *testing.T) { var rules = slices.Concat(maps.Values(dashboardRules)...) id := rand.Int63n(1000) // in Postgres ID is integer, so limit range // make sure id is not known @@ -137,6 +164,27 @@ func TestIntegrationAlertStateHistoryStore(t *testing.T) { require.ErrorIs(t, err, ErrLokiStoreNotFound) }) + t.Run("should return empty response if rule is not found by UID", func(t *testing.T) { + query := annotations.ItemQuery{ + OrgID: 1, + AlertUID: "not-found-uid", + From: start.UnixMilli(), + To: start.Add(time.Second * time.Duration(numTransitions+1)).UnixMilli(), + } + res, err := store.Get( + context.Background(), + query, + &annotation_ac.AccessResources{ + Dashboards: map[string]int64{ + dashboard1.UID: dashboard1.ID, + }, + CanAccessDashAnnotations: true, + }, + ) + require.NoError(t, err) + require.Empty(t, res) + }) + t.Run("can query history by dashboard id", func(t *testing.T) { fakeLokiClient.rangeQueryRes = []historian.Stream{ historian.StatesToStream(ruleMetaFromRule(t, dashboardRules[dashboard1.UID][0]), transitions, map[string]string{}, log.NewNopLogger()), diff --git a/pkg/services/annotations/annotationsimpl/xorm_store.go b/pkg/services/annotations/annotationsimpl/xorm_store.go index 603ec3865e8..7b957189312 100644 --- a/pkg/services/annotations/annotationsimpl/xorm_store.go +++ b/pkg/services/annotations/annotationsimpl/xorm_store.go @@ -267,10 +267,10 @@ func (r *xormRepositoryImpl) Get(ctx context.Context, query annotations.ItemQuer annotation.updated, usr.email, usr.login, - alert.name as alert_name + r.title as alert_name FROM annotation LEFT OUTER JOIN ` + r.db.GetDialect().Quote("user") + ` as usr on usr.id = annotation.user_id - LEFT OUTER JOIN alert on alert.id = annotation.alert_id + LEFT OUTER JOIN alert_rule as r on r.id = annotation.alert_id INNER JOIN ( SELECT a.id from annotation a `) @@ -287,6 +287,9 @@ func (r *xormRepositoryImpl) Get(ctx context.Context, query annotations.ItemQuer if query.AlertID != 0 { sql.WriteString(` AND a.alert_id = ?`) params = append(params, query.AlertID) + } else if query.AlertUID != "" { + sql.WriteString(` AND a.alert_id = (SELECT id FROM alert_rule WHERE uid = ? and org_id = ?)`) + params = append(params, query.AlertUID, query.OrgID) } if query.DashboardID != 0 { diff --git a/pkg/services/annotations/models.go b/pkg/services/annotations/models.go index c9f22140d6f..9dcd23755ea 100644 --- a/pkg/services/annotations/models.go +++ b/pkg/services/annotations/models.go @@ -11,6 +11,7 @@ type ItemQuery struct { To int64 `json:"to"` UserID int64 `json:"userId"` AlertID int64 `json:"alertId"` + AlertUID string `json:"alertUID"` DashboardID int64 `json:"dashboardId"` DashboardUID string `json:"dashboardUID"` PanelID int64 `json:"panelId"` diff --git a/public/api-merged.json b/public/api-merged.json index 04ffb5ef505..09bd1962ae9 100644 --- a/public/api-merged.json +++ b/public/api-merged.json @@ -1833,10 +1833,16 @@ { "type": "integer", "format": "int64", - "description": "Find annotations for a specified alert.", + "description": "Find annotations for a specified alert rule by its ID.\ndeprecated: AlertID is deprecated and will be removed in future versions. Please use AlertUID instead.", "name": "alertId", "in": "query" }, + { + "type": "string", + "description": "Find annotations for a specified alert rule by its UID.", + "name": "alertUID", + "in": "query" + }, { "type": "integer", "format": "int64", diff --git a/public/app/features/alerting/unified/api/annotations.test.ts b/public/app/features/alerting/unified/api/annotations.test.ts index 01da8c9da97..f80d7e8f22c 100644 --- a/public/app/features/alerting/unified/api/annotations.test.ts +++ b/public/app/features/alerting/unified/api/annotations.test.ts @@ -21,7 +21,7 @@ describe('annotations', () => { it('should fetch annotation for an alertId', () => { const ALERT_ID = 'abc123'; fetchAnnotations(ALERT_ID); - expect(get).toBeCalledWith('/api/annotations', { alertId: ALERT_ID }); + expect(get).toBeCalledWith('/api/annotations', { alertUID: ALERT_ID }); }); }); diff --git a/public/app/features/alerting/unified/api/annotations.ts b/public/app/features/alerting/unified/api/annotations.ts index 39195dc1f48..2e0d577ffbf 100644 --- a/public/app/features/alerting/unified/api/annotations.ts +++ b/public/app/features/alerting/unified/api/annotations.ts @@ -1,10 +1,10 @@ import { getBackendSrv } from '@grafana/runtime'; import { StateHistoryItem } from 'app/types/unified-alerting'; -export function fetchAnnotations(alertId: string): Promise { +export function fetchAnnotations(alertUID: string): Promise { return getBackendSrv() .get('/api/annotations', { - alertId, + alertUID, }) .then((result) => { return result?.sort(sortStateHistory); diff --git a/public/app/features/alerting/unified/components/rule-viewer/tabs/History.tsx b/public/app/features/alerting/unified/components/rule-viewer/tabs/History.tsx index bed499ce2a2..b946c0a65f7 100644 --- a/public/app/features/alerting/unified/components/rule-viewer/tabs/History.tsx +++ b/public/app/features/alerting/unified/components/rule-viewer/tabs/History.tsx @@ -26,13 +26,12 @@ const History = ({ rule }: HistoryProps) => { ? StateHistoryImplementation.Loki : StateHistoryImplementation.Annotations; - const ruleID = rule.grafana_alert.id ?? ''; const ruleUID = rule.grafana_alert.uid; return ( {implementation === StateHistoryImplementation.Loki && } - {implementation === StateHistoryImplementation.Annotations && } + {implementation === StateHistoryImplementation.Annotations && } ); }; diff --git a/public/app/features/alerting/unified/components/rules/state-history/StateHistory.tsx b/public/app/features/alerting/unified/components/rules/state-history/StateHistory.tsx index a0388b41464..128dbaf1087 100644 --- a/public/app/features/alerting/unified/components/rules/state-history/StateHistory.tsx +++ b/public/app/features/alerting/unified/components/rules/state-history/StateHistory.tsx @@ -27,16 +27,16 @@ type StateHistoryMap = Record; type StateHistoryRow = DynamicTableItemProps; interface Props { - alertId: string; + ruleUID: string; } -const StateHistory = ({ alertId }: Props) => { +const StateHistory = ({ ruleUID }: Props) => { const [textFilter, setTextFilter] = useState(''); const handleTextFilter = useCallback((event: FormEvent) => { setTextFilter(event.currentTarget.value); }, []); - const { loading, error, result = [] } = useManagedAlertStateHistory(alertId); + const { loading, error, result = [] } = useManagedAlertStateHistory(ruleUID); const styles = useStyles2(getStyles); diff --git a/public/app/features/alerting/unified/hooks/useManagedAlertStateHistory.ts b/public/app/features/alerting/unified/hooks/useManagedAlertStateHistory.ts index 43b10d666f5..e7902fcf507 100644 --- a/public/app/features/alerting/unified/hooks/useManagedAlertStateHistory.ts +++ b/public/app/features/alerting/unified/hooks/useManagedAlertStateHistory.ts @@ -8,15 +8,15 @@ import { AsyncRequestState } from '../utils/redux'; import { useUnifiedAlertingSelector } from './useUnifiedAlertingSelector'; -export function useManagedAlertStateHistory(alertId: string) { +export function useManagedAlertStateHistory(ruleUID: string) { const dispatch = useDispatch(); const history = useUnifiedAlertingSelector>( (state) => state.managedAlertStateHistory ); useEffect(() => { - dispatch(fetchGrafanaAnnotationsAction(alertId)); - }, [dispatch, alertId]); + dispatch(fetchGrafanaAnnotationsAction(ruleUID)); + }, [dispatch, ruleUID]); return history; } diff --git a/public/app/features/alerting/unified/hooks/useStateHistoryModal.tsx b/public/app/features/alerting/unified/hooks/useStateHistoryModal.tsx index c5c527c414a..3da0bc08e07 100644 --- a/public/app/features/alerting/unified/hooks/useStateHistoryModal.tsx +++ b/public/app/features/alerting/unified/hooks/useStateHistoryModal.tsx @@ -61,7 +61,7 @@ function useStateHistoryModal() { {implementation === StateHistoryImplementation.Loki && } {implementation === StateHistoryImplementation.Annotations && ( - + )} diff --git a/public/app/features/alerting/unified/state/actions.ts b/public/app/features/alerting/unified/state/actions.ts index 7c05181fd53..91adf4a5568 100644 --- a/public/app/features/alerting/unified/state/actions.ts +++ b/public/app/features/alerting/unified/state/actions.ts @@ -183,7 +183,7 @@ export function fetchAllPromRulesAction( export const fetchGrafanaAnnotationsAction = createAsyncThunk( 'unifiedalerting/fetchGrafanaAnnotations', - (alertId: string): Promise => withSerializedError(fetchAnnotations(alertId)) + (ruleUID: string): Promise => withSerializedError(fetchAnnotations(ruleUID)) ); interface UpdateAlertManagerConfigActionOptions { diff --git a/public/openapi3.json b/public/openapi3.json index 3aa7a775761..e5be6cd60ce 100644 --- a/public/openapi3.json +++ b/public/openapi3.json @@ -15316,7 +15316,7 @@ } }, { - "description": "Find annotations for a specified alert.", + "description": "Find annotations for a specified alert rule by its ID.\ndeprecated: AlertID is deprecated and will be removed in future versions. Please use AlertUID instead.", "in": "query", "name": "alertId", "schema": { @@ -15324,6 +15324,14 @@ "type": "integer" } }, + { + "description": "Find annotations for a specified alert rule by its UID.", + "in": "query", + "name": "alertUID", + "schema": { + "type": "string" + } + }, { "description": "Find annotations that are scoped to a specific dashboard", "in": "query", From 95b88e592dcaa04557c05e1a61d70964d8012dc7 Mon Sep 17 00:00:00 2001 From: Kim Nylander <104772500+knylander-grafana@users.noreply.github.com> Date: Fri, 21 Feb 2025 17:19:54 -0500 Subject: [PATCH 08/26] [DOC] Add shared note for Explore app rename to Drilldown (#101166) --- .../explore/simplified-exploration/_index.md | 4 +++- .../simplified-exploration/metrics/index.md | 12 +++++++----- docs/sources/shared/plugins/rename-note.md | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 docs/sources/shared/plugins/rename-note.md diff --git a/docs/sources/explore/simplified-exploration/_index.md b/docs/sources/explore/simplified-exploration/_index.md index bf43b04d41e..360b37aee5a 100644 --- a/docs/sources/explore/simplified-exploration/_index.md +++ b/docs/sources/explore/simplified-exploration/_index.md @@ -12,7 +12,7 @@ hero: level: 1 width: 100 height: 100 - description: Use the Drilldown apps to investigate and identify issues using telemetry data. + description: Use the Grafana Drilldown apps to investigate and identify issues using telemetry data. cards: title_class: pt-0 lh-1 items: @@ -40,6 +40,8 @@ The Grafana Drilldown apps are designed for effortless data exploration through Easily explore telemetry signals with these specialized tools, tailored specifically for the Grafana databases to provide quick and accurate insights. +{{< docs/shared source="grafana" lookup="plugins/rename-note.md" version="" >}} + To learn more, read: - [From multi-line queries to no-code investigations: meeting Grafana users where they are](https://grafana.com/blog/2024/10/22/from-multi-line-queries-to-no-code-investigations-meeting-grafana-users-where-they-are/) diff --git a/docs/sources/explore/simplified-exploration/metrics/index.md b/docs/sources/explore/simplified-exploration/metrics/index.md index a9d5d291772..eb6d23509e5 100644 --- a/docs/sources/explore/simplified-exploration/metrics/index.md +++ b/docs/sources/explore/simplified-exploration/metrics/index.md @@ -16,7 +16,9 @@ weight: 200 Grafana Metrics Drilldown is a query-less experience for browsing **Prometheus-compatible** metrics. Quickly find related metrics with just a few simple clicks, without needing to write PromQL queries to retrieve metrics. -With Grafana Metrics Drilldown, you can: +{{< docs/shared source="grafana" lookup="plugins/rename-note.md" version="" >}} + +With Metrics Drilldown, you can: - Easily segment metrics based on their labels, so you can immediately spot anomalies and identify issues. - Automatically display the optimal visualization for each metric type (gauge vs. counter, for example) without manual setup. @@ -25,13 +27,13 @@ With Grafana Metrics Drilldown, you can: - View a history of user steps when navigating through metrics and their filters. - Seamlessly pivot to related telemetry, including log data. -{{< docs/play title="Grafana Metrics Drilldown" url="https://play.grafana.org/explore/metrics/trail?from=now-1h&to=now&var-ds=grafanacloud-demoinfra-prom&var-filters=&refresh=&metricPrefix=all" >}} +{{< docs/play title="Metrics Drilldown" url="https://play.grafana.org/explore/metrics/trail?from=now-1h&to=now&var-ds=grafanacloud-demoinfra-prom&var-filters=&refresh=&metricPrefix=all" >}} -You can access Grafana Metrics Drilldown either as a standalone experience or as part of Grafana dashboards. +You can access Metrics Drilldown either as a standalone experience or as part of Grafana dashboards. ## Standalone experience -To access Grafana Metrics Drilldown as a standalone experience: +To access Metrics Drilldown as a standalone experience: 1. Click the arrow next to **Drilldown** in the Grafana left-side menu and click **Metrics**. You are taken to an overview page that shows recent metrics, bookmarks, and the option to select a new metric exploration. 1. To get started with a new exploration, click **Let's start!**. @@ -63,7 +65,7 @@ After you have gathered your metrics exploration data you can: ## Dashboard experience -To access Grafana Metrics Drilldown via a dashboard: +To access Metrics Drilldown via a dashboard: 1. Navigate to your dashboard. 1. Select a time series panel. diff --git a/docs/sources/shared/plugins/rename-note.md b/docs/sources/shared/plugins/rename-note.md new file mode 100644 index 00000000000..5eaa9a1adf4 --- /dev/null +++ b/docs/sources/shared/plugins/rename-note.md @@ -0,0 +1,19 @@ +--- +headless: true +labels: + products: + - enterprise + - oss +--- + +[//]: # 'This file contains a rename note for Explore to Drilldown apps.' +[//]: # 'This shared file is included in a lot of files. Check the app docs in' +[//]: # 'drilldown-traces, drilldown-logs, drilldown-profiles, grafana, and website/grafana-cloud.' +[//]: # 'If you make changes to this file, verify that the meaning and content are not changed in any place where the file is included.' +[//]: # 'Any links should be fully qualified and not relative: /docs/grafana/ instead of ../grafana/.' + +{{< admonition type="note" >}} +The Grafana Explore apps have changed to Grafana Drilldown apps. +For example, Explore Logs is now Logs Drilldown. +To learn more, read [Grafana Drilldown apps: the improved queryless experience known as the Explore apps](https://grafana.com/blog/2025/02/20/grafana-drilldown-apps-the-improved-queryless-experience-formerly-known-as-the-explore-apps/). +{{< /admonition >}} From ba352af6638b91379bb6e898c1fb54e9cb27e927 Mon Sep 17 00:00:00 2001 From: Yuri Tseretyan Date: Fri, 21 Feb 2025 18:46:03 -0500 Subject: [PATCH 09/26] Alerting: Github Action to update alerting module (#100999) --- .github/CODEOWNERS | 1 + .github/workflows/alerting-update-module.yml | 130 +++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 .github/workflows/alerting-update-module.yml diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 90689b24a05..92581c315aa 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -757,6 +757,7 @@ embed.go @grafana/grafana-as-code /.github/workflows/add-to-whats-new.yml @grafana/docs-tooling /.github/workflows/auto-triager/ @grafana/plugins-platform-frontend /.github/workflows/alerting-swagger-gen.yml @grafana/alerting-backend +/.github/workflows/alerting-update-module.yml @grafana/alerting-backend /.github/workflows/auto-milestone.yml @grafana/grafana-developer-enablement-squad /.github/workflows/backport.yml @grafana/grafana-developer-enablement-squad /.github/workflows/bump-version.yml @grafana/grafana-developer-enablement-squad diff --git a/.github/workflows/alerting-update-module.yml b/.github/workflows/alerting-update-module.yml new file mode 100644 index 00000000000..eece934525e --- /dev/null +++ b/.github/workflows/alerting-update-module.yml @@ -0,0 +1,130 @@ +name: Update Alerting Module + +on: + workflow_dispatch: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + update-grafana: + runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: write + + steps: + - name: Checkout repository + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # 4.2.2 + + - name: Check if update branch exists + run: | + if git ls-remote --heads origin update-alerting-module | grep -q 'update-alerting-module'; then + echo "Branch 'update-alerting-module' already exists. There might be an open PR with Grafana updates." + echo "Please review and merge/close the existing PR before running this workflow again." + exit 1 + fi + + - name: Setup Go + uses: actions/setup-go@f111f3307d8850f501ac008e886eec1fd1932a34 # 5.3.0 + with: + "go-version-file": "go.mod" + + - name: Extract current commit hash of alerting module + id: current-commit + run: | + FROM_COMMIT=$(go list -m -json github.com/grafana/alerting | jq -r '.Version' | grep -oP '(?<=-)[a-f0-9]+$') + echo "from_commit=$FROM_COMMIT" >> $GITHUB_OUTPUT + + - name: Get latest commit + id: latest-commit + env: + GH_TOKEN: ${{ github.token }} + run: | + TO_COMMIT=$(gh api repos/grafana/alerting/commits/main --jq '.sha') + if [ -z "$TO_COMMIT" ]; then + echo "Failed to fetch latest commit" + exit 1 + fi + echo "to_commit=$TO_COMMIT" >> $GITHUB_OUTPUT + + - name: Compare commit hashes + run: | + FROM_COMMIT="${{ steps.current-commit.outputs.from_commit }}" + TO_COMMIT="${{ steps.latest-commit.outputs.to_commit }}" + + # Compare just the length of the shorter hash + SHORT_TO_COMMIT="${TO_COMMIT:0:${#FROM_COMMIT}}" + + if [ "$FROM_COMMIT" = "$SHORT_TO_COMMIT" ]; then + echo "Current version ($FROM_COMMIT) is already at latest ($SHORT_TO_COMMIT). No update needed." + exit 0 + fi + echo "Updates available: $FROM_COMMIT -> $TO_COMMIT" + + - name: Check for commit history + id: check-commits + env: + GH_TOKEN: ${{ github.token }} + run: | + # get all commits that contains 'Alerting:' in the message + ALERTING_COMMITS=$(gh api repos/grafana/alerting/compare/${{ steps.current-commit.outputs.from_commit }}...${{ steps.latest-commit.outputs.to_commit }} \ + --jq '.commits[].commit.message | split("\n")[0]') || true + + # Use printf instead of echo -e for better multiline handling + printf "%s\n" "$ALERTING_COMMITS" + + # make the list for markdown and replace PR numbers with links + ALERTING_COMMITS_FORMATTED=$(echo "$ALERTING_COMMITS" | while read -r line; do echo "- $line" | sed -E 's/\(#([0-9]+)\)/[#\1](https:\/\/github.com\/grafana\/grafana\/pull\/\1)/g'; done) + + echo "alerting_commits<> $GITHUB_OUTPUT + echo "$ALERTING_COMMITS_FORMATTED" >> $GITHUB_OUTPUT + echo "EOF" >> $GITHUB_OUTPUT + + - name: Update alerting module + env: + GOSUMDB: off + run: | + go get github.com/grafana/alerting@${{ steps.latest-commit.outputs.to_commit }} + make update-workspace + + - id: get-secrets + uses: grafana/shared-workflows/actions/get-vault-secrets@28361cdb22223e5f1e34358c86c20908e7248760 # 1.1.0 + with: + repo_secrets: | + GITHUB_APP_ID=github-app:app-id + GITHUB_APP_PRIVATE_KEY=github-app:private-key + + - name: "Generate token" + id: generate_token + uses: actions/create-github-app-token@0d564482f06ca65fa9e77e2510873638c82206f2 # 1.11.5 + with: + app-id: ${{ env.GITHUB_APP_ID }} + private-key: ${{ env.GITHUB_APP_PRIVATE_KEY }} + + - name: Create Pull Request + uses: peter-evans/create-pull-request@67ccf781d68cd99b580ae25a5c18a1cc84ffff1f # 7.0.6 + id: create-pr + with: + token: '${{ steps.generate_token.outputs.token }}' + title: 'Alerting: Update alerting module to ${{ steps.latest-commit.outputs.to_commit }}' + branch: alerting/update-alerting-module + delete-branch: true + body: | + Updates Grafana Alerting module to latest version. + + Compare changes: https://github.com/grafana/alerting/compare/${{ steps.current-commit.outputs.from_commit }}...${{ steps.latest-commit.outputs.to_commit }} +
+ Commits + + ${{ steps.check-commits.outputs.alerting_commits }} + +
+ + Created by: [GitHub Action Job](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}) + - name: Add PR URL to Summary + if: steps.create-pr.outputs.pull-request-url != '' + run: | + echo "## Pull Request Created" >> $GITHUB_STEP_SUMMARY + echo "🔗 [View Pull Request](${{ steps.create-pr.outputs.pull-request-url }})" >> $GITHUB_STEP_SUMMARY \ No newline at end of file From 436dc86a0968ccc34638ac594662bbc1987944a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Sat, 22 Feb 2025 11:06:29 +0100 Subject: [PATCH 10/26] TabsLayout: Implements url sync and removes double scene object reference (#101115) * TabsLayout: Implementts url sync and removes double scene object reference * Do not allow removing last tab * Update public/app/features/dashboard-scene/scene/layout-tabs/TabsLayoutManager.tsx Co-authored-by: Bogdan Matei * Update public/app/features/dashboard-scene/scene/layout-tabs/TabsLayoutManager.tsx Co-authored-by: Bogdan Matei * Update --------- Co-authored-by: Bogdan Matei --- .../scene/layout-tabs/TabItem.tsx | 4 - .../scene/layout-tabs/TabItemRenderer.tsx | 24 +++--- .../scene/layout-tabs/TabsLayoutManager.tsx | 75 ++++++++++++++----- .../layout-tabs/TabsLayoutManagerRenderer.tsx | 7 +- .../layoutSerializers/TabsLayoutSerializer.ts | 2 +- .../transformSceneToSaveModelSchemaV2.test.ts | 9 +-- 6 files changed, 74 insertions(+), 47 deletions(-) diff --git a/public/app/features/dashboard-scene/scene/layout-tabs/TabItem.tsx b/public/app/features/dashboard-scene/scene/layout-tabs/TabItem.tsx index 73ce7af6b19..4f4a2b45766 100644 --- a/public/app/features/dashboard-scene/scene/layout-tabs/TabItem.tsx +++ b/public/app/features/dashboard-scene/scene/layout-tabs/TabItem.tsx @@ -70,10 +70,6 @@ export class TabItem return new TabItems(items.filter((item) => item instanceof TabItem)); } - public onChangeTab() { - this.getParentLayout().changeTab(this); - } - public onChangeTitle(title: string) { this.setState({ title }); } diff --git a/public/app/features/dashboard-scene/scene/layout-tabs/TabItemRenderer.tsx b/public/app/features/dashboard-scene/scene/layout-tabs/TabItemRenderer.tsx index 782fa9eeb3b..37c653ab354 100644 --- a/public/app/features/dashboard-scene/scene/layout-tabs/TabItemRenderer.tsx +++ b/public/app/features/dashboard-scene/scene/layout-tabs/TabItemRenderer.tsx @@ -1,5 +1,7 @@ import { useMemo } from 'react'; +import { useLocation } from 'react-router'; +import { locationUtil } from '@grafana/data'; import { SceneComponentProps, sceneGraph } from '@grafana/scenes'; import { Tab, useElementSelection } from '@grafana/ui'; @@ -12,29 +14,27 @@ export function TabItemRenderer({ model }: SceneComponentProps) { const { title, key } = model.useState(); const isClone = useMemo(() => isClonedKey(key!), [key]); const parentLayout = model.getParentLayout(); - const { currentTab } = parentLayout.useState(); + const { tabs, currentTabIndex } = parentLayout.useState(); const dashboard = getDashboardSceneFor(model); const { isEditing } = dashboard.useState(); const titleInterpolated = sceneGraph.interpolate(model, title, undefined, 'text'); const { isSelected, onSelect } = useElementSelection(key); + const myIndex = tabs.findIndex((tab) => tab === model); + const isActive = myIndex === currentTabIndex; + const location = useLocation(); + const href = locationUtil.getUrlForPartial(location, { tab: myIndex }); return ( { - evt.stopPropagation(); - - if (isEditing) { - if (isClone) { - dashboard.state.editPane.clearSelection(); - } else { - onSelect?.(evt); - } + if (isEditing && isActive && !isClone) { + evt.stopPropagation(); + onSelect?.(evt); } - - parentLayout.changeTab(model); }} /> ); diff --git a/public/app/features/dashboard-scene/scene/layout-tabs/TabsLayoutManager.tsx b/public/app/features/dashboard-scene/scene/layout-tabs/TabsLayoutManager.tsx index daa86286dae..3e2c338b26f 100644 --- a/public/app/features/dashboard-scene/scene/layout-tabs/TabsLayoutManager.tsx +++ b/public/app/features/dashboard-scene/scene/layout-tabs/TabsLayoutManager.tsx @@ -1,4 +1,10 @@ -import { SceneObjectBase, SceneObjectState, VizPanel } from '@grafana/scenes'; +import { + SceneObjectBase, + SceneObjectState, + SceneObjectUrlSyncConfig, + SceneObjectUrlValues, + VizPanel, +} from '@grafana/scenes'; import { t } from 'app/core/internationalization'; import { DashboardLayoutManager } from '../types/DashboardLayoutManager'; @@ -9,7 +15,7 @@ import { TabsLayoutManagerRenderer } from './TabsLayoutManagerRenderer'; interface TabsLayoutManagerState extends SceneObjectState { tabs: TabItem[]; - currentTab: TabItem; + currentTabIndex: number; } export class TabsLayoutManager extends SceneObjectBase implements DashboardLayoutManager { @@ -26,14 +32,42 @@ export class TabsLayoutManager extends SceneObjectBase i }, id: 'tabs-layout', createFromLayout: TabsLayoutManager.createFromLayout, - kind: 'TabsLayout', }; public readonly descriptor = TabsLayoutManager.descriptor; + protected _urlSync = new SceneObjectUrlSyncConfig(this, { keys: ['tab'] }); + + public constructor(state: Partial) { + super({ + ...state, + tabs: state.tabs ?? [new TabItem()], + currentTabIndex: state.currentTabIndex ?? 0, + }); + } + + public getUrlState() { + return { tab: this.state.currentTabIndex.toString() }; + } + + public updateFromUrl(values: SceneObjectUrlValues) { + if (!values.tab) { + return; + } + if (typeof values.tab === 'string') { + this.setState({ currentTabIndex: parseInt(values.tab, 10) }); + } + } + + public getCurrentTab(): TabItem { + return this.state.tabs.length > this.state.currentTabIndex + ? this.state.tabs[this.state.currentTabIndex] + : this.state.tabs[0]; + } + public addPanel(vizPanel: VizPanel) { - this.state.currentTab.getLayout().addPanel(vizPanel); + this.getCurrentTab().getLayout().addPanel(vizPanel); } public getVizPanels(): VizPanel[] { @@ -62,12 +96,12 @@ export class TabsLayoutManager extends SceneObjectBase i } public addNewRow() { - this.state.currentTab.getLayout().addNewRow(); + this.getCurrentTab().getLayout().addNewRow(); } public addNewTab() { const currentTab = new TabItem(); - this.setState({ tabs: [...this.state.tabs, currentTab], currentTab }); + this.setState({ tabs: [...this.state.tabs, currentTab], currentTabIndex: this.state.tabs.length }); } public editModeChanged(isEditing: boolean) { @@ -78,32 +112,33 @@ export class TabsLayoutManager extends SceneObjectBase i this.state.tabs.forEach((tab) => tab.getLayout().activateRepeaters?.()); } - public removeTab(tab: TabItem) { - if (this.state.currentTab === tab) { - const currentTabIndex = this.state.tabs.indexOf(tab); - const nextTabIndex = currentTabIndex === 0 ? 1 : currentTabIndex - 1; - const nextTab = this.state.tabs[nextTabIndex]; - this.setState({ tabs: this.state.tabs.filter((t) => t !== tab), currentTab: nextTab }); + public removeTab(tabToRemove: TabItem) { + // Do not allow removing last tab (for now) + if (this.state.tabs.length === 1) { return; } - const filteredTab = this.state.tabs.filter((tab) => tab !== this.state.currentTab); + const currentTab = this.getCurrentTab(); + + if (currentTab === tabToRemove) { + const nextTabIndex = this.state.currentTabIndex > 0 ? this.state.currentTabIndex - 1 : 0; + this.setState({ tabs: this.state.tabs.filter((t) => t !== tabToRemove), currentTabIndex: nextTabIndex }); + return; + } + + const filteredTab = this.state.tabs.filter((tab) => tab !== tabToRemove); const tabs = filteredTab.length === 0 ? [new TabItem()] : filteredTab; - this.setState({ tabs, currentTab: tabs[tabs.length - 1] }); - } - - public changeTab(tab: TabItem) { - this.setState({ currentTab: tab }); + this.setState({ tabs, currentTabIndex: 0 }); } public static createEmpty(): TabsLayoutManager { const tab = new TabItem(); - return new TabsLayoutManager({ tabs: [tab], currentTab: tab }); + return new TabsLayoutManager({ tabs: [tab] }); } public static createFromLayout(layout: DashboardLayoutManager): TabsLayoutManager { const tab = new TabItem({ layout: layout.clone() }); - return new TabsLayoutManager({ tabs: [tab], currentTab: tab }); + return new TabsLayoutManager({ tabs: [tab] }); } } diff --git a/public/app/features/dashboard-scene/scene/layout-tabs/TabsLayoutManagerRenderer.tsx b/public/app/features/dashboard-scene/scene/layout-tabs/TabsLayoutManagerRenderer.tsx index 68d4840ae19..443ae6e1f1c 100644 --- a/public/app/features/dashboard-scene/scene/layout-tabs/TabsLayoutManagerRenderer.tsx +++ b/public/app/features/dashboard-scene/scene/layout-tabs/TabsLayoutManagerRenderer.tsx @@ -8,7 +8,8 @@ import { TabsLayoutManager } from './TabsLayoutManager'; export function TabsLayoutManagerRenderer({ model }: SceneComponentProps) { const styles = useStyles2(getStyles); - const { tabs, currentTab } = model.useState(); + const { tabs, currentTabIndex } = model.useState(); + const currentTab = tabs[currentTabIndex]; const { layout } = currentTab.useState(); return ( @@ -18,7 +19,9 @@ export function TabsLayoutManagerRenderer({ model }: SceneComponentProps ))} - {layout && } + + {currentTab && } + ); } diff --git a/public/app/features/dashboard-scene/serialization/layoutSerializers/TabsLayoutSerializer.ts b/public/app/features/dashboard-scene/serialization/layoutSerializers/TabsLayoutSerializer.ts index d6eb10803a1..d5093fb4663 100644 --- a/public/app/features/dashboard-scene/serialization/layoutSerializers/TabsLayoutSerializer.ts +++ b/public/app/features/dashboard-scene/serialization/layoutSerializers/TabsLayoutSerializer.ts @@ -44,6 +44,6 @@ export class TabsLayoutSerializer implements LayoutManagerSerializer { layout: layoutSerializerRegistry.get(layout.kind).serializer.deserialize(layout, elements, preload), }); }); - return new TabsLayoutManager({ tabs, currentTab: tabs[0] }); + return new TabsLayoutManager({ tabs }); } } diff --git a/public/app/features/dashboard-scene/serialization/transformSceneToSaveModelSchemaV2.test.ts b/public/app/features/dashboard-scene/serialization/transformSceneToSaveModelSchemaV2.test.ts index 4ff7c331b78..050565bc711 100644 --- a/public/app/features/dashboard-scene/serialization/transformSceneToSaveModelSchemaV2.test.ts +++ b/public/app/features/dashboard-scene/serialization/transformSceneToSaveModelSchemaV2.test.ts @@ -549,14 +549,7 @@ describe('dynamic layouts', () => { }), ]; - const scene = setupDashboardScene( - getMinimalSceneState( - new TabsLayoutManager({ - currentTab: tabs[0], - tabs, - }) - ) - ); + const scene = setupDashboardScene(getMinimalSceneState(new TabsLayoutManager({ tabs }))); const result = transformSceneToSaveModelSchemaV2(scene); expect(result.layout.kind).toBe('TabsLayout'); const tabsLayout = result.layout.spec as TabsLayoutSpec; From 5a6d9a99f3481b58485ee068aae7901ef81cb21a Mon Sep 17 00:00:00 2001 From: Alexander Akhmetov Date: Sat, 22 Feb 2025 11:06:42 +0100 Subject: [PATCH 11/26] Alerting: Generate stable UIDs for alert rules in Prometheus conversion (#100973) --- pkg/services/ngalert/prom/convert.go | 33 +++++++ pkg/services/ngalert/prom/convert_test.go | 109 ++++++++++++++++++++++ 2 files changed, 142 insertions(+) diff --git a/pkg/services/ngalert/prom/convert.go b/pkg/services/ngalert/prom/convert.go index d14a37aa9f5..07495e0e2e1 100644 --- a/pkg/services/ngalert/prom/convert.go +++ b/pkg/services/ngalert/prom/convert.go @@ -5,10 +5,19 @@ import ( "fmt" "time" + "github.com/google/uuid" "gopkg.in/yaml.v3" "github.com/grafana/grafana/pkg/services/datasources" "github.com/grafana/grafana/pkg/services/ngalert/models" + "github.com/grafana/grafana/pkg/util" +) + +const ( + // ruleUIDLabel is a special label that can be used to set a custom UID for a Prometheus + // alert rule when converting it to a Grafana alert rule. If this label is not present, + // a stable UID will be generated automatically based on the rule's data. + ruleUIDLabel = "__grafana_alert_rule_uid__" ) type Config struct { @@ -114,6 +123,12 @@ func (p *Converter) convertRuleGroup(orgID int64, namespaceUID string, promGroup gr.Title = fmt.Sprintf("%s (%d)", gr.Title, val) } + uid, err := getUID(orgID, namespaceUID, promGroup.Name, i, rule) + if err != nil { + return nil, fmt.Errorf("failed to generate UID for rule '%s': %w", gr.Title, err) + } + gr.UID = uid + rules = append(rules, gr) } @@ -127,6 +142,24 @@ func (p *Converter) convertRuleGroup(orgID int64, namespaceUID string, promGroup return result, nil } +// getUID returns a UID for a Prometheus rule. +// If the rule has a special label its value is used. +// Otherwise, a stable UUID is generated by using a hash of the rule's data. +func getUID(orgID int64, namespaceUID string, group string, position int, promRule PrometheusRule) (string, error) { + if uid, ok := promRule.Labels[ruleUIDLabel]; ok { + if err := util.ValidateUID(uid); err != nil { + return "", fmt.Errorf("invalid UID label value: %s; %w", uid, err) + } + return uid, nil + } + + // Generate stable UUID based on the orgID, namespace, group and position. + uidData := fmt.Sprintf("%d|%s|%s|%d", orgID, namespaceUID, group, position) + u := uuid.NewSHA1(uuid.NameSpaceOID, []byte(uidData)) + + return u.String(), nil +} + func (p *Converter) convertRule(orgID int64, namespaceUID, group string, rule PrometheusRule) (models.AlertRule, error) { var forInterval time.Duration if rule.For != nil { diff --git a/pkg/services/ngalert/prom/convert_test.go b/pkg/services/ngalert/prom/convert_test.go index f175686fd3d..1b85c0d9d92 100644 --- a/pkg/services/ngalert/prom/convert_test.go +++ b/pkg/services/ngalert/prom/convert_test.go @@ -1,15 +1,18 @@ package prom import ( + "fmt" "testing" "time" + "github.com/google/uuid" prommodel "github.com/prometheus/common/model" "github.com/stretchr/testify/require" "gopkg.in/yaml.v3" "github.com/grafana/grafana/pkg/services/datasources" "github.com/grafana/grafana/pkg/services/ngalert/models" + "github.com/grafana/grafana/pkg/util" ) func TestPrometheusRulesToGrafana(t *testing.T) { @@ -136,6 +139,10 @@ func TestPrometheusRulesToGrafana(t *testing.T) { expectedLabels[k] = v } + uidData := fmt.Sprintf("%d|%s|%s|%d", tc.orgID, tc.namespace, tc.promGroup.Name, j) + u := uuid.NewSHA1(uuid.NameSpaceOID, []byte(uidData)) + require.Equal(t, u.String(), grafanaRule.UID, tc.name) + require.Equal(t, expectedLabels, grafanaRule.Labels, tc.name) require.Equal(t, promRule.Annotations, grafanaRule.Annotations, tc.name) require.Equal(t, models.Duration(0*time.Minute), grafanaRule.Data[0].RelativeTimeRange.To) @@ -190,3 +197,105 @@ func TestPrometheusRulesToGrafanaWithDuplicateRuleNames(t *testing.T) { require.Equal(t, "another alert", group.Rules[2].Title) require.Equal(t, "alert (3)", group.Rules[3].Title) } + +func TestPrometheusRulesToGrafana_UID(t *testing.T) { + orgID := int64(1) + namespace := "some-namespace" + + promGroup := PrometheusRuleGroup{ + Name: "test-group-1", + Interval: prommodel.Duration(10 * time.Second), + Rules: []PrometheusRule{ + { + Alert: "alert-1", + Expr: "cpu_usage > 80", + For: util.Pointer(prommodel.Duration(5 * time.Minute)), + Labels: map[string]string{ + "severity": "critical", + ruleUIDLabel: "rule-uid-1", + }, + Annotations: map[string]string{ + "summary": "CPU usage is critical", + }, + }, + }, + } + + converter, err := NewConverter(Config{ + DatasourceUID: "datasource-uid", + DatasourceType: datasources.DS_PROMETHEUS, + }) + require.NoError(t, err) + + t.Run("if not specified, UID is generated based on the rule index", func(t *testing.T) { + grafanaGroup, err := converter.PrometheusRulesToGrafana(orgID, namespace, promGroup) + require.NoError(t, err) + + firstUID := grafanaGroup.Rules[0].UID + + // Convert again + grafanaGroup, err = converter.PrometheusRulesToGrafana(orgID, namespace, promGroup) + require.NoError(t, err) + + secondUID := grafanaGroup.Rules[0].UID + + // They must be equal + require.NotEmpty(t, firstUID) + require.Equal(t, firstUID, secondUID) + }) + + t.Run("if the special label is specified", func(t *testing.T) { + t.Run("and the label is valid it should be used", func(t *testing.T) { + orgID := int64(1) + namespace := "some-namespace" + + converter, err := NewConverter(Config{ + DatasourceUID: "datasource-uid", + DatasourceType: datasources.DS_PROMETHEUS, + }) + require.NoError(t, err) + + promGroup.Rules[0].Labels[ruleUIDLabel] = "rule-uid-1" + + grafanaGroup, err := converter.PrometheusRulesToGrafana(orgID, namespace, promGroup) + require.NoError(t, err) + + require.Equal(t, "rule-uid-1", grafanaGroup.Rules[0].UID) + }) + + t.Run("and the label is invalid", func(t *testing.T) { + orgID := int64(1) + namespace := "some-namespace" + + converter, err := NewConverter(Config{ + DatasourceUID: "datasource-uid", + DatasourceType: datasources.DS_PROMETHEUS, + }) + require.NoError(t, err) + + // create a string of 50 characters + promGroup.Rules[0].Labels[ruleUIDLabel] = "aaaabbbbccccddddeeeeffffgggghhhhiiiijjjjkkkkllllmm" // too long + + grafanaGroup, err := converter.PrometheusRulesToGrafana(orgID, namespace, promGroup) + require.Errorf(t, err, "invalid UID label value") + require.Nil(t, grafanaGroup) + }) + + t.Run("and the label is empty", func(t *testing.T) { + orgID := int64(1) + namespace := "some-namespace" + + converter, err := NewConverter(Config{ + DatasourceUID: "datasource-uid", + DatasourceType: datasources.DS_PROMETHEUS, + }) + require.NoError(t, err) + + promGroup.Rules[0].Labels[ruleUIDLabel] = "" + + grafanaGroup, err := converter.PrometheusRulesToGrafana(orgID, namespace, promGroup) + require.Errorf(t, err, "invalid UID label value") + require.Nil(t, grafanaGroup) + }) + }) +} From 9dac0c9eeb15e273bb363a0c136219033d7bf526 Mon Sep 17 00:00:00 2001 From: Alexander Akhmetov Date: Sat, 22 Feb 2025 12:36:58 +0100 Subject: [PATCH 12/26] Alerting: Add math node to the converted Prometheus rules (#101097) --- pkg/services/ngalert/prom/convert.go | 107 ++++++++++-------- pkg/services/ngalert/prom/convert_test.go | 122 +++++++++++++++++++++ pkg/services/ngalert/prom/query.go | 127 ++++++++++++++++++++++ 3 files changed, 312 insertions(+), 44 deletions(-) create mode 100644 pkg/services/ngalert/prom/query.go diff --git a/pkg/services/ngalert/prom/convert.go b/pkg/services/ngalert/prom/convert.go index 07495e0e2e1..bb9d4c3d616 100644 --- a/pkg/services/ngalert/prom/convert.go +++ b/pkg/services/ngalert/prom/convert.go @@ -1,7 +1,6 @@ package prom import ( - "encoding/json" "fmt" "time" @@ -20,6 +19,13 @@ const ( ruleUIDLabel = "__grafana_alert_rule_uid__" ) +const ( + queryRefID = "query" + prometheusMathRefID = "prometheus_math" + thresholdRefID = "threshold" +) + +// Config defines the configuration options for the Prometheus to Grafana rules converter. type Config struct { DatasourceUID string DatasourceType string @@ -31,6 +37,7 @@ type Config struct { AlertRules RulesConfig } +// RulesConfig contains configuration that applies to either recording or alerting rules. type RulesConfig struct { IsPaused bool } @@ -43,7 +50,7 @@ var ( FromTimeRange: &defaultTimeRange, EvaluationOffset: &defaultEvaluationOffset, ExecErrState: models.ErrorErrState, - NoDataState: models.NoData, + NoDataState: models.OK, } ) @@ -51,6 +58,9 @@ type Converter struct { cfg Config } +// NewConverter creates a new Converter instance with the provided configuration. +// It validates the configuration and returns an error if any required fields are missing +// or if the configuration is invalid. func NewConverter(cfg Config) (*Converter, error) { if cfg.DatasourceUID == "" { return nil, fmt.Errorf("datasource UID is required") @@ -166,15 +176,28 @@ func (p *Converter) convertRule(orgID int64, namespaceUID, group string, rule Pr forInterval = time.Duration(*rule.For) } - queryNode, err := createAlertQueryNode(p.cfg.DatasourceUID, p.cfg.DatasourceType, rule.Expr, *p.cfg.FromTimeRange, *p.cfg.EvaluationOffset) + var query []models.AlertQuery + var title string + var isPaused bool + var record *models.Record + var err error + + isRecordingRule := rule.Record != "" + query, err = p.createQuery(rule.Expr, isRecordingRule) if err != nil { return models.AlertRule{}, err } - var title string - if rule.Record != "" { + if isRecordingRule { + record = &models.Record{ + From: queryRefID, + Metric: rule.Record, + } + + isPaused = p.cfg.RecordingRules.IsPaused title = rule.Record } else { + isPaused = p.cfg.AlertRules.IsPaused title = rule.Alert } @@ -192,14 +215,16 @@ func (p *Converter) convertRule(orgID int64, namespaceUID, group string, rule Pr OrgID: orgID, NamespaceUID: namespaceUID, Title: title, - Data: []models.AlertQuery{queryNode}, - Condition: "A", + Data: query, + Condition: query[len(query)-1].RefID, NoDataState: p.cfg.NoDataState, ExecErrState: p.cfg.ExecErrState, Annotations: rule.Annotations, Labels: labels, For: forInterval, RuleGroup: group, + IsPaused: isPaused, + Record: record, Metadata: models.AlertRuleMetadata{ PrometheusStyleRule: &models.PrometheusStyleRule{ OriginalRuleDefinition: string(originalRuleDefinition), @@ -207,47 +232,41 @@ func (p *Converter) convertRule(orgID int64, namespaceUID, group string, rule Pr }, } - if rule.Record != "" { - result.Record = &models.Record{ - From: "A", - Metric: rule.Record, - } - result.IsPaused = p.cfg.RecordingRules.IsPaused - } else { - result.IsPaused = p.cfg.AlertRules.IsPaused - } - return result, nil } -func createAlertQueryNode(datasourceUID, datasourceType, expr string, fromTimeRange, evaluationOffset time.Duration) (models.AlertQuery, error) { - modelData := map[string]interface{}{ - "datasource": map[string]interface{}{ - "type": datasourceType, - "uid": datasourceUID, - }, - "expr": expr, - "instant": true, - "range": false, - "refId": "A", - } - - if datasourceType == datasources.DS_LOKI { - modelData["queryType"] = "instant" - } - - modelJSON, err := json.Marshal(modelData) +// createQuery constructs the alert query nodes for a given Prometheus rule expression. +// It returns a slice of AlertQuery that represent the evaluation steps for the rule. +// +// For recording rules it generates a single query node that +// executes the PromQL query in the configured datasource. +// +// For alerting rules, it generates three query nodes: +// 1. Query Node (query): Executes the PromQL query using the configured datasource. +// 2. Math Node (prometheus_math): Applies a math expression "is_number($query) || is_nan($query) || is_inf($query)". +// 3. Threshold Node (threshold): Gets the result from the math node and checks that it's greater than 0. +// +// This is needed to ensure that we keep the Prometheus behaviour, where any returned result +// is considered alerting, and only when the query returns no data is the alert treated as normal. +func (p *Converter) createQuery(expr string, isRecordingRule bool) ([]models.AlertQuery, error) { + queryNode, err := createQueryNode(p.cfg.DatasourceUID, p.cfg.DatasourceType, expr, *p.cfg.FromTimeRange, *p.cfg.EvaluationOffset) if err != nil { - return models.AlertQuery{}, err + return nil, err } - return models.AlertQuery{ - DatasourceUID: datasourceUID, - Model: modelJSON, - RefID: "A", - RelativeTimeRange: models.RelativeTimeRange{ - From: models.Duration(fromTimeRange + evaluationOffset), - To: models.Duration(0 + evaluationOffset), - }, - }, nil + if isRecordingRule { + return []models.AlertQuery{queryNode}, nil + } + + mathNode, err := createMathNode() + if err != nil { + return nil, err + } + + thresholdNode, err := createThresholdNode() + if err != nil { + return nil, err + } + + return []models.AlertQuery{queryNode, mathNode, thresholdNode}, nil } diff --git a/pkg/services/ngalert/prom/convert_test.go b/pkg/services/ngalert/prom/convert_test.go index 1b85c0d9d92..3bad4795fcb 100644 --- a/pkg/services/ngalert/prom/convert_test.go +++ b/pkg/services/ngalert/prom/convert_test.go @@ -1,6 +1,7 @@ package prom import ( + "encoding/json" "fmt" "testing" "time" @@ -10,6 +11,8 @@ import ( "github.com/stretchr/testify/require" "gopkg.in/yaml.v3" + "github.com/grafana/grafana/pkg/expr" + "github.com/grafana/grafana/pkg/expr/mathexp" "github.com/grafana/grafana/pkg/services/datasources" "github.com/grafana/grafana/pkg/services/ngalert/models" "github.com/grafana/grafana/pkg/util" @@ -124,6 +127,9 @@ func TestPrometheusRulesToGrafana(t *testing.T) { if promRule.Record != "" { require.Equal(t, promRule.Record, grafanaRule.Title) + require.NotNil(t, grafanaRule.Record) + require.Equal(t, grafanaRule.Record.From, queryRefID) + require.Equal(t, promRule.Record, grafanaRule.Record.Metric) } else { require.Equal(t, promRule.Alert, grafanaRule.Title) } @@ -198,6 +204,122 @@ func TestPrometheusRulesToGrafanaWithDuplicateRuleNames(t *testing.T) { require.Equal(t, "alert (3)", group.Rules[3].Title) } +func TestCreateMathNode(t *testing.T) { + node, err := createMathNode() + require.NoError(t, err) + + require.Equal(t, expr.DatasourceUID, node.DatasourceUID) + require.Equal(t, string(expr.QueryTypeMath), node.QueryType) + require.Equal(t, "prometheus_math", node.RefID) + + var model map[string]interface{} + err = json.Unmarshal(node.Model, &model) + require.NoError(t, err) + + require.Equal(t, "prometheus_math", model["refId"]) + require.Equal(t, string(expr.QueryTypeMath), model["type"]) + require.Equal(t, "is_number($query) || is_nan($query) || is_inf($query)", model["expression"]) + + ds := model["datasource"].(map[string]interface{}) + require.Equal(t, expr.DatasourceUID, ds["name"]) + require.Equal(t, expr.DatasourceType, ds["type"]) + require.Equal(t, expr.DatasourceUID, ds["uid"]) +} + +func TestCreateThresholdNode(t *testing.T) { + node, err := createThresholdNode() + require.NoError(t, err) + + require.Equal(t, expr.DatasourceUID, node.DatasourceUID) + require.Equal(t, string(expr.QueryTypeThreshold), node.QueryType) + require.Equal(t, "threshold", node.RefID) + + var model map[string]interface{} + err = json.Unmarshal(node.Model, &model) + require.NoError(t, err) + + require.Equal(t, "threshold", model["refId"]) + require.Equal(t, string(expr.QueryTypeThreshold), model["type"]) + + ds := model["datasource"].(map[string]interface{}) + require.Equal(t, expr.DatasourceUID, ds["name"]) + require.Equal(t, expr.DatasourceType, ds["type"]) + require.Equal(t, expr.DatasourceUID, ds["uid"]) + + conditions := model["conditions"].([]interface{}) + require.Len(t, conditions, 1) + + condition := conditions[0].(map[string]interface{}) + evaluator := condition["evaluator"].(map[string]interface{}) + require.Equal(t, string(expr.ThresholdIsAbove), evaluator["type"]) + require.Equal(t, []interface{}{float64(0)}, evaluator["params"]) +} + +func TestPrometheusRulesToGrafana_NodesInRules(t *testing.T) { + cfg := Config{ + DatasourceUID: "datasource-uid", + DatasourceType: datasources.DS_PROMETHEUS, + } + converter, err := NewConverter(cfg) + require.NoError(t, err) + + t.Run("alert rule should have math and threshold nodes", func(t *testing.T) { + group := PrometheusRuleGroup{ + Name: "test", + Rules: []PrometheusRule{ + { + Alert: "alert1", + Expr: "up == 0", + }, + }, + } + + result, err := converter.PrometheusRulesToGrafana(1, "namespace", group) + require.NoError(t, err) + require.Len(t, result.Rules, 1) + require.Len(t, result.Rules[0].Data, 3) + + // First node should be query + require.Equal(t, "query", result.Rules[0].Data[0].RefID) + + // Second node should be math + require.Equal(t, "prometheus_math", result.Rules[0].Data[1].RefID) + require.Equal(t, string(expr.QueryTypeMath), result.Rules[0].Data[1].QueryType) + // Check that the math expression is valid + var model map[string]interface{} + err = json.Unmarshal(result.Rules[0].Data[1].Model, &model) + require.NoError(t, err) + require.Equal(t, "is_number($query) || is_nan($query) || is_inf($query)", model["expression"]) + // The math expression should be parsed successfully + _, err = mathexp.New(model["expression"].(string)) + require.NoError(t, err) + + // Third node should be threshold + require.Equal(t, "threshold", result.Rules[0].Data[2].RefID) + require.Equal(t, string(expr.QueryTypeThreshold), result.Rules[0].Data[2].QueryType) + }) + + t.Run("recording rule should only have query node", func(t *testing.T) { + group := PrometheusRuleGroup{ + Name: "test", + Rules: []PrometheusRule{ + { + Record: "metric", + Expr: "sum(rate(http_requests_total[5m]))", + }, + }, + } + + result, err := converter.PrometheusRulesToGrafana(1, "namespace", group) + require.NoError(t, err) + require.Len(t, result.Rules, 1) + require.Len(t, result.Rules[0].Data, 1) + + // Should only have query node + require.Equal(t, "query", result.Rules[0].Data[0].RefID) + }) +} + func TestPrometheusRulesToGrafana_UID(t *testing.T) { orgID := int64(1) namespace := "some-namespace" diff --git a/pkg/services/ngalert/prom/query.go b/pkg/services/ngalert/prom/query.go new file mode 100644 index 00000000000..74aed34f83f --- /dev/null +++ b/pkg/services/ngalert/prom/query.go @@ -0,0 +1,127 @@ +package prom + +import ( + "encoding/json" + "fmt" + "time" + + "github.com/grafana/grafana/pkg/expr" + "github.com/grafana/grafana/pkg/services/datasources" + "github.com/grafana/grafana/pkg/services/ngalert/models" +) + +type CommonQueryModel struct { + Datasource datasources.DataSource `json:"datasource"` + RefID string `json:"refId"` + Type expr.QueryType `json:"type"` +} + +func createQueryNode(datasourceUID, datasourceType, expr string, fromTimeRange, evaluationOffset time.Duration) (models.AlertQuery, error) { + modelData := map[string]interface{}{ + "datasource": map[string]interface{}{ + "type": datasourceType, + "uid": datasourceUID, + }, + "expr": expr, + "instant": true, + "range": false, + "refId": queryRefID, + } + + if datasourceType == datasources.DS_LOKI { + modelData["queryType"] = "instant" + } + + modelJSON, err := json.Marshal(modelData) + if err != nil { + return models.AlertQuery{}, err + } + + return models.AlertQuery{ + DatasourceUID: datasourceUID, + Model: modelJSON, + RefID: queryRefID, + RelativeTimeRange: models.RelativeTimeRange{ + From: models.Duration(fromTimeRange + evaluationOffset), + To: models.Duration(0 + evaluationOffset), + }, + }, nil +} + +type MathQueryModel struct { + expr.MathQuery + CommonQueryModel +} + +func createMathNode() (models.AlertQuery, error) { + ds, err := expr.DataSourceModelFromNodeType(expr.TypeCMDNode) + if err != nil { + return models.AlertQuery{}, err + } + + model := MathQueryModel{ + CommonQueryModel: CommonQueryModel{ + Datasource: *ds, + RefID: prometheusMathRefID, + Type: expr.QueryTypeMath, + }, + MathQuery: expr.MathQuery{ + Expression: fmt.Sprintf("is_number($%[1]s) || is_nan($%[1]s) || is_inf($%[1]s)", queryRefID), + }, + } + + modelJSON, err := json.Marshal(model) + if err != nil { + return models.AlertQuery{}, err + } + + return models.AlertQuery{ + DatasourceUID: expr.DatasourceUID, + Model: modelJSON, + RefID: prometheusMathRefID, + QueryType: string(model.Type), + }, nil +} + +type ThresholdQueryModel struct { + expr.ThresholdQuery + CommonQueryModel +} + +func createThresholdNode() (models.AlertQuery, error) { + ds, err := expr.DataSourceModelFromNodeType(expr.TypeCMDNode) + if err != nil { + return models.AlertQuery{}, err + } + + model := ThresholdQueryModel{ + CommonQueryModel: CommonQueryModel{ + Datasource: *ds, + RefID: thresholdRefID, + Type: expr.QueryTypeThreshold, + }, + ThresholdQuery: expr.ThresholdQuery{ + Expression: prometheusMathRefID, + Conditions: []expr.ThresholdConditionJSON{ + { + Evaluator: expr.ConditionEvalJSON{ + Type: expr.ThresholdIsAbove, + Params: []float64{0}, + }, + }, + }, + }, + } + + modelJSON, err := json.Marshal(model) + if err != nil { + return models.AlertQuery{}, err + } + + return models.AlertQuery{ + DatasourceUID: expr.DatasourceUID, + Model: modelJSON, + RefID: thresholdRefID, + QueryType: string(model.Type), + }, nil +} From 6d7147c38df08c8c2423fdc175ff9643802fb9c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Sat, 22 Feb 2025 14:15:26 +0100 Subject: [PATCH 13/26] Dashboard: Edit pane tabs (#101145) * Dashboard: Edit pane tabs * update design * Added translation elements * Update * Update * Update * Fix css issue * Update --- .../edit-pane/DashboardAddPane.tsx | 75 ++++++++++++++++++ .../edit-pane/DashboardEditPane.tsx | 52 ++++++++++++- .../scene/NavToolbarActions.tsx | 77 ------------------- public/locales/en-US/grafana.json | 11 ++- public/locales/pseudo-LOCALE/grafana.json | 11 ++- 5 files changed, 144 insertions(+), 82 deletions(-) create mode 100644 public/app/features/dashboard-scene/edit-pane/DashboardAddPane.tsx diff --git a/public/app/features/dashboard-scene/edit-pane/DashboardAddPane.tsx b/public/app/features/dashboard-scene/edit-pane/DashboardAddPane.tsx new file mode 100644 index 00000000000..f58103b09c9 --- /dev/null +++ b/public/app/features/dashboard-scene/edit-pane/DashboardAddPane.tsx @@ -0,0 +1,75 @@ +import { selectors } from '@grafana/e2e-selectors'; +import { Box, Card, Icon } from '@grafana/ui'; +import { t, Trans } from 'app/core/internationalization'; + +import { DashboardInteractions } from '../utils/interactions'; +import { getDashboardSceneFor } from '../utils/utils'; + +import { DashboardEditPane } from './DashboardEditPane'; + +export interface Props { + editPane: DashboardEditPane; +} + +export function DashboardAddPane({ editPane }: Props) { + const dashboard = getDashboardSceneFor(editPane); + + return ( + + dashboard.onCreateNewPanel()} + data-testid={selectors.components.PageToolbar.itemButton('add_visualization')} + title={t('dashboard.toolbar.add-panel-description', 'A container for visualizations and other content')} + > + + Panel + + + + + + { + dashboard.onShowAddLibraryPanelDrawer(); + DashboardInteractions.toolbarAddButtonClicked({ item: 'add_library_panel' }); + }} + data-testid={selectors.pages.AddDashboard.itemButton('Add new panel from panel library menu item')} + title={t( + 'dashboard.toolbar.libray-panel-description', + 'Libray panels allow you share and reuse panels between dashboards' + )} + > + + Import library panel + + + + + + dashboard.onCreateNewRow()} + data-testid={selectors.components.PageToolbar.itemButton('add_row')} + title={t('dashboard.toolbar.row-description', 'A group of panels with an optional header')} + > + + Row + + + + + + dashboard.onCreateNewTab()} + data-testid={selectors.components.PageToolbar.itemButton('add_tab')} + title={t('dashboard.toolbar.tabs-description', 'Break up your dashboard into different horizontal tabs')} + > + + Tab + + + + + + + ); +} diff --git a/public/app/features/dashboard-scene/edit-pane/DashboardEditPane.tsx b/public/app/features/dashboard-scene/edit-pane/DashboardEditPane.tsx index e2e2b091420..fbaf5aa5260 100644 --- a/public/app/features/dashboard-scene/edit-pane/DashboardEditPane.tsx +++ b/public/app/features/dashboard-scene/edit-pane/DashboardEditPane.tsx @@ -4,12 +4,20 @@ import { useEffect, useRef } from 'react'; import { GrafanaTheme2 } from '@grafana/data'; import { SceneObjectState, SceneObjectBase, SceneObject, sceneGraph, useSceneObjectState } from '@grafana/scenes'; -import { ElementSelectionContextItem, ElementSelectionContextState, ToolbarButton, useStyles2 } from '@grafana/ui'; +import { + ElementSelectionContextItem, + ElementSelectionContextState, + Tab, + TabsBar, + ToolbarButton, + useStyles2, +} from '@grafana/ui'; import { t } from 'app/core/internationalization'; import { isInCloneChain } from '../utils/clone'; import { getDashboardSceneFor } from '../utils/utils'; +import { DashboardAddPane } from './DashboardAddPane'; import { ElementEditPane } from './ElementEditPane'; import { ElementSelection } from './ElementSelection'; import { useEditableElement } from './useEditableElement'; @@ -17,8 +25,11 @@ import { useEditableElement } from './useEditableElement'; export interface DashboardEditPaneState extends SceneObjectState { selection?: ElementSelection; selectionContext: ElementSelectionContextState; + tab?: EditPaneTab; } +export type EditPaneTab = 'add' | 'configure' | 'outline'; + export class DashboardEditPane extends SceneObjectBase { public constructor() { super({ @@ -122,6 +133,10 @@ export class DashboardEditPane extends SceneObjectBase { }, }); } + + public onChangeTab = (tab: EditPaneTab) => { + this.setState({ tab }); + }; } export interface Props { @@ -157,7 +172,7 @@ export function DashboardEditPaneRenderer({ editPane, isCollapsed, onToggleColla } }, [editPane, isCollapsed]); - const { selection } = useSceneObjectState(editPane, { shouldActivateOrKeepAlive: true }); + const { selection, tab = 'configure' } = useSceneObjectState(editPane, { shouldActivateOrKeepAlive: true }); const styles = useStyles2(getStyles); const paneRef = useRef(null); const editableElement = useEditableElement(selection); @@ -191,7 +206,28 @@ export function DashboardEditPaneRenderer({ editPane, isCollapsed, onToggleColla return (
- + + editPane.onChangeTab('add')} + /> + editPane.onChangeTab('configure')} + /> + editPane.onChangeTab('outline')} + /> + +
+ {tab === 'add' && } + {tab === 'configure' && } + {tab === 'outline' &&
} +
); } @@ -202,11 +238,21 @@ function getStyles(theme: GrafanaTheme2) { display: 'flex', flexDirection: 'column', flex: '1 1 0', + }), + tabContent: css({ + display: 'flex', + flex: '1 1 0', + flexDirection: 'column', + minHeight: 0, overflow: 'auto', }), rotate180: css({ rotate: '180deg', }), + tabsbar: css({ + padding: theme.spacing(0, 1), + margin: theme.spacing(0.5, 1), + }), expandOptionsWrapper: css({ display: 'flex', flexDirection: 'column', diff --git a/public/app/features/dashboard-scene/scene/NavToolbarActions.tsx b/public/app/features/dashboard-scene/scene/NavToolbarActions.tsx index edda9d01268..d5564aa2814 100644 --- a/public/app/features/dashboard-scene/scene/NavToolbarActions.tsx +++ b/public/app/features/dashboard-scene/scene/NavToolbarActions.tsx @@ -156,83 +156,6 @@ export function ToolbarActions({ dashboard }: Props) { } if (dashboardNewLayouts) { - leftActions.push({ - group: 'add-panel', - condition: isEditingAndShowingDashboard, - render: () => ( - - ), - }); - leftActions.push({ - group: 'add-panel', - condition: isEditingAndShowingDashboard, - render: () => ( - - ), - }); - leftActions.push({ - group: 'add-panel', - condition: isEditingAndShowingDashboard, - render: () => ( - - ), - }); - leftActions.push({ - group: 'add-panel', - condition: isEditingAndShowingDashboard, - render: () => ( - - ), - }); leftActions.push({ group: 'hidden-elements', condition: isEditingAndShowingDashboard, diff --git a/public/locales/en-US/grafana.json b/public/locales/en-US/grafana.json index 0e93bc677a7..7d657396dde 100644 --- a/public/locales/en-US/grafana.json +++ b/public/locales/en-US/grafana.json @@ -1057,6 +1057,11 @@ } } }, + "editpane": { + "add": "Add", + "configure": "Configure", + "outline": "Outline" + }, "empty": { "add-library-panel-body": "Add visualizations that are shared with other dashboards.", "add-library-panel-button": "Add library panel", @@ -1194,7 +1199,8 @@ "toolbar": { "add": "Add", "add-panel": "Panel", - "add-panel-lib": "Import", + "add-panel-description": "A container for visualizations and other content", + "add-panel-lib": "Import library panel", "add-row": "Row", "add-tab": "Tab", "alert-rules": "Alert rules", @@ -1219,6 +1225,7 @@ "label": "Exit edit", "tooltip": "Exits edit mode and discards unsaved changes" }, + "libray-panel-description": "Libray panels allow you share and reuse panels between dashboards", "mark-favorite": "Mark as favorite", "more-save-options": "More save options", "open-original": "Open original dashboard", @@ -1227,6 +1234,7 @@ "playlist-stop": "Stop playlist", "public-dashboard": "Public", "refresh": "Refresh dashboard", + "row-description": "A group of panels with an optional header", "save": "Save dashboard", "save-dashboard": { "label": "Save dashboard", @@ -1246,6 +1254,7 @@ "share-button": "Share", "show-hidden-elements": "Show hidden", "switch-old-dashboard": "Switch to old dashboard page", + "tabs-description": "Break up your dashboard into different horizontal tabs", "unlink-library-panel": "Unlink library panel", "unmark-favorite": "Unmark as favorite" }, diff --git a/public/locales/pseudo-LOCALE/grafana.json b/public/locales/pseudo-LOCALE/grafana.json index 583a6e6832f..aa5a7951a0e 100644 --- a/public/locales/pseudo-LOCALE/grafana.json +++ b/public/locales/pseudo-LOCALE/grafana.json @@ -1057,6 +1057,11 @@ } } }, + "editpane": { + "add": "Åđđ", + "configure": "Cőʼnƒįģūřę", + "outline": "Øūŧľįʼnę" + }, "empty": { "add-library-panel-body": "Åđđ vįşūäľįžäŧįőʼnş ŧĥäŧ äřę şĥäřęđ ŵįŧĥ őŧĥęř đäşĥþőäřđş.", "add-library-panel-button": "Åđđ ľįþřäřy päʼnęľ", @@ -1194,7 +1199,8 @@ "toolbar": { "add": "Åđđ", "add-panel": "Päʼnęľ", - "add-panel-lib": "Ĩmpőřŧ", + "add-panel-description": "Å čőʼnŧäįʼnęř ƒőř vįşūäľįžäŧįőʼnş äʼnđ őŧĥęř čőʼnŧęʼnŧ", + "add-panel-lib": "Ĩmpőřŧ ľįþřäřy päʼnęľ", "add-row": "Ŗőŵ", "add-tab": "Ŧäþ", "alert-rules": "Åľęřŧ řūľęş", @@ -1219,6 +1225,7 @@ "label": "Ēχįŧ ęđįŧ", "tooltip": "Ēχįŧş ęđįŧ mőđę äʼnđ đįşčäřđş ūʼnşävęđ čĥäʼnģęş" }, + "libray-panel-description": "Ŀįþřäy päʼnęľş äľľőŵ yőū şĥäřę äʼnđ řęūşę päʼnęľş þęŧŵęęʼn đäşĥþőäřđş", "mark-favorite": "Mäřĸ äş ƒävőřįŧę", "more-save-options": "Mőřę şävę őpŧįőʼnş", "open-original": "Øpęʼn őřįģįʼnäľ đäşĥþőäřđ", @@ -1227,6 +1234,7 @@ "playlist-stop": "Ŝŧőp pľäyľįşŧ", "public-dashboard": "Pūþľįč", "refresh": "Ŗęƒřęşĥ đäşĥþőäřđ", + "row-description": "Å ģřőūp őƒ päʼnęľş ŵįŧĥ äʼn őpŧįőʼnäľ ĥęäđęř", "save": "Ŝävę đäşĥþőäřđ", "save-dashboard": { "label": "Ŝävę đäşĥþőäřđ", @@ -1246,6 +1254,7 @@ "share-button": "Ŝĥäřę", "show-hidden-elements": "Ŝĥőŵ ĥįđđęʼn", "switch-old-dashboard": "Ŝŵįŧčĥ ŧő őľđ đäşĥþőäřđ päģę", + "tabs-description": "ßřęäĸ ūp yőūř đäşĥþőäřđ įʼnŧő đįƒƒęřęʼnŧ ĥőřįžőʼnŧäľ ŧäþş", "unlink-library-panel": "Ůʼnľįʼnĸ ľįþřäřy päʼnęľ", "unmark-favorite": "Ůʼnmäřĸ äş ƒävőřįŧę" }, From 48029e2ed99dd290f88decd250528a5c3d5bc38c Mon Sep 17 00:00:00 2001 From: "grafana-pr-automation[bot]" <140550294+grafana-pr-automation[bot]@users.noreply.github.com> Date: Sun, 23 Feb 2025 02:42:34 +0200 Subject: [PATCH 14/26] I18n: Download translations from Crowdin (#101181) New Crowdin translations by GitHub Action Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- public/locales/de-DE/grafana.json | 35 +++++++++++++++++++++++++++++ public/locales/es-ES/grafana.json | 35 +++++++++++++++++++++++++++++ public/locales/fr-FR/grafana.json | 35 +++++++++++++++++++++++++++++ public/locales/pt-BR/grafana.json | 35 +++++++++++++++++++++++++++++ public/locales/zh-Hans/grafana.json | 35 +++++++++++++++++++++++++++++ 5 files changed, 175 insertions(+) diff --git a/public/locales/de-DE/grafana.json b/public/locales/de-DE/grafana.json index 1c26fc857cc..93ff994ba79 100644 --- a/public/locales/de-DE/grafana.json +++ b/public/locales/de-DE/grafana.json @@ -528,6 +528,19 @@ }, "pause": { "label": "" + }, + "threshold": { + "recovery": { + "stop-alerting-above": "", + "stop-alerting-bellow": "", + "stop-alerting-equal": "", + "stop-alerting-inside-range": "", + "stop-alerting-less": "", + "stop-alerting-more": "", + "stop-alerting-not-equal": "", + "stop-alerting-outside-range": "", + "title": "" + } } }, "rule-groups": { @@ -810,6 +823,7 @@ "collapse": "", "edit": "", "help": "", + "loading": "", "locale": { "default": "Standard" }, @@ -1043,6 +1057,11 @@ } } }, + "editpane": { + "add": "", + "configure": "", + "outline": "" + }, "empty": { "add-library-panel-body": "Visualisierungen hinzufügen, die mit anderen Dashboards geteilt werden.", "add-library-panel-button": "Bibliotheksfenster hinzufügen", @@ -1180,6 +1199,7 @@ "toolbar": { "add": "Hinzufügen", "add-panel": "", + "add-panel-description": "", "add-panel-lib": "", "add-row": "", "add-tab": "", @@ -1205,6 +1225,7 @@ "label": "", "tooltip": "" }, + "libray-panel-description": "", "mark-favorite": "Als Favorit markieren", "more-save-options": "", "open-original": "Original-Dashboard öffnen", @@ -1213,6 +1234,7 @@ "playlist-stop": "Wiedergabeliste stoppen", "public-dashboard": "", "refresh": "Dashboard aktualisieren", + "row-description": "", "save": "Dashboard speichern", "save-dashboard": { "label": "", @@ -1232,6 +1254,7 @@ "share-button": "Teilen", "show-hidden-elements": "", "switch-old-dashboard": "", + "tabs-description": "", "unlink-library-panel": "", "unmark-favorite": "Markierung als Favorit entfernen" }, @@ -1642,6 +1665,10 @@ "incomplete-request-error": "", "send-custom-feedback": "" }, + "get-enterprise": { + "requires-license": "", + "title": "" + }, "grafana-ui": { "action-editor": { "button": { @@ -2045,6 +2072,7 @@ "render-image-error-description": "" } }, + "lock-icon": "", "login": { "divider": { "connecting-text": "" @@ -2839,6 +2867,7 @@ }, "plugins": { "catalog": { + "no-updates-available": "", "update-all": { "all-plugins-updated": "", "available-header": "", @@ -2898,6 +2927,12 @@ "empty-state": { "message": "" }, + "filter": { + "disabled": "", + "sort": "", + "sort-list": "", + "state": "" + }, "plugin-help": { "error": "", "not-found": "" diff --git a/public/locales/es-ES/grafana.json b/public/locales/es-ES/grafana.json index aead0c584e5..a7f1f3424f4 100644 --- a/public/locales/es-ES/grafana.json +++ b/public/locales/es-ES/grafana.json @@ -528,6 +528,19 @@ }, "pause": { "label": "" + }, + "threshold": { + "recovery": { + "stop-alerting-above": "", + "stop-alerting-bellow": "", + "stop-alerting-equal": "", + "stop-alerting-inside-range": "", + "stop-alerting-less": "", + "stop-alerting-more": "", + "stop-alerting-not-equal": "", + "stop-alerting-outside-range": "", + "title": "" + } } }, "rule-groups": { @@ -810,6 +823,7 @@ "collapse": "", "edit": "", "help": "", + "loading": "", "locale": { "default": "Por defecto" }, @@ -1043,6 +1057,11 @@ } } }, + "editpane": { + "add": "", + "configure": "", + "outline": "" + }, "empty": { "add-library-panel-body": "Añadir las visualizaciones que se comparten con otros tableros.", "add-library-panel-button": "Añadir panel de biblioteca", @@ -1180,6 +1199,7 @@ "toolbar": { "add": "Añadir", "add-panel": "", + "add-panel-description": "", "add-panel-lib": "", "add-row": "", "add-tab": "", @@ -1205,6 +1225,7 @@ "label": "", "tooltip": "" }, + "libray-panel-description": "", "mark-favorite": "Marcar como favorito", "more-save-options": "", "open-original": "Abrir el panel de control original", @@ -1213,6 +1234,7 @@ "playlist-stop": "Detener la lista de reproducción", "public-dashboard": "", "refresh": "Actualizar panel de control", + "row-description": "", "save": "Guardar panel de control", "save-dashboard": { "label": "", @@ -1232,6 +1254,7 @@ "share-button": "Compartir", "show-hidden-elements": "", "switch-old-dashboard": "", + "tabs-description": "", "unlink-library-panel": "", "unmark-favorite": "Deshacer marca como favorito" }, @@ -1642,6 +1665,10 @@ "incomplete-request-error": "", "send-custom-feedback": "" }, + "get-enterprise": { + "requires-license": "", + "title": "" + }, "grafana-ui": { "action-editor": { "button": { @@ -2045,6 +2072,7 @@ "render-image-error-description": "" } }, + "lock-icon": "", "login": { "divider": { "connecting-text": "" @@ -2839,6 +2867,7 @@ }, "plugins": { "catalog": { + "no-updates-available": "", "update-all": { "all-plugins-updated": "", "available-header": "", @@ -2898,6 +2927,12 @@ "empty-state": { "message": "" }, + "filter": { + "disabled": "", + "sort": "", + "sort-list": "", + "state": "" + }, "plugin-help": { "error": "", "not-found": "" diff --git a/public/locales/fr-FR/grafana.json b/public/locales/fr-FR/grafana.json index d7bc91e81db..b7d309f71b5 100644 --- a/public/locales/fr-FR/grafana.json +++ b/public/locales/fr-FR/grafana.json @@ -528,6 +528,19 @@ }, "pause": { "label": "" + }, + "threshold": { + "recovery": { + "stop-alerting-above": "", + "stop-alerting-bellow": "", + "stop-alerting-equal": "", + "stop-alerting-inside-range": "", + "stop-alerting-less": "", + "stop-alerting-more": "", + "stop-alerting-not-equal": "", + "stop-alerting-outside-range": "", + "title": "" + } } }, "rule-groups": { @@ -810,6 +823,7 @@ "collapse": "", "edit": "", "help": "", + "loading": "", "locale": { "default": "Par défaut" }, @@ -1043,6 +1057,11 @@ } } }, + "editpane": { + "add": "", + "configure": "", + "outline": "" + }, "empty": { "add-library-panel-body": "Ajoutez des visualisations partagées avec d'autres tableaux de bord.", "add-library-panel-button": "Ajouter un panneau Bibliothèque", @@ -1180,6 +1199,7 @@ "toolbar": { "add": "Ajouter", "add-panel": "", + "add-panel-description": "", "add-panel-lib": "", "add-row": "", "add-tab": "", @@ -1205,6 +1225,7 @@ "label": "", "tooltip": "" }, + "libray-panel-description": "", "mark-favorite": "Marquer comme favori", "more-save-options": "", "open-original": "Ouvrir le tableau de bord d'origine", @@ -1213,6 +1234,7 @@ "playlist-stop": "Arrêter la liste de lecture", "public-dashboard": "", "refresh": "Actualiser le tableau de bord", + "row-description": "", "save": "Enregistrer le tableau de bord", "save-dashboard": { "label": "", @@ -1232,6 +1254,7 @@ "share-button": "Partager", "show-hidden-elements": "", "switch-old-dashboard": "", + "tabs-description": "", "unlink-library-panel": "", "unmark-favorite": "Supprimer des favoris" }, @@ -1642,6 +1665,10 @@ "incomplete-request-error": "", "send-custom-feedback": "" }, + "get-enterprise": { + "requires-license": "", + "title": "" + }, "grafana-ui": { "action-editor": { "button": { @@ -2045,6 +2072,7 @@ "render-image-error-description": "" } }, + "lock-icon": "", "login": { "divider": { "connecting-text": "" @@ -2839,6 +2867,7 @@ }, "plugins": { "catalog": { + "no-updates-available": "", "update-all": { "all-plugins-updated": "", "available-header": "", @@ -2898,6 +2927,12 @@ "empty-state": { "message": "" }, + "filter": { + "disabled": "", + "sort": "", + "sort-list": "", + "state": "" + }, "plugin-help": { "error": "", "not-found": "" diff --git a/public/locales/pt-BR/grafana.json b/public/locales/pt-BR/grafana.json index d9df30cae3b..32cddf19af3 100644 --- a/public/locales/pt-BR/grafana.json +++ b/public/locales/pt-BR/grafana.json @@ -528,6 +528,19 @@ }, "pause": { "label": "" + }, + "threshold": { + "recovery": { + "stop-alerting-above": "", + "stop-alerting-bellow": "", + "stop-alerting-equal": "", + "stop-alerting-inside-range": "", + "stop-alerting-less": "", + "stop-alerting-more": "", + "stop-alerting-not-equal": "", + "stop-alerting-outside-range": "", + "title": "" + } } }, "rule-groups": { @@ -810,6 +823,7 @@ "collapse": "", "edit": "", "help": "", + "loading": "", "locale": { "default": "Padrão" }, @@ -1043,6 +1057,11 @@ } } }, + "editpane": { + "add": "", + "configure": "", + "outline": "" + }, "empty": { "add-library-panel-body": "Adicione visualizações que são compartilhadas com outros painéis de controle.", "add-library-panel-button": "Adicionar painel de biblioteca", @@ -1180,6 +1199,7 @@ "toolbar": { "add": "Adicionar", "add-panel": "", + "add-panel-description": "", "add-panel-lib": "", "add-row": "", "add-tab": "", @@ -1205,6 +1225,7 @@ "label": "", "tooltip": "" }, + "libray-panel-description": "", "mark-favorite": "Marcar como favorito", "more-save-options": "", "open-original": "Abrir painel de controle original", @@ -1213,6 +1234,7 @@ "playlist-stop": "Parar lista de reprodução", "public-dashboard": "", "refresh": "Atualizar painel de controle", + "row-description": "", "save": "Salvar painel de controle", "save-dashboard": { "label": "", @@ -1232,6 +1254,7 @@ "share-button": "Compartilhar", "show-hidden-elements": "", "switch-old-dashboard": "", + "tabs-description": "", "unlink-library-panel": "", "unmark-favorite": "Desmarcar como favorito" }, @@ -1642,6 +1665,10 @@ "incomplete-request-error": "", "send-custom-feedback": "" }, + "get-enterprise": { + "requires-license": "", + "title": "" + }, "grafana-ui": { "action-editor": { "button": { @@ -2045,6 +2072,7 @@ "render-image-error-description": "" } }, + "lock-icon": "", "login": { "divider": { "connecting-text": "" @@ -2839,6 +2867,7 @@ }, "plugins": { "catalog": { + "no-updates-available": "", "update-all": { "all-plugins-updated": "", "available-header": "", @@ -2898,6 +2927,12 @@ "empty-state": { "message": "" }, + "filter": { + "disabled": "", + "sort": "", + "sort-list": "", + "state": "" + }, "plugin-help": { "error": "", "not-found": "" diff --git a/public/locales/zh-Hans/grafana.json b/public/locales/zh-Hans/grafana.json index 441b75cca69..f51ef704205 100644 --- a/public/locales/zh-Hans/grafana.json +++ b/public/locales/zh-Hans/grafana.json @@ -524,6 +524,19 @@ }, "pause": { "label": "" + }, + "threshold": { + "recovery": { + "stop-alerting-above": "", + "stop-alerting-bellow": "", + "stop-alerting-equal": "", + "stop-alerting-inside-range": "", + "stop-alerting-less": "", + "stop-alerting-more": "", + "stop-alerting-not-equal": "", + "stop-alerting-outside-range": "", + "title": "" + } } }, "rule-groups": { @@ -801,6 +814,7 @@ "collapse": "", "edit": "", "help": "", + "loading": "", "locale": { "default": "默认" }, @@ -1034,6 +1048,11 @@ } } }, + "editpane": { + "add": "", + "configure": "", + "outline": "" + }, "empty": { "add-library-panel-body": "添加与其他仪表板共享的可视化。", "add-library-panel-button": "添加库面板", @@ -1171,6 +1190,7 @@ "toolbar": { "add": "添加", "add-panel": "", + "add-panel-description": "", "add-panel-lib": "", "add-row": "", "add-tab": "", @@ -1196,6 +1216,7 @@ "label": "", "tooltip": "" }, + "libray-panel-description": "", "mark-favorite": "标记为收藏", "more-save-options": "", "open-original": "打开原始仪表板", @@ -1204,6 +1225,7 @@ "playlist-stop": "停止播放列表", "public-dashboard": "", "refresh": "刷新仪表板", + "row-description": "", "save": "保存仪表板", "save-dashboard": { "label": "", @@ -1223,6 +1245,7 @@ "share-button": "分享", "show-hidden-elements": "", "switch-old-dashboard": "", + "tabs-description": "", "unlink-library-panel": "", "unmark-favorite": "取消标记为收藏" }, @@ -1633,6 +1656,10 @@ "incomplete-request-error": "", "send-custom-feedback": "" }, + "get-enterprise": { + "requires-license": "", + "title": "" + }, "grafana-ui": { "action-editor": { "button": { @@ -2035,6 +2062,7 @@ "render-image-error-description": "" } }, + "lock-icon": "", "login": { "divider": { "connecting-text": "" @@ -2829,6 +2857,7 @@ }, "plugins": { "catalog": { + "no-updates-available": "", "update-all": { "all-plugins-updated": "", "available-header": "", @@ -2888,6 +2917,12 @@ "empty-state": { "message": "" }, + "filter": { + "disabled": "", + "sort": "", + "sort-list": "", + "state": "" + }, "plugin-help": { "error": "", "not-found": "" From 8f9972a509544bf724a3cacfb15e2a435654b18e Mon Sep 17 00:00:00 2001 From: Domas Date: Mon, 24 Feb 2025 07:00:18 +0200 Subject: [PATCH 15/26] Histogram: Handle multiple native histograms (#98404) Co-authored-by: Leon Sorokin --- .../transformers/histogram.test.ts | 117 +++++++++++++++++- .../transformations/transformers/histogram.ts | 61 +++++++++ .../panel/histogram/HistogramPanel.tsx | 18 ++- 3 files changed, 188 insertions(+), 8 deletions(-) diff --git a/packages/grafana-data/src/transformations/transformers/histogram.test.ts b/packages/grafana-data/src/transformations/transformers/histogram.test.ts index 7bbd03397a0..f8aa3f9c991 100644 --- a/packages/grafana-data/src/transformations/transformers/histogram.test.ts +++ b/packages/grafana-data/src/transformations/transformers/histogram.test.ts @@ -1,8 +1,14 @@ import { toDataFrame } from '../../dataframe/processDataFrame'; -import { FieldType } from '../../types/dataFrame'; +import { Field, FieldType } from '../../types/dataFrame'; import { mockTransformationsRegistry } from '../../utils/tests/mockTransformationsRegistry'; -import { histogramTransformer, buildHistogram, histogramFieldsToFrame } from './histogram'; +import { + histogramTransformer, + buildHistogram, + histogramFieldsToFrame, + HistogramFields, + joinHistograms, +} from './histogram'; describe('histogram frames frames', () => { beforeAll(() => { @@ -280,3 +286,110 @@ describe('histogram frames frames', () => { `); }); }); + +describe('joinHistograms', () => { + type TestHistogram = { + xMin: number[]; + xMax: number[]; + counts: number[][]; + }; + + function toField(name: string, values: number[]): Field { + return { + config: {}, + name, + type: FieldType.number, + values, + }; + } + + function testHistogramToHistogram(test: TestHistogram): HistogramFields { + return { + xMin: toField('xMin', test.xMin), + xMax: toField('xMax', test.xMax), + counts: test.counts.map((values) => toField(`count`, values)), + }; + } + + type TestCase = { + name: string; + histograms: TestHistogram[]; + expected: TestHistogram; + }; + + const testCases: TestCase[] = [ + { + name: 'just one histogram', + histograms: [ + { + xMin: [1, 2, 3], + xMax: [2, 3, 4], + counts: [[1, 2, 3]], + }, + ], + expected: { + xMin: [1, 2, 3], + xMax: [2, 3, 4], + counts: [[1, 2, 3]], + }, + }, + { + name: 'two histograms with same bucket sizes', + histograms: [ + { + xMin: [1, 3, 4], + xMax: [2, 4, 5], + counts: [[1, 2, 3]], + }, + { + xMin: [1, 3, 4], + xMax: [2, 4, 5], + counts: [[4, 5, 6]], + }, + ], + expected: { + xMin: [1, 3, 4], + xMax: [2, 4, 5], + counts: [ + [1, 2, 3], + [4, 5, 6], + ], + }, + }, + { + name: 'two histograms with same bucket sizes but counts in some different buckets', + histograms: [ + { + xMin: [1, 3, 4], + xMax: [2, 4, 5], + counts: [[1, 2, 3]], + }, + { + xMin: [2, 3, 6], + xMax: [3, 4, 7], + counts: [[4, 5, 6]], + }, + ], + expected: { + xMin: [1, 2, 3, 4, 6], + xMax: [2, 3, 4, 5, 7], + counts: [ + [1, 0, 2, 3, 0], + [0, 4, 5, 0, 6], + ], + }, + }, + ]; + + testCases.forEach((tc) => { + it(tc.name, () => { + const result = joinHistograms(tc.histograms.map(testHistogramToHistogram)); + + expect({ + xMin: result.xMin.values, + xMax: result.xMax.values, + counts: result.counts.map((f) => f.values), + }).toEqual(tc.expected); + }); + }); +}); diff --git a/packages/grafana-data/src/transformations/transformers/histogram.ts b/packages/grafana-data/src/transformations/transformers/histogram.ts index 7e5def97a2f..00549ad07ae 100644 --- a/packages/grafana-data/src/transformations/transformers/histogram.ts +++ b/packages/grafana-data/src/transformations/transformers/histogram.ts @@ -1,5 +1,6 @@ import { map } from 'rxjs/operators'; +import { outerJoinDataFrames } from '../..'; import { getDisplayProcessor } from '../../field/displayProcessor'; import { createTheme } from '../../themes/createTheme'; import { GrafanaTheme2 } from '../../themes/types'; @@ -588,3 +589,63 @@ export function histogramFieldsToFrame(info: HistogramFields, theme?: GrafanaThe refId: `${DataTransformerID.histogram}`, }; } + +/** + * + * Join multiple histograms into a histogram with multiple counts. + * Useful eg if you want to overlay them for comparison. + * + * This is needed because histogram results from database + * will have buckets omitted for 0 counts, but when joining multiple histograms + * we need to fill in the 0 values for missing buckets. + * + * Returns field configs of the first provided histogram. + * @alpha + */ + +export function joinHistograms(histograms: HistogramFields[]): HistogramFields { + if (histograms.length === 1) { + return histograms[0]; + } + + let joined = outerJoinDataFrames({ + frames: histograms.map((h) => ({ + length: h.xMax.values.length, + fields: [h.xMax, h.xMin, ...h.counts], + })), + joinBy: (field) => field.name === 'xMax', + })!; + + let xMaxField: Field | null = null; + let xMinField: Field | null = null; + let countFields: Field[] = []; + + // merge all xMin fields into first xMin field + // and default all count fields to 0 + joined.fields.forEach((f) => { + if (f.name === 'xMax') { + xMaxField = f; + } else if (f.name === 'xMin') { + if (xMinField == null) { + xMinField = f; + } else { + for (let i = 0; i < f.values.length; i++) { + xMinField.values[i] ??= f.values[i]; + } + } + } else { + countFields.push({ + ...f, + values: f.values.map((v) => v ?? 0), + }); + } + }); + + const result: HistogramFields = { + xMin: xMinField!, + xMax: xMaxField!, + counts: countFields, + }; + + return result; +} diff --git a/public/app/plugins/panel/histogram/HistogramPanel.tsx b/public/app/plugins/panel/histogram/HistogramPanel.tsx index 2c2805069a8..03938736458 100644 --- a/public/app/plugins/panel/histogram/HistogramPanel.tsx +++ b/public/app/plugins/panel/histogram/HistogramPanel.tsx @@ -1,7 +1,7 @@ import { useMemo } from 'react'; -import { PanelProps, buildHistogram, cacheFieldDisplayNames, getHistogramFields } from '@grafana/data'; -import { histogramFieldsToFrame } from '@grafana/data/src/transformations/transformers/histogram'; +import { DataFrameType, PanelProps, buildHistogram, cacheFieldDisplayNames, getHistogramFields } from '@grafana/data'; +import { histogramFieldsToFrame, joinHistograms } from '@grafana/data/src/transformations/transformers/histogram'; import { TooltipDisplayMode, TooltipPlugin2, useTheme2 } from '@grafana/ui'; import { TooltipHoverMode } from '@grafana/ui/src/components/uPlot/plugins/TooltipPlugin2'; @@ -34,10 +34,16 @@ export const HistogramPanel = ({ data, options, width, height }: Props) => { cacheFieldDisplayNames(data.series); - if (data.series.length === 1) { - const info = getHistogramFields(data.series[0]); - if (info) { - return histogramFieldsToFrame(info); + if ( + data.series.length === 1 || + data.series.every( + (frame) => frame.meta?.type === DataFrameType.HeatmapCells || frame.meta?.type === DataFrameType.HeatmapRows + ) + ) { + const histograms = data.series.map((frame) => getHistogramFields(frame)).filter((hist) => hist != null); + + if (histograms.length) { + return histogramFieldsToFrame(joinHistograms(histograms), theme); } } const hist = buildHistogram(data.series, options); From 279b6414693d241510af0f1b680d6a2cbbc8071e Mon Sep 17 00:00:00 2001 From: Mariell Hoversholm Date: Mon, 24 Feb 2025 09:08:58 +0100 Subject: [PATCH 16/26] Provisioning: Define large parts of our infrastructure (#101029) * Provisioning: Define secrets service * Provisioning: Create and store secrets service * Provisioning: Define safepath * Provisioning: Define the repository * Identity: Support a provisioning service * Provisioning: Define a job queue * Chore: Regen code * Provisioning: Show progress more often Co-Authored-By: Ryan McKinley * Provisioning: Rename hash field to lastRef Co-Authored-By: =?UTF-8?q?Roberto=20Jim=C3=A9nez=20S=C3=A1nchez?= * Provisioning: Workflows as write access Co-Authored-By: Ryan McKinley * Provisioning: Regen OpenAPI snapshot * Provisioning: Update tests to match new fields --------- Co-authored-by: Ryan McKinley Co-authored-by: =?UTF-8?q?Roberto=20Jim=C3=A9nez=20S=C3=A1nchez?= --- pkg/apis/provisioning/v0alpha1/jobs.go | 2 +- pkg/apis/provisioning/v0alpha1/types.go | 26 +- .../v0alpha1/zz_generated.deepcopy.go | 18 +- .../v0alpha1/zz_generated.openapi.go | 54 +- ...enerated.openapi_violation_exceptions.list | 2 +- .../v0alpha1/githubrepositoryconfig.go | 34 +- .../provisioning/v0alpha1/repositoryspec.go | 14 +- .../provisioning/v0alpha1/syncstatus.go | 10 +- .../apis/provisioning/jobs/progress.go | 203 +++++ pkg/registry/apis/provisioning/jobs/queue.go | 47 ++ pkg/registry/apis/provisioning/jobs/store.go | 367 +++++++++ .../apis/provisioning/jobs/watchset.go | 379 +++++++++ pkg/registry/apis/provisioning/register.go | 45 +- .../provisioning/repository/repository.go | 128 ++++ .../apis/provisioning/safepath/path.go | 59 ++ .../apis/provisioning/safepath/path_test.go | 74 ++ .../apis/provisioning/safepath/walk.go | 31 + .../apis/provisioning/secrets/secret.go | 35 + .../provisioning.grafana.app-v0alpha1.json | 721 +++++++++++++++++- .../provisioning/testdata/github-example.json | 3 +- .../provisioning/testdata/local-devenv.json | 2 +- 21 files changed, 2120 insertions(+), 134 deletions(-) create mode 100644 pkg/registry/apis/provisioning/jobs/progress.go create mode 100644 pkg/registry/apis/provisioning/jobs/queue.go create mode 100644 pkg/registry/apis/provisioning/jobs/store.go create mode 100644 pkg/registry/apis/provisioning/jobs/watchset.go create mode 100644 pkg/registry/apis/provisioning/repository/repository.go create mode 100644 pkg/registry/apis/provisioning/safepath/path.go create mode 100644 pkg/registry/apis/provisioning/safepath/path_test.go create mode 100644 pkg/registry/apis/provisioning/safepath/walk.go create mode 100644 pkg/registry/apis/provisioning/secrets/secret.go diff --git a/pkg/apis/provisioning/v0alpha1/jobs.go b/pkg/apis/provisioning/v0alpha1/jobs.go index c41e67eefa2..0d35a509420 100644 --- a/pkg/apis/provisioning/v0alpha1/jobs.go +++ b/pkg/apis/provisioning/v0alpha1/jobs.go @@ -119,7 +119,7 @@ type JobStatus struct { Progress float64 `json:"progress,omitempty"` // Summary of processed actions - Summary []JobResourceSummary `json:"summary,omitempty"` + Summary []*JobResourceSummary `json:"summary,omitempty"` } type JobResourceSummary struct { diff --git a/pkg/apis/provisioning/v0alpha1/types.go b/pkg/apis/provisioning/v0alpha1/types.go index 166ddddea6b..77514c920b9 100644 --- a/pkg/apis/provisioning/v0alpha1/types.go +++ b/pkg/apis/provisioning/v0alpha1/types.go @@ -28,10 +28,10 @@ type LocalRepositoryConfig struct { type Workflow string const ( + // WriteWorkflow allows a user to write directly to the repository + WriteWorkflow Workflow = "write" // BranchWorkflow creates a branch for changes BranchWorkflow Workflow = "branch" - // PushWorkflow pushes changes directly the configured branch - PushWorkflow Workflow = "push" ) type GitHubRepositoryConfig struct { @@ -39,23 +39,13 @@ type GitHubRepositoryConfig struct { URL string `json:"url,omitempty"` // The branch to use in the repository. - // By default, this is the main branch. - Branch string `json:"branch,omitempty"` + Branch string `json:"branch"` // Token for accessing the repository. If set, it will be encrypted into encryptedToken, then set to an empty string again. Token string `json:"token,omitempty"` // Token for accessing the repository, but encrypted. This is not possible to read back to a user decrypted. // +listType=atomic EncryptedToken []byte `json:"encryptedToken,omitempty"` - // Workflow allowed for changes to the repository. - // The order is relevant for defining the precedence of the workflows. - // Possible values: pull-request, branch, push. - Workflows []Workflow `json:"workflows,omitempty"` - - // Whether we should commit to change branches and use a Pull Request flow to achieve this. - // By default, this is false (i.e. we will commit straight to the main branch). - BranchWorkflow bool `json:"branchWorkflow,omitempty"` - // Whether we should show dashboard previews for pull requests. // By default, this is false (i.e. we will not create previews). GenerateDashboardPreviews bool `json:"generateDashboardPreviews,omitempty"` @@ -78,8 +68,10 @@ type RepositorySpec struct { // Repository description Description string `json:"description,omitempty"` - // ReadOnly repository does not allow any write commands - ReadOnly bool `json:"readOnly"` + // UI driven Workflow that allow changes to the contends of the repository. + // The order is relevant for defining the precedence of the workflows. + // When empty, the repository does not support any edits (eg, readonly) + Workflows []Workflow `json:"workflows"` // Sync settings -- how values are pulled from the repository into grafana Sync SyncOptions `json:"sync"` @@ -183,8 +175,8 @@ type SyncStatus struct { // +listType=atomic Message []string `json:"message"` - // The repository hash when the last sync ran - Hash string `json:"hash,omitempty"` + // The repository ref when the last successful sync ran + LastRef string `json:"lastRef,omitempty"` // Incremental synchronization for versioned repositories Incremental bool `json:"incremental,omitempty"` diff --git a/pkg/apis/provisioning/v0alpha1/zz_generated.deepcopy.go b/pkg/apis/provisioning/v0alpha1/zz_generated.deepcopy.go index 9d259f876c0..48d39f845b8 100644 --- a/pkg/apis/provisioning/v0alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/provisioning/v0alpha1/zz_generated.deepcopy.go @@ -98,11 +98,6 @@ func (in *GitHubRepositoryConfig) DeepCopyInto(out *GitHubRepositoryConfig) { *out = make([]byte, len(*in)) copy(*out, *in) } - if in.Workflows != nil { - in, out := &in.Workflows, &out.Workflows - *out = make([]Workflow, len(*in)) - copy(*out, *in) - } return } @@ -314,9 +309,13 @@ func (in *JobStatus) DeepCopyInto(out *JobStatus) { } if in.Summary != nil { in, out := &in.Summary, &out.Summary - *out = make([]JobResourceSummary, len(*in)) + *out = make([]*JobResourceSummary, len(*in)) for i := range *in { - (*in)[i].DeepCopyInto(&(*out)[i]) + if (*in)[i] != nil { + in, out := &(*in)[i], &(*out)[i] + *out = new(JobResourceSummary) + (*in).DeepCopyInto(*out) + } } } return @@ -444,6 +443,11 @@ func (in *RepositoryList) DeepCopyObject() runtime.Object { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *RepositorySpec) DeepCopyInto(out *RepositorySpec) { *out = *in + if in.Workflows != nil { + in, out := &in.Workflows, &out.Workflows + *out = make([]Workflow, len(*in)) + copy(*out, *in) + } out.Sync = in.Sync if in.Local != nil { in, out := &in.Local, &out.Local diff --git a/pkg/apis/provisioning/v0alpha1/zz_generated.openapi.go b/pkg/apis/provisioning/v0alpha1/zz_generated.openapi.go index fe65c556b4f..e84dd52a8d0 100644 --- a/pkg/apis/provisioning/v0alpha1/zz_generated.openapi.go +++ b/pkg/apis/provisioning/v0alpha1/zz_generated.openapi.go @@ -246,7 +246,8 @@ func schema_pkg_apis_provisioning_v0alpha1_GitHubRepositoryConfig(ref common.Ref }, "branch": { SchemaProps: spec.SchemaProps{ - Description: "The branch to use in the repository. By default, this is the main branch.", + Description: "The branch to use in the repository.", + Default: "", Type: []string{"string"}, Format: "", }, @@ -270,29 +271,6 @@ func schema_pkg_apis_provisioning_v0alpha1_GitHubRepositoryConfig(ref common.Ref Format: "byte", }, }, - "workflows": { - SchemaProps: spec.SchemaProps{ - Description: "Workflow allowed for changes to the repository. The order is relevant for defining the precedence of the workflows. Possible values: pull-request, branch, push.", - Type: []string{"array"}, - Items: &spec.SchemaOrArray{ - Schema: &spec.Schema{ - SchemaProps: spec.SchemaProps{ - Default: "", - Type: []string{"string"}, - Format: "", - Enum: []interface{}{"branch", "push"}, - }, - }, - }, - }, - }, - "branchWorkflow": { - SchemaProps: spec.SchemaProps{ - Description: "Whether we should commit to change branches and use a Pull Request flow to achieve this. By default, this is false (i.e. we will commit straight to the main branch).", - Type: []string{"boolean"}, - Format: "", - }, - }, "generateDashboardPreviews": { SchemaProps: spec.SchemaProps{ Description: "Whether we should show dashboard previews for pull requests. By default, this is false (i.e. we will not create previews).", @@ -301,6 +279,7 @@ func schema_pkg_apis_provisioning_v0alpha1_GitHubRepositoryConfig(ref common.Ref }, }, }, + Required: []string{"branch"}, }, }, } @@ -749,8 +728,7 @@ func schema_pkg_apis_provisioning_v0alpha1_JobStatus(ref common.ReferenceCallbac Items: &spec.SchemaOrArray{ Schema: &spec.Schema{ SchemaProps: spec.SchemaProps{ - Default: map[string]interface{}{}, - Ref: ref("github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1.JobResourceSummary"), + Ref: ref("github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1.JobResourceSummary"), }, }, }, @@ -977,12 +955,20 @@ func schema_pkg_apis_provisioning_v0alpha1_RepositorySpec(ref common.ReferenceCa Format: "", }, }, - "readOnly": { + "workflows": { SchemaProps: spec.SchemaProps{ - Description: "ReadOnly repository does not allow any write commands", - Default: false, - Type: []string{"boolean"}, - Format: "", + Description: "UI driven Workflow that allow changes to the contends of the repository. The order is relevant for defining the precedence of the workflows. When empty, the repository does not support any edits (eg, readonly)", + Type: []string{"array"}, + Items: &spec.SchemaOrArray{ + Schema: &spec.Schema{ + SchemaProps: spec.SchemaProps{ + Default: "", + Type: []string{"string"}, + Format: "", + Enum: []interface{}{"branch", "write"}, + }, + }, + }, }, }, "sync": { @@ -1014,7 +1000,7 @@ func schema_pkg_apis_provisioning_v0alpha1_RepositorySpec(ref common.ReferenceCa }, }, }, - Required: []string{"title", "readOnly", "sync", "type"}, + Required: []string{"title", "workflows", "sync", "type"}, }, }, Dependencies: []string{ @@ -1717,9 +1703,9 @@ func schema_pkg_apis_provisioning_v0alpha1_SyncStatus(ref common.ReferenceCallba }, }, }, - "hash": { + "lastRef": { SchemaProps: spec.SchemaProps{ - Description: "The repository hash when the last sync ran", + Description: "The repository ref when the last successful sync ran", Type: []string{"string"}, Format: "", }, diff --git a/pkg/apis/provisioning/v0alpha1/zz_generated.openapi_violation_exceptions.list b/pkg/apis/provisioning/v0alpha1/zz_generated.openapi_violation_exceptions.list index 313713c794b..696b361ec80 100644 --- a/pkg/apis/provisioning/v0alpha1/zz_generated.openapi_violation_exceptions.list +++ b/pkg/apis/provisioning/v0alpha1/zz_generated.openapi_violation_exceptions.list @@ -1,10 +1,10 @@ API rule violation: list_type_missing,github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1,FileList,Items -API rule violation: list_type_missing,github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1,GitHubRepositoryConfig,Workflows API rule violation: list_type_missing,github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1,HistoryList,Items API rule violation: list_type_missing,github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1,JobResourceSummary,Errors API rule violation: list_type_missing,github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1,JobStatus,Errors API rule violation: list_type_missing,github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1,JobStatus,Summary API rule violation: list_type_missing,github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1,RepositoryList,Items +API rule violation: list_type_missing,github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1,RepositorySpec,Workflows API rule violation: list_type_missing,github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1,RepositoryViewList,Items API rule violation: list_type_missing,github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1,ResourceList,Items API rule violation: list_type_missing,github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1,ResourceStats,Items diff --git a/pkg/generated/applyconfiguration/provisioning/v0alpha1/githubrepositoryconfig.go b/pkg/generated/applyconfiguration/provisioning/v0alpha1/githubrepositoryconfig.go index 065fd444d55..e3cc1f0829c 100644 --- a/pkg/generated/applyconfiguration/provisioning/v0alpha1/githubrepositoryconfig.go +++ b/pkg/generated/applyconfiguration/provisioning/v0alpha1/githubrepositoryconfig.go @@ -4,20 +4,14 @@ package v0alpha1 -import ( - provisioningv0alpha1 "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1" -) - // GitHubRepositoryConfigApplyConfiguration represents a declarative configuration of the GitHubRepositoryConfig type for use // with apply. type GitHubRepositoryConfigApplyConfiguration struct { - URL *string `json:"url,omitempty"` - Branch *string `json:"branch,omitempty"` - Token *string `json:"token,omitempty"` - EncryptedToken []byte `json:"encryptedToken,omitempty"` - Workflows []provisioningv0alpha1.Workflow `json:"workflows,omitempty"` - BranchWorkflow *bool `json:"branchWorkflow,omitempty"` - GenerateDashboardPreviews *bool `json:"generateDashboardPreviews,omitempty"` + URL *string `json:"url,omitempty"` + Branch *string `json:"branch,omitempty"` + Token *string `json:"token,omitempty"` + EncryptedToken []byte `json:"encryptedToken,omitempty"` + GenerateDashboardPreviews *bool `json:"generateDashboardPreviews,omitempty"` } // GitHubRepositoryConfigApplyConfiguration constructs a declarative configuration of the GitHubRepositoryConfig type for use with @@ -60,24 +54,6 @@ func (b *GitHubRepositoryConfigApplyConfiguration) WithEncryptedToken(values ... return b } -// WithWorkflows adds the given value to the Workflows field in the declarative configuration -// and returns the receiver, so that objects can be build by chaining "With" function invocations. -// If called multiple times, values provided by each call will be appended to the Workflows field. -func (b *GitHubRepositoryConfigApplyConfiguration) WithWorkflows(values ...provisioningv0alpha1.Workflow) *GitHubRepositoryConfigApplyConfiguration { - for i := range values { - b.Workflows = append(b.Workflows, values[i]) - } - return b -} - -// WithBranchWorkflow sets the BranchWorkflow field in the declarative configuration to the given value -// and returns the receiver, so that objects can be built by chaining "With" function invocations. -// If called multiple times, the BranchWorkflow field is set to the value of the last call. -func (b *GitHubRepositoryConfigApplyConfiguration) WithBranchWorkflow(value bool) *GitHubRepositoryConfigApplyConfiguration { - b.BranchWorkflow = &value - return b -} - // WithGenerateDashboardPreviews sets the GenerateDashboardPreviews field in the declarative configuration to the given value // and returns the receiver, so that objects can be built by chaining "With" function invocations. // If called multiple times, the GenerateDashboardPreviews field is set to the value of the last call. diff --git a/pkg/generated/applyconfiguration/provisioning/v0alpha1/repositoryspec.go b/pkg/generated/applyconfiguration/provisioning/v0alpha1/repositoryspec.go index b0d4769286d..75508c2003b 100644 --- a/pkg/generated/applyconfiguration/provisioning/v0alpha1/repositoryspec.go +++ b/pkg/generated/applyconfiguration/provisioning/v0alpha1/repositoryspec.go @@ -13,7 +13,7 @@ import ( type RepositorySpecApplyConfiguration struct { Title *string `json:"title,omitempty"` Description *string `json:"description,omitempty"` - ReadOnly *bool `json:"readOnly,omitempty"` + Workflows []provisioningv0alpha1.Workflow `json:"workflows,omitempty"` Sync *SyncOptionsApplyConfiguration `json:"sync,omitempty"` Type *provisioningv0alpha1.RepositoryType `json:"type,omitempty"` Local *LocalRepositoryConfigApplyConfiguration `json:"local,omitempty"` @@ -42,11 +42,13 @@ func (b *RepositorySpecApplyConfiguration) WithDescription(value string) *Reposi return b } -// WithReadOnly sets the ReadOnly field in the declarative configuration to the given value -// and returns the receiver, so that objects can be built by chaining "With" function invocations. -// If called multiple times, the ReadOnly field is set to the value of the last call. -func (b *RepositorySpecApplyConfiguration) WithReadOnly(value bool) *RepositorySpecApplyConfiguration { - b.ReadOnly = &value +// WithWorkflows adds the given value to the Workflows field in the declarative configuration +// and returns the receiver, so that objects can be build by chaining "With" function invocations. +// If called multiple times, values provided by each call will be appended to the Workflows field. +func (b *RepositorySpecApplyConfiguration) WithWorkflows(values ...provisioningv0alpha1.Workflow) *RepositorySpecApplyConfiguration { + for i := range values { + b.Workflows = append(b.Workflows, values[i]) + } return b } diff --git a/pkg/generated/applyconfiguration/provisioning/v0alpha1/syncstatus.go b/pkg/generated/applyconfiguration/provisioning/v0alpha1/syncstatus.go index 9db1d73f054..6b8f5abbac1 100644 --- a/pkg/generated/applyconfiguration/provisioning/v0alpha1/syncstatus.go +++ b/pkg/generated/applyconfiguration/provisioning/v0alpha1/syncstatus.go @@ -17,7 +17,7 @@ type SyncStatusApplyConfiguration struct { Finished *int64 `json:"finished,omitempty"` Scheduled *int64 `json:"scheduled,omitempty"` Message []string `json:"message,omitempty"` - Hash *string `json:"hash,omitempty"` + LastRef *string `json:"lastRef,omitempty"` Incremental *bool `json:"incremental,omitempty"` } @@ -77,11 +77,11 @@ func (b *SyncStatusApplyConfiguration) WithMessage(values ...string) *SyncStatus return b } -// WithHash sets the Hash field in the declarative configuration to the given value +// WithLastRef sets the LastRef field in the declarative configuration to the given value // and returns the receiver, so that objects can be built by chaining "With" function invocations. -// If called multiple times, the Hash field is set to the value of the last call. -func (b *SyncStatusApplyConfiguration) WithHash(value string) *SyncStatusApplyConfiguration { - b.Hash = &value +// If called multiple times, the LastRef field is set to the value of the last call. +func (b *SyncStatusApplyConfiguration) WithLastRef(value string) *SyncStatusApplyConfiguration { + b.LastRef = &value return b } diff --git a/pkg/registry/apis/provisioning/jobs/progress.go b/pkg/registry/apis/provisioning/jobs/progress.go new file mode 100644 index 00000000000..3a38fc902ce --- /dev/null +++ b/pkg/registry/apis/provisioning/jobs/progress.go @@ -0,0 +1,203 @@ +package jobs + +import ( + "context" + "fmt" + "time" + + "github.com/grafana/grafana-app-sdk/logging" + provisioning "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1" + "github.com/grafana/grafana/pkg/registry/apis/provisioning/repository" +) + +// maybeNotifyProgress will only notify if a certain amount of time has passed +// or if the job completed +func maybeNotifyProgress(threshold time.Duration, fn ProgressFn) ProgressFn { + var last time.Time + + return func(ctx context.Context, status provisioning.JobStatus) error { + if status.Finished != 0 || last.IsZero() || time.Since(last) > threshold { + last = time.Now() + return fn(ctx, status) + } + + return nil + } +} + +// FIXME: ProgressRecorder should be initialized in the queue +type JobResourceResult struct { + Name string + Resource string + Group string + Path string + Action repository.FileAction + Error error +} + +type jobProgressRecorder struct { + started time.Time + total int + ref string + message string + resultCount int + errorCount int + errors []string + progressFn ProgressFn + summaries map[string]*provisioning.JobResourceSummary +} + +func newJobProgressRecorder(ProgressFn ProgressFn) JobProgressRecorder { + return &jobProgressRecorder{ + started: time.Now(), + progressFn: maybeNotifyProgress(5*time.Second, ProgressFn), + summaries: make(map[string]*provisioning.JobResourceSummary), + } +} + +func (r *jobProgressRecorder) Record(ctx context.Context, result JobResourceResult) { + r.resultCount++ + + logger := logging.FromContext(ctx).With("path", result.Path, "resource", result.Resource, "group", result.Group, "action", result.Action, "name", result.Name) + if result.Error != nil { + logger.Error("job resource operation failed", "err", result.Error) + if len(r.errors) < 20 { + r.errors = append(r.errors, result.Error.Error()) + } + r.errorCount++ + } else { + logger.Info("job resource operation succeeded") + } + + r.updateSummary(result) + r.notify(ctx) +} + +func (r *jobProgressRecorder) SetMessage(msg string) { + r.message = msg +} + +func (r *jobProgressRecorder) GetMessage() string { + return r.message +} + +func (r *jobProgressRecorder) SetRef(ref string) { + r.ref = ref +} + +func (r *jobProgressRecorder) GetRef() string { + return r.ref +} + +func (r *jobProgressRecorder) SetTotal(total int) { + r.total = total +} + +func (r *jobProgressRecorder) TooManyErrors() error { + if r.errorCount > 20 { + return fmt.Errorf("too many errors: %d", r.errorCount) + } + + return nil +} + +func (r *jobProgressRecorder) summary() []*provisioning.JobResourceSummary { + if len(r.summaries) == 0 { + return nil + } + + summaries := make([]*provisioning.JobResourceSummary, 0, len(r.summaries)) + for _, summary := range r.summaries { + summaries = append(summaries, summary) + } + + return summaries +} + +func (r *jobProgressRecorder) updateSummary(result JobResourceResult) { + key := result.Resource + ":" + result.Group + summary, exists := r.summaries[key] + if !exists { + summary = &provisioning.JobResourceSummary{ + Resource: result.Resource, + Group: result.Group, + } + r.summaries[key] = summary + } + + if result.Error != nil { + summary.Errors = append(summary.Errors, result.Error.Error()) + summary.Error++ + } else { + switch result.Action { + case repository.FileActionDeleted: + summary.Delete++ + case repository.FileActionUpdated: + summary.Update++ + case repository.FileActionCreated: + summary.Create++ + case repository.FileActionIgnored: + summary.Noop++ + case repository.FileActionRenamed: + summary.Delete++ + summary.Create++ + } + summary.Write = summary.Create + summary.Update + } +} + +func (r *jobProgressRecorder) progress() float64 { + if r.total == 0 { + return 0 + } + + return float64(r.resultCount) / float64(r.total) * 100 +} + +func (r *jobProgressRecorder) notify(ctx context.Context) { + jobStatus := provisioning.JobStatus{ + State: provisioning.JobStateWorking, + Message: r.message, + Errors: r.errors, + Progress: r.progress(), + Summary: r.summary(), + } + + logger := logging.FromContext(ctx) + if err := r.progressFn(ctx, jobStatus); err != nil { + logger.Warn("error notifying progress", "err", err) + } +} + +func (r *jobProgressRecorder) Complete(ctx context.Context, err error) provisioning.JobStatus { + // Initialize base job status + jobStatus := provisioning.JobStatus{ + Started: r.started.UnixMilli(), + // FIXME: if we call this method twice, the state will be different + // This results in sync status to be different from job status + Finished: time.Now().UnixMilli(), + State: provisioning.JobStateSuccess, + Message: "completed successfully", + } + + if err != nil { + jobStatus.State = provisioning.JobStateError + jobStatus.Message = err.Error() + } + + jobStatus.Summary = r.summary() + jobStatus.Errors = r.errors + + // Check for errors during execution + if len(jobStatus.Errors) > 0 && jobStatus.State != provisioning.JobStateError { + jobStatus.State = provisioning.JobStateError + jobStatus.Message = "completed with errors" + } + + // Override message if progress have a more explicit message + if r.message != "" && jobStatus.State != provisioning.JobStateError { + jobStatus.Message = r.message + } + + return jobStatus +} diff --git a/pkg/registry/apis/provisioning/jobs/queue.go b/pkg/registry/apis/provisioning/jobs/queue.go new file mode 100644 index 00000000000..cb865e91578 --- /dev/null +++ b/pkg/registry/apis/provisioning/jobs/queue.go @@ -0,0 +1,47 @@ +package jobs + +import ( + "context" + + provisioning "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1" + "github.com/grafana/grafana/pkg/registry/apis/provisioning/repository" +) + +type RepoGetter interface { + GetRepository(ctx context.Context, name string) (repository.Repository, error) +} + +// Basic job queue infrastructure +type JobQueue interface { + // Add a new Job to the Queue. The status must be empty + Add(ctx context.Context, job *provisioning.Job) (*provisioning.Job, error) + + // Get the next job we should process + Next(ctx context.Context) *provisioning.Job + + // Update the status on a given job + // This is only valid if current job is not finished + Update(ctx context.Context, namespace string, name string, status provisioning.JobStatus) error + + // Register a worker (inline for now) + Register(worker Worker) +} + +type JobProgressRecorder interface { + Record(ctx context.Context, result JobResourceResult) + SetMessage(msg string) + GetMessage() string + SetRef(ref string) + GetRef() string + SetTotal(total int) + TooManyErrors() error + Complete(ctx context.Context, err error) provisioning.JobStatus +} + +type Worker interface { + IsSupported(ctx context.Context, job provisioning.Job) bool + Process(ctx context.Context, repo repository.Repository, job provisioning.Job, progress JobProgressRecorder) error +} + +// ProgressFn is a function that can be called to update the progress of a job +type ProgressFn func(ctx context.Context, status provisioning.JobStatus) error diff --git a/pkg/registry/apis/provisioning/jobs/store.go b/pkg/registry/apis/provisioning/jobs/store.go new file mode 100644 index 00000000000..89926c8828a --- /dev/null +++ b/pkg/registry/apis/provisioning/jobs/store.go @@ -0,0 +1,367 @@ +package jobs + +import ( + "context" + "errors" + "fmt" + "net/http" + "strconv" + "sync" + "time" + + apierrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/apis/meta/internalversion" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/fields" + "k8s.io/apimachinery/pkg/labels" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/watch" + "k8s.io/apiserver/pkg/endpoints/request" + "k8s.io/apiserver/pkg/registry/rest" + "k8s.io/apiserver/pkg/storage" + + "github.com/grafana/grafana-app-sdk/logging" + "github.com/grafana/grafana/pkg/apimachinery/identity" + provisioning "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1" + "github.com/grafana/grafana/pkg/util" +) + +var ( + _ JobQueue = (*jobStore)(nil) + _ rest.Scoper = (*jobStore)(nil) + _ rest.SingularNameProvider = (*jobStore)(nil) + _ rest.Getter = (*jobStore)(nil) + _ rest.Lister = (*jobStore)(nil) + _ rest.Storage = (*jobStore)(nil) + _ rest.Watcher = (*jobStore)(nil) +) + +func NewJobStore(capacity int, getter RepoGetter) *jobStore { + return &jobStore{ + workers: make([]Worker, 0), + getter: getter, + rv: 1, + capacity: capacity, + jobs: []provisioning.Job{}, + watchSet: NewWatchSet(), + versioner: &storage.APIObjectVersioner{}, + } +} + +type jobStore struct { + getter RepoGetter + capacity int + workers []Worker + + // All jobs + jobs []provisioning.Job + rv int64 // updates whenever changed + watchSet *WatchSet + versioner storage.Versioner + + mutex sync.RWMutex +} + +// Implementing Kube interfaces + +func (s *jobStore) New() runtime.Object { + return provisioning.JobResourceInfo.NewFunc() +} + +func (s *jobStore) Destroy() {} + +func (s *jobStore) NamespaceScoped() bool { + return true // namespace == org +} + +func (s *jobStore) GetSingularName() string { + return provisioning.JobResourceInfo.GetSingularName() +} + +func (s *jobStore) NewList() runtime.Object { + return provisioning.JobResourceInfo.NewListFunc() +} + +func (s *jobStore) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) { + return provisioning.JobResourceInfo.TableConverter().ConvertToTable(ctx, object, tableOptions) +} + +func (s *jobStore) List(ctx context.Context, options *internalversion.ListOptions) (runtime.Object, error) { + ns, ok := request.NamespaceFrom(ctx) + if !ok { + return nil, fmt.Errorf("missing namespace") + } + + queue := &provisioning.JobList{ + ListMeta: metav1.ListMeta{ + ResourceVersion: strconv.FormatInt(s.rv, 10), + }, + } + + query := options.LabelSelector + + s.mutex.RLock() + defer s.mutex.RUnlock() + + for _, job := range s.jobs { + if job.Namespace != ns { + continue + } + + // maybe filter + if query != nil && !query.Matches(labels.Set(job.Labels)) { + continue + } + + copy := job.DeepCopy() + queue.Items = append(queue.Items, *copy) + } + + return queue, nil +} + +func (s *jobStore) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) { + s.mutex.RLock() + defer s.mutex.RUnlock() + + ns, ok := request.NamespaceFrom(ctx) + if !ok { + return nil, fmt.Errorf("missing namespace") + } + + for _, job := range s.jobs { + if job.Name == name && job.Namespace == ns { + return job.DeepCopy(), nil + } + } + + return nil, apierrors.NewNotFound(provisioning.JobResourceInfo.GroupResource(), name) +} + +func (s *jobStore) Watch(ctx context.Context, opts *internalversion.ListOptions) (watch.Interface, error) { + ns, ok := request.NamespaceFrom(ctx) + if !ok { + return nil, fmt.Errorf("missing namespace") + } + + p := storage.SelectionPredicate{ + Label: labels.Everything(), // TODO... limit + Field: fields.Everything(), + } + + // Can watch by label selection + jw := s.watchSet.newWatch(ctx, 0, p, s.versioner, &ns) + jw.Start() + return jw, nil +} + +// Implementing JobQueue + +// Register a worker (inline for now) +func (s *jobStore) Register(worker Worker) { + s.workers = append(s.workers, worker) +} + +func (s *jobStore) Add(ctx context.Context, job *provisioning.Job) (*provisioning.Job, error) { + if job.Namespace == "" { + return nil, apierrors.NewBadRequest("missing metadata.namespace") + } + if job.Name != "" { + return nil, apierrors.NewBadRequest("name will always be generated") + } + if job.Spec.Repository == "" { + return nil, apierrors.NewBadRequest("missing spec.repository") + } + if job.Spec.Action == "" { + return nil, apierrors.NewBadRequest("missing spec.action") + } + if job.Spec.Action == provisioning.JobActionExport && job.Spec.Export == nil { + return nil, apierrors.NewBadRequest("missing spec.export") + } + + if job.Spec.Action == provisioning.JobActionSync && job.Spec.Sync == nil { + return nil, apierrors.NewBadRequest("missing spec.sync") + } + + // Only for add + if job.Status.State != "" { + return nil, apierrors.NewBadRequest("must add jobs with empty status") + } + + if job.Labels == nil { + job.Labels = make(map[string]string) + } + job.Labels["repository"] = job.Spec.Repository // for now, make sure we can search Multi-tenant + job.Name = fmt.Sprintf("%s:%s:%s", job.Spec.Repository, job.Spec.Action, util.GenerateShortUID()) + + s.mutex.Lock() + defer s.mutex.Unlock() + + s.rv++ + job.ResourceVersion = strconv.FormatInt(s.rv, 10) + job.Status.State = provisioning.JobStatePending + job.CreationTimestamp = metav1.NewTime(time.Now()) + + jobs := make([]provisioning.Job, 0, len(s.jobs)+2) + jobs = append(jobs, *job) + for i, j := range s.jobs { + if i >= s.capacity { + // Remove the old jobs + s.watchSet.notifyWatchers(watch.Event{ + Object: j.DeepCopyObject(), + Type: watch.Deleted, + }, nil) + continue + } + jobs = append(jobs, j) + } + + // Send add event + s.watchSet.notifyWatchers(watch.Event{ + Object: job.DeepCopyObject(), + Type: watch.Added, + }, nil) + + // For now, start a thread processing each job + go s.drainPending() + + s.jobs = jobs // replace existing list + return job, nil +} + +// Reads the queue until no jobs remain +func (s *jobStore) drainPending() { + logger := logging.DefaultLogger.With("logger", "job-store") + ctx := logging.Context(context.Background(), logger) + + var err error + for { + time.Sleep(time.Microsecond * 200) + + job := s.Next(ctx) + if job == nil { + return // done + } + logger := logger.With("job", job.GetName(), "namespace", job.GetNamespace()) + ctx := logging.Context(ctx, logger) + + var foundWorker bool + recorder := newJobProgressRecorder(func(ctx context.Context, j provisioning.JobStatus) error { + return s.Update(ctx, job.Namespace, job.Name, j) + }) + + for _, worker := range s.workers { + if !worker.IsSupported(ctx, *job) { + continue + } + + // Already found a worker, no need to continue + foundWorker = true + err = s.processByWorker(ctx, worker, *job, recorder) + break + } + + if !foundWorker { + err = errors.New("no registered worker supports this job") + } + + status := recorder.Complete(ctx, err) + err = s.Update(ctx, job.Namespace, job.Name, status) + if err != nil { + logger.Error("error running job", "error", err) + } + logger.Debug("job has been fully completed") + } +} + +func (s *jobStore) processByWorker(ctx context.Context, worker Worker, job provisioning.Job, recorder JobProgressRecorder) error { + ctx = request.WithNamespace(ctx, job.Namespace) + ctx, _, err := identity.WithProvisioningIdentitiy(ctx, job.Namespace) + if err != nil { + return fmt.Errorf("get worker identity: %w", err) + } + repoName := job.Spec.Repository + + logger := logging.FromContext(ctx) + logger = logger.With("repository", repoName) + ctx = logging.Context(ctx, logger) + + repo, err := s.getter.GetRepository(ctx, repoName) + if err != nil { + return fmt.Errorf("get repository: %w", err) + } + + // TODO: does this really happen? + if repo == nil { + return errors.New("unknown repository") + } + + return worker.Process(ctx, repo, job, recorder) +} + +// Checkout the next "pending" job +func (s *jobStore) Next(ctx context.Context) *provisioning.Job { + s.mutex.Lock() + defer s.mutex.Unlock() + + // The oldest jobs should be checked out first + for i := len(s.jobs) - 1; i >= 0; i-- { + if s.jobs[i].Status.State == provisioning.JobStatePending { + oldObj := s.jobs[i].DeepCopyObject() + + s.rv++ + s.jobs[i].ResourceVersion = strconv.FormatInt(s.rv, 10) + s.jobs[i].Status.State = provisioning.JobStateWorking + s.jobs[i].Status.Started = time.Now().UnixMilli() + job := s.jobs[i] + + s.watchSet.notifyWatchers(watch.Event{ + Object: job.DeepCopyObject(), + Type: watch.Modified, + }, oldObj) + return &job + } + } + return nil +} + +func (s *jobStore) Update(ctx context.Context, namespace string, name string, status provisioning.JobStatus) error { + s.mutex.Lock() + defer s.mutex.Unlock() + + s.rv++ + + if status.State == "" { + return apierrors.NewBadRequest("The state must be set") + } + if status.Progress > 100 || status.Progress < 0 { + return apierrors.NewBadRequest("progress must be between 0 and 100") + } + + for idx, job := range s.jobs { + if job.Name == name && job.Namespace == namespace { + if job.Status.State.Finished() { + return &apierrors.StatusError{ErrStatus: metav1.Status{ + Code: http.StatusPreconditionFailed, + Message: "The job is already finished and can not be updated", + }} + } + if status.State.Finished() { + status.Finished = time.Now().UnixMilli() + } + + oldObj := job.DeepCopyObject() + job.ResourceVersion = strconv.FormatInt(s.rv, 10) + job.Status = status + s.jobs[idx] = job + + s.watchSet.notifyWatchers(watch.Event{ + Object: job.DeepCopyObject(), + Type: watch.Modified, + }, oldObj) + return nil + } + } + + return apierrors.NewNotFound(provisioning.JobResourceInfo.GroupResource(), name) +} diff --git a/pkg/registry/apis/provisioning/jobs/watchset.go b/pkg/registry/apis/provisioning/jobs/watchset.go new file mode 100644 index 00000000000..f9a2934482d --- /dev/null +++ b/pkg/registry/apis/provisioning/jobs/watchset.go @@ -0,0 +1,379 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// Provenance-includes-location: https://github.com/tilt-dev/tilt-apiserver/blob/main/pkg/storage/filepath/watchset.go +// Provenance-includes-license: Apache-2.0 +// Provenance-includes-copyright: The Kubernetes Authors. + +// See also +// https://github.com/grafana/grafana/blob/v11.1.9/pkg/apiserver/storage/file/watchset.go + +package jobs + +import ( + "context" + "fmt" + "sync" + "sync/atomic" + + "k8s.io/apimachinery/pkg/api/meta" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/watch" + "k8s.io/apiserver/pkg/storage" + "k8s.io/klog/v2" +) + +const ( + UpdateChannelSize = 25 + InitialWatchNodesSize = 20 + InitialBufferedEventsSize = 25 +) + +type eventWrapper struct { + ev watch.Event + // optional: oldObject is only set for modifications for determining their type as necessary (when using predicate filtering) + oldObject runtime.Object +} + +type watchNode struct { + ctx context.Context + s *WatchSet + id uint64 + updateCh chan eventWrapper + outCh chan watch.Event + requestedRV uint64 + // the watch may or may not be namespaced for a namespaced resource. This is always nil for cluster-scoped kinds + watchNamespace *string + predicate storage.SelectionPredicate + versioner storage.Versioner +} + +// Keeps track of which watches need to be notified +type WatchSet struct { + mu sync.RWMutex + // mu protects both nodes and counter + nodes map[uint64]*watchNode + counter atomic.Uint64 + buffered []eventWrapper + bufferedMutex sync.RWMutex +} + +func NewWatchSet() *WatchSet { + return &WatchSet{ + buffered: make([]eventWrapper, 0, InitialBufferedEventsSize), + nodes: make(map[uint64]*watchNode, InitialWatchNodesSize), + } +} + +// Creates a new watch with a unique id, but +// does not start sending events to it until start() is called. +func (s *WatchSet) newWatch(ctx context.Context, requestedRV uint64, p storage.SelectionPredicate, versioner storage.Versioner, namespace *string) *watchNode { + s.counter.Add(1) + + node := &watchNode{ + ctx: ctx, + requestedRV: requestedRV, + id: s.counter.Load(), + s: s, + // updateCh size needs to be > 1 to allow slower clients to not block passing new events + updateCh: make(chan eventWrapper, UpdateChannelSize), + // outCh size needs to be > 1 for single process use-cases such as tests where watch and event seeding from CUD + // events is happening on the same thread + outCh: make(chan watch.Event, UpdateChannelSize), + predicate: p, + watchNamespace: namespace, + versioner: versioner, + } + + return node +} + +func (s *WatchSet) CleanupWatchers() { + s.mu.Lock() + defer s.mu.Unlock() + for _, w := range s.nodes { + w.stop() + } +} + +// oldObject is only passed in the event of a modification +// in case a predicate filtered watch is impacted as a result of modification +// NOTE: this function gives one the misperception that a newly added node will never +// get a double event, one from buffered and one from the update channel +// That perception is not true. Even though this function maintains the lock throughout the function body +// it is not true of the Start function. So basically, the Start function running after this function +// fully stands the chance of another future notifyWatchers double sending it the event through the two means mentioned +func (s *WatchSet) notifyWatchers(ev watch.Event, oldObject runtime.Object) { + s.mu.RLock() + defer s.mu.RUnlock() + + updateEv := eventWrapper{ + ev: ev, + } + if oldObject != nil { + updateEv.oldObject = oldObject + } + + // Events are always buffered. + // this is because of an inadvertent delay which is built into the watch process + // Watch() from storage returns Watch.Interface with a async start func. + // The only way to guarantee that we can interpret the passed RV correctly is to play it against missed events + // (notice the loop below over s.nodes isn't exactly going to work on a new node + // unless start is called on it) + s.bufferedMutex.Lock() + s.buffered = append(s.buffered, updateEv) + s.bufferedMutex.Unlock() + + for _, w := range s.nodes { + w.updateCh <- updateEv + } +} + +// isValid is not necessary to be called on oldObject in UpdateEvents - assuming the Watch pushes correctly setup eventWrapper our way +// first bool is whether the event is valid for current watcher +// second bool is whether checking the old value against the predicate may be valuable to the caller +// second bool may be a helpful aid to establish context around MODIFIED events +// (note that this second bool is only marked true if we pass other checks first, namely RV and namespace) +func (w *watchNode) isValid(e eventWrapper) (bool, bool, error) { + obj, err := meta.Accessor(e.ev.Object) + if err != nil { + klog.Error("Could not get accessor to object in event") + return false, false, nil + } + + eventRV, err := w.getResourceVersionAsInt(e.ev.Object) + if err != nil { + return false, false, err + } + + if eventRV < w.requestedRV { + return false, false, nil + } + + if w.watchNamespace != nil && *w.watchNamespace != obj.GetNamespace() { + return false, false, err + } + + valid, err := w.predicate.Matches(e.ev.Object) + if err != nil { + return false, false, err + } + + return valid, e.ev.Type == watch.Modified, nil +} + +// Only call this method if current object matches the predicate +func (w *watchNode) handleAddedForFilteredList(e eventWrapper) (*watch.Event, error) { + if e.oldObject == nil { + return nil, fmt.Errorf("oldObject should be set for modified events") + } + + ok, err := w.predicate.Matches(e.oldObject) + if err != nil { + return nil, err + } + + if !ok { + e.ev.Type = watch.Added + return &e.ev, nil + } + + return nil, nil +} + +func (w *watchNode) handleDeletedForFilteredList(e eventWrapper) (*watch.Event, error) { + if e.oldObject == nil { + return nil, fmt.Errorf("oldObject should be set for modified events") + } + + ok, err := w.predicate.Matches(e.oldObject) + if err != nil { + return nil, err + } + + if !ok { + return nil, nil + } + + // isn't a match but used to be + e.ev.Type = watch.Deleted + + oldObjectAccessor, err := meta.Accessor(e.oldObject) + if err != nil { + klog.Errorf("Could not get accessor to correct the old RV of filtered out object") + return nil, err + } + + currentRV, err := getResourceVersion(e.ev.Object) + if err != nil { + klog.Errorf("Could not get accessor to object in event") + return nil, err + } + + oldObjectAccessor.SetResourceVersion(currentRV) + e.ev.Object = e.oldObject + + return &e.ev, nil +} + +func (w *watchNode) processEvent(e eventWrapper, isInitEvent bool) error { + if isInitEvent { + // Init events have already been vetted against the predicate and other RV behavior + // Let them pass through + w.outCh <- e.ev + return nil + } + + valid, runDeleteFromFilteredListHandler, err := w.isValid(e) + if err != nil { + klog.Errorf("Could not determine validity of the event: %v", err) + return err + } + if valid { + if e.ev.Type == watch.Modified { + ev, err := w.handleAddedForFilteredList(e) + if err != nil { + return err + } + if ev != nil { + w.outCh <- *ev + } else { + // forward the original event if add handling didn't signal any impact + w.outCh <- e.ev + } + } else { + w.outCh <- e.ev + } + return nil + } + + if runDeleteFromFilteredListHandler { + if e.ev.Type == watch.Modified { + ev, err := w.handleDeletedForFilteredList(e) + if err != nil { + return err + } + if ev != nil { + w.outCh <- *ev + } + } // explicitly doesn't have an event forward for the else case here + return nil + } + + return nil +} + +// Start sending events to this watch. +func (w *watchNode) Start(initEvents ...watch.Event) { + w.s.mu.Lock() + w.s.nodes[w.id] = w + w.s.mu.Unlock() + + go func() { + maxRV := uint64(0) + for _, ev := range initEvents { + currentRV, err := w.getResourceVersionAsInt(ev.Object) + if err != nil { + klog.Errorf("Could not determine init event RV for deduplication of buffered events: %v", err) + continue + } + + if maxRV < currentRV { + maxRV = currentRV + } + + if err := w.processEvent(eventWrapper{ev: ev}, true); err != nil { + klog.Errorf("Could not process event: %v", err) + } + } + + // If we had no init events, simply rely on the passed RV + if maxRV == 0 { + maxRV = w.requestedRV + } + + w.s.bufferedMutex.RLock() + for _, e := range w.s.buffered { + eventRV, err := w.getResourceVersionAsInt(e.ev.Object) + if err != nil { + klog.Errorf("Could not determine RV for deduplication of buffered events: %v", err) + continue + } + + if maxRV >= eventRV { + continue + } else { + maxRV = eventRV + } + + if err := w.processEvent(e, false); err != nil { + klog.Errorf("Could not process event: %v", err) + } + } + w.s.bufferedMutex.RUnlock() + + for { + select { + case e, ok := <-w.updateCh: + if !ok { + close(w.outCh) + return + } + + eventRV, err := w.getResourceVersionAsInt(e.ev.Object) + if err != nil { + klog.Errorf("Could not determine RV for deduplication of channel events: %v", err) + continue + } + + if maxRV >= eventRV { + continue + } else { + maxRV = eventRV + } + + if err := w.processEvent(e, false); err != nil { + klog.Errorf("Could not process event: %v", err) + } + case <-w.ctx.Done(): + close(w.outCh) + return + } + } + }() +} + +func (w *watchNode) Stop() { + w.s.mu.Lock() + defer w.s.mu.Unlock() + w.stop() +} + +// Unprotected func: ensure mutex on the parent watch set is locked before calling +func (w *watchNode) stop() { + if _, ok := w.s.nodes[w.id]; ok { + delete(w.s.nodes, w.id) + close(w.updateCh) + } +} + +func (w *watchNode) ResultChan() <-chan watch.Event { + return w.outCh +} + +func getResourceVersion(obj runtime.Object) (string, error) { + accessor, err := meta.Accessor(obj) + if err != nil { + klog.Error("Could not get accessor to object in event") + return "", err + } + return accessor.GetResourceVersion(), nil +} + +func (w *watchNode) getResourceVersionAsInt(obj runtime.Object) (uint64, error) { + accessor, err := meta.Accessor(obj) + if err != nil { + klog.Error("Could not get accessor to object in event") + return 0, err + } + + return w.versioner.ParseResourceVersion(accessor.GetResourceVersion()) +} diff --git a/pkg/registry/apis/provisioning/register.go b/pkg/registry/apis/provisioning/register.go index 3171f55c66a..d5a12275733 100644 --- a/pkg/registry/apis/provisioning/register.go +++ b/pkg/registry/apis/provisioning/register.go @@ -6,8 +6,12 @@ import ( provisioning "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1" grafanaregistry "github.com/grafana/grafana/pkg/apiserver/registry/generic" + "github.com/grafana/grafana/pkg/registry/apis/provisioning/jobs" + "github.com/grafana/grafana/pkg/registry/apis/provisioning/repository" + "github.com/grafana/grafana/pkg/registry/apis/provisioning/secrets" "github.com/grafana/grafana/pkg/services/apiserver/builder" "github.com/grafana/grafana/pkg/services/featuremgmt" + grafanasecrets "github.com/grafana/grafana/pkg/services/secrets" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" @@ -22,16 +26,28 @@ import ( ) var ( - _ builder.APIGroupBuilder = (*APIBuilder)(nil) + _ builder.APIGroupBuilder = (*APIBuilder)(nil) + _ builder.APIGroupMutation = (*APIBuilder)(nil) + _ builder.APIGroupValidation = (*APIBuilder)(nil) + _ builder.APIGroupPostStartHookProvider = (*APIBuilder)(nil) + _ builder.OpenAPIPostProcessor = (*APIBuilder)(nil) ) -type APIBuilder struct{} +type APIBuilder struct { + secrets secrets.Service + jobs jobs.JobQueue + getter rest.Getter +} // NewAPIBuilder creates an API builder. // It avoids anything that is core to Grafana, such that it can be used in a multi-tenant service down the line. // This means there are no hidden dependencies, and no use of e.g. *settings.Cfg. -func NewAPIBuilder() *APIBuilder { - return &APIBuilder{} +func NewAPIBuilder( + secrets secrets.Service, +) *APIBuilder { + return &APIBuilder{ + secrets: secrets, + } } // RegisterAPIService returns an API builder, from [NewAPIBuilder]. It is called by Wire. @@ -39,13 +55,14 @@ func NewAPIBuilder() *APIBuilder { func RegisterAPIService( features featuremgmt.FeatureToggles, apiregistration builder.APIRegistrar, + secretsSvc grafanasecrets.Service, ) (*APIBuilder, error) { if !features.IsEnabledGlobally(featuremgmt.FlagProvisioning) && !features.IsEnabledGlobally(featuremgmt.FlagGrafanaAPIServerWithExperimentalAPIs) { return nil, nil // skip registration unless opting into experimental apis OR the feature specifically } - builder := NewAPIBuilder() + builder := NewAPIBuilder(secrets.NewSingleTenant(secretsSvc)) apiregistration.RegisterAPI(builder) return builder, nil } @@ -87,9 +104,14 @@ func (b *APIBuilder) UpdateAPIGroupInfo(apiGroupInfo *genericapiserver.APIGroupI return fmt.Errorf("failed to create repository storage: %w", err) } + // FIXME: Make job queue store the jobs somewhere persistent. + jobStore := jobs.NewJobStore(50, b) // in memory, for now... + b.jobs = jobStore + repositoryStatusStorage := grafanaregistry.NewRegistryStatusStore(opts.Scheme, repositoryStorage) storage := map[string]rest.Storage{} + storage[provisioning.JobResourceInfo.StoragePath()] = jobStore storage[provisioning.RepositoryResourceInfo.StoragePath()] = repositoryStorage storage[provisioning.RepositoryResourceInfo.StoragePath("status")] = repositoryStatusStorage apiGroupInfo.VersionedResourcesStorageMap[provisioning.VERSION] = storage @@ -158,3 +180,16 @@ func (b *APIBuilder) PostProcessOpenAPI(oas *spec3.OpenAPI) (*spec3.OpenAPI, err return oas, nil } + +// Helpers for fetching valid Repository objects + +func (b *APIBuilder) GetRepository(ctx context.Context, name string) (repository.Repository, error) { + obj, err := b.getter.Get(ctx, name, &metav1.GetOptions{}) + if err != nil { + return nil, err + } + + _ = obj + // FIXME: Return a valid Repository object with the correct underlying storage. + panic("FIXME") +} diff --git a/pkg/registry/apis/provisioning/repository/repository.go b/pkg/registry/apis/provisioning/repository/repository.go new file mode 100644 index 00000000000..d4dd2aee63d --- /dev/null +++ b/pkg/registry/apis/provisioning/repository/repository.go @@ -0,0 +1,128 @@ +package repository + +import ( + "context" + "io/fs" + "net/http" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/validation/field" + + provisioning "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1" +) + +type Repository interface { + // Config returns the saved Kubernetes object. + Config() *provisioning.Repository + + // Validate ensures the resource _looks_ correct. + // It should be called before trying to upsert a resource into the Kubernetes API server. + // This is not an indication that the connection information works, just that they are reasonably configured (see also Test). + Validate() field.ErrorList + + // Test checks if the connection information actually works. + Test(ctx context.Context) (*provisioning.TestResults, error) +} + +// ErrFileNotFound indicates that a path could not be found in the repository. +var ErrFileNotFound error = fs.ErrNotExist + +type FileInfo struct { + // Path to the file on disk. + // No leading or trailing slashes will be contained within. + // This uses '/' for separation. Use the 'path' package to interact with this. + Path string + // The raw bytes + Data []byte + // The git branch or reference commit + Ref string + // The git hash for a given file + Hash string + // When was the file changed (if known) + Modified *metav1.Time +} + +// An entry in the file tree, as returned by 'ReadFileTree'. Like FileInfo, but contains less information. +type FileTreeEntry struct { + // The path to the file from the base path given (if any). + // No leading or trailing slashes will be contained within. + // This uses '/' for separation. Use the 'path' package to interact with this. + Path string + // The hash for the file. Lower-case hex. + // Empty string if Blob is false. + Hash string + // The size of the file. + // 0 if Blob is false. + Size int64 + // Whether this entry is a blob or a subtree. + Blob bool +} + +type Reader interface { + // Read a file from the resource + // This data will be parsed and validated before it is shown to end users + Read(ctx context.Context, path, ref string) (*FileInfo, error) + + // Read all file names from the tree. + // This data will be parsed and validated before it is shown. + // + // TODO: Make some API contract that lets us ignore files that aren't relevant to us (e.g. CI/CD, CODEOWNERS, other configs or source code). + // TODO: Test scale: do we want to stream entries instead somehow? + ReadTree(ctx context.Context, ref string) ([]FileTreeEntry, error) +} + +type Writer interface { + // Write a file to the repository. + // The data has already been validated and is ready for save + Create(ctx context.Context, path, ref string, data []byte, message string) error + + // Update a file in the remote repository + // The data has already been validated and is ready for save + Update(ctx context.Context, path, ref string, data []byte, message string) error + + // Write a file to the repository. + // Functionally the same as Read then Create or Update, but more efficient depending on the backend + Write(ctx context.Context, path, ref string, data []byte, message string) error + + // Delete a file in the remote repository + Delete(ctx context.Context, path, ref, message string) error +} + +// Hooks called after the repository has been created, updated or deleted +type Hooks interface { + // For repositories that support webhooks + Webhook(ctx context.Context, req *http.Request) (*provisioning.WebhookResponse, error) + OnCreate(ctx context.Context) (*provisioning.WebhookStatus, error) + OnUpdate(ctx context.Context) (*provisioning.WebhookStatus, error) + OnDelete(ctx context.Context) error +} + +type FileAction string + +const ( + FileActionCreated FileAction = "created" + FileActionUpdated FileAction = "updated" + FileActionDeleted FileAction = "deleted" + FileActionIgnored FileAction = "ignored" + + // Renamed actions may be reconstructed as delete then create + FileActionRenamed FileAction = "renamed" +) + +type VersionedFileChange struct { + Action FileAction + Path string + + Ref string + PreviousRef string // rename | update + PreviousPath string // rename +} + +// Versioned is a repository that supports versioning. +// This interface may be extended to make the the original Repository interface more agnostic to the underlying storage system. +type Versioned interface { + // History of changes for a path + History(ctx context.Context, path, ref string) ([]provisioning.HistoryItem, error) + LatestRef(ctx context.Context) (string, error) + CompareFiles(ctx context.Context, base, ref string) ([]VersionedFileChange, error) +} diff --git a/pkg/registry/apis/provisioning/safepath/path.go b/pkg/registry/apis/provisioning/safepath/path.go new file mode 100644 index 00000000000..17f0adb18f9 --- /dev/null +++ b/pkg/registry/apis/provisioning/safepath/path.go @@ -0,0 +1,59 @@ +package safepath + +import ( + "os" + "path" + "strings" + + apierrors "k8s.io/apimachinery/pkg/api/errors" +) + +// ErrUnsafePathTraversal indicates that an input path had a path traversal which led to escaping the required prefix. +// E.g. Join("/test", "..") would return this, because it doesn't stay within the '/test' directory. +var ErrUnsafePathTraversal = apierrors.NewBadRequest("the input path had an unacceptable path traversal") + +// Join joins any number of elements in a path under a common prefix path. +// If the elems do path traversal, they are permitted to do so under their own directories. +// The output result will _always_ have a prefix of the given prefix, and no path traversals in the output string. +// The output result will not end with a trailing slash. +// The output result will have a leading slash if one is given as a prefix. +// If the prefix would ultimately be escaped, an error is returned. +// +// This function is safe for . +func Join(prefix string, elem ...string) (string, error) { + // We clean early to make the HasPrefix check be sensible after path.Join does a Clean for us. + prefix = replaceOSSeparators(path.Clean(prefix)) + if len(elem) == 0 { + return prefix, nil + } + + for i, e := range elem { + // We don't use Clean here because the output of path.Join will clean for us. + elem[i] = replaceOSSeparators(e) + } + subPath := path.Join(elem...) // performs a Clean after joining + completePath := path.Join(prefix, subPath) + if !strings.HasPrefix(completePath, prefix) { + return "", ErrUnsafePathTraversal + } + return completePath, nil +} + +// Performs a [path.Clean] on the path, as well as replacing its OS separators. +// Note that this does no effort to ensure the paths are safe to use. It only cleans them. +func Clean(p string) string { + return path.Clean(replaceOSSeparators(p)) +} + +// osSeparator is declared as a var here only to ensure we can change it in tests. +var osSeparator = os.PathSeparator + +// This replaces the OS separator with a slash. +// All OSes we target (Linux, macOS, and Windows) support forward-slashes in path traversals, as such it's simpler to use the same character everywhere. +// BSDs do as well (even though they're not a target as of writing). +func replaceOSSeparators(p string) string { + if osSeparator == '/' { // perf: nothing to do! + return p + } + return strings.ReplaceAll(p, string(osSeparator), "/") +} diff --git a/pkg/registry/apis/provisioning/safepath/path_test.go b/pkg/registry/apis/provisioning/safepath/path_test.go new file mode 100644 index 00000000000..abd4f14abc1 --- /dev/null +++ b/pkg/registry/apis/provisioning/safepath/path_test.go @@ -0,0 +1,74 @@ +package safepath + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestPathJoin(t *testing.T) { + orig := osSeparator + osSeparator = '\\' // pretend we're on Windows + defer func() { osSeparator = orig }() + + testCases := []struct { + Comment string + In []string + Out any // string or error + }{ + {"Empty elements should not change input", []string{"/test/"}, "/test"}, + {"Empty elements without leading slash should not change input", []string{"test/"}, "test"}, + {"Single element should be added to path", []string{"/test/", "abc"}, "/test/abc"}, + {"Single element should be added to path with current dir prefix", []string{"./test/", "abc"}, "test/abc"}, + {"Single element with leading slash should be added to path", []string{"/test/", "/abc"}, "/test/abc"}, + {"Many elements are all appended to path", []string{"/test/", "a", "b", "c"}, "/test/a/b/c"}, + {"Path traversal within same directory should be expanded", []string{"/test/", "a", "..", "b", ".", "..", "c"}, "/test/c"}, + {"Path traversal escaping root dir prefix should return err", []string{"/test/", ".."}, ErrUnsafePathTraversal}, + {"Path traversal escaping no dir prefix should return err", []string{"test/", ".."}, ErrUnsafePathTraversal}, + {"Path traversal escaping current dir prefix should return err", []string{"./test/", ".."}, ErrUnsafePathTraversal}, + {"Complex path traversal escaping prefix should return err", []string{"/test/", "a/..///c/", "../../test/d/../a/../.."}, ErrUnsafePathTraversal}, + {"Complex path traversal remaining in prefix should be expanded", []string{"/test/", "a/..///c/", "../../test/d/"}, "/test/d"}, + {"Problematic code example from the g304 website", []string{"/safe/path", "../../private/path"}, ErrUnsafePathTraversal}, + {"Traversing beyond root should be expanded", []string{"/test/", "/../a"}, "/test/a"}, + {"OS separator should be replaced with a slash", []string{"/test\\test", "abc\\test"}, "/test/test/abc/test"}, + } + + for _, tc := range testCases { + tc := tc + t.Run(tc.Comment, func(t *testing.T) { + path, err := Join(tc.In[0], tc.In[1:]...) + if ee, ok := tc.Out.(error); ok { + assert.ErrorIs(t, err, ee, "expected unsuccessful outcome") + assert.Empty(t, path, "expected empty string when unsuccessful") + } else if str, ok := tc.Out.(string); ok { + assert.NoError(t, err, "expected successful outcome") + assert.Equal(t, str, path) + } else { + panic("expected out was neither string nor error") + } + }) + } +} + +func TestPathClean(t *testing.T) { + orig := osSeparator + osSeparator = '\\' // pretend we're on Windows + defer func() { osSeparator = orig }() + + testCases := []struct { + Comment string + In string + Out string + }{ + {"Simple path", "/test/", "/test"}, + {"Simple path with OS separators", "\\test\\here", "/test/here"}, + {"Simple path with mixed separators", "\\test/here", "/test/here"}, + } + + for _, tc := range testCases { + tc := tc + t.Run(tc.Comment, func(t *testing.T) { + assert.Equal(t, tc.Out, Clean(tc.In)) + }) + } +} diff --git a/pkg/registry/apis/provisioning/safepath/walk.go b/pkg/registry/apis/provisioning/safepath/walk.go new file mode 100644 index 00000000000..57a6c4a9bb6 --- /dev/null +++ b/pkg/registry/apis/provisioning/safepath/walk.go @@ -0,0 +1,31 @@ +package safepath + +import ( + "context" + "path" + "strings" +) + +type WalkFunc = func(ctx context.Context, path string) error + +// Walk walks the given folder path and calls the given function for each folder. +func Walk(ctx context.Context, p string, fn WalkFunc) error { + if p == "." || p == "/" { + return nil + } + + var currentPath string + for _, folder := range strings.Split(p, "/") { + if folder == "" { + // Trailing / leading slash? + continue + } + + currentPath = path.Join(currentPath, folder) + if err := fn(ctx, currentPath); err != nil { + return err + } + } + + return nil +} diff --git a/pkg/registry/apis/provisioning/secrets/secret.go b/pkg/registry/apis/provisioning/secrets/secret.go new file mode 100644 index 00000000000..f0e8498825d --- /dev/null +++ b/pkg/registry/apis/provisioning/secrets/secret.go @@ -0,0 +1,35 @@ +package secrets + +import ( + "context" + + "github.com/grafana/grafana/pkg/services/secrets" +) + +// A secrets encryption service. It only operates on values, no names or similar. +// It is likely we will need to change this when the multi-tenant service comes around. +// +// FIXME: this is a temporary service/package until we can make use of +// the new secrets service in app platform. +type Service interface { + Encrypt(ctx context.Context, data []byte) ([]byte, error) + Decrypt(ctx context.Context, data []byte) ([]byte, error) +} + +var _ Service = (*singleTenant)(nil) + +type singleTenant struct { + inner secrets.Service +} + +func NewSingleTenant(svc secrets.Service) *singleTenant { + return &singleTenant{svc} +} + +func (s *singleTenant) Encrypt(ctx context.Context, data []byte) ([]byte, error) { + return s.inner.Encrypt(ctx, data, secrets.WithoutScope()) +} + +func (s *singleTenant) Decrypt(ctx context.Context, data []byte) ([]byte, error) { + return s.inner.Decrypt(ctx, data) +} diff --git a/pkg/tests/apis/openapi_snapshots/provisioning.grafana.app-v0alpha1.json b/pkg/tests/apis/openapi_snapshots/provisioning.grafana.app-v0alpha1.json index 39e95468748..d573d14730c 100644 --- a/pkg/tests/apis/openapi_snapshots/provisioning.grafana.app-v0alpha1.json +++ b/pkg/tests/apis/openapi_snapshots/provisioning.grafana.app-v0alpha1.json @@ -36,6 +36,380 @@ } } }, + "/apis/provisioning.grafana.app/v0alpha1/jobs": { + "get": { + "tags": [ + "Job" + ], + "description": "list or watch objects of kind Job", + "operationId": "listJobForAllNamespaces", + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobList" + } + }, + "application/json;stream=watch": { + "schema": { + "$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobList" + } + }, + "application/vnd.kubernetes.protobuf": { + "schema": { + "$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobList" + } + }, + "application/vnd.kubernetes.protobuf;stream=watch": { + "schema": { + "$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobList" + } + }, + "application/yaml": { + "schema": { + "$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobList" + } + } + } + } + }, + "x-kubernetes-action": "list", + "x-kubernetes-group-version-kind": { + "group": "provisioning.grafana.app", + "version": "v0alpha1", + "kind": "Job" + } + }, + "parameters": [ + { + "name": "allowWatchBookmarks", + "in": "query", + "description": "allowWatchBookmarks requests watch events with type \"BOOKMARK\". Servers that do not implement bookmarks may ignore this flag and bookmarks are sent at the server's discretion. Clients should not assume bookmarks are returned at any specific interval, nor may they assume the server will send any BOOKMARK event during a session. If this is not a watch, this field is ignored.", + "schema": { + "type": "boolean", + "uniqueItems": true + } + }, + { + "name": "continue", + "in": "query", + "description": "The continue option should be set when retrieving more results from the server. Since this value is server defined, clients may only use the continue value from a previous query result with identical query parameters (except for the value of continue) and the server may reject a continue value it does not recognize. If the specified continue value is no longer valid whether due to expiration (generally five to fifteen minutes) or a configuration change on the server, the server will respond with a 410 ResourceExpired error together with a continue token. If the client needs a consistent list, it must restart their list without the continue field. Otherwise, the client may send another list request with the token received with the 410 error, the server will respond with a list starting from the next key, but from the latest snapshot, which is inconsistent from the previous list results - objects that are created, modified, or deleted after the first list request will be included in the response, as long as their keys are after the \"next key\".\n\nThis field is not supported when watch is true. Clients may start a watch from the last resourceVersion value returned by the server and not miss any modifications.", + "schema": { + "type": "string", + "uniqueItems": true + } + }, + { + "name": "fieldSelector", + "in": "query", + "description": "A selector to restrict the list of returned objects by their fields. Defaults to everything.", + "schema": { + "type": "string", + "uniqueItems": true + } + }, + { + "name": "labelSelector", + "in": "query", + "description": "A selector to restrict the list of returned objects by their labels. Defaults to everything.", + "schema": { + "type": "string", + "uniqueItems": true + } + }, + { + "name": "limit", + "in": "query", + "description": "limit is a maximum number of responses to return for a list call. If more items exist, the server will set the `continue` field on the list metadata to a value that can be used with the same initial query to retrieve the next set of results. Setting a limit may return fewer than the requested amount of items (up to zero items) in the event all requested objects are filtered out and clients should only use the presence of the continue field to determine whether more results are available. Servers may choose not to support the limit argument and will return all of the available results. If limit is specified and the continue field is empty, clients may assume that no more results are available. This field is not supported if watch is true.\n\nThe server guarantees that the objects returned when using continue will be identical to issuing a single list call without a limit - that is, no objects created, modified, or deleted after the first request is issued will be included in any subsequent continued requests. This is sometimes referred to as a consistent snapshot, and ensures that a client that is using limit to receive smaller chunks of a very large result can ensure they see all possible objects. If objects are updated during a chunked list the version of the object that was present at the time the first list result was calculated is returned.", + "schema": { + "type": "integer", + "uniqueItems": true + } + }, + { + "name": "pretty", + "in": "query", + "description": "If 'true', then the output is pretty printed. Defaults to 'false' unless the user-agent indicates a browser or command-line HTTP tool (curl and wget).", + "schema": { + "type": "string", + "uniqueItems": true + } + }, + { + "name": "resourceVersion", + "in": "query", + "description": "resourceVersion sets a constraint on what resource versions a request may be served from. See https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions for details.\n\nDefaults to unset", + "schema": { + "type": "string", + "uniqueItems": true + } + }, + { + "name": "resourceVersionMatch", + "in": "query", + "description": "resourceVersionMatch determines how resourceVersion is applied to list calls. It is highly recommended that resourceVersionMatch be set for list calls where resourceVersion is set See https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions for details.\n\nDefaults to unset", + "schema": { + "type": "string", + "uniqueItems": true + } + }, + { + "name": "sendInitialEvents", + "in": "query", + "description": "`sendInitialEvents=true` may be set together with `watch=true`. In that case, the watch stream will begin with synthetic events to produce the current state of objects in the collection. Once all such events have been sent, a synthetic \"Bookmark\" event will be sent. The bookmark will report the ResourceVersion (RV) corresponding to the set of objects, and be marked with `\"k8s.io/initial-events-end\": \"true\"` annotation. Afterwards, the watch stream will proceed as usual, sending watch events corresponding to changes (subsequent to the RV) to objects watched.\n\nWhen `sendInitialEvents` option is set, we require `resourceVersionMatch` option to also be set. The semantic of the watch request is as following: - `resourceVersionMatch` = NotOlderThan\n is interpreted as \"data at least as new as the provided `resourceVersion`\"\n and the bookmark event is send when the state is synced\n to a `resourceVersion` at least as fresh as the one provided by the ListOptions.\n If `resourceVersion` is unset, this is interpreted as \"consistent read\" and the\n bookmark event is send when the state is synced at least to the moment\n when request started being processed.\n- `resourceVersionMatch` set to any other value or unset\n Invalid error is returned.\n\nDefaults to true if `resourceVersion=\"\"` or `resourceVersion=\"0\"` (for backward compatibility reasons) and to false otherwise.", + "schema": { + "type": "boolean", + "uniqueItems": true + } + }, + { + "name": "timeoutSeconds", + "in": "query", + "description": "Timeout for the list/watch call. This limits the duration of the call, regardless of any activity or inactivity.", + "schema": { + "type": "integer", + "uniqueItems": true + } + }, + { + "name": "watch", + "in": "query", + "description": "Watch for changes to the described resources and return them as a stream of add, update, and remove notifications. Specify resourceVersion.", + "schema": { + "type": "boolean", + "uniqueItems": true + } + } + ] + }, + "/apis/provisioning.grafana.app/v0alpha1/namespaces/{namespace}/jobs": { + "get": { + "tags": [ + "Job" + ], + "description": "list or watch objects of kind Job", + "operationId": "listJob", + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobList" + } + }, + "application/json;stream=watch": { + "schema": { + "$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobList" + } + }, + "application/vnd.kubernetes.protobuf": { + "schema": { + "$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobList" + } + }, + "application/vnd.kubernetes.protobuf;stream=watch": { + "schema": { + "$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobList" + } + }, + "application/yaml": { + "schema": { + "$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobList" + } + } + } + } + }, + "x-kubernetes-action": "list", + "x-kubernetes-group-version-kind": { + "group": "provisioning.grafana.app", + "version": "v0alpha1", + "kind": "Job" + } + }, + "parameters": [ + { + "name": "allowWatchBookmarks", + "in": "query", + "description": "allowWatchBookmarks requests watch events with type \"BOOKMARK\". Servers that do not implement bookmarks may ignore this flag and bookmarks are sent at the server's discretion. Clients should not assume bookmarks are returned at any specific interval, nor may they assume the server will send any BOOKMARK event during a session. If this is not a watch, this field is ignored.", + "schema": { + "type": "boolean", + "uniqueItems": true + } + }, + { + "name": "continue", + "in": "query", + "description": "The continue option should be set when retrieving more results from the server. Since this value is server defined, clients may only use the continue value from a previous query result with identical query parameters (except for the value of continue) and the server may reject a continue value it does not recognize. If the specified continue value is no longer valid whether due to expiration (generally five to fifteen minutes) or a configuration change on the server, the server will respond with a 410 ResourceExpired error together with a continue token. If the client needs a consistent list, it must restart their list without the continue field. Otherwise, the client may send another list request with the token received with the 410 error, the server will respond with a list starting from the next key, but from the latest snapshot, which is inconsistent from the previous list results - objects that are created, modified, or deleted after the first list request will be included in the response, as long as their keys are after the \"next key\".\n\nThis field is not supported when watch is true. Clients may start a watch from the last resourceVersion value returned by the server and not miss any modifications.", + "schema": { + "type": "string", + "uniqueItems": true + } + }, + { + "name": "fieldSelector", + "in": "query", + "description": "A selector to restrict the list of returned objects by their fields. Defaults to everything.", + "schema": { + "type": "string", + "uniqueItems": true + } + }, + { + "name": "labelSelector", + "in": "query", + "description": "A selector to restrict the list of returned objects by their labels. Defaults to everything.", + "schema": { + "type": "string", + "uniqueItems": true + } + }, + { + "name": "limit", + "in": "query", + "description": "limit is a maximum number of responses to return for a list call. If more items exist, the server will set the `continue` field on the list metadata to a value that can be used with the same initial query to retrieve the next set of results. Setting a limit may return fewer than the requested amount of items (up to zero items) in the event all requested objects are filtered out and clients should only use the presence of the continue field to determine whether more results are available. Servers may choose not to support the limit argument and will return all of the available results. If limit is specified and the continue field is empty, clients may assume that no more results are available. This field is not supported if watch is true.\n\nThe server guarantees that the objects returned when using continue will be identical to issuing a single list call without a limit - that is, no objects created, modified, or deleted after the first request is issued will be included in any subsequent continued requests. This is sometimes referred to as a consistent snapshot, and ensures that a client that is using limit to receive smaller chunks of a very large result can ensure they see all possible objects. If objects are updated during a chunked list the version of the object that was present at the time the first list result was calculated is returned.", + "schema": { + "type": "integer", + "uniqueItems": true + } + }, + { + "name": "namespace", + "in": "path", + "description": "object name and auth scope, such as for teams and projects", + "required": true, + "schema": { + "type": "string", + "uniqueItems": true + } + }, + { + "name": "pretty", + "in": "query", + "description": "If 'true', then the output is pretty printed. Defaults to 'false' unless the user-agent indicates a browser or command-line HTTP tool (curl and wget).", + "schema": { + "type": "string", + "uniqueItems": true + } + }, + { + "name": "resourceVersion", + "in": "query", + "description": "resourceVersion sets a constraint on what resource versions a request may be served from. See https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions for details.\n\nDefaults to unset", + "schema": { + "type": "string", + "uniqueItems": true + } + }, + { + "name": "resourceVersionMatch", + "in": "query", + "description": "resourceVersionMatch determines how resourceVersion is applied to list calls. It is highly recommended that resourceVersionMatch be set for list calls where resourceVersion is set See https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions for details.\n\nDefaults to unset", + "schema": { + "type": "string", + "uniqueItems": true + } + }, + { + "name": "sendInitialEvents", + "in": "query", + "description": "`sendInitialEvents=true` may be set together with `watch=true`. In that case, the watch stream will begin with synthetic events to produce the current state of objects in the collection. Once all such events have been sent, a synthetic \"Bookmark\" event will be sent. The bookmark will report the ResourceVersion (RV) corresponding to the set of objects, and be marked with `\"k8s.io/initial-events-end\": \"true\"` annotation. Afterwards, the watch stream will proceed as usual, sending watch events corresponding to changes (subsequent to the RV) to objects watched.\n\nWhen `sendInitialEvents` option is set, we require `resourceVersionMatch` option to also be set. The semantic of the watch request is as following: - `resourceVersionMatch` = NotOlderThan\n is interpreted as \"data at least as new as the provided `resourceVersion`\"\n and the bookmark event is send when the state is synced\n to a `resourceVersion` at least as fresh as the one provided by the ListOptions.\n If `resourceVersion` is unset, this is interpreted as \"consistent read\" and the\n bookmark event is send when the state is synced at least to the moment\n when request started being processed.\n- `resourceVersionMatch` set to any other value or unset\n Invalid error is returned.\n\nDefaults to true if `resourceVersion=\"\"` or `resourceVersion=\"0\"` (for backward compatibility reasons) and to false otherwise.", + "schema": { + "type": "boolean", + "uniqueItems": true + } + }, + { + "name": "timeoutSeconds", + "in": "query", + "description": "Timeout for the list/watch call. This limits the duration of the call, regardless of any activity or inactivity.", + "schema": { + "type": "integer", + "uniqueItems": true + } + }, + { + "name": "watch", + "in": "query", + "description": "Watch for changes to the described resources and return them as a stream of add, update, and remove notifications. Specify resourceVersion.", + "schema": { + "type": "boolean", + "uniqueItems": true + } + } + ] + }, + "/apis/provisioning.grafana.app/v0alpha1/namespaces/{namespace}/jobs/{name}": { + "get": { + "tags": [ + "Job" + ], + "description": "read the specified Job", + "operationId": "getJob", + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.Job" + } + }, + "application/vnd.kubernetes.protobuf": { + "schema": { + "$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.Job" + } + }, + "application/yaml": { + "schema": { + "$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.Job" + } + } + } + } + }, + "x-kubernetes-action": "get", + "x-kubernetes-group-version-kind": { + "group": "provisioning.grafana.app", + "version": "v0alpha1", + "kind": "Job" + } + }, + "parameters": [ + { + "name": "name", + "in": "path", + "description": "name of the Job", + "required": true, + "schema": { + "type": "string", + "uniqueItems": true + } + }, + { + "name": "namespace", + "in": "path", + "description": "object name and auth scope, such as for teams and projects", + "required": true, + "schema": { + "type": "string", + "uniqueItems": true + } + }, + { + "name": "pretty", + "in": "query", + "description": "If 'true', then the output is pretty printed. Defaults to 'false' unless the user-agent indicates a browser or command-line HTTP tool (curl and wget).", + "schema": { + "type": "string", + "uniqueItems": true + } + } + ] + }, "/apis/provisioning.grafana.app/v0alpha1/namespaces/{namespace}/repositories": { "get": { "tags": [ @@ -1295,17 +1669,46 @@ }, "components": { "schemas": { - "com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.GitHubRepositoryConfig": { + "com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.ExportJobOptions": { "type": "object", + "required": [ + "identifier" + ], "properties": { "branch": { - "description": "The branch to use in the repository. By default, this is the main branch.", + "description": "Target branch for export (only git)", "type": "string" }, - "branchWorkflow": { - "description": "Whether we should commit to change branches and use a Pull Request flow to achieve this. By default, this is false (i.e. we will commit straight to the main branch).", + "folder": { + "description": "The source folder (or empty) to export", + "type": "string" + }, + "history": { + "description": "Preserve history (if possible)", "type": "boolean" }, + "identifier": { + "description": "Include the identifier in the exported metadata", + "type": "boolean", + "default": false + }, + "prefix": { + "description": "Target file prefix", + "type": "string" + } + } + }, + "com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.GitHubRepositoryConfig": { + "type": "object", + "required": [ + "branch" + ], + "properties": { + "branch": { + "description": "The branch to use in the repository.", + "type": "string", + "default": "" + }, "encryptedToken": { "description": "Token for accessing the repository, but encrypted. This is not possible to read back to a user decrypted.", "type": "string", @@ -1323,18 +1726,6 @@ "url": { "description": "The repository URL (e.g. `https://github.com/example/test`).", "type": "string" - }, - "workflows": { - "description": "Workflow allowed for changes to the repository. The order is relevant for defining the precedence of the workflows. Possible values: pull-request, branch, push.", - "type": "array", - "items": { - "type": "string", - "default": "", - "enum": [ - "branch", - "push" - ] - } } } }, @@ -1365,6 +1756,243 @@ } } }, + "com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.Job": { + "description": "The repository name and type are stored as labels", + "type": "object", + "properties": { + "apiVersion": { + "description": "APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources", + "type": "string" + }, + "kind": { + "description": "Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds", + "type": "string" + }, + "metadata": { + "default": {}, + "allOf": [ + { + "$ref": "#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta" + } + ] + }, + "spec": { + "default": {}, + "allOf": [ + { + "$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobSpec" + } + ] + }, + "status": { + "default": {}, + "allOf": [ + { + "$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobStatus" + } + ] + } + }, + "x-kubernetes-group-version-kind": [ + { + "group": "provisioning.grafana.app", + "kind": "Job", + "version": "__internal" + }, + { + "group": "provisioning.grafana.app", + "kind": "Job", + "version": "v0alpha1" + } + ] + }, + "com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobList": { + "type": "object", + "properties": { + "apiVersion": { + "description": "APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources", + "type": "string" + }, + "items": { + "type": "array", + "items": { + "default": {}, + "allOf": [ + { + "$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.Job" + } + ] + } + }, + "kind": { + "description": "Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds", + "type": "string" + }, + "metadata": { + "default": {}, + "allOf": [ + { + "$ref": "#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.ListMeta" + } + ] + } + }, + "x-kubernetes-group-version-kind": [ + { + "group": "provisioning.grafana.app", + "kind": "JobList", + "version": "__internal" + }, + { + "group": "provisioning.grafana.app", + "kind": "JobList", + "version": "v0alpha1" + } + ] + }, + "com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobResourceSummary": { + "type": "object", + "properties": { + "create": { + "type": "integer", + "format": "int64" + }, + "delete": { + "type": "integer", + "format": "int64" + }, + "error": { + "description": "Create or update (export)", + "type": "integer", + "format": "int64" + }, + "errors": { + "description": "Report errors for this resource type This may not be an exhaustive list and recommend looking at the logs for more info", + "type": "array", + "items": { + "type": "string", + "default": "" + } + }, + "group": { + "type": "string" + }, + "noop": { + "description": "No action required (useful for sync)", + "type": "integer", + "format": "int64" + }, + "resource": { + "type": "string" + }, + "total": { + "type": "integer", + "format": "int64" + }, + "update": { + "type": "integer", + "format": "int64" + }, + "write": { + "type": "integer", + "format": "int64" + } + } + }, + "com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobSpec": { + "type": "object", + "required": [ + "action", + "repository" + ], + "properties": { + "action": { + "description": "Possible enum values:\n - `\"export\"` Export from grafana into the remote repository\n - `\"pr\"` Update a pull request -- send preview images, links etc\n - `\"sync\"` Sync the remote branch with the grafana instance", + "type": "string", + "default": "", + "enum": [ + "export", + "pr", + "sync" + ] + }, + "export": { + "description": "Required when the action is `export`", + "allOf": [ + { + "$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.ExportJobOptions" + } + ] + }, + "pr": { + "description": "Pull request options", + "allOf": [ + { + "$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.PullRequestJobOptions" + } + ] + }, + "repository": { + "description": "The the repository reference (for now also in labels)", + "type": "string", + "default": "" + }, + "sync": { + "description": "Required when the action is `sync`", + "allOf": [ + { + "$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.SyncJobOptions" + } + ] + } + } + }, + "com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobStatus": { + "description": "The job status", + "type": "object", + "properties": { + "errors": { + "type": "array", + "items": { + "type": "string", + "default": "" + } + }, + "finished": { + "type": "integer", + "format": "int64" + }, + "message": { + "type": "string" + }, + "progress": { + "description": "Optional value 0-100 that can be set while running", + "type": "number", + "format": "double" + }, + "started": { + "type": "integer", + "format": "int64" + }, + "state": { + "description": "Possible enum values:\n - `\"error\"` Finished with errors\n - `\"pending\"` Job has been submitted, but not processed yet\n - `\"success\"` Finished with success\n - `\"working\"` The job is running", + "type": "string", + "enum": [ + "error", + "pending", + "success", + "working" + ] + }, + "summary": { + "description": "Summary of processed actions", + "type": "array", + "items": { + "$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobResourceSummary" + } + } + } + }, "com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.LocalRepositoryConfig": { "type": "object", "properties": { @@ -1373,6 +2001,27 @@ } } }, + "com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.PullRequestJobOptions": { + "type": "object", + "properties": { + "hash": { + "type": "string" + }, + "pr": { + "description": "Pull request number (when appropriate)", + "type": "integer", + "format": "int32" + }, + "ref": { + "description": "The branch of commit hash", + "type": "string" + }, + "url": { + "description": "URL to the originator (eg, PR URL)", + "type": "string" + } + } + }, "com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.Repository": { "description": "When this code is changed, make sure to update the code generation. As of writing, this can be done via the hack dir in the root of the repo: ./hack/update-codegen.sh provisioning If you've opened the generated files in this dir at some point in VSCode, you may also have to re-open them to clear errors.", "type": "object", @@ -1472,7 +2121,7 @@ "type": "object", "required": [ "title", - "readOnly", + "workflows", "sync", "type" ], @@ -1497,11 +2146,6 @@ } ] }, - "readOnly": { - "description": "ReadOnly repository does not allow any write commands", - "type": "boolean", - "default": false - }, "sync": { "description": "Sync settings -- how values are pulled from the repository into grafana", "default": {}, @@ -1524,6 +2168,18 @@ "github", "local" ] + }, + "workflows": { + "description": "UI driven Workflow that allow changes to the contends of the repository. The order is relevant for defining the precedence of the workflows. When empty, the repository does not support any edits (eg, readonly)", + "type": "array", + "items": { + "type": "string", + "default": "", + "enum": [ + "branch", + "write" + ] + } } } }, @@ -1610,6 +2266,19 @@ } } }, + "com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.SyncJobOptions": { + "type": "object", + "required": [ + "incremental" + ], + "properties": { + "incremental": { + "description": "Incremental synchronization for versioned repositories", + "type": "boolean", + "default": false + } + } + }, "com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.SyncOptions": { "type": "object", "required": [ @@ -1650,10 +2319,6 @@ "type": "integer", "format": "int64" }, - "hash": { - "description": "The repository hash when the last sync ran", - "type": "string" - }, "incremental": { "description": "Incremental synchronization for versioned repositories", "type": "boolean" @@ -1662,6 +2327,10 @@ "description": "The ID for the job that ran this sync", "type": "string" }, + "lastRef": { + "description": "The repository ref when the last successful sync ran", + "type": "string" + }, "message": { "description": "Summary messages (will be shown to users)", "type": "array", diff --git a/pkg/tests/apis/provisioning/testdata/github-example.json b/pkg/tests/apis/provisioning/testdata/github-example.json index b737a51ff16..b9bcba83779 100644 --- a/pkg/tests/apis/provisioning/testdata/github-example.json +++ b/pkg/tests/apis/provisioning/testdata/github-example.json @@ -11,7 +11,6 @@ "github": { "url": "https://github.com/grafana/git-ui-sync-demo", "branch": "dummy-branch", - "branchWorkflow": true, "generateDashboardPreviews": true, "token": "github_pat_dummy" }, @@ -20,6 +19,6 @@ "target": "", "intervalSeconds": 60 }, - "readOnly": false + "workflows": ["push"] } } \ No newline at end of file diff --git a/pkg/tests/apis/provisioning/testdata/local-devenv.json b/pkg/tests/apis/provisioning/testdata/local-devenv.json index 39b8046a6ff..218f2715311 100644 --- a/pkg/tests/apis/provisioning/testdata/local-devenv.json +++ b/pkg/tests/apis/provisioning/testdata/local-devenv.json @@ -7,7 +7,7 @@ "spec": { "title": "Load devenv dashboards", "description": "Load /devenv/dev-dashboards (from root of repository)", - "readOnly": false, + "workflows": ["write"], "sync": { "enabled": true, "target": "mirror", From 0bbb6ab9475108aa532691086bb8090375f50c4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Jamr=C3=B3z?= Date: Mon, 24 Feb 2025 10:40:31 +0100 Subject: [PATCH 17/26] Explore: Make Drilldowns box dismissible (#101193) --- .../features/explore/DrilldownAlertBox.tsx | 42 +++++++++++++++++++ public/app/features/explore/Explore.tsx | 30 +------------ 2 files changed, 44 insertions(+), 28 deletions(-) create mode 100644 public/app/features/explore/DrilldownAlertBox.tsx diff --git a/public/app/features/explore/DrilldownAlertBox.tsx b/public/app/features/explore/DrilldownAlertBox.tsx new file mode 100644 index 00000000000..b6618f3f479 --- /dev/null +++ b/public/app/features/explore/DrilldownAlertBox.tsx @@ -0,0 +1,42 @@ +import { useLocalStorage } from 'react-use'; + +import { Alert, LinkButton, Stack } from '@grafana/ui'; + +import { t, Trans } from '../../core/internationalization'; + +type Props = { + datasourceType: string; +}; + +export function DrilldownAlertBox(props: Props) { + const isDsCompatibleWithDrilldown = ['prometheus', 'loki', 'tempo', 'grafana-pyroscope-datasource'].includes( + props.datasourceType + ); + + const [dismissed, setDismissed] = useLocalStorage('grafana.explore.drilldownsBoxDismissed', false); + + return ( + isDsCompatibleWithDrilldown && + !dismissed && ( + { + setDismissed(true); + }} + > + + + + Looking for the Grafana Explore apps? They are now called the Grafana Drilldown apps and can be found + under Menu > Drilldown + + + + Go to Grafana Drilldown + + + + ) + ); +} diff --git a/public/app/features/explore/Explore.tsx b/public/app/features/explore/Explore.tsx index 4927fc5be4a..7229276849e 100644 --- a/public/app/features/explore/Explore.tsx +++ b/public/app/features/explore/Explore.tsx @@ -23,18 +23,14 @@ import { getDataSourceSrv, reportInteraction } from '@grafana/runtime'; import { DataQuery } from '@grafana/schema'; import { AdHocFilterItem, - Alert, ErrorBoundaryAlert, - LinkButton, PanelContainer, ScrollContainer, - Stack, Themeable2, withTheme2, } from '@grafana/ui'; import { FILTER_FOR_OPERATOR, FILTER_OUT_OPERATOR } from '@grafana/ui/src/components/Table/types'; import { supportedFeatures } from 'app/core/history/richHistoryStorageProvider'; -import { t, Trans } from 'app/core/internationalization'; import { MIXED_DATASOURCE_NAME } from 'app/plugins/datasource/mixed/MixedDataSource'; import { StoreState } from 'app/types'; @@ -45,6 +41,7 @@ import { ContentOutlineContextProvider } from './ContentOutline/ContentOutlineCo import { ContentOutlineItem } from './ContentOutline/ContentOutlineItem'; import { CorrelationHelper } from './CorrelationHelper'; import { CustomContainer } from './CustomContainer'; +import { DrilldownAlertBox } from './DrilldownAlertBox'; import { ExploreToolbar } from './ExploreToolbar'; import { FlameGraphExploreContainer } from './FlameGraph/FlameGraphExploreContainer'; import { GraphContainer } from './Graph/GraphContainer'; @@ -568,9 +565,6 @@ export class Explore extends PureComponent { if (showCorrelationHelper && correlationEditorHelperData !== undefined) { correlationsBox = ; } - const isDsCompatibleWithDrilldown = ['prometheus', 'loki', 'tempo', 'grafana-pyroscope-datasource'].includes( - datasourceInstance?.type || '' - ); return ( @@ -600,27 +594,7 @@ export class Explore extends PureComponent { <> - {isDsCompatibleWithDrilldown && ( - - - - - Looking for the Grafana Explore apps? They are now called the Grafana Drilldown apps - and can be found under Menu > Drilldown - - - - Go to Grafana Drilldown - - - - )} + {correlationsBox} Date: Mon, 24 Feb 2025 10:00:22 +0000 Subject: [PATCH 18/26] Alerting: Add webhook timeout option for upstream alertmanagers (#101154) --- .../unified/utils/cloud-alertmanager-notifier-types.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/public/app/features/alerting/unified/utils/cloud-alertmanager-notifier-types.ts b/public/app/features/alerting/unified/utils/cloud-alertmanager-notifier-types.ts index a172c9cd19d..96bc19b4aff 100644 --- a/public/app/features/alerting/unified/utils/cloud-alertmanager-notifier-types.ts +++ b/public/app/features/alerting/unified/utils/cloud-alertmanager-notifier-types.ts @@ -400,6 +400,14 @@ export const cloudNotifierTypes: Array> = [ }, } ), + option( + 'timeout', + 'Timeout', + 'The maximum time to wait for a webhook request to complete, before failing the request and allowing it to be retried. The default value of 0s indicates that no timeout should be applied. NOTE: This will have no effect if set higher than the group_interval.', + { + placeholder: 'Use duration format, for example: 1.2s, 100ms', + } + ), httpConfigOption, ], }, From 01b57f412f005c58d9739f580c671df183df65cb Mon Sep 17 00:00:00 2001 From: Oscar Kilhed Date: Mon, 24 Feb 2025 11:35:55 +0100 Subject: [PATCH 19/26] =?UTF-8?q?Dashboards:=20WeekStart=20is=20now=20of?= =?UTF-8?q?=20type=20WeekStart=20|=C2=A0undefined=20instead=20of=20string?= =?UTF-8?q?=20(#101123)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * change weektype from string to WeekStart | undefined * Change to WeekStart in more places, fix lint * change in more places * More weekstart changes * fix snapshot, update betterer * keep weekstart as '' in test dashboards to make sure it doesn't break old dashboards --- .betterer.results | 8 ++------ e2e/dashboards-suite/utils/makeDashboard.ts | 2 +- .../components/DateTimePickers/WeekStartPicker.tsx | 12 ++++++------ packages/grafana-ui/src/components/index.ts | 2 +- .../SharedPreferences/SharedPreferences.tsx | 9 +++++---- .../transformSceneToSaveModel.test.ts.snap | 1 - .../serialization/transformSaveModelToScene.ts | 3 ++- .../settings/GeneralSettingsEditView.tsx | 4 ++-- .../features/dashboard/api/ResponseTransformers.ts | 12 +++++++++--- .../components/DashNav/DashNavTimeControls.tsx | 4 ++-- .../components/DashboardSettings/GeneralSettings.tsx | 3 ++- .../DashboardSettings/TimePickerSettings.tsx | 6 +++--- public/app/features/dashboard/state/actions.ts | 3 ++- public/app/features/dashboard/state/initDashboard.ts | 2 +- public/app/features/profile/state/reducers.ts | 7 ++++--- 15 files changed, 42 insertions(+), 36 deletions(-) diff --git a/.betterer.results b/.betterer.results index 4197643351c..b4f67dde103 100644 --- a/.betterer.results +++ b/.betterer.results @@ -635,9 +635,6 @@ exports[`better eslint`] = { [0, 0, 0, "No untranslated strings in text props. Wrap text with or use t()", "1"], [0, 0, 0, "No untranslated strings in text props. Wrap text with or use t()", "2"] ], - "packages/grafana-ui/src/components/DateTimePickers/WeekStartPicker.tsx:5381": [ - [0, 0, 0, "Do not use any type assertions.", "0"] - ], "packages/grafana-ui/src/components/FileDropzone/FileDropzone.tsx:5381": [ [0, 0, 0, "No untranslated strings in text props. Wrap text with or use t()", "0"] ], @@ -3661,10 +3658,9 @@ exports[`better eslint`] = { [0, 0, 0, "Do not use any type assertions.", "1"], [0, 0, 0, "Do not use any type assertions.", "2"], [0, 0, 0, "Do not use any type assertions.", "3"], - [0, 0, 0, "Do not use any type assertions.", "4"], + [0, 0, 0, "Unexpected any. Specify a different type.", "4"], [0, 0, 0, "Unexpected any. Specify a different type.", "5"], - [0, 0, 0, "Unexpected any. Specify a different type.", "6"], - [0, 0, 0, "Unexpected any. Specify a different type.", "7"] + [0, 0, 0, "Unexpected any. Specify a different type.", "6"] ], "public/app/features/dashboard/api/v1.ts:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"] diff --git a/e2e/dashboards-suite/utils/makeDashboard.ts b/e2e/dashboards-suite/utils/makeDashboard.ts index 4deaaa773ce..2ada48db7a9 100644 --- a/e2e/dashboards-suite/utils/makeDashboard.ts +++ b/e2e/dashboards-suite/utils/makeDashboard.ts @@ -48,8 +48,8 @@ export function makeNewDashboardRequestBody(dashboardName: string, folderUid?: s timezone: '', title: dashboardName, version: 0, - weekStart: '', uid: '', + weekStart: '', }, message: '', overwrite: false, diff --git a/packages/grafana-ui/src/components/DateTimePickers/WeekStartPicker.tsx b/packages/grafana-ui/src/components/DateTimePickers/WeekStartPicker.tsx index aebcc7075d2..139a03713bd 100644 --- a/packages/grafana-ui/src/components/DateTimePickers/WeekStartPicker.tsx +++ b/packages/grafana-ui/src/components/DateTimePickers/WeekStartPicker.tsx @@ -7,8 +7,8 @@ import { Combobox } from '../Combobox/Combobox'; import { ComboboxOption } from '../Combobox/types'; export interface Props { - onChange: (weekStart: WeekStart) => void; - value: string; + onChange: (weekStart?: WeekStart) => void; + value?: WeekStart; width?: number; autoFocus?: boolean; onBlur?: () => void; @@ -24,9 +24,9 @@ const weekStarts: ComboboxOption[] = [ { value: 'monday', label: 'Monday' }, ]; -const isWeekStart = (value: string): value is WeekStart => { +export function isWeekStart(value: string): value is WeekStart { return ['saturday', 'sunday', 'monday'].includes(value); -}; +} declare global { interface Window { @@ -57,13 +57,13 @@ export const WeekStartPicker = (props: Props) => { const onChangeWeekStart = useCallback( (selectable: ComboboxOption | null) => { if (selectable && selectable.value !== undefined) { - onChange(selectable.value as WeekStart); + onChange(isWeekStart(selectable.value) ? selectable.value : undefined); } }, [onChange] ); - const selected = useMemo(() => weekStarts.find((item) => item.value === value)?.value ?? null, [value]); + const selected = useMemo(() => weekStarts.find((item) => item.value === value)?.value ?? '', [value]); return ( { this.setState({ timezone: timezone }); }; - onWeekStartChanged = (weekStart: string) => { - this.setState({ weekStart: weekStart }); + onWeekStartChanged = (weekStart?: WeekStart) => { + this.setState({ weekStart: weekStart ?? '' }); }; onHomeDashboardChanged = (dashboardUID: string) => { @@ -249,7 +250,7 @@ export class SharedPreferences extends PureComponent { data-testid={selectors.components.WeekStartPicker.containerV2} > diff --git a/public/app/features/dashboard-scene/serialization/__snapshots__/transformSceneToSaveModel.test.ts.snap b/public/app/features/dashboard-scene/serialization/__snapshots__/transformSceneToSaveModel.test.ts.snap index 8146499446c..585b51daf4e 100644 --- a/public/app/features/dashboard-scene/serialization/__snapshots__/transformSceneToSaveModel.test.ts.snap +++ b/public/app/features/dashboard-scene/serialization/__snapshots__/transformSceneToSaveModel.test.ts.snap @@ -334,7 +334,6 @@ exports[`transformSceneToSaveModel Given a scene with rows Should transform back "title": "Repeating rows", "uid": "Repeating-rows-uid", "version": 1, - "weekStart": "", } `; diff --git a/public/app/features/dashboard-scene/serialization/transformSaveModelToScene.ts b/public/app/features/dashboard-scene/serialization/transformSaveModelToScene.ts index b5ca18ea717..b271697a87e 100644 --- a/public/app/features/dashboard-scene/serialization/transformSaveModelToScene.ts +++ b/public/app/features/dashboard-scene/serialization/transformSaveModelToScene.ts @@ -22,6 +22,7 @@ import { SceneInteractionProfileEvent, SceneObjectState, } from '@grafana/scenes'; +import { isWeekStart } from '@grafana/ui'; import { contextSrv } from 'app/core/core'; import { DashboardModel } from 'app/features/dashboard/state/DashboardModel'; import { PanelModel } from 'app/features/dashboard/state/PanelModel'; @@ -274,7 +275,7 @@ export function createDashboardSceneFromDashboardModel(oldModel: DashboardModel, to: oldModel.time.to, fiscalYearStartMonth: oldModel.fiscalYearStartMonth, timeZone: oldModel.timezone, - weekStart: oldModel.weekStart, + weekStart: isWeekStart(oldModel.weekStart) ? oldModel.weekStart : undefined, UNSAFE_nowDelay: oldModel.timepicker?.nowDelay, }), $variables: variables, diff --git a/public/app/features/dashboard-scene/settings/GeneralSettingsEditView.tsx b/public/app/features/dashboard-scene/settings/GeneralSettingsEditView.tsx index ef7ae0489e6..d688616afc0 100644 --- a/public/app/features/dashboard-scene/settings/GeneralSettingsEditView.tsx +++ b/public/app/features/dashboard-scene/settings/GeneralSettingsEditView.tsx @@ -123,7 +123,7 @@ export class GeneralSettingsEditView }); }; - public onWeekStartChange = (value: WeekStart) => { + public onWeekStartChange = (value?: WeekStart) => { this.getTimeRange().setState({ weekStart: value }); }; @@ -258,7 +258,7 @@ export class GeneralSettingsEditView nowDelay={nowDelay || ''} liveNow={liveNow} timezone={timeZone || ''} - weekStart={weekStart || ''} + weekStart={weekStart} /> {/* @todo: Update "Graph tooltip" description to remove prompt about reloading when resolving #46581 */} diff --git a/public/app/features/dashboard/api/ResponseTransformers.ts b/public/app/features/dashboard/api/ResponseTransformers.ts index b57465de211..5ad1d7836d6 100644 --- a/public/app/features/dashboard/api/ResponseTransformers.ts +++ b/public/app/features/dashboard/api/ResponseTransformers.ts @@ -41,7 +41,7 @@ import { GridLayoutItemKind, } from '@grafana/schema/dist/esm/schema/dashboard/v2alpha0'; import { DashboardLink, DataTransformerConfig } from '@grafana/schema/src/raw/dashboard/x/dashboard_types.gen'; -import { WeekStart } from '@grafana/ui'; +import { isWeekStart, WeekStart } from '@grafana/ui'; import { AnnoKeyCreatedBy, AnnoKeyDashboardGnetId, @@ -161,8 +161,7 @@ export function ensureV2Response( fiscalYearStartMonth: dashboard.fiscalYearStartMonth || timeSettingsDefaults.fiscalYearStartMonth, hideTimepicker: dashboard.timepicker?.hidden || timeSettingsDefaults.hideTimepicker, quickRanges: dashboard.timepicker?.quick_ranges, - // casting WeekStart here to avoid editing old schema - weekStart: (dashboard.weekStart as WeekStart) || timeSettingsDefaults.weekStart, + weekStart: getWeekStart(dashboard.weekStart, timeSettingsDefaults.weekStart), nowDelay: dashboard.timepicker?.nowDelay || timeSettingsDefaults.nowDelay, }, links: dashboard.links || [], @@ -332,6 +331,13 @@ function isRowPanel(panel: Panel | RowPanel): panel is RowPanel { return panel.type === 'row'; } +function getWeekStart(weekStart?: string, defaultWeekStart?: WeekStart): WeekStart | undefined { + if (!weekStart || !isWeekStart(weekStart)) { + return defaultWeekStart; + } + return weekStart; +} + function buildRowKind(p: RowPanel, elements: GridLayoutItemKind[]): GridLayoutRowKind { return { kind: 'GridLayoutRow', diff --git a/public/app/features/dashboard/components/DashNav/DashNavTimeControls.tsx b/public/app/features/dashboard/components/DashNav/DashNavTimeControls.tsx index 7be88761ec9..6c66c66dc82 100644 --- a/public/app/features/dashboard/components/DashNav/DashNavTimeControls.tsx +++ b/public/app/features/dashboard/components/DashNav/DashNavTimeControls.tsx @@ -3,7 +3,7 @@ import { Unsubscribable } from 'rxjs'; import { dateMath, TimeRange, TimeZone } from '@grafana/data'; import { TimeRangeUpdatedEvent } from '@grafana/runtime'; -import { defaultIntervals, RefreshPicker } from '@grafana/ui'; +import { defaultIntervals, isWeekStart, RefreshPicker } from '@grafana/ui'; import { TimePickerWithHistory } from 'app/core/components/TimePicker/TimePickerWithHistory'; import { appEvents } from 'app/core/core'; import { t } from 'app/core/internationalization'; @@ -121,7 +121,7 @@ export class DashNavTimeControls extends Component { onChangeFiscalYearStartMonth={this.onChangeFiscalYearStartMonth} isOnCanvas={isOnCanvas} onToolbarTimePickerClick={this.props.onToolbarTimePickerClick} - weekStart={weekStart} + weekStart={isWeekStart(weekStart) ? weekStart : undefined} quickRanges={quick_ranges} /> { + const onWeekStartChange = (weekStart?: WeekStart) => { dashboard.weekStart = weekStart; setRenderCounter(renderCounter + 1); updateWeekStart(weekStart); diff --git a/public/app/features/dashboard/components/DashboardSettings/TimePickerSettings.tsx b/public/app/features/dashboard/components/DashboardSettings/TimePickerSettings.tsx index d88e3deaa64..1b31c3ee035 100644 --- a/public/app/features/dashboard/components/DashboardSettings/TimePickerSettings.tsx +++ b/public/app/features/dashboard/components/DashboardSettings/TimePickerSettings.tsx @@ -10,7 +10,7 @@ import { t } from 'app/core/internationalization'; import { AutoRefreshIntervals } from './AutoRefreshIntervals'; interface Props { - onWeekStartChange: (weekStart: WeekStart) => void; + onWeekStartChange: (weekStart?: WeekStart) => void; onTimeZoneChange: (timeZone: TimeZone) => void; onRefreshIntervalChange: (interval: string[]) => void; onNowDelayChange: (nowDelay: string) => void; @@ -20,7 +20,7 @@ interface Props { timePickerHidden?: boolean; nowDelay?: string; timezone: TimeZone; - weekStart: string; + weekStart?: WeekStart; liveNow?: boolean; } @@ -62,7 +62,7 @@ export class TimePickerSettings extends PureComponent { this.props.onTimeZoneChange(timeZone); }; - onWeekStartChange = (weekStart: WeekStart) => { + onWeekStartChange = (weekStart?: WeekStart) => { this.props.onWeekStartChange(weekStart); }; diff --git a/public/app/features/dashboard/state/actions.ts b/public/app/features/dashboard/state/actions.ts index 3f9b8d529f8..ed28663f745 100644 --- a/public/app/features/dashboard/state/actions.ts +++ b/public/app/features/dashboard/state/actions.ts @@ -1,5 +1,6 @@ import { TimeZone } from '@grafana/data'; import { getBackendSrv } from '@grafana/runtime'; +import { WeekStart } from '@grafana/ui'; import { notifyApp } from 'app/core/actions'; import { createSuccessNotification } from 'app/core/copy/appNotification'; import { getDashboardAPI } from 'app/features/dashboard/api/dashboard_api'; @@ -56,7 +57,7 @@ export const updateTimeZoneDashboard = }; export const updateWeekStartDashboard = - (weekStart: string): ThunkResult => + (weekStart?: WeekStart): ThunkResult => (dispatch) => { dispatch(updateWeekStartForSession(weekStart)); getTimeSrv().refreshTimeModel(); diff --git a/public/app/features/dashboard/state/initDashboard.ts b/public/app/features/dashboard/state/initDashboard.ts index 23b36244519..71c5cbf79eb 100644 --- a/public/app/features/dashboard/state/initDashboard.ts +++ b/public/app/features/dashboard/state/initDashboard.ts @@ -273,7 +273,7 @@ export function initDashboard(args: InitDashboardArgs): ThunkResult { } // set week start - if (dashboard.weekStart !== '') { + if (dashboard.weekStart !== '' && dashboard.weekStart !== undefined) { setWeekStart(dashboard.weekStart); } else { setWeekStart(config.bootData.user.weekStart); diff --git a/public/app/features/profile/state/reducers.ts b/public/app/features/profile/state/reducers.ts index 5098fa3670d..f4de9c34f0b 100644 --- a/public/app/features/profile/state/reducers.ts +++ b/public/app/features/profile/state/reducers.ts @@ -2,6 +2,7 @@ import { createSlice, PayloadAction } from '@reduxjs/toolkit'; import { isEmpty, isString, set } from 'lodash'; import { dateTimeFormatTimeAgo, setWeekStart, TimeZone } from '@grafana/data'; +import { getWeekStart, WeekStart } from '@grafana/ui'; import config from 'app/core/config'; import { contextSrv } from 'app/core/core'; import { Team, ThunkResult, UserDTO, UserOrg, UserSession } from 'app/types'; @@ -116,10 +117,10 @@ export const updateTimeZoneForSession = (timeZone: TimeZone): ThunkResult }; }; -export const updateWeekStartForSession = (weekStart: string): ThunkResult => { +export const updateWeekStartForSession = (weekStart?: WeekStart): ThunkResult => { return async (dispatch) => { - if (!isString(weekStart) || isEmpty(weekStart)) { - weekStart = config?.bootData?.user?.weekStart; + if (!weekStart) { + weekStart = getWeekStart(); } set(contextSrv, 'user.weekStart', weekStart); From 10b4868d910bd0ce0a0bb69c55718a2f2400309a Mon Sep 17 00:00:00 2001 From: Gilles De Mey Date: Mon, 24 Feb 2025 12:26:17 +0100 Subject: [PATCH 20/26] Alerting: Use RTKQ for fetching folder information (#100645) --- .../alerting/unified/hooks/useFolder.ts | 34 ++++++------------- .../alerting/unified/state/actions.ts | 16 +-------- .../alerting/unified/state/reducers.ts | 2 -- .../api/browseDashboardsAPI.ts | 1 + 4 files changed, 13 insertions(+), 40 deletions(-) diff --git a/public/app/features/alerting/unified/hooks/useFolder.ts b/public/app/features/alerting/unified/hooks/useFolder.ts index 8939ae8ad4c..7b06bf92020 100644 --- a/public/app/features/alerting/unified/hooks/useFolder.ts +++ b/public/app/features/alerting/unified/hooks/useFolder.ts @@ -1,35 +1,23 @@ -import { useEffect } from 'react'; +import { skipToken } from '@reduxjs/toolkit/query/react'; -import { FolderDTO, useDispatch } from 'app/types'; - -import { fetchFolderIfNotFetchedAction } from '../state/actions'; -import { initialAsyncRequestState } from '../utils/redux'; - -import { useUnifiedAlertingSelector } from './useUnifiedAlertingSelector'; +import { useGetFolderQuery } from 'app/features/browse-dashboards/api/browseDashboardsAPI'; +import { FolderDTO } from 'app/types'; interface ReturnBag { folder?: FolderDTO; loading: boolean; } +/** + * Returns a folderDTO for the given uid – uses cached values + * @TODO propagate error state + */ export function useFolder(uid?: string): ReturnBag { - const dispatch = useDispatch(); - const folderRequests = useUnifiedAlertingSelector((state) => state.folders); - useEffect(() => { - if (uid) { - dispatch(fetchFolderIfNotFetchedAction(uid)); - } - }, [dispatch, uid]); + const fetchFolderState = useGetFolderQuery(uid || skipToken); - if (uid) { - const request = folderRequests[uid] || initialAsyncRequestState; - return { - folder: request.result, - loading: request.loading, - }; - } return { - loading: false, + loading: fetchFolderState.isLoading, + folder: fetchFolderState.data, }; } @@ -39,6 +27,6 @@ export function stringifyFolder({ title, parents }: FolderDTO) { : encodeTitle(title); } -export function encodeTitle(title: string): string { +function encodeTitle(title: string): string { return title.replaceAll('/', '\\/'); } diff --git a/public/app/features/alerting/unified/state/actions.ts b/public/app/features/alerting/unified/state/actions.ts index 91adf4a5568..5b03e36fe6b 100644 --- a/public/app/features/alerting/unified/state/actions.ts +++ b/public/app/features/alerting/unified/state/actions.ts @@ -10,11 +10,10 @@ import { Receiver, TestReceiversAlert, } from 'app/plugins/datasource/alertmanager/types'; -import { FolderDTO, ThunkResult } from 'app/types'; +import { ThunkResult } from 'app/types'; import { RuleIdentifier, RuleNamespace, StateHistoryItem } from 'app/types/unified-alerting'; import { RulerRuleDTO, RulerRulesConfigDTO } from 'app/types/unified-alerting-dto'; -import { backendSrv } from '../../../../core/services/backend_srv'; import { withPromRulesMetadataLogging, withRulerRulesMetadataLogging } from '../Analytics'; import { deleteAlertManagerConfig, @@ -241,19 +240,6 @@ export const updateAlertManagerConfigAction = createAsyncThunk => withSerializedError(backendSrv.getFolderByUid(uid, { withAccessControl: true })) -); - -export const fetchFolderIfNotFetchedAction = (uid: string): ThunkResult => { - return (dispatch, getState) => { - if (!getState().unifiedAlerting.folders[uid]?.dispatched) { - dispatch(fetchFolderAction(uid)); - } - }; -}; - export const fetchAlertGroupsAction = createAsyncThunk( 'unifiedalerting/fetchAlertGroups', (alertManagerSourceName: string): Promise => { diff --git a/public/app/features/alerting/unified/state/reducers.ts b/public/app/features/alerting/unified/state/reducers.ts index 19153a3bee1..71ba6a54e66 100644 --- a/public/app/features/alerting/unified/state/reducers.ts +++ b/public/app/features/alerting/unified/state/reducers.ts @@ -5,7 +5,6 @@ import { createAsyncMapSlice, createAsyncSlice } from '../utils/redux'; import { deleteAlertManagerConfigAction, fetchAlertGroupsAction, - fetchFolderAction, fetchGrafanaAnnotationsAction, fetchPromRulesAction, fetchRulerRulesAction, @@ -19,7 +18,6 @@ export const reducer = combineReducers({ .reducer, saveAMConfig: createAsyncSlice('saveAMConfig', updateAlertManagerConfigAction).reducer, deleteAMConfig: createAsyncSlice('deleteAMConfig', deleteAlertManagerConfigAction).reducer, - folders: createAsyncMapSlice('folders', fetchFolderAction, (uid) => uid).reducer, amAlertGroups: createAsyncMapSlice( 'amAlertGroups', fetchAlertGroupsAction, diff --git a/public/app/features/browse-dashboards/api/browseDashboardsAPI.ts b/public/app/features/browse-dashboards/api/browseDashboardsAPI.ts index fb411699d8a..51f08201927 100644 --- a/public/app/features/browse-dashboards/api/browseDashboardsAPI.ts +++ b/public/app/features/browse-dashboards/api/browseDashboardsAPI.ts @@ -446,6 +446,7 @@ export const { useDeleteItemsMutation, useGetAffectedItemsQuery, useGetFolderQuery, + useLazyGetFolderQuery, useMoveFolderMutation, useMoveItemsMutation, useNewFolderMutation, From 9f00e086e4d3a148be3687e13645fb9604e84a84 Mon Sep 17 00:00:00 2001 From: Sonia Aguilar <33540275+soniaAguilarPeiron@users.noreply.github.com> Date: Mon, 24 Feb 2025 13:10:23 +0100 Subject: [PATCH 21/26] Alerting: Use uid instead of id in AnnotationsStateHistory (#101207) Use uid instead of id in AnnotationsStateHistory --- .../features/alerting/unified/hooks/useStateHistoryModal.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/app/features/alerting/unified/hooks/useStateHistoryModal.tsx b/public/app/features/alerting/unified/hooks/useStateHistoryModal.tsx index 3da0bc08e07..8c33574bf6c 100644 --- a/public/app/features/alerting/unified/hooks/useStateHistoryModal.tsx +++ b/public/app/features/alerting/unified/hooks/useStateHistoryModal.tsx @@ -61,7 +61,7 @@ function useStateHistoryModal() { {implementation === StateHistoryImplementation.Loki && } {implementation === StateHistoryImplementation.Annotations && ( - + )} From 19789cf5f8a1236d5592db301c893afa7644a3a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20Fern=C3=A1ndez?= Date: Mon, 24 Feb 2025 13:12:56 +0100 Subject: [PATCH 22/26] Combobox: use `useOptions` (#100604) --- .betterer.results | 3 - .../components/MetricCombobox.test.tsx | 25 +-- .../src/components/Combobox/Combobox.test.tsx | 19 +- .../src/components/Combobox/Combobox.tsx | 165 +++++------------- .../src/components/Combobox/useOptions.ts | 21 +-- 5 files changed, 77 insertions(+), 156 deletions(-) diff --git a/.betterer.results b/.betterer.results index b4f67dde103..eedd45faab7 100644 --- a/.betterer.results +++ b/.betterer.results @@ -540,9 +540,6 @@ exports[`better eslint`] = { [0, 0, 0, "No untranslated strings in text props. Wrap text with or use t()", "0"], [0, 0, 0, "No untranslated strings in text props. Wrap text with or use t()", "1"] ], - "packages/grafana-ui/src/components/Combobox/Combobox.tsx:5381": [ - [0, 0, 0, "Do not use any type assertions.", "0"] - ], "packages/grafana-ui/src/components/Combobox/MultiCombobox.tsx:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"] ], diff --git a/packages/grafana-prometheus/src/querybuilder/components/MetricCombobox.test.tsx b/packages/grafana-prometheus/src/querybuilder/components/MetricCombobox.test.tsx index c9ff1d55cd2..c832a547bdc 100644 --- a/packages/grafana-prometheus/src/querybuilder/components/MetricCombobox.test.tsx +++ b/packages/grafana-prometheus/src/querybuilder/components/MetricCombobox.test.tsx @@ -35,9 +35,13 @@ describe('MetricCombobox', () => { } as unknown as DataSourceInstanceSettings; const mockDatasource = new PrometheusDatasource(instanceSettings); - const mockValues = [{ label: 'random_metric' }, { label: 'unique_metric' }, { label: 'more_unique_metric' }]; - // Mock metricFindQuery which will call backend API + // Options returned when user first opens the combobox - returned by onGetMetrics + const initialMockValues = [{ label: 'top_metric_one' }, { label: 'top_metric_two' }, { label: 'top_metric_three' }]; + const mockOnGetMetrics = jest.fn(() => Promise.resolve(initialMockValues.map((v) => ({ value: v.label })))); + + // Options returned when user searches for a metric + const mockValues = [{ label: 'random_metric' }, { label: 'unique_metric' }, { label: 'more_unique_metric' }]; mockDatasource.metricFindQuery = jest.fn((query: string) => { // return Promise.resolve([]); // Use the label values regex to get the values inside the label_values function call @@ -61,7 +65,6 @@ describe('MetricCombobox', () => { }); const mockOnChange = jest.fn(); - const mockOnGetMetrics = jest.fn(() => Promise.resolve(mockValues.map((v) => ({ value: v.label })))); const defaultProps: MetricComboboxProps = { metricLookupDisabled: false, @@ -92,10 +95,11 @@ describe('MetricCombobox', () => { const combobox = screen.getByPlaceholderText('Select metric'); await userEvent.click(combobox); - expect(mockOnGetMetrics).toHaveBeenCalledTimes(1); - - const item = await screen.findByRole('option', { name: 'random_metric' }); + const item = await screen.findByRole('option', { name: 'top_metric_one' }); expect(item).toBeInTheDocument(); + + // This should be asserted by the above check, but double check anyway + expect(mockOnGetMetrics).toHaveBeenCalledTimes(1); }); it('fetches metrics for the users query', async () => { @@ -108,8 +112,9 @@ describe('MetricCombobox', () => { const item = await screen.findByRole('option', { name: 'unique_metric' }); expect(item).toBeInTheDocument(); - const negativeItem = screen.queryByRole('option', { name: 'random_metric' }); - expect(negativeItem).not.toBeInTheDocument(); + // This should be asserted by the above check, but double check anyway + // This is the actual argument, created by formatKeyValueStringsForLabelValuesQuery() + expect(mockDatasource.metricFindQuery).toHaveBeenCalledWith('label_values({__name__=~".*unique.*"},__name__)'); }); it('calls onChange with the correct value when a metric is selected', async () => { @@ -118,10 +123,10 @@ describe('MetricCombobox', () => { const combobox = screen.getByPlaceholderText('Select metric'); await userEvent.click(combobox); - const item = await screen.findByRole('option', { name: 'random_metric' }); + const item = await screen.findByRole('option', { name: 'top_metric_two' }); await userEvent.click(item); - expect(mockOnChange).toHaveBeenCalledWith({ metric: 'random_metric', labels: [], operations: [] }); + expect(mockOnChange).toHaveBeenCalledWith({ metric: 'top_metric_two', labels: [], operations: [] }); }); it('shows the metrics explorer button by default', () => { diff --git a/packages/grafana-ui/src/components/Combobox/Combobox.test.tsx b/packages/grafana-ui/src/components/Combobox/Combobox.test.tsx index 6127c007e56..e54c94d1977 100644 --- a/packages/grafana-ui/src/components/Combobox/Combobox.test.tsx +++ b/packages/grafana-ui/src/components/Combobox/Combobox.test.tsx @@ -1,4 +1,4 @@ -import { act, render, screen, fireEvent } from '@testing-library/react'; +import { act, render, screen, fireEvent, waitFor } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import React from 'react'; @@ -395,7 +395,9 @@ describe('Combobox', () => { const input = screen.getByRole('combobox'); await user.click(input); - expect(asyncSpy).toHaveBeenCalledTimes(1); // Called on open + expect(asyncSpy).not.toHaveBeenCalledTimes(1); // Not called yet + act(() => jest.advanceTimersByTime(200)); // Add the debounce time + expect(asyncSpy).toHaveBeenCalledTimes(1); // Then check if called on open asyncSpy.mockClear(); await user.keyboard('a'); @@ -434,9 +436,9 @@ describe('Combobox', () => { }); it('should display message when there is an error loading async options', async () => { - const asyncOptions = jest.fn(() => { - throw new Error('Could not retrieve options'); - }); + const fetchData = jest.fn(); + const asyncOptions = fetchData.mockRejectedValue(new Error('Could not retrieve options')); + const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(); render(); @@ -445,12 +447,15 @@ describe('Combobox', () => { await user.type(input, 'test'); await act(async () => { - jest.advanceTimersToNextTimer(); + jest.advanceTimersByTimeAsync(500); }); + expect(asyncOptions).rejects.toThrow('Could not retrieve options'); + await waitFor(() => expect(consoleErrorSpy).toHaveBeenCalled()); const emptyMessage = screen.queryByText('An error occurred while loading options.'); - expect(emptyMessage).toBeInTheDocument(); + + asyncOptions.mockClear(); }); describe('with a value already selected', () => { diff --git a/packages/grafana-ui/src/components/Combobox/Combobox.tsx b/packages/grafana-ui/src/components/Combobox/Combobox.tsx index f69d15b3038..2a1ea6ae28c 100644 --- a/packages/grafana-ui/src/components/Combobox/Combobox.tsx +++ b/packages/grafana-ui/src/components/Combobox/Combobox.tsx @@ -1,11 +1,9 @@ import { cx } from '@emotion/css'; import { useVirtualizer } from '@tanstack/react-virtual'; import { useCombobox } from 'downshift'; -import { debounce } from 'lodash'; -import { useCallback, useId, useMemo, useState } from 'react'; +import { useId, useMemo } from 'react'; import { useStyles2 } from '../../themes'; -import { logOptions } from '../../utils'; import { t } from '../../utils/i18n'; import { Icon } from '../Icon/Icon'; import { AutoSizeInput } from '../Input/AutoSizeInput'; @@ -14,11 +12,11 @@ import { Portal } from '../Portal/Portal'; import { ScrollContainer } from '../ScrollContainer/ScrollContainer'; import { AsyncError, NotFoundError } from './MessageRows'; -import { fuzzyFind, itemToString } from './filter'; +import { itemToString } from './filter'; import { getComboboxStyles, MENU_OPTION_HEIGHT, MENU_OPTION_HEIGHT_DESCRIPTION } from './getComboboxStyles'; import { ComboboxOption } from './types'; import { useComboboxFloat } from './useComboboxFloat'; -import { StaleResultError, useLatestAsyncCall } from './useLatestAsyncCall'; +import { useOptions } from './useOptions'; // TODO: It would be great if ComboboxOption["label"] was more generic so that if consumers do pass it in (for async), // then the onChange handler emits ComboboxOption with the label as non-undefined. @@ -64,8 +62,6 @@ export interface ComboboxBaseProps onBlur?: () => void; } -const RECOMMENDED_ITEMS_AMOUNT = 100_000; - type ClearableConditionals = | { /** @@ -102,7 +98,6 @@ export type ComboboxProps = ComboboxBaseProps & ClearableConditionals; const noop = () => {}; -const asyncNoop = () => Promise.resolve([]); export const VIRTUAL_OVERSCAN_ITEMS = 4; @@ -113,7 +108,7 @@ export const VIRTUAL_OVERSCAN_ITEMS = 4; */ export const Combobox = (props: ComboboxProps) => { const { - options, + options: allOptions, onChange, value: valueProp, placeholder: placeholderProp, @@ -135,45 +130,13 @@ export const Combobox = (props: ComboboxProps) => // get a consistent Value from it const value = typeof valueProp === 'object' ? valueProp?.value : valueProp; - const isAsync = typeof options === 'function'; - const loadOptions = useLatestAsyncCall(isAsync ? options : asyncNoop); // loadOptions isn't called at all if not async - const [asyncLoading, setAsyncLoading] = useState(false); - const [asyncError, setAsyncError] = useState(false); - - // A custom setter to always prepend the custom value at the beginning, if needed - const [items, baseSetItems] = useState(isAsync ? [] : options); - const setItems = useCallback( - (items: Array>, inputValue: string | undefined) => { - let itemsToSet = items; - logOptions(itemsToSet.length, RECOMMENDED_ITEMS_AMOUNT, id, ariaLabelledBy); - if (inputValue && createCustomValue) { - //Since the label of a normal option does not have to match its value and a custom option has the same value and label, - //we just focus on the value to check if the option already exists - const optionMatchingInput = items.find((opt) => opt.value === inputValue); - - if (!optionMatchingInput) { - const customValueOption = { - label: inputValue, - // Type casting needed to make this work when T is a number - value: inputValue as T, - description: t('combobox.custom-value.description', 'Use custom value'), - }; - - itemsToSet = items.slice(0); - itemsToSet.unshift(customValueOption); - } - } - - baseSetItems(itemsToSet); - }, - [createCustomValue, id, ariaLabelledBy] - ); - - // Memoize for using in fuzzy search - const stringifiedItems = useMemo( - () => (isAsync ? [] : options.map((item) => itemToString(item))), - [options, isAsync] - ); + const { + options: filteredOptions, + updateOptions, + asyncLoading, + asyncError, + } = useOptions(props.options, createCustomValue); + const isAsync = typeof allOptions === 'function'; const selectedItemIndex = useMemo(() => { if (isAsync) { @@ -184,13 +147,13 @@ export const Combobox = (props: ComboboxProps) => return null; } - const index = options.findIndex((option) => option.value === value); + const index = allOptions.findIndex((option) => option.value === value); if (index === -1) { return null; } return index; - }, [valueProp, options, value, isAsync]); + }, [valueProp, allOptions, value, isAsync]); const selectedItem = useMemo(() => { if (valueProp === undefined || valueProp === null) { @@ -198,11 +161,11 @@ export const Combobox = (props: ComboboxProps) => } if (selectedItemIndex !== null && !isAsync) { - return options[selectedItemIndex]; + return allOptions[selectedItemIndex]; } return typeof valueProp === 'object' ? valueProp : { value: valueProp, label: valueProp.toString() }; - }, [selectedItemIndex, isAsync, valueProp, options]); + }, [selectedItemIndex, isAsync, valueProp, allOptions]); const menuId = `downshift-${useId().replace(/:/g, '--')}-menu`; const labelId = `downshift-${useId().replace(/:/g, '--')}-label`; @@ -210,33 +173,15 @@ export const Combobox = (props: ComboboxProps) => const styles = useStyles2(getComboboxStyles); const virtualizerOptions = { - count: items.length, + count: filteredOptions.length, getScrollElement: () => scrollRef.current, - estimateSize: (index: number) => (items[index].description ? MENU_OPTION_HEIGHT_DESCRIPTION : MENU_OPTION_HEIGHT), + estimateSize: (index: number) => + filteredOptions[index].description ? MENU_OPTION_HEIGHT_DESCRIPTION : MENU_OPTION_HEIGHT, overscan: VIRTUAL_OVERSCAN_ITEMS, }; const rowVirtualizer = useVirtualizer(virtualizerOptions); - const debounceAsync = useMemo( - () => - debounce((inputValue: string) => { - loadOptions(inputValue) - .then((opts) => { - setItems(opts, inputValue); - setAsyncLoading(false); - setAsyncError(false); - }) - .catch((err) => { - if (!(err instanceof StaleResultError)) { - setAsyncError(true); - setAsyncLoading(false); - } - }); - }, 200), - [loadOptions, setItems] - ); - const { isOpen, highlightedIndex, @@ -250,7 +195,7 @@ export const Combobox = (props: ComboboxProps) => menuId, labelId, inputId: id, - items, + items: filteredOptions, itemToString, selectedItem, @@ -267,48 +212,9 @@ export const Combobox = (props: ComboboxProps) => scrollIntoView: () => {}, - onInputValueChange: ({ inputValue, isOpen }) => { - if (!isOpen) { - // Prevent stale options from showing on reopen - if (isAsync) { - setItems([], ''); - } - - // Otherwise there's nothing else to do when the menu isnt open - return; - } - - if (!isAsync) { - const filteredItems = fuzzyFind(options, stringifiedItems, inputValue); - setItems(filteredItems, inputValue); - } else { - if (inputValue && createCustomValue) { - setItems([], inputValue); - } - - setAsyncLoading(true); - debounceAsync(inputValue); - } - }, - onIsOpenChange: ({ isOpen, inputValue }) => { - // Loading async options mostly happens in onInputValueChange, but if the menu is opened with an empty input - // then onInputValueChange isn't called (because the input value hasn't changed) - if (isAsync && isOpen && inputValue === '') { - setAsyncLoading(true); - // TODO: dedupe this loading logic with debounceAsync - loadOptions(inputValue) - .then((opts) => { - setItems(opts, inputValue); - setAsyncLoading(false); - setAsyncError(false); - }) - .catch((err) => { - if (!(err instanceof StaleResultError)) { - setAsyncError(true); - setAsyncLoading(false); - } - }); + if (isOpen && inputValue === '') { + updateOptions(inputValue); } }, @@ -317,7 +223,16 @@ export const Combobox = (props: ComboboxProps) => rowVirtualizer.scrollToIndex(highlightedIndex); } }, + onStateChange: ({ inputValue: newInputValue, type, selectedItem: newSelectedItem }) => { + switch (type) { + case useCombobox.stateChangeTypes.InputChange: + updateOptions(newInputValue ?? ''); + break; + default: + break; + } + }, stateReducer(state, actionAndChanges) { let { changes } = actionAndChanges; const menuBeingOpened = state.isOpen === false && changes.isOpen === true; @@ -353,7 +268,7 @@ export const Combobox = (props: ComboboxProps) => }, }); - const { inputRef, floatingRef, floatStyles, scrollRef } = useComboboxFloat(items, isOpen); + const { inputRef, floatingRef, floatStyles, scrollRef } = useComboboxFloat(filteredOptions, isOpen); const isAutoSize = width === 'auto'; @@ -429,14 +344,16 @@ export const Combobox = (props: ComboboxProps) => {!asyncError && (
    {rowVirtualizer.getVirtualItems().map((virtualRow) => { + const item = filteredOptions[virtualRow.index]; + return (
  • (props: ComboboxProps) => transform: `translateY(${virtualRow.start}px)`, }} {...getItemProps({ - item: items[virtualRow.index], + item: item, index: virtualRow.index, })} >
    - - {items[virtualRow.index].label ?? items[virtualRow.index].value} - - {items[virtualRow.index].description && ( - {items[virtualRow.index].description} - )} + {item.label ?? item.value} + {item.description && {item.description}}
  • ); @@ -463,7 +376,7 @@ export const Combobox = (props: ComboboxProps) => )}
    {asyncError && } - {items.length === 0 && !asyncError && } + {filteredOptions.length === 0 && !asyncError && }
    )} diff --git a/packages/grafana-ui/src/components/Combobox/useOptions.ts b/packages/grafana-ui/src/components/Combobox/useOptions.ts index fc2747cee1e..fffad1613cb 100644 --- a/packages/grafana-ui/src/components/Combobox/useOptions.ts +++ b/packages/grafana-ui/src/components/Combobox/useOptions.ts @@ -3,7 +3,7 @@ import { useState, useCallback, useMemo } from 'react'; import { t } from '../../utils/i18n'; -import { itemFilter } from './filter'; +import { fuzzyFind, itemToString } from './filter'; import { ComboboxOption } from './types'; import { StaleResultError, useLatestAsyncCall } from './useLatestAsyncCall'; @@ -83,14 +83,11 @@ export function useOptions(rawOptions: AsyncOptions { - if (!isAsync) { - setUserTypedSearch(inputValue); - return; + setUserTypedSearch(inputValue); + if (isAsync) { + setAsyncLoading(true); + debouncedLoadOptions(inputValue); } - - setAsyncLoading(true); - - debouncedLoadOptions(inputValue); }, [debouncedLoadOptions, isAsync] ); @@ -122,12 +119,16 @@ export function useOptions(rawOptions: AsyncOptions { + return isAsync ? [] : rawOptions.map(itemToString); + }, [isAsync, rawOptions]); + const finalOptions = useMemo(() => { - const currentOptions = isAsync ? asyncOptions : rawOptions.filter(itemFilter(userTypedSearch)); + const currentOptions = isAsync ? asyncOptions : fuzzyFind(rawOptions, stringifiedOptions, userTypedSearch); const currentOptionsOrganised = organizeOptionsByGroup(currentOptions); return addCustomValue(currentOptionsOrganised); - }, [isAsync, organizeOptionsByGroup, addCustomValue, asyncOptions, rawOptions, userTypedSearch]); + }, [isAsync, organizeOptionsByGroup, addCustomValue, asyncOptions, rawOptions, userTypedSearch, stringifiedOptions]); return { options: finalOptions, updateOptions, asyncLoading, asyncError }; } From ec14822dd010c18eafd5f3513b206d221a58ec65 Mon Sep 17 00:00:00 2001 From: jackyin <648588267@qq.com> Date: Mon, 24 Feb 2025 21:45:32 +0800 Subject: [PATCH 23/26] Panel: Histogram tooltip unit unexpected show (#100163) * unit unexpected show * format * Build display in while building the counts, and remove the post processing iteration over the counts. --------- Co-authored-by: Kristina Durivage --- .../transformations/transformers/histogram.ts | 21 ++++++++++++------- .../panel/histogram/HistogramPanel.tsx | 2 +- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/packages/grafana-data/src/transformations/transformers/histogram.ts b/packages/grafana-data/src/transformations/transformers/histogram.ts index 00549ad07ae..8fa69f64a40 100644 --- a/packages/grafana-data/src/transformations/transformers/histogram.ts +++ b/packages/grafana-data/src/transformations/transformers/histogram.ts @@ -326,7 +326,11 @@ export function getHistogramFields(frame: DataFrame): HistogramFields | undefine /** * @alpha */ -export function buildHistogram(frames: DataFrame[], options?: HistogramTransformerOptions): HistogramFields | null { +export function buildHistogram( + frames: DataFrame[], + options?: HistogramTransformerOptions, + theme?: GrafanaTheme2 +): HistogramFields | null { let bucketSize = options?.bucketSize; let bucketCount = options?.bucketCount ?? DEFAULT_BUCKET_COUNT; let bucketOffset = options?.bucketOffset ?? 0; @@ -413,13 +417,20 @@ export function buildHistogram(frames: DataFrame[], options?: HistogramTransform if (field.type === FieldType.number) { let fieldHist = histogram(field.values, getBucket, histFilter, histSort); histograms.push(fieldHist); - counts.push({ + + const count = { ...field, config: { ...field.config, unit: field.config.unit === 'short' ? 'short' : undefined, }, + }; + + count.display = getDisplayProcessor({ + field: count, + theme: theme ?? createTheme(), }); + counts.push(count); if (!config && field.config.unit) { config = field.config; } @@ -574,12 +585,6 @@ export function histogramFieldsToFrame(info: HistogramFields, theme?: GrafanaThe info.xMax.display = display; } - // ensure updated units are reflected on the count field used for y axis formatting - info.counts[0].display = getDisplayProcessor({ - field: info.counts[0], - theme: theme ?? createTheme(), - }); - return { length: info.xMin.values.length, meta: { diff --git a/public/app/plugins/panel/histogram/HistogramPanel.tsx b/public/app/plugins/panel/histogram/HistogramPanel.tsx index 03938736458..517f41187fc 100644 --- a/public/app/plugins/panel/histogram/HistogramPanel.tsx +++ b/public/app/plugins/panel/histogram/HistogramPanel.tsx @@ -46,7 +46,7 @@ export const HistogramPanel = ({ data, options, width, height }: Props) => { return histogramFieldsToFrame(joinHistograms(histograms), theme); } } - const hist = buildHistogram(data.series, options); + const hist = buildHistogram(data.series, options, theme); if (!hist) { return undefined; } From 608d974585c696253ac629f3c7bfc3a0043cbd49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agn=C3=A8s=20Toulet?= <35176601+AgnesToulet@users.noreply.github.com> Date: Mon, 24 Feb 2025 15:43:06 +0100 Subject: [PATCH 24/26] Rendering: Stop preloading apps for rendering requests (#100221) * Rendering: stop preloading apps * add feature toggle * add comment * add const * fix linter * rename feature toggle * delete old ff * update toggles_gen.json --- .../grafana-data/src/types/featureToggles.gen.ts | 1 + pkg/services/featuremgmt/registry.go | 9 +++++++++ pkg/services/featuremgmt/toggles_gen.csv | 1 + pkg/services/featuremgmt/toggles_gen.go | 4 ++++ pkg/services/featuremgmt/toggles_gen.json | 15 +++++++++++++++ public/app/app.ts | 5 ++++- 6 files changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/grafana-data/src/types/featureToggles.gen.ts b/packages/grafana-data/src/types/featureToggles.gen.ts index 84091bf6c14..cda45576882 100644 --- a/packages/grafana-data/src/types/featureToggles.gen.ts +++ b/packages/grafana-data/src/types/featureToggles.gen.ts @@ -256,4 +256,5 @@ export interface FeatureToggles { alertingJiraIntegration?: boolean; alertingRuleVersionHistoryRestore?: boolean; newShareReportDrawer?: boolean; + rendererDisableAppPluginsPreload?: boolean; } diff --git a/pkg/services/featuremgmt/registry.go b/pkg/services/featuremgmt/registry.go index a612113f87a..6ea80687ab7 100644 --- a/pkg/services/featuremgmt/registry.go +++ b/pkg/services/featuremgmt/registry.go @@ -1789,6 +1789,15 @@ var ( HideFromAdminPage: true, HideFromDocs: true, }, + { + Name: "rendererDisableAppPluginsPreload", + Description: "Disable pre-loading app plugins when the request is coming from the renderer", + Stage: FeatureStageExperimental, + Owner: grafanaSharingSquad, + HideFromAdminPage: true, + HideFromDocs: true, + FrontendOnly: true, + }, } ) diff --git a/pkg/services/featuremgmt/toggles_gen.csv b/pkg/services/featuremgmt/toggles_gen.csv index 159071fd378..1a74645d2a3 100644 --- a/pkg/services/featuremgmt/toggles_gen.csv +++ b/pkg/services/featuremgmt/toggles_gen.csv @@ -237,3 +237,4 @@ pluginsCDNSyncLoader,experimental,@grafana/plugins-platform-backend,false,false, alertingJiraIntegration,experimental,@grafana/alerting-squad,false,false,true alertingRuleVersionHistoryRestore,experimental,@grafana/alerting-squad,false,false,true newShareReportDrawer,experimental,@grafana/sharing-squad,false,false,false +rendererDisableAppPluginsPreload,experimental,@grafana/sharing-squad,false,false,true diff --git a/pkg/services/featuremgmt/toggles_gen.go b/pkg/services/featuremgmt/toggles_gen.go index 79eaf5a3a11..043d9446a3a 100644 --- a/pkg/services/featuremgmt/toggles_gen.go +++ b/pkg/services/featuremgmt/toggles_gen.go @@ -958,4 +958,8 @@ const ( // FlagNewShareReportDrawer // Enables the report creation drawer in a dashboard FlagNewShareReportDrawer = "newShareReportDrawer" + + // FlagRendererDisableAppPluginsPreload + // Disable pre-loading app plugins when the request is coming from the renderer + FlagRendererDisableAppPluginsPreload = "rendererDisableAppPluginsPreload" ) diff --git a/pkg/services/featuremgmt/toggles_gen.json b/pkg/services/featuremgmt/toggles_gen.json index bf878a38563..67a16387684 100644 --- a/pkg/services/featuremgmt/toggles_gen.json +++ b/pkg/services/featuremgmt/toggles_gen.json @@ -3593,6 +3593,21 @@ "hideFromAdminPage": true } }, + { + "metadata": { + "name": "rendererDisableAppPluginsPreload", + "resourceVersion": "1740386710764", + "creationTimestamp": "2025-02-24T08:45:10Z" + }, + "spec": { + "description": "Disable pre-loading app plugins when the request is coming from the renderer", + "stage": "experimental", + "codeowner": "@grafana/sharing-squad", + "frontend": true, + "hideFromAdminPage": true, + "hideFromDocs": true + } + }, { "metadata": { "name": "reportingRetries", diff --git a/public/app/app.ts b/public/app/app.ts index 697e909fc37..7a3ba0bf140 100644 --- a/public/app/app.ts +++ b/public/app/app.ts @@ -205,7 +205,10 @@ export class GrafanaApp { setDataSourceSrv(dataSourceSrv); initWindowRuntime(); - if (contextSrv.user.orgRole !== '') { + // Do not pre-load apps if rendererDisableAppPluginsPreload is true and the request comes from the image renderer + const skipAppPluginsPreload = + config.featureToggles.rendererDisableAppPluginsPreload && contextSrv.user.authenticatedBy === 'render'; + if (contextSrv.user.orgRole !== '' && !skipAppPluginsPreload) { const appPluginsToAwait = getAppPluginsToAwait(); const appPluginsToPreload = getAppPluginsToPreload(); From b58d616495617929f15d28d185f2d34e9bfd5be3 Mon Sep 17 00:00:00 2001 From: Santiago Date: Mon, 24 Feb 2025 15:43:19 +0100 Subject: [PATCH 25/26] Alerting: Handle err-mimir-max-label-names-per-series as a user error in the prom writer (#101214) --- pkg/services/ngalert/writer/prom.go | 25 +++++++++++------------- pkg/services/ngalert/writer/prom_test.go | 18 ++++++++++++++++- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/pkg/services/ngalert/writer/prom.go b/pkg/services/ngalert/writer/prom.go index 9778d2f824b..ff735fecc26 100644 --- a/pkg/services/ngalert/writer/prom.go +++ b/pkg/services/ngalert/writer/prom.go @@ -25,10 +25,11 @@ const backendType = "prometheus" const ( // Fixed error messages - MimirDuplicateTimestampError = "err-mimir-sample-duplicate-timestamp" - MimirInvalidLabelError = "err-mimir-label-invalid" - MimirMaxSeriesPerUserError = "err-mimir-max-series-per-user" - MimirLabelValueTooLongError = "err-mimir-label-value-too-long" + MimirDuplicateTimestampError = "err-mimir-sample-duplicate-timestamp" + MimirInvalidLabelError = "err-mimir-label-invalid" + MimirLabelValueTooLongError = "err-mimir-label-value-too-long" + MimirMaxLabelNamesPerSeriesError = "err-mimir-max-label-names-per-series" + MimirMaxSeriesPerUserError = "err-mimir-max-series-per-user" // Best effort error messages PrometheusDuplicateTimestampError = "duplicate sample for timestamp" @@ -267,16 +268,12 @@ func checkWriteError(writeErr promremote.WriteError) (err error, ignored bool) { } } - if strings.Contains(msg, MimirInvalidLabelError) { - return errors.Join(ErrRejectedWrite, writeErr), false - } - - // this can happen when user exceeded defined maximum of - if strings.Contains(msg, MimirMaxSeriesPerUserError) { - return errors.Join(ErrRejectedWrite, writeErr), false - } - - if strings.Contains(msg, MimirLabelValueTooLongError) { + // Check for expected user errors. + switch { + case strings.Contains(msg, MimirInvalidLabelError), + strings.Contains(msg, MimirMaxSeriesPerUserError), + strings.Contains(msg, MimirMaxLabelNamesPerSeriesError), + strings.Contains(msg, MimirLabelValueTooLongError): return errors.Join(ErrRejectedWrite, writeErr), false } diff --git a/pkg/services/ngalert/writer/prom_test.go b/pkg/services/ngalert/writer/prom_test.go index af5bd5e9f29..c300d700d93 100644 --- a/pkg/services/ngalert/writer/prom_test.go +++ b/pkg/services/ngalert/writer/prom_test.go @@ -240,7 +240,23 @@ func TestPrometheusWriter_Write(t *testing.T) { }) t.Run("too long labels fit under the client error category", func(t *testing.T) { - msg := "received a series whose label value length exceeds the limit, label: 'label-1', value: 'value-1' (truncated) series: 'some_series (err-mimir-label-value-too-long). To adjust the related per-tenant limit, configure -validation.max-length-label-value, or contact your service administrator." + msg := "received a series whose label value length exceeds the limit, label: 'label-1', value: 'value-1' (truncated) series: 'some_series' (err-mimir-label-value-too-long). To adjust the related per-tenant limit, configure -validation.max-length-label-value, or contact your service administrator." + clientErr := testClientWriteError{ + statusCode: http.StatusBadRequest, + msg: &msg, + } + client.writeSeriesFunc = func(ctx context.Context, ts promremote.TSList, opts promremote.WriteOptions) (promremote.WriteResult, promremote.WriteError) { + return promremote.WriteResult{}, clientErr + } + + err := writer.Write(ctx, "test", now, frames, 1, map[string]string{"extra": "label"}) + + require.Error(t, err) + require.ErrorIs(t, err, ErrRejectedWrite) + }) + + t.Run("too many labels fit under the client error category", func(t *testing.T) { + msg := "received a series whose number of labels exceeds the limit (actual: 50, limit: 40) series: 'some_series' (err-mimir-max-label-names-per-series). To adjust the related per-tenant limit, configure -validation.max-label-names-per-series, or contact your service administrator." clientErr := testClientWriteError{ statusCode: http.StatusBadRequest, msg: &msg, From 74632a25c3f790e205e778c1e050b0fc81e1567d Mon Sep 17 00:00:00 2001 From: Karl Persson <23356117+kalleep@users.noreply.github.com> Date: Mon, 24 Feb 2025 16:03:14 +0100 Subject: [PATCH 26/26] Authz: folder api tls settings (#101213) * Skip certificate verification * Add more settings for folder api --- pkg/services/authz/rbac.go | 10 +++++++--- pkg/services/authz/rbac_settings.go | 13 +++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/pkg/services/authz/rbac.go b/pkg/services/authz/rbac.go index f83a2b4d4a7..fcb398667ab 100644 --- a/pkg/services/authz/rbac.go +++ b/pkg/services/authz/rbac.go @@ -151,20 +151,24 @@ func RegisterRBACAuthZService( reg prometheus.Registerer, cache cache.Cache, exchangeClient authnlib.TokenExchanger, - folderAPIURL string, + cfg RBACServerSettings, ) { var folderStore store.FolderStore // FIXME: for now we default to using database read proxy for folders if the api url is not configured. // we should remove this and the sql implementation once we have verified that is works correctly - if folderAPIURL == "" { + if cfg.Folder.Host == "" { folderStore = store.NewSQLFolderStore(db, tracer) } else { folderStore = store.NewAPIFolderStore(tracer, func(ctx context.Context) (*rest.Config, error) { return &rest.Config{ - Host: folderAPIURL, + Host: cfg.Folder.Host, WrapTransport: func(rt http.RoundTripper) http.RoundTripper { return &tokenExhangeRoundTripper{te: exchangeClient, rt: rt} }, + TLSClientConfig: rest.TLSClientConfig{ + Insecure: cfg.Folder.Insecure, + CAFile: cfg.Folder.CAFile, + }, QPS: 50, Burst: 100, }, nil diff --git a/pkg/services/authz/rbac_settings.go b/pkg/services/authz/rbac_settings.go index 0ee661c2b50..9d643e73af5 100644 --- a/pkg/services/authz/rbac_settings.go +++ b/pkg/services/authz/rbac_settings.go @@ -57,3 +57,16 @@ func readAuthzClientSettings(cfg *setting.Cfg) (*authzClientSettings, error) { return s, nil } + +type RBACServerSettings struct { + Folder FolderAPISettings +} + +type FolderAPISettings struct { + // Host is hostname for folder api + Host string + // Insecure will skip verification of ceritificates. Should only be used for testing + Insecure bool + // CAFile is a filepath to trusted root certificates for server + CAFile string +}