diff --git a/pkg/controllers/accesslogpolicy_controller.go b/pkg/controllers/accesslogpolicy_controller.go index 47cbfce6..c9ff5037 100644 --- a/pkg/controllers/accesslogpolicy_controller.go +++ b/pkg/controllers/accesslogpolicy_controller.go @@ -104,13 +104,9 @@ func RegisterAccessLogPolicyController( } func (r *accessLogPolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - ctx = gwlog.NewTrace(ctx) - gwlog.AddMetadata(ctx, "type", "accesslogpolicy") - gwlog.AddMetadata(ctx, "name", req.Name) - - r.log.Infow(ctx, "reconcile starting", gwlog.GetMetadata(ctx)...) + ctx = gwlog.StartReconcileTrace(ctx, r.log, "accesslogpolicy", req.Name) defer func() { - r.log.Infow(ctx, "reconcile completed", gwlog.GetMetadata(ctx)...) + gwlog.EndReconcileTrace(ctx, r.log) }() recErr := r.reconcile(ctx, req) diff --git a/pkg/controllers/gateway_controller.go b/pkg/controllers/gateway_controller.go index a1c9a804..93303f5e 100644 --- a/pkg/controllers/gateway_controller.go +++ b/pkg/controllers/gateway_controller.go @@ -119,13 +119,9 @@ func RegisterGatewayController( //+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gateways/finalizers,verbs=update func (r *gatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - ctx = gwlog.NewTrace(ctx) - gwlog.AddMetadata(ctx, "type", "gateway") - gwlog.AddMetadata(ctx, "name", req.Name) - - r.log.Infow(ctx, "reconcile starting", gwlog.GetMetadata(ctx)...) + ctx = gwlog.StartReconcileTrace(ctx, r.log, "gateway", req.Name) defer func() { - r.log.Infow(ctx, "reconcile completed", gwlog.GetMetadata(ctx)...) + gwlog.EndReconcileTrace(ctx, r.log) }() recErr := r.reconcile(ctx, req) diff --git a/pkg/controllers/gatewayclass_controller.go b/pkg/controllers/gatewayclass_controller.go index 09ede54f..4939c0aa 100644 --- a/pkg/controllers/gatewayclass_controller.go +++ b/pkg/controllers/gatewayclass_controller.go @@ -56,13 +56,9 @@ func RegisterGatewayClassController(log gwlog.Logger, mgr ctrl.Manager) error { //+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gatewayclasses/finalizers,verbs=update func (r *gatewayClassReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - ctx = gwlog.NewTrace(ctx) - gwlog.AddMetadata(ctx, "type", "gatewayclass") - gwlog.AddMetadata(ctx, "name", req.Name) - - r.log.Infow(ctx, "reconcile starting", gwlog.GetMetadata(ctx)...) + ctx = gwlog.StartReconcileTrace(ctx, r.log, "gatewayclass", req.Name) defer func() { - r.log.Infow(ctx, "reconcile completed", gwlog.GetMetadata(ctx)...) + gwlog.EndReconcileTrace(ctx, r.log) }() gwClass := &gwv1beta1.GatewayClass{} diff --git a/pkg/controllers/iamauthpolicy_controller.go b/pkg/controllers/iamauthpolicy_controller.go index 01a41507..b84f5fc5 100644 --- a/pkg/controllers/iamauthpolicy_controller.go +++ b/pkg/controllers/iamauthpolicy_controller.go @@ -78,13 +78,9 @@ func RegisterIAMAuthPolicyController(log gwlog.Logger, mgr ctrl.Manager, cloud p // // Policy Attachment Spec is defined in [GEP-713]: https://gateway-api.sigs.k8s.io/geps/gep-713/. func (c *IAMAuthPolicyController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - ctx = gwlog.NewTrace(ctx) - gwlog.AddMetadata(ctx, "type", "iamauthpolicy") - gwlog.AddMetadata(ctx, "name", req.Name) - - c.log.Infow(ctx, "reconcile starting", gwlog.GetMetadata(ctx)...) + ctx = gwlog.StartReconcileTrace(ctx, c.log, "iamauthpolicy", req.Name) defer func() { - c.log.Infow(ctx, "reconcile completed", gwlog.GetMetadata(ctx)...) + gwlog.EndReconcileTrace(ctx, c.log) }() k8sPolicy := &anv1alpha1.IAMAuthPolicy{} diff --git a/pkg/controllers/route_controller.go b/pkg/controllers/route_controller.go index 54e9506e..ac598009 100644 --- a/pkg/controllers/route_controller.go +++ b/pkg/controllers/route_controller.go @@ -149,9 +149,10 @@ func RegisterAllRouteControllers( } func (r *routeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - ctx = gwlog.NewTrace(ctx) - gwlog.AddMetadata(ctx, "type", "route") - gwlog.AddMetadata(ctx, "name", req.Name) + ctx = gwlog.StartReconcileTrace(ctx, r.log, "route", req.Name) + defer func() { + gwlog.EndReconcileTrace(ctx, r.log) + }() r.log.Infow(ctx, "reconcile starting", gwlog.GetMetadata(ctx)...) diff --git a/pkg/controllers/service_controller.go b/pkg/controllers/service_controller.go index 62f0e4cf..28c15dfe 100644 --- a/pkg/controllers/service_controller.go +++ b/pkg/controllers/service_controller.go @@ -73,13 +73,9 @@ func RegisterServiceController( //+kubebuilder:rbac:groups=core,resources=configmaps, verbs=create;delete;patch;update;get;list;watch func (r *serviceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - ctx = gwlog.NewTrace(ctx) - gwlog.AddMetadata(ctx, "type", "service") - gwlog.AddMetadata(ctx, "name", req.Name) - - r.log.Infow(ctx, "reconcile starting", gwlog.GetMetadata(ctx)...) + ctx = gwlog.StartReconcileTrace(ctx, r.log, "service", req.Name) defer func() { - r.log.Infow(ctx, "reconcile completed", gwlog.GetMetadata(ctx)...) + gwlog.EndReconcileTrace(ctx, r.log) }() recErr := r.reconcile(ctx, req) diff --git a/pkg/controllers/serviceexport_controller.go b/pkg/controllers/serviceexport_controller.go index 5197d846..e7bf1f74 100644 --- a/pkg/controllers/serviceexport_controller.go +++ b/pkg/controllers/serviceexport_controller.go @@ -102,13 +102,9 @@ func RegisterServiceExportController( //+kubebuilder:rbac:groups=application-networking.k8s.aws,resources=serviceexports/finalizers,verbs=update func (r *serviceExportReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - ctx = gwlog.NewTrace(ctx) - gwlog.AddMetadata(ctx, "type", "serviceexport") - gwlog.AddMetadata(ctx, "name", req.Name) - - r.log.Infow(ctx, "reconcile starting", gwlog.GetMetadata(ctx)...) + ctx = gwlog.StartReconcileTrace(ctx, r.log, "serviceexport", req.Name) defer func() { - r.log.Infow(ctx, "reconcile completed", gwlog.GetMetadata(ctx)...) + gwlog.EndReconcileTrace(ctx, r.log) }() recErr := r.reconcile(ctx, req) diff --git a/pkg/controllers/serviceimport_controller.go b/pkg/controllers/serviceimport_controller.go index 1313aadc..f3ae4717 100644 --- a/pkg/controllers/serviceimport_controller.go +++ b/pkg/controllers/serviceimport_controller.go @@ -71,12 +71,9 @@ func RegisterServiceImportController( //+kubebuilder:rbac:groups=application-networking.k8s.aws,resources=serviceimports/finalizers,verbs=update func (r *serviceImportReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - ctx = gwlog.NewTrace(ctx) - gwlog.AddMetadata(ctx, "type", "serviceimport") - gwlog.AddMetadata(ctx, "name", req.Name) - r.log.Infow(ctx, "reconcile starting", gwlog.GetMetadata(ctx)...) + ctx = gwlog.StartReconcileTrace(ctx, r.log, "serviceimport", req.Name) defer func() { - r.log.Infow(ctx, "reconcile completed", gwlog.GetMetadata(ctx)...) + gwlog.EndReconcileTrace(ctx, r.log) }() serviceImport := &anv1alpha1.ServiceImport{} diff --git a/pkg/controllers/targetgrouppolicy_controller.go b/pkg/controllers/targetgrouppolicy_controller.go index 4473315b..d9ba79f2 100644 --- a/pkg/controllers/targetgrouppolicy_controller.go +++ b/pkg/controllers/targetgrouppolicy_controller.go @@ -41,13 +41,9 @@ func RegisterTargetGroupPolicyController(log gwlog.Logger, mgr ctrl.Manager) err } func (c *TargetGroupPolicyController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - ctx = gwlog.NewTrace(ctx) - gwlog.AddMetadata(ctx, "type", "targetgrouppolicy") - gwlog.AddMetadata(ctx, "name", req.Name) - - c.log.Infow(ctx, "reconcile starting", gwlog.GetMetadata(ctx)...) + ctx = gwlog.StartReconcileTrace(ctx, c.log, "targetgrouppolicy", req.Name) defer func() { - c.log.Infow(ctx, "reconcile completed", gwlog.GetMetadata(ctx)...) + gwlog.EndReconcileTrace(ctx, c.log) }() tgPolicy := &TGP{} diff --git a/pkg/controllers/vpcassociationpolicy_controller.go b/pkg/controllers/vpcassociationpolicy_controller.go index 0a6988d7..215df155 100644 --- a/pkg/controllers/vpcassociationpolicy_controller.go +++ b/pkg/controllers/vpcassociationpolicy_controller.go @@ -55,13 +55,9 @@ func RegisterVpcAssociationPolicyController(log gwlog.Logger, cloud pkg_aws.Clou } func (c *vpcAssociationPolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - ctx = gwlog.NewTrace(ctx) - gwlog.AddMetadata(ctx, "type", "vpcassociationpolicy") - gwlog.AddMetadata(ctx, "name", req.Name) - - c.log.Infow(ctx, "reconcile starting", gwlog.GetMetadata(ctx)...) + ctx = gwlog.StartReconcileTrace(ctx, c.log, "vpcassociationpolicy", req.Name) defer func() { - c.log.Infow(ctx, "reconcile completed", gwlog.GetMetadata(ctx)...) + gwlog.EndReconcileTrace(ctx, c.log) }() k8sPolicy := &anv1alpha1.VpcAssociationPolicy{} diff --git a/pkg/utils/gwlog/actions.go b/pkg/utils/gwlog/actions.go new file mode 100644 index 00000000..ef5bb1dd --- /dev/null +++ b/pkg/utils/gwlog/actions.go @@ -0,0 +1,4 @@ +package gwlog + +const ReconcileStart = "[ACTION_RECONCILE_START]" +const ReconcileEnd = "[ACTION_RECONCILE_END]" diff --git a/pkg/utils/gwlog/metadata.go b/pkg/utils/gwlog/metadata.go index 5798a868..345cbb6d 100644 --- a/pkg/utils/gwlog/metadata.go +++ b/pkg/utils/gwlog/metadata.go @@ -49,6 +49,10 @@ func GetMetadata(ctx context.Context) []interface{} { if ctx.Value(metadata) != nil { for k, v := range ctx.Value(metadata).(*metadataValue).m { + if k == traceID { + // skip since there's a separate method to grab the trace id + continue + } fields = append(fields, k) fields = append(fields, v) } @@ -66,3 +70,17 @@ func GetTrace(ctx context.Context) string { } return "" } + +func StartReconcileTrace(ctx context.Context, log Logger, k8sresourcetype, name string) context.Context { + ctx = NewTrace(ctx) + AddMetadata(ctx, "type", k8sresourcetype) + AddMetadata(ctx, "name", name) + + log.Infow(ctx, ReconcileStart, GetMetadata(ctx)...) + + return ctx +} + +func EndReconcileTrace(ctx context.Context, log Logger) { + log.Infow(ctx, ReconcileEnd, GetMetadata(ctx)...) +}