improve some functions

This commit is contained in:
Livio Amstutz 2020-03-30 07:23:43 +02:00
parent b753e06f0b
commit fa750a5068
9 changed files with 21 additions and 29 deletions

View File

@ -13,7 +13,6 @@ import (
func MustToPBStruct(object interface{}) *pb_struct.Struct { func MustToPBStruct(object interface{}) *pb_struct.Struct {
s, err := ToPBStruct(object) s, err := ToPBStruct(object)
logging.Log("PROTO-7Aa3t").OnError(err).Panic("unable to map object to pb-struct") logging.Log("PROTO-7Aa3t").OnError(err).Panic("unable to map object to pb-struct")
return s return s
} }
@ -24,15 +23,12 @@ func BytesToPBStruct(b []byte) (*pb_struct.Struct, error) {
} }
func ToPBStruct(object interface{}) (*pb_struct.Struct, error) { func ToPBStruct(object interface{}) (*pb_struct.Struct, error) {
fields := new(pb_struct.Struct)
marshalled, err := json.Marshal(object) marshalled, err := json.Marshal(object)
if err != nil { if err != nil {
return nil, err return nil, err
} }
fields := new(pb_struct.Struct)
err = jsonpb.Unmarshal(bytes.NewReader(marshalled), fields) err = jsonpb.Unmarshal(bytes.NewReader(marshalled), fields)
return fields, err return fields, err
} }
@ -47,6 +43,5 @@ func FromPBStruct(object interface{}, s *pb_struct.Struct) error {
if err != nil { if err != nil {
return err return err
} }
return json.Unmarshal([]byte(jsonString), object) return json.Unmarshal([]byte(jsonString), object)
} }

View File

@ -8,17 +8,15 @@ import (
func GetCaller() string { func GetCaller() string {
fpcs := make([]uintptr, 1) fpcs := make([]uintptr, 1)
n := runtime.Callers(3, fpcs) n := runtime.Callers(3, fpcs)
if n == 0 { if n == 0 {
logging.Log("HELPE-rWjfC").Debug("no caller") logging.Log("TRACE-rWjfC").Debug("no caller")
return ""
} }
caller := runtime.FuncForPC(fpcs[0] - 1) caller := runtime.FuncForPC(fpcs[0] - 1)
if caller == nil { 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() return caller.Name()
} }

View File

@ -3,9 +3,7 @@ package config
import ( import (
"encoding/json" "encoding/json"
"google.golang.org/grpc/codes" "github.com/caos/zitadel/internal/errors"
"google.golang.org/grpc/status"
"github.com/caos/zitadel/internal/tracing" "github.com/caos/zitadel/internal/tracing"
tracing_g "github.com/caos/zitadel/internal/tracing/google" tracing_g "github.com/caos/zitadel/internal/tracing/google"
tracing_log "github.com/caos/zitadel/internal/tracing/log" 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 { 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 c.Type = rc.Type
@ -36,7 +34,7 @@ func (c *TracingConfig) UnmarshalJSON(data []byte) error {
var err error var err error
c.Config, err = newTracingConfig(c.Type, rc.Config) c.Config, err = newTracingConfig(c.Type, rc.Config)
if err != nil { if err != nil {
return status.Errorf(codes.Internal, "%v parse config: %v", "TRACE-Ws9E", err) return err
} }
return c.Config.NewTracer() return c.Config.NewTracer()
@ -45,7 +43,7 @@ func (c *TracingConfig) UnmarshalJSON(data []byte) error {
func newTracingConfig(tracerType string, configData []byte) (tracing.Config, error) { func newTracingConfig(tracerType string, configData []byte) (tracing.Config, error) {
t, ok := tracer[tracerType] t, ok := tracer[tracerType]
if !ok { 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() tracingConfig := t()
@ -54,7 +52,7 @@ func newTracingConfig(tracerType string, configData []byte) (tracing.Config, err
} }
if err := json.Unmarshal(configData, tracingConfig); err != nil { 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 return tracingConfig, nil

View File

@ -2,9 +2,8 @@ package google
import ( import (
"go.opencensus.io/trace" "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" "github.com/caos/zitadel/internal/tracing"
) )
@ -16,7 +15,7 @@ type Config struct {
func (c *Config) NewTracer() error { func (c *Config) NewTracer() error {
if !envIsSet() { 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)} tracing.T = &Tracer{projectID: c.ProjectID, metricPrefix: c.MetricPrefix, sampler: trace.ProbabilitySampler(c.Fraction)}

View File

@ -7,6 +7,8 @@ import (
"go.opencensus.io/trace" "go.opencensus.io/trace"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
"github.com/caos/zitadel/internal/errors"
) )
type Span struct { type Span struct {
@ -80,7 +82,7 @@ func toTraceAttribute(key string, value interface{}) (attr trace.Attribute, err
if valueInt, err := convertToInt64(value); err == nil { if valueInt, err := convertToInt64(value); err == nil {
return trace.Int64Attribute(key, valueInt), 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) { func convertToInt64(value interface{}) (int64, error) {

View File

@ -10,8 +10,8 @@ import (
) )
type Config struct { type Config struct {
App *app.Config App app.Config
API *api.Config API api.Config
} }
func Start(ctx context.Context, config Config, authZ auth.Config) error { func Start(ctx context.Context, config Config, authZ auth.Config) error {

View File

@ -3,5 +3,5 @@ package api
import "github.com/caos/zitadel/internal/api/grpc" import "github.com/caos/zitadel/internal/api/grpc"
type Config struct { type Config struct {
GRPC *grpc.Config GRPC grpc.Config
} }

View File

@ -3,5 +3,5 @@ package api
import "github.com/caos/zitadel/internal/api/grpc" import "github.com/caos/zitadel/internal/api/grpc"
type Config struct { type Config struct {
GRPC *grpc.Config GRPC grpc.Config
} }

View File

@ -10,8 +10,8 @@ import (
) )
type Config struct { type Config struct {
App *app.Config App app.Config
API *api.Config API api.Config
} }
func Start(ctx context.Context, config Config, authZ auth.Config) error { func Start(ctx context.Context, config Config, authZ auth.Config) error {