From ffd15a2d6f673bf39cc0c08cc62ab9d3830b7e4b Mon Sep 17 00:00:00 2001 From: Alexey Bynenkov <54314036+abynenkov-ib@users.noreply.github.com> Date: Tue, 17 Nov 2020 21:20:18 +0300 Subject: [PATCH] 'unable to get field from token' spam message (#205) * 'unable to get field from token' spam message * fix TestHeadersToAttributes map --- logging/gateway_interceptor.go | 7 ++++++- logging/interceptor.go | 13 ++++++------- logging/interceptor_test.go | 4 +++- tracing/http_test.go | 14 +++++++++++++- 4 files changed, 28 insertions(+), 10 deletions(-) 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) {