Skip to content

Commit 7a55558

Browse files
chore: Add log fields for apply chaos and surrounding code (chaos-mesh#4278)
* chore: Add log fields for apply chaos and surrounding code Signed-off-by: Mike Tonks <[email protected]> * chore: Update CHANGELOG Signed-off-by: Mike Tonks <[email protected]> --------- Signed-off-by: Mike Tonks <[email protected]>
1 parent ee64258 commit 7a55558

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ For more information and how-to, see [RFC: Keep A Changelog](https://github.com/
1717
- Add `values.schema.json` [#4205](https://github.com/chaos-mesh/chaos-mesh/pull/4205)
1818
- Add [`GreptimeDB`](https://greptime.com) to ADOPTERS.md [#4245](https://github.com/chaos-mesh/chaos-mesh/pull/4245)
1919
- Support configurable chaos-dns-server pod affinities[#4260](https://github.com/chaos-mesh/chaos-mesh/pull/4260)
20+
- Add experiment `name`, `namespace` and `kind` to "apply chaos" and "recover chaos" log messages [4278](https://github.com/chaos-mesh/chaos-mesh/pull/4278)
2021

2122
### Changed
2223

controllers/common/records/controller.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ const (
6262
// Reconcile the chaos records
6363
func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
6464
obj := r.Object.DeepCopyObject().(v1alpha1.InnerObjectWithSelector)
65-
6665
if err := r.Client.Get(context.TODO(), req.NamespacedName, obj); err != nil {
6766
if apierrors.IsNotFound(err) {
6867
r.Log.Info("chaos not found")
@@ -79,11 +78,13 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
7978
records := obj.GetStatus().Experiment.Records
8079
selectors := obj.GetSelectorSpecs()
8180

81+
logger := r.Log.WithValues("name", obj.GetName(), "namespace", obj.GetNamespace(), "kind", obj.GetObjectKind().GroupVersionKind().Kind)
82+
8283
if records == nil {
8384
for name, sel := range selectors {
8485
targets, err := r.Selector.Select(context.TODO(), sel)
8586
if err != nil {
86-
r.Log.Error(err, "fail to select")
87+
logger.Error(err, "fail to select")
8788
r.Recorder.Event(obj, recorder.Failed{
8889
Activity: "select targets",
8990
Err: err.Error(),
@@ -92,7 +93,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
9293
}
9394

9495
if len(targets) == 0 {
95-
r.Log.Info("no target has been selected")
96+
logger.Info("no target has been selected")
9697
r.Recorder.Event(obj, recorder.Failed{
9798
Activity: "select targets",
9899
Err: "no target has been selected",
@@ -115,7 +116,8 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
115116
needRetry := false
116117
for index, record := range records {
117118
var err error
118-
r.Log.Info("iterating record", "record", record, "desiredPhase", desiredPhase)
119+
idLogger := logger.WithValues("id", records[index].Id)
120+
idLogger.Info("iterating record", "record", record, "desiredPhase", desiredPhase)
119121

120122
// The whole running logic is a cycle:
121123
// Not Injected -> Not Injected/* -> Injected -> Injected/* -> Not Injected
@@ -146,15 +148,15 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
146148
}
147149

148150
if operation == Apply {
149-
r.Log.Info("apply chaos", "id", records[index].Id)
151+
idLogger.Info("apply chaos")
150152
record.Phase, err = r.Impl.Apply(context.TODO(), index, records, obj)
151153
if record.Phase != originalPhase {
152154
shouldUpdate = true
153155
}
154156
if err != nil {
155157
// TODO: add backoff and retry mechanism
156158
// but the retry shouldn't block other resource process
157-
r.Log.Error(err, "fail to apply chaos")
159+
idLogger.Error(err, "fail to apply chaos")
158160
applyFailedEvent := newRecordEvent(v1alpha1.TypeFailed, v1alpha1.Apply, err.Error())
159161
records[index].Events = append(records[index].Events, *applyFailedEvent)
160162
r.Recorder.Event(obj, recorder.Failed{
@@ -176,15 +178,15 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
176178
})
177179
}
178180
} else if operation == Recover {
179-
r.Log.Info("recover chaos", "id", records[index].Id)
181+
idLogger.Info("recover chaos")
180182
record.Phase, err = r.Impl.Recover(context.TODO(), index, records, obj)
181183
if record.Phase != originalPhase {
182184
shouldUpdate = true
183185
}
184186
if err != nil {
185187
// TODO: add backoff and retry mechanism
186188
// but the retry shouldn't block other resource process
187-
r.Log.Error(err, "fail to recover chaos")
189+
idLogger.Error(err, "fail to recover chaos")
188190
recoverFailedEvent := newRecordEvent(v1alpha1.TypeFailed, v1alpha1.Recover, err.Error())
189191
records[index].Events = append(records[index].Events, *recoverFailedEvent)
190192
r.Recorder.Event(obj, recorder.Failed{
@@ -215,11 +217,11 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
215217
}
216218
if shouldUpdate {
217219
updateError := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
218-
r.Log.Info("updating records", "records", records)
220+
logger.Info("updating records", "records", records)
219221
obj := r.Object.DeepCopyObject().(v1alpha1.InnerObjectWithSelector)
220222

221223
if err := r.Client.Get(context.TODO(), req.NamespacedName, obj); err != nil {
222-
r.Log.Error(err, "unable to get chaos")
224+
logger.Error(err, "unable to get chaos")
223225
return err
224226
}
225227

@@ -232,7 +234,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
232234
return r.Client.Update(context.TODO(), obj)
233235
})
234236
if updateError != nil {
235-
r.Log.Error(updateError, "fail to update")
237+
logger.Error(updateError, "fail to update")
236238
r.Recorder.Event(obj, recorder.Failed{
237239
Activity: "update records",
238240
Err: updateError.Error(),

0 commit comments

Comments
 (0)