diff --git a/logging/gateway_interceptor.go b/logging/gateway_interceptor.go index 4e586831..a7f7e8f0 100644 --- a/logging/gateway_interceptor.go +++ b/logging/gateway_interceptor.go @@ -19,6 +19,10 @@ import ( "github.com/infobloxopen/atlas-app-toolkit/requestid" ) +const ( + valueUndefined = "undefined" +) + type gwLogCfg struct { dynamicLogLvl bool noRequestID bool @@ -139,7 +143,8 @@ func GatewayLoggingInterceptor(logger *logrus.Logger, opts ...GWLogOption) grpc. if accountID, err := auth.GetAccountID(metadata.NewIncomingContext(ctx, md), cfg.acctIDKeyfunc); err == nil { fields[auth.MultiTenancyField] = accountID } else { - logger.Error(err) + logger.Info(err) + fields[auth.MultiTenancyField] = valueUndefined } } diff --git a/logging/interceptor.go b/logging/interceptor.go index 47fe452f..6f999fe1 100644 --- a/logging/interceptor.go +++ b/logging/interceptor.go @@ -170,9 +170,8 @@ func setClientInterceptorFields(ctx context.Context, fields logrus.Fields, logge err = addAccountIDField(ctx, fields) if err != nil { - logger.Warn(err) + logger.Info(err) } - setInterceptorFields(ctx, fields, logger, options) } @@ -206,14 +205,14 @@ func addRequestIDField(ctx context.Context, fields logrus.Fields) error { } func addAccountIDField(ctx context.Context, fields logrus.Fields) error { - accountID, err := auth.GetAccountID(ctx, nil) - if err != nil { + if accountID, err := auth.GetAccountID(ctx, nil); err == nil { + fields[DefaultAccountIDKey] = accountID + } else { + fields[DefaultAccountIDKey] = valueUndefined return fmt.Errorf("Unable to get %q from context", DefaultAccountIDKey) } - fields[DefaultAccountIDKey] = accountID - - return err + return nil } func addCustomField(ctx context.Context, fields logrus.Fields, customField string) error { diff --git a/logging/interceptor_test.go b/logging/interceptor_test.go index cfb74c53..45d62095 100644 --- a/logging/interceptor_test.go +++ b/logging/interceptor_test.go @@ -396,9 +396,11 @@ func TestAddAccountIDField(t *testing.T) { func TestAddAccountID_Failed(t *testing.T) { ctx := context.Background() - err := addAccountIDField(ctx, logrus.Fields{}) + result := logrus.Fields{} + err := addAccountIDField(ctx, result) assert.Error(t, err) assert.Equal(t, fmt.Sprintf("Unable to get %q from context", DefaultAccountIDKey), err.Error()) + assert.Equal(t, valueUndefined, result[DefaultAccountIDKey]) } func TestAddCustomField(t *testing.T) { diff --git a/tracing/http_test.go b/tracing/http_test.go index 1733d41d..8313f2fe 100644 --- a/tracing/http_test.go +++ b/tracing/http_test.go @@ -211,8 +211,20 @@ func TestHeadersToAttributes(t *testing.T) { } result := headersToAttributes(testHeaders, "prefix-", defaultHeaderMatcher) + assert.Len(t, result, 2) - assert.Equal(t, expected, result) + for _, attribute := range result { + found := false + for _, expAttribute := range expected { + if reflect.DeepEqual(expAttribute, attribute) { + found = true + break + } + } + if !found { + t.Errorf("Attribute %+v not found in result", attribute) + } + } } func TestMarkSpanTruncated(t *testing.T) {