-
Notifications
You must be signed in to change notification settings - Fork 535
fix(metrics): centralize abnormal_instance observe/clear at reconcile entry #6840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ti-chi-bot
merged 4 commits into
pingcap:main
from
fgksgf:fix/abnormal-instance-observe-at-entry
Apr 22, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8684e74
fix(metrics): centralize abnormal_instance observe/clear at reconcile…
fgksgf 6a3163d
fix(metrics): use the repo-wide license header year
fgksgf 46e150e
fix(metrics): qualify ClearByKey by component to avoid cross-kind sweep
fgksgf 15fe2a5
fix(metrics): wire TaskObserveInstance into router/scheduler/resource…
fgksgf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Copyright 2024 PingCAP, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package common | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| coreutil "github.com/pingcap/tidb-operator/v2/pkg/apiutil/core/v1alpha1" | ||
| "github.com/pingcap/tidb-operator/v2/pkg/metrics" | ||
| "github.com/pingcap/tidb-operator/v2/pkg/runtime" | ||
| "github.com/pingcap/tidb-operator/v2/pkg/runtime/scope" | ||
| "github.com/pingcap/tidb-operator/v2/pkg/utils/task/v3" | ||
| ) | ||
|
|
||
| // TaskObserveInstance refreshes the AbnormalInstance gauge for the reconciled | ||
| // instance every time the pipeline runs, and clears it when the instance CR | ||
| // has been removed from the API server (state.Object() == nil). | ||
| // | ||
| // It mirrors the shape of TaskTrack so observe and clear live in one place: | ||
| // the same branch that decides whether the object still exists also decides | ||
| // whether to publish or retract the gauge. Placing this after TaskTrack and | ||
| // before the `CondObjectHasBeenDeleted` IfBreak means it always runs once | ||
| // per reconcile, including the reconcile triggered by the informer's DELETE | ||
| // event - covering graceful delete, force-delete that strips finalizers, and | ||
| // any other path that lands in the watch stream. | ||
| func TaskObserveInstance[ | ||
| S scope.Instance[F, T], | ||
| F Object[P], | ||
| T runtime.Instance, | ||
| P any, | ||
| ](state TrackState[F]) task.Task { | ||
| return task.NameTaskFunc("ObserveInstance", func(context.Context) task.Result { | ||
| obj := state.Object() | ||
| if obj == nil { | ||
| key := state.Key() | ||
| // scope.Component[S]() is a compile-time constant for the reconcile | ||
| // kind, so we can qualify the sweep even after the CR is gone and | ||
| // its labels are no longer readable. | ||
| metrics.ClearInstanceConditionMetricsByKey(key.Namespace, scope.Component[S](), key.Name) | ||
| return task.Complete().With("cleared metrics for deleted %s", key) | ||
| } | ||
| conds := coreutil.StatusConditions[S](obj) | ||
| metrics.ObserveConditions(obj, conds) | ||
| return task.Complete().With("observed metrics for %s/%s", obj.GetNamespace(), obj.GetName()) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| // Copyright 2024 PingCAP, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package common | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| dto "github.com/prometheus/client_model/go" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
|
||
| "github.com/pingcap/tidb-operator/api/v2/core/v1alpha1" | ||
| "github.com/pingcap/tidb-operator/v2/pkg/metrics" | ||
| "github.com/pingcap/tidb-operator/v2/pkg/runtime/scope" | ||
| "github.com/pingcap/tidb-operator/v2/pkg/utils/fake" | ||
| "github.com/pingcap/tidb-operator/v2/pkg/utils/task/v3" | ||
| ) | ||
|
|
||
| // hasGaugeSample returns true if the AbnormalInstance gauge has any sample | ||
| // whose (namespace, instance) labels match the inputs. | ||
| func hasGaugeSample(t *testing.T, namespace, instance string) bool { | ||
| t.Helper() | ||
| ch := make(chan prometheus.Metric, 32) | ||
| metrics.AbnormalInstance.Collect(ch) | ||
| close(ch) | ||
| for m := range ch { | ||
| dm := &dto.Metric{} | ||
| if err := m.Write(dm); err != nil { | ||
| continue | ||
| } | ||
| labels := map[string]string{} | ||
| for _, lp := range dm.GetLabel() { | ||
| labels[lp.GetName()] = lp.GetValue() | ||
| } | ||
| if labels["namespace"] == namespace && labels["instance"] == instance { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func TestTaskObserveInstance_ObservesConditions(t *testing.T) { | ||
| const ns, name = "ns-observe", "pd-observe" | ||
| defer metrics.ClearInstanceConditionMetricsByKey(ns, v1alpha1.LabelValComponentPD, name) | ||
|
|
||
| obj := fake.FakeObj(name, func(o *v1alpha1.PD) *v1alpha1.PD { | ||
| o.Namespace = ns | ||
| o.Labels = map[string]string{ | ||
| v1alpha1.LabelKeyCluster: "c", | ||
| v1alpha1.LabelKeyComponent: "pd", | ||
| v1alpha1.LabelKeyGroup: "g", | ||
| } | ||
| o.Status.Conditions = []metav1.Condition{ | ||
| {Type: v1alpha1.CondReady, Status: metav1.ConditionFalse}, | ||
| {Type: v1alpha1.CondSynced, Status: metav1.ConditionTrue}, | ||
| } | ||
| return o | ||
| }) | ||
| state := &fakeState[v1alpha1.PD]{ns: ns, name: name, obj: obj} | ||
|
|
||
| res, done := task.RunTask(context.Background(), TaskObserveInstance[scope.PD](state)) | ||
| require.Equal(t, task.SComplete, res.Status()) | ||
| require.False(t, done) | ||
| assert.True(t, hasGaugeSample(t, ns, name), | ||
| "ObserveInstance must write at least one series for the instance") | ||
| } | ||
|
|
||
| func TestTaskObserveInstance_ClearsOnMissingObject(t *testing.T) { | ||
| const ns, name = "ns-clear", "pd-clear" | ||
|
|
||
| // Pre-populate a series as if a previous reconcile had observed the instance. | ||
| seed := fake.FakeObj(name, func(o *v1alpha1.PD) *v1alpha1.PD { | ||
| o.Namespace = ns | ||
| o.Labels = map[string]string{ | ||
| v1alpha1.LabelKeyCluster: "c", | ||
| v1alpha1.LabelKeyComponent: "pd", | ||
| v1alpha1.LabelKeyGroup: "g", | ||
| } | ||
| return o | ||
| }) | ||
| metrics.ObserveConditions(seed, []metav1.Condition{ | ||
| {Type: v1alpha1.CondReady, Status: metav1.ConditionFalse}, | ||
| }) | ||
| require.True(t, hasGaugeSample(t, ns, name), "test precondition: seed series exists") | ||
|
|
||
| // Simulate a reconcile where TaskContextObject saw NotFound. | ||
| state := &fakeState[v1alpha1.PD]{ns: ns, name: name, obj: nil} | ||
|
|
||
| res, done := task.RunTask(context.Background(), TaskObserveInstance[scope.PD](state)) | ||
| require.Equal(t, task.SComplete, res.Status()) | ||
| require.False(t, done) | ||
| assert.False(t, hasGaugeSample(t, ns, name), | ||
| "ObserveInstance must clear every series for the instance when the object is gone") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.