Skip to content

Commit

Permalink
'unable to get field from token' spam message (#205)
Browse files Browse the repository at this point in the history
* 'unable to get field from token' spam message

* fix TestHeadersToAttributes map
  • Loading branch information
abynenkov-ib committed Nov 17, 2020
1 parent f1a1940 commit ffd15a2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
7 changes: 6 additions & 1 deletion logging/gateway_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import (
"github.com/infobloxopen/atlas-app-toolkit/requestid"
)

const (
valueUndefined = "undefined"
)

type gwLogCfg struct {
dynamicLogLvl bool
noRequestID bool
Expand Down Expand Up @@ -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
}
}

Expand Down
13 changes: 6 additions & 7 deletions logging/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion logging/interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 13 additions & 1 deletion tracing/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit ffd15a2

Please sign in to comment.