Skip to content

Commit bf0433f

Browse files
authored
logs: added embedded logger into simple.App conversion/admission & custom routes (#1310)
## Summary - Add top-level error/success logging for admission (validate, mutate), conversion, and custom route handlers in `simple.App` - Extract logging helpers into `simple/logging.go`
1 parent 85d5e76 commit bf0433f

2 files changed

Lines changed: 77 additions & 9 deletions

File tree

simple/app.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
6362
type 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
}

simple/logging.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package simple
2+
3+
import (
4+
"context"
5+
6+
"github.com/grafana/grafana-app-sdk/app"
7+
"github.com/grafana/grafana-app-sdk/logging"
8+
)
9+
10+
func admissionLoggerFromContext(ctx context.Context, req *app.AdmissionRequest) logging.Logger {
11+
if req == nil {
12+
return logging.FromContext(ctx)
13+
}
14+
return logging.FromContext(ctx).With("group", req.Group, "version", req.Version, "kind", req.Kind)
15+
}
16+
17+
func conversionLoggerFromContext(ctx context.Context, req app.ConversionRequest) logging.Logger {
18+
return logging.FromContext(ctx).
19+
With("sourceGroup", req.SourceGVK.Group,
20+
"sourceVersion", req.SourceGVK.Version,
21+
"sourceKind", req.SourceGVK.Kind,
22+
"targetGroup", req.TargetGVK.Group,
23+
"targetVersion", req.TargetGVK.Version,
24+
"targetKind", req.TargetGVK.Kind,
25+
)
26+
}
27+
28+
func handleCustomRouteWithLogging(ctx context.Context, handler AppCustomRouteHandler, writer app.CustomRouteResponseWriter, req *app.CustomRouteRequest) error {
29+
logger := logging.FromContext(ctx)
30+
if req != nil {
31+
logger = logger.With(
32+
"method", req.Method,
33+
"path", req.Path,
34+
"group", req.ResourceIdentifier.Group,
35+
"version", req.ResourceIdentifier.Version,
36+
"kind", req.ResourceIdentifier.Kind,
37+
)
38+
}
39+
ctx = logging.Context(ctx, logger)
40+
err := handler(ctx, writer, req)
41+
if err != nil {
42+
logger.With("error", err).Error("custom route handler failed")
43+
return err
44+
}
45+
logger.Info("custom route handler succeeded")
46+
return nil
47+
}

0 commit comments

Comments
 (0)