Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Veronika Solovei committed Jun 2, 2021
1 parent cce1f37 commit 0e87b44
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 53 deletions.
1 change: 1 addition & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ func TestFullConfig(t *testing.T) {
cmpStrings(t, "request_validation.ipv6_private_networks", cfg.RequestValidation.IPv6PrivateNetworks[0], "1111::/16")
cmpStrings(t, "request_validation.ipv6_private_networks", cfg.RequestValidation.IPv6PrivateNetworks[1], "2222::/16")
cmpBools(t, "generate_bid_id", cfg.GenerateBidID, true)
cmpStrings(t, "debug.override_token", cfg.Debug.OverrideToken, "")
}

func TestUnmarshalAdapterExtraInfo(t *testing.T) {
Expand Down
10 changes: 5 additions & 5 deletions endpoints/openrtb2/video_auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (deps *endpointDeps) VideoAuctionEndpoint(w http.ResponseWriter, r *http.Re
Regexp: deps.debugLogRegexp,
DebugOverride: exchange.IsDebugOverrideEnabled(r.Header.Get(exchange.DebugOverrideHeader), deps.cfg.Debug.OverrideToken),
}
debugLog.DebugConsolidated = debugLog.Enabled || debugLog.DebugOverride
debugLog.DebugEnabledOrOverridden = debugLog.Enabled || debugLog.DebugOverride

defer func() {
if len(debugLog.CacheKey) > 0 && vo.VideoResponse == nil {
Expand All @@ -159,7 +159,7 @@ func (deps *endpointDeps) VideoAuctionEndpoint(w http.ResponseWriter, r *http.Re
}

resolvedRequest := requestJson
if debugLog.DebugConsolidated {
if debugLog.DebugEnabledOrOverridden {
debugLog.Data.Request = string(requestJson)
if headerBytes, err := json.Marshal(r.Header); err == nil {
debugLog.Data.Headers = string(headerBytes)
Expand Down Expand Up @@ -211,7 +211,7 @@ func (deps *endpointDeps) VideoAuctionEndpoint(w http.ResponseWriter, r *http.Re
//create full open rtb req from full video request
mergeData(videoBidReq, bidReq)
// If debug query param is set, force the response to enable test flag
if debugLog.DebugConsolidated {
if debugLog.DebugEnabledOrOverridden {
bidReq.Test = 1
}

Expand Down Expand Up @@ -308,7 +308,7 @@ func (deps *endpointDeps) VideoAuctionEndpoint(w http.ResponseWriter, r *http.Re
bidResp.Ext = response.Ext
}

if len(bidResp.AdPods) == 0 && debugLog.DebugConsolidated {
if len(bidResp.AdPods) == 0 && debugLog.DebugEnabledOrOverridden {
err := debugLog.PutDebugLogError(deps.cache, deps.cfg.CacheURL.ExpectedTimeMillis, vo.Errors)
if err != nil {
vo.Errors = append(vo.Errors, err)
Expand Down Expand Up @@ -346,7 +346,7 @@ func cleanupVideoBidRequest(videoReq *openrtb_ext.BidRequestVideo, podErrors []P
}

func handleError(labels *metrics.Labels, w http.ResponseWriter, errL []error, vo *analytics.VideoObject, debugLog *exchange.DebugLog) {
if debugLog != nil && debugLog.DebugConsolidated {
if debugLog != nil && debugLog.DebugEnabledOrOverridden {
if rawUUID, err := uuid.NewV4(); err == nil {
debugLog.CacheKey = rawUUID.String()
}
Expand Down
8 changes: 4 additions & 4 deletions endpoints/openrtb2/video_auction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1041,10 +1041,10 @@ func TestHandleErrorDebugLog(t *testing.T) {
Headers: "test headers string",
Response: "test response string",
},
TTL: int64(3600),
Regexp: regexp.MustCompile(`[<>]`),
DebugOverride: false,
DebugConsolidated: true,
TTL: int64(3600),
Regexp: regexp.MustCompile(`[<>]`),
DebugOverride: false,
DebugEnabledOrOverridden: true,
}
handleError(&labels, recorder, []error{err1, err2}, &vo, &debugLog)

Expand Down
9 changes: 3 additions & 6 deletions exchange/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type DebugLog struct {
Regexp *regexp.Regexp
DebugOverride bool
//little optimization, it stores value of debugLog.Enabled || debugLog.DebugOverride
DebugConsolidated bool
DebugEnabledOrOverridden bool
}

type DebugData struct {
Expand All @@ -55,10 +55,7 @@ func (d *DebugLog) BuildCacheString() {
}

func IsDebugOverrideEnabled(debugHeader, configOverrideToken string) bool {
if configOverrideToken != "" && debugHeader == configOverrideToken {
return true
}
return false
return configOverrideToken != "" && debugHeader == configOverrideToken
}

func (d *DebugLog) PutDebugLogError(cache prebid_cache_client.Client, timeout int, errors []error) error {
Expand Down Expand Up @@ -252,7 +249,7 @@ func (a *auction) doCache(ctx context.Context, cache prebid_cache_client.Client,
}
}

if len(toCache) > 0 && debugLog != nil && debugLog.DebugConsolidated {
if len(toCache) > 0 && debugLog != nil && debugLog.DebugEnabledOrOverridden {
debugLog.CacheKey = hbCacheID
debugLog.BuildCacheString()
if jsonBytes, err := json.Marshal(debugLog.CacheString); err == nil {
Expand Down
47 changes: 24 additions & 23 deletions exchange/auction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,50 +119,51 @@ func TestCacheJSON(t *testing.T) {
}

func TestIsDebugOverrideEnabled(t *testing.T) {

type inTest struct {
debugHeader string
configToken string
}
type outTest struct {
result bool
}
type aTest struct {
desc string
in inTest
out outTest
desc string
in inTest
result bool
}
testCases := []aTest{
{
desc: "test debug header is empty, config token is empty",
in: inTest{debugHeader: "", configToken: ""},
out: outTest{result: false},
desc: "test debug header is empty, config token is empty",
in: inTest{debugHeader: "", configToken: ""},
result: false,
},
{
desc: "test debug header is present, config token is empty",
in: inTest{debugHeader: "TestToken", configToken: ""},
result: false,
},
{
desc: "test debug header is present, config token is empty",
in: inTest{debugHeader: "TestToken", configToken: ""},
out: outTest{result: false},
desc: "test debug header is empty, config token is present",
in: inTest{debugHeader: "", configToken: "TestToken"},
result: false,
},
{
desc: "test debug header is empty, config token is present",
in: inTest{debugHeader: "", configToken: "TestToken"},
out: outTest{result: false},
desc: "test debug header is present, config token is present, not equal",
in: inTest{debugHeader: "TestToken123", configToken: "TestToken"},
result: false,
},
{
desc: "test debug header is present, config token is present, not equal",
in: inTest{debugHeader: "TestToken123", configToken: "TestToken"},
out: outTest{result: false},
desc: "test debug header is present, config token is present, equal",
in: inTest{debugHeader: "TestToken", configToken: "TestToken"},
result: true,
},
{
desc: "test debug header is present, config token is present, equal",
in: inTest{debugHeader: "TestToken", configToken: "TestToken"},
out: outTest{result: true},
desc: "test debug header is present, config token is present, not case equal",
in: inTest{debugHeader: "TestTokeN", configToken: "TestToken"},
result: false,
},
}

for _, test := range testCases {
result := IsDebugOverrideEnabled(test.in.debugHeader, test.in.configToken)
assert.Equal(t, test.out.result, result, test.desc)
assert.Equal(t, test.result, result, test.desc)
}

}
Expand Down
24 changes: 14 additions & 10 deletions exchange/bidder.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,21 @@ func (bidder *bidderAdapter) requestBid(ctx context.Context, request *openrtb2.B
// - debugContextKey (url param) in true
// - account debug is allowed
// - bidder debug is allowed
debugInfo := ctx.Value(DebugContextKey)
if headerDebugAllowed || (debugInfo != nil && debugInfo.(bool)) {
if headerDebugAllowed || accountDebugAllowed {
if headerDebugAllowed || bidder.config.DebugInfo.Allow {
seatBid.httpCalls = append(seatBid.httpCalls, makeExt(httpInfo))
} else {
debugDisabledWarning := errortypes.Warning{
WarningCode: errortypes.BidderLevelDebugDisabledWarningCode,
Message: "debug turned off for bidder",
if headerDebugAllowed {
seatBid.httpCalls = append(seatBid.httpCalls, makeExt(httpInfo))
} else {
debugInfo := ctx.Value(DebugContextKey)
if debugInfo != nil && debugInfo.(bool) {
if accountDebugAllowed {
if bidder.config.DebugInfo.Allow {
seatBid.httpCalls = append(seatBid.httpCalls, makeExt(httpInfo))
} else {
debugDisabledWarning := errortypes.Warning{
WarningCode: errortypes.BidderLevelDebugDisabledWarningCode,
Message: "debug turned off for bidder",
}
errs = append(errs, &debugDisabledWarning)
}
errs = append(errs, &debugDisabledWarning)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion exchange/cachetest/debuglog_enabled.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"debugLog": {
"Enabled": true,
"DebugConsolidated": true,
"DebugEnabledOrOverridden": true,
"DebugOverride": false,
"CacheType": "xml",
"TTL": 3600,
Expand Down
4 changes: 2 additions & 2 deletions exchange/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (e *exchange) HoldAuction(ctx context.Context, r AuctionRequest, debugLog *
}

bidResponseExt = e.makeExtBidResponse(adapterBids, adapterExtra, r, debugInfo, errs)
if debugLog.DebugConsolidated {
if debugLog.DebugEnabledOrOverridden {
if bidRespExtBytes, err := json.Marshal(bidResponseExt); err == nil {
debugLog.Data.Response = string(bidRespExtBytes)
} else {
Expand All @@ -260,7 +260,7 @@ func (e *exchange) HoldAuction(ctx context.Context, r AuctionRequest, debugLog *
} else {
bidResponseExt = e.makeExtBidResponse(adapterBids, adapterExtra, r, debugInfo, errs)

if debugLog.DebugConsolidated {
if debugLog.DebugEnabledOrOverridden {

if bidRespExtBytes, err := json.Marshal(bidResponseExt); err == nil {
debugLog.Data.Response = string(bidRespExtBytes)
Expand Down
2 changes: 1 addition & 1 deletion exchange/exchangetest/debuglog_enabled.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"debugLog": {
"Enabled": true,
"DebugConsolidated": true,
"DebugEnabledOrOverridden": true,
"DebugOverride": false,
"CacheType": "xml",
"TTL": 3600,
Expand Down
2 changes: 1 addition & 1 deletion exchange/exchangetest/debuglog_enabled_no_bids.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"debugLog": {
"Enabled": true,
"DebugConsolidated": true,
"DebugEnabledOrOverridden": true,
"DebugOverride": false,
"CacheType": "xml",
"TTL": 3600,
Expand Down

0 comments on commit 0e87b44

Please sign in to comment.