From fa750a506829774a4418e6c78b89f92f37815af2 Mon Sep 17 00:00:00 2001 From: Livio Amstutz Date: Mon, 30 Mar 2020 07:23:43 +0200 Subject: [PATCH] improve some functions --- internal/proto/struct.go | 7 +------ internal/tracing/caller.go | 10 ++++------ internal/tracing/config/config.go | 12 +++++------- internal/tracing/google/config.go | 5 ++--- internal/tracing/span.go | 4 +++- pkg/admin/admin.go | 4 ++-- pkg/admin/api/config.go | 2 +- pkg/auth/api/config.go | 2 +- pkg/auth/auth.go | 4 ++-- 9 files changed, 21 insertions(+), 29 deletions(-) diff --git a/internal/proto/struct.go b/internal/proto/struct.go index 24a6ce0e36..f1b5eb29f9 100644 --- a/internal/proto/struct.go +++ b/internal/proto/struct.go @@ -13,7 +13,6 @@ import ( func MustToPBStruct(object interface{}) *pb_struct.Struct { s, err := ToPBStruct(object) logging.Log("PROTO-7Aa3t").OnError(err).Panic("unable to map object to pb-struct") - return s } @@ -24,15 +23,12 @@ func BytesToPBStruct(b []byte) (*pb_struct.Struct, error) { } func ToPBStruct(object interface{}) (*pb_struct.Struct, error) { - fields := new(pb_struct.Struct) - marshalled, err := json.Marshal(object) if err != nil { return nil, err } - + fields := new(pb_struct.Struct) err = jsonpb.Unmarshal(bytes.NewReader(marshalled), fields) - return fields, err } @@ -47,6 +43,5 @@ func FromPBStruct(object interface{}, s *pb_struct.Struct) error { if err != nil { return err } - return json.Unmarshal([]byte(jsonString), object) } diff --git a/internal/tracing/caller.go b/internal/tracing/caller.go index c5dfb9a319..c8ab9071b0 100644 --- a/internal/tracing/caller.go +++ b/internal/tracing/caller.go @@ -8,17 +8,15 @@ import ( func GetCaller() string { fpcs := make([]uintptr, 1) - n := runtime.Callers(3, fpcs) if n == 0 { - logging.Log("HELPE-rWjfC").Debug("no caller") + logging.Log("TRACE-rWjfC").Debug("no caller") + return "" } - caller := runtime.FuncForPC(fpcs[0] - 1) if caller == nil { - logging.Log("HELPE-25POw").Debug("caller was nil") + logging.Log("TRACE-25POw").Debug("caller was nil") + return "" } - - // Print the name of the function return caller.Name() } diff --git a/internal/tracing/config/config.go b/internal/tracing/config/config.go index e2310fbce3..3681ddf107 100644 --- a/internal/tracing/config/config.go +++ b/internal/tracing/config/config.go @@ -3,9 +3,7 @@ package config import ( "encoding/json" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" - + "github.com/caos/zitadel/internal/errors" "github.com/caos/zitadel/internal/tracing" tracing_g "github.com/caos/zitadel/internal/tracing/google" tracing_log "github.com/caos/zitadel/internal/tracing/log" @@ -28,7 +26,7 @@ func (c *TracingConfig) UnmarshalJSON(data []byte) error { } if err := json.Unmarshal(data, &rc); err != nil { - return status.Errorf(codes.Internal, "%v parse config: %v", "TRACE-vmjS", err) + return errors.ThrowInternal(err, "TRACE-vmjS", "error parsing config") } c.Type = rc.Type @@ -36,7 +34,7 @@ func (c *TracingConfig) UnmarshalJSON(data []byte) error { var err error c.Config, err = newTracingConfig(c.Type, rc.Config) if err != nil { - return status.Errorf(codes.Internal, "%v parse config: %v", "TRACE-Ws9E", err) + return err } return c.Config.NewTracer() @@ -45,7 +43,7 @@ func (c *TracingConfig) UnmarshalJSON(data []byte) error { func newTracingConfig(tracerType string, configData []byte) (tracing.Config, error) { t, ok := tracer[tracerType] if !ok { - return nil, status.Errorf(codes.Internal, "%v No config: %v", "TRACE-HMEJ", tracerType) + return nil, errors.ThrowInternalf(nil, "TRACE-HMEJ", "config type %s not supported", tracerType) } tracingConfig := t() @@ -54,7 +52,7 @@ func newTracingConfig(tracerType string, configData []byte) (tracing.Config, err } if err := json.Unmarshal(configData, tracingConfig); err != nil { - return nil, status.Errorf(codes.Internal, "%v Could not read conifg: %v", "TRACE-1tSS", err) + return nil, errors.ThrowInternal(err, "TRACE-1tSS", "Could not read config: %v") } return tracingConfig, nil diff --git a/internal/tracing/google/config.go b/internal/tracing/google/config.go index 92d93f61bd..64ee3223ba 100644 --- a/internal/tracing/google/config.go +++ b/internal/tracing/google/config.go @@ -2,9 +2,8 @@ package google import ( "go.opencensus.io/trace" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" + "github.com/caos/zitadel/internal/errors" "github.com/caos/zitadel/internal/tracing" ) @@ -16,7 +15,7 @@ type Config struct { func (c *Config) NewTracer() error { if !envIsSet() { - return status.Error(codes.InvalidArgument, "env not properly set, GOOGLE_APPLICATION_CREDENTIALS is misconfigured or missing") + return errors.ThrowInvalidArgument(nil, "GOOGL-sdh3a", "env not properly set, GOOGLE_APPLICATION_CREDENTIALS is misconfigured or missing") } tracing.T = &Tracer{projectID: c.ProjectID, metricPrefix: c.MetricPrefix, sampler: trace.ProbabilitySampler(c.Fraction)} diff --git a/internal/tracing/span.go b/internal/tracing/span.go index d958cb4b52..1ad46ca0b1 100644 --- a/internal/tracing/span.go +++ b/internal/tracing/span.go @@ -7,6 +7,8 @@ import ( "go.opencensus.io/trace" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + + "github.com/caos/zitadel/internal/errors" ) type Span struct { @@ -80,7 +82,7 @@ func toTraceAttribute(key string, value interface{}) (attr trace.Attribute, err if valueInt, err := convertToInt64(value); err == nil { return trace.Int64Attribute(key, valueInt), nil } - return attr, status.Error(codes.InvalidArgument, "Attribute is not of type bool, string or int64") + return attr, errors.ThrowInternal(nil, "TRACE-jlq3s", "Attribute is not of type bool, string or int64") } func convertToInt64(value interface{}) (int64, error) { diff --git a/pkg/admin/admin.go b/pkg/admin/admin.go index bdf9010253..0a18a4b145 100644 --- a/pkg/admin/admin.go +++ b/pkg/admin/admin.go @@ -10,8 +10,8 @@ import ( ) type Config struct { - App *app.Config - API *api.Config + App app.Config + API api.Config } func Start(ctx context.Context, config Config, authZ auth.Config) error { diff --git a/pkg/admin/api/config.go b/pkg/admin/api/config.go index b63086cc83..8fce0aca62 100644 --- a/pkg/admin/api/config.go +++ b/pkg/admin/api/config.go @@ -3,5 +3,5 @@ package api import "github.com/caos/zitadel/internal/api/grpc" type Config struct { - GRPC *grpc.Config + GRPC grpc.Config } diff --git a/pkg/auth/api/config.go b/pkg/auth/api/config.go index b63086cc83..8fce0aca62 100644 --- a/pkg/auth/api/config.go +++ b/pkg/auth/api/config.go @@ -3,5 +3,5 @@ package api import "github.com/caos/zitadel/internal/api/grpc" type Config struct { - GRPC *grpc.Config + GRPC grpc.Config } diff --git a/pkg/auth/auth.go b/pkg/auth/auth.go index 43527dc43b..f8838f83c0 100644 --- a/pkg/auth/auth.go +++ b/pkg/auth/auth.go @@ -10,8 +10,8 @@ import ( ) type Config struct { - App *app.Config - API *api.Config + App app.Config + API api.Config } func Start(ctx context.Context, config Config, authZ auth.Config) error {