Skip to content
Snippets Groups Projects
Select Git revision
  • 05526f4dd7c2e6a11b2437c548bd0e7b73b0c4f8
  • master default protected
  • bugfix/fix-return-var-in-find
  • feature/upgrade2
  • v1.10.0
  • v1.8.2
  • v1.8.1
  • v1.8.0
  • 1.7.3
  • v1.7.1
  • v1.6.1
  • v1.6.0
  • v1.5.0
  • v1.4.1
  • v1.3.0
  • v1.2.2
  • v1.2.1
  • v1.2.0
  • v1.0.1
  • v1.0.0
  • v0.0.23
  • v0.0.17
  • v0.0.10
  • v0.0.9
24 results

extension.py

Blame
  • logging_middleware.go 5.63 KiB
    package service
    
    // Code generated by gowrap. DO NOT EDIT.
    // template: ../../../assets/templates/middleware/access_log
    // gowrap: http://github.com/hexdigest/gowrap
    
    //go:generate gowrap gen -p git.perx.ru/perxis/perxis-go/pkg/spaces -i Spaces -t ../../../assets/templates/middleware/access_log -o logging_middleware.go -l ""
    
    import (
    	"context"
    	"fmt"
    	"time"
    
    	"git.perx.ru/perxis/perxis-go/pkg/auth"
    	"git.perx.ru/perxis/perxis-go/pkg/spaces"
    	"go.uber.org/zap"
    	"go.uber.org/zap/zapcore"
    )
    
    // loggingMiddleware implements spaces.Spaces that is instrumented with logging
    type loggingMiddleware struct {
    	logger *zap.Logger
    	next   spaces.Spaces
    }
    
    // LoggingMiddleware instruments an implementation of the spaces.Spaces with simple logging
    func LoggingMiddleware(logger *zap.Logger) Middleware {
    	return func(next spaces.Spaces) spaces.Spaces {
    		return &loggingMiddleware{
    			next:   next,
    			logger: logger,
    		}
    	}
    }
    
    func (m *loggingMiddleware) Create(ctx context.Context, space *spaces.Space) (created *spaces.Space, err error) {
    	begin := time.Now()
    	var fields []zapcore.Field
    	for k, v := range map[string]interface{}{
    		"ctx":   ctx,
    		"space": space} {
    		if k == "ctx" {
    			fields = append(fields, zap.String("principal", fmt.Sprint(auth.GetPrincipal(ctx))))
    			continue
    		}
    		fields = append(fields, zap.Reflect(k, v))
    	}
    
    	m.logger.Debug("Create.Request", fields...)
    
    	created, err = m.next.Create(ctx, space)
    
    	fields = []zapcore.Field{
    		zap.Duration("time", time.Since(begin)),
    		zap.Error(err),
    	}
    
    	for k, v := range map[string]interface{}{
    		"created": created,
    		"err":     err} {
    		if k == "err" {
    			continue
    		}
    		fields = append(fields, zap.Reflect(k, v))
    	}
    
    	m.logger.Debug("Create.Response", fields...)
    
    	return created, err
    }
    
    func (m *loggingMiddleware) Delete(ctx context.Context, spaceId string) (err error) {
    	begin := time.Now()
    	var fields []zapcore.Field
    	for k, v := range map[string]interface{}{
    		"ctx":     ctx,
    		"spaceId": spaceId} {
    		if k == "ctx" {
    			fields = append(fields, zap.String("principal", fmt.Sprint(auth.GetPrincipal(ctx))))
    			continue
    		}
    		fields = append(fields, zap.Reflect(k, v))
    	}
    
    	m.logger.Debug("Delete.Request", fields...)
    
    	err = m.next.Delete(ctx, spaceId)
    
    	fields = []zapcore.Field{
    		zap.Duration("time", time.Since(begin)),
    		zap.Error(err),
    	}
    
    	for k, v := range map[string]interface{}{
    		"err": err} {
    		if k == "err" {
    			continue
    		}
    		fields = append(fields, zap.Reflect(k, v))
    	}
    
    	m.logger.Debug("Delete.Response", fields...)
    
    	return err
    }
    
    func (m *loggingMiddleware) Get(ctx context.Context, spaceId string) (space *spaces.Space, err error) {
    	begin := time.Now()
    	var fields []zapcore.Field
    	for k, v := range map[string]interface{}{
    		"ctx":     ctx,
    		"spaceId": spaceId} {
    		if k == "ctx" {
    			fields = append(fields, zap.String("principal", fmt.Sprint(auth.GetPrincipal(ctx))))
    			continue
    		}
    		fields = append(fields, zap.Reflect(k, v))
    	}
    
    	m.logger.Debug("Get.Request", fields...)
    
    	space, err = m.next.Get(ctx, spaceId)
    
    	fields = []zapcore.Field{
    		zap.Duration("time", time.Since(begin)),
    		zap.Error(err),
    	}
    
    	for k, v := range map[string]interface{}{
    		"space": space,
    		"err":   err} {
    		if k == "err" {
    			continue
    		}
    		fields = append(fields, zap.Reflect(k, v))
    	}
    
    	m.logger.Debug("Get.Response", fields...)
    
    	return space, err
    }
    
    func (m *loggingMiddleware) List(ctx context.Context, orgId string) (spaces []*spaces.Space, err error) {
    	begin := time.Now()
    	var fields []zapcore.Field
    	for k, v := range map[string]interface{}{
    		"ctx":   ctx,
    		"orgId": orgId} {
    		if k == "ctx" {
    			fields = append(fields, zap.String("principal", fmt.Sprint(auth.GetPrincipal(ctx))))
    			continue
    		}
    		fields = append(fields, zap.Reflect(k, v))
    	}
    
    	m.logger.Debug("List.Request", fields...)
    
    	spaces, err = m.next.List(ctx, orgId)
    
    	fields = []zapcore.Field{
    		zap.Duration("time", time.Since(begin)),
    		zap.Error(err),
    	}
    
    	for k, v := range map[string]interface{}{
    		"spaces": spaces,
    		"err":    err} {
    		if k == "err" {
    			continue
    		}
    		fields = append(fields, zap.Reflect(k, v))
    	}
    
    	m.logger.Debug("List.Response", fields...)
    
    	return spaces, err
    }
    
    func (m *loggingMiddleware) Update(ctx context.Context, space *spaces.Space) (err error) {
    	begin := time.Now()
    	var fields []zapcore.Field
    	for k, v := range map[string]interface{}{
    		"ctx":   ctx,
    		"space": space} {
    		if k == "ctx" {
    			fields = append(fields, zap.String("principal", fmt.Sprint(auth.GetPrincipal(ctx))))
    			continue
    		}
    		fields = append(fields, zap.Reflect(k, v))
    	}
    
    	m.logger.Debug("Update.Request", fields...)
    
    	err = m.next.Update(ctx, space)
    
    	fields = []zapcore.Field{
    		zap.Duration("time", time.Since(begin)),
    		zap.Error(err),
    	}
    
    	for k, v := range map[string]interface{}{
    		"err": err} {
    		if k == "err" {
    			continue
    		}
    		fields = append(fields, zap.Reflect(k, v))
    	}
    
    	m.logger.Debug("Update.Response", fields...)
    
    	return err
    }
    
    func (m *loggingMiddleware) UpdateConfig(ctx context.Context, spaceId string, config *spaces.Config) (err error) {
    	begin := time.Now()
    	var fields []zapcore.Field
    	for k, v := range map[string]interface{}{
    		"ctx":     ctx,
    		"spaceId": spaceId,
    		"config":  config} {
    		if k == "ctx" {
    			fields = append(fields, zap.String("principal", fmt.Sprint(auth.GetPrincipal(ctx))))
    			continue
    		}
    		fields = append(fields, zap.Reflect(k, v))
    	}
    
    	m.logger.Debug("UpdateConfig.Request", fields...)
    
    	err = m.next.UpdateConfig(ctx, spaceId, config)
    
    	fields = []zapcore.Field{
    		zap.Duration("time", time.Since(begin)),
    		zap.Error(err),
    	}
    
    	for k, v := range map[string]interface{}{
    		"err": err} {
    		if k == "err" {
    			continue
    		}
    		fields = append(fields, zap.Reflect(k, v))
    	}
    
    	m.logger.Debug("UpdateConfig.Response", fields...)
    
    	return err
    }