@@ -16,6 +16,7 @@ import (
1616 "github.com/grafana/grafana-app-sdk/app"
1717 "github.com/grafana/grafana-app-sdk/health"
1818 "github.com/grafana/grafana-app-sdk/k8s"
19+ "github.com/grafana/grafana-app-sdk/logging"
1920 "github.com/grafana/grafana-app-sdk/operator"
2021 "github.com/grafana/grafana-app-sdk/resource"
2122)
@@ -55,9 +56,7 @@ func NewAppProvider(manifest app.Manifest, cfg app.SpecificConfig, newAppFunc fu
5556 }
5657}
5758
58- var (
59- _ app.App = & App {}
60- )
59+ var _ app.App = & App {}
6160
6261// KindMutator is an interface which describes an object which can mutate a kind, used in AppManagedKind
6362type KindMutator interface {
@@ -574,7 +573,15 @@ func (a *App) Validate(ctx context.Context, req *app.AdmissionRequest) error {
574573 if k .Validator == nil {
575574 return app .ErrNotImplemented
576575 }
577- return k .Validator .Validate (ctx , req )
576+ logger := admissionLoggerFromContext (ctx , req ).With ("action" , "validate" )
577+ ctx = logging .Context (ctx , logger )
578+ err := k .Validator .Validate (ctx , req )
579+ if err != nil {
580+ logger .With ("error" , err ).Error ("validation failed" )
581+ return err
582+ }
583+ logger .Info ("validation succeeded" )
584+ return nil
578585}
579586
580587// Mutate implements app.App and handles Mutating Admission Requests
@@ -587,16 +594,25 @@ func (a *App) Mutate(ctx context.Context, req *app.AdmissionRequest) (*app.Mutat
587594 if k .Mutator == nil {
588595 return nil , app .ErrNotImplemented
589596 }
590- return k .Mutator .Mutate (ctx , req )
597+ logger := admissionLoggerFromContext (ctx , req ).With ("action" , "mutate" )
598+ ctx = logging .Context (ctx , logger )
599+ res , err := k .Mutator .Mutate (ctx , req )
600+ if err != nil {
601+ logger .With ("error" , err ).Error ("mutation failed" )
602+ return nil , err
603+ }
604+ logger .Info ("mutation succeeded" )
605+ return res , nil
591606}
592607
593608// Convert implements app.App and handles resource conversion requests
594- func (a * App ) Convert (_ context.Context , req app.ConversionRequest ) (* app.RawObject , error ) {
609+ func (a * App ) Convert (ctx context.Context , req app.ConversionRequest ) (* app.RawObject , error ) {
595610 converter , ok := a .converters [req .SourceGVK .GroupKind ().String ()]
596611 if ! ok {
597612 // Default conversion?
598613 return nil , app .ErrNotImplemented
599614 }
615+ logger := conversionLoggerFromContext (ctx , req )
600616 srcAPIVersion , _ := req .SourceGVK .ToAPIVersionAndKind ()
601617 dstAPIVersion , _ := req .TargetGVK .ToAPIVersionAndKind ()
602618 converted , err := converter .Convert (k8s.RawKind {
@@ -606,9 +622,14 @@ func (a *App) Convert(_ context.Context, req app.ConversionRequest) (*app.RawObj
606622 Version : req .SourceGVK .Version ,
607623 Raw : req .Raw .Raw ,
608624 }, dstAPIVersion )
625+ if err != nil {
626+ logger .With ("error" , err ).Error ("conversion failed" )
627+ return nil , err
628+ }
629+ logger .Info ("conversion succeeded" )
609630 return & app.RawObject {
610631 Raw : converted ,
611- }, err
632+ }, nil
612633}
613634
614635// CallCustomRoute implements app.App and handles custom resource route requests
@@ -621,7 +642,7 @@ func (a *App) CallCustomRoute(ctx context.Context, writer app.CustomRouteRespons
621642 if handler , ok := a .customRoutes [a .customRouteHandlerKey (schema.GroupVersionKind {
622643 Version : req .ResourceIdentifier .Version ,
623644 }, req .Method , req .Path , scope )]; ok {
624- return handler (ctx , writer , req )
645+ return handleCustomRouteWithLogging (ctx , handler , writer , req )
625646 }
626647 }
627648 key := gvk (req .ResourceIdentifier .Group , req .ResourceIdentifier .Version , req .ResourceIdentifier .Kind )
@@ -634,7 +655,7 @@ func (a *App) CallCustomRoute(ctx context.Context, writer app.CustomRouteRespons
634655 return app .ErrCustomRouteNotFound
635656 }
636657 if handler , ok := a .customRoutes [a .customRouteHandlerKey (k .Kind .GroupVersionKind (), req .Method , req .Path , k .Kind .Scope ())]; ok {
637- return handler (ctx , writer , req )
658+ return handleCustomRouteWithLogging (ctx , handler , writer , req )
638659 }
639660 return app .ErrCustomRouteNotFound
640661}
0 commit comments