From b0230ea5a3ec55cef98dbf2075e243560a5c7d75 Mon Sep 17 00:00:00 2001 From: Veronika Solovei Date: Thu, 10 Jun 2021 12:27:37 -0700 Subject: [PATCH] Debug override header (#1853) --- config/config.go | 2 + config/config_test.go | 1 + endpoints/openrtb2/video_auction.go | 18 ++--- endpoints/openrtb2/video_auction_test.go | 6 +- exchange/auction.go | 27 +++++--- exchange/auction_test.go | 50 ++++++++++++++ exchange/bidder.go | 28 +++++--- exchange/bidder_test.go | 18 +++-- exchange/bidder_validate_bids.go | 4 +- exchange/bidder_validate_bids_test.go | 10 +-- exchange/cachetest/debuglog_enabled.json | 2 + exchange/exchange.go | 19 +++--- exchange/exchange_test.go | 68 +++++++++++++------ exchange/exchangetest/debuglog_enabled.json | 2 + .../debuglog_enabled_no_bids.json | 2 + 15 files changed, 186 insertions(+), 71 deletions(-) diff --git a/config/config.go b/config/config.go index 45c0e6660f3..0f81c1f16a2 100644 --- a/config/config.go +++ b/config/config.go @@ -454,6 +454,7 @@ type DefReqFiles struct { type Debug struct { TimeoutNotification TimeoutNotification `mapstructure:"timeout_notification"` + OverrideToken string `mapstructure:"override_token"` } func (cfg *Debug) validate(errs []error) []error { @@ -1000,6 +1001,7 @@ func SetupViper(v *viper.Viper, filename string) { v.SetDefault("debug.timeout_notification.log", false) v.SetDefault("debug.timeout_notification.sampling_rate", 0.0) v.SetDefault("debug.timeout_notification.fail_only", false) + v.SetDefault("debug.override_token", "") /* IPv4 /* Site Local: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 diff --git a/config/config_test.go b/config/config_test.go index 1d4c00a5cd1..84d3b4794a9 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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) { diff --git a/endpoints/openrtb2/video_auction.go b/endpoints/openrtb2/video_auction.go index 84f0699e011..0af3ba512bb 100644 --- a/endpoints/openrtb2/video_auction.go +++ b/endpoints/openrtb2/video_auction.go @@ -128,11 +128,13 @@ func (deps *endpointDeps) VideoAuctionEndpoint(w http.ResponseWriter, r *http.Re cacheTTL = int64(deps.cfg.CacheURL.DefaultTTLs.Video) } debugLog := exchange.DebugLog{ - Enabled: strings.EqualFold(debugQuery, "true"), - CacheType: prebid_cache_client.TypeXML, - TTL: cacheTTL, - Regexp: deps.debugLogRegexp, + Enabled: strings.EqualFold(debugQuery, "true"), + CacheType: prebid_cache_client.TypeXML, + TTL: cacheTTL, + Regexp: deps.debugLogRegexp, + DebugOverride: exchange.IsDebugOverrideEnabled(r.Header.Get(exchange.DebugOverrideHeader), deps.cfg.Debug.OverrideToken), } + debugLog.DebugEnabledOrOverridden = debugLog.Enabled || debugLog.DebugOverride defer func() { if len(debugLog.CacheKey) > 0 && vo.VideoResponse == nil { @@ -157,7 +159,7 @@ func (deps *endpointDeps) VideoAuctionEndpoint(w http.ResponseWriter, r *http.Re } resolvedRequest := requestJson - if debugLog.Enabled { + if debugLog.DebugEnabledOrOverridden { debugLog.Data.Request = string(requestJson) if headerBytes, err := json.Marshal(r.Header); err == nil { debugLog.Data.Headers = string(headerBytes) @@ -209,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.Enabled { + if debugLog.DebugEnabledOrOverridden { bidReq.Test = 1 } @@ -306,7 +308,7 @@ func (deps *endpointDeps) VideoAuctionEndpoint(w http.ResponseWriter, r *http.Re bidResp.Ext = response.Ext } - if len(bidResp.AdPods) == 0 && debugLog.Enabled { + 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) @@ -344,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.Enabled { + if debugLog != nil && debugLog.DebugEnabledOrOverridden { if rawUUID, err := uuid.NewV4(); err == nil { debugLog.CacheKey = rawUUID.String() } diff --git a/endpoints/openrtb2/video_auction_test.go b/endpoints/openrtb2/video_auction_test.go index 5377a0c2570..2246311f317 100644 --- a/endpoints/openrtb2/video_auction_test.go +++ b/endpoints/openrtb2/video_auction_test.go @@ -1041,8 +1041,10 @@ func TestHandleErrorDebugLog(t *testing.T) { Headers: "test headers string", Response: "test response string", }, - TTL: int64(3600), - Regexp: regexp.MustCompile(`[<>]`), + TTL: int64(3600), + Regexp: regexp.MustCompile(`[<>]`), + DebugOverride: false, + DebugEnabledOrOverridden: true, } handleError(&labels, recorder, []error{err1, err2}, &vo, &debugLog) diff --git a/exchange/auction.go b/exchange/auction.go index f2c37f7a8bd..2b5d6f75aeb 100644 --- a/exchange/auction.go +++ b/exchange/auction.go @@ -17,14 +17,21 @@ import ( "github.com/prebid/prebid-server/prebid_cache_client" ) +const ( + DebugOverrideHeader string = "x-pbs-debug-override" +) + type DebugLog struct { - Enabled bool - CacheType prebid_cache_client.PayloadType - Data DebugData - TTL int64 - CacheKey string - CacheString string - Regexp *regexp.Regexp + Enabled bool + CacheType prebid_cache_client.PayloadType + Data DebugData + TTL int64 + CacheKey string + CacheString string + Regexp *regexp.Regexp + DebugOverride bool + //little optimization, it stores value of debugLog.Enabled || debugLog.DebugOverride + DebugEnabledOrOverridden bool } type DebugData struct { @@ -47,6 +54,10 @@ func (d *DebugLog) BuildCacheString() { d.CacheString = fmt.Sprintf("%s%s%s%s", xml.Header, d.Data.Request, d.Data.Headers, d.Data.Response) } +func IsDebugOverrideEnabled(debugHeader, configOverrideToken string) bool { + return configOverrideToken != "" && debugHeader == configOverrideToken +} + func (d *DebugLog) PutDebugLogError(cache prebid_cache_client.Client, timeout int, errors []error) error { if len(d.Data.Response) == 0 && len(errors) == 0 { d.Data.Response = "No response or errors created" @@ -238,7 +249,7 @@ func (a *auction) doCache(ctx context.Context, cache prebid_cache_client.Client, } } - if len(toCache) > 0 && debugLog != nil && debugLog.Enabled { + if len(toCache) > 0 && debugLog != nil && debugLog.DebugEnabledOrOverridden { debugLog.CacheKey = hbCacheID debugLog.BuildCacheString() if jsonBytes, err := json.Marshal(debugLog.CacheString); err == nil { diff --git a/exchange/auction_test.go b/exchange/auction_test.go index 1730309287c..04aef256a81 100644 --- a/exchange/auction_test.go +++ b/exchange/auction_test.go @@ -132,6 +132,56 @@ func TestCacheJSON(t *testing.T) { } } +func TestIsDebugOverrideEnabled(t *testing.T) { + type inTest struct { + debugHeader string + configToken string + } + type aTest struct { + desc string + in inTest + result bool + } + testCases := []aTest{ + { + 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 empty, config token is present", + in: inTest{debugHeader: "", configToken: "TestToken"}, + 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, equal", + in: inTest{debugHeader: "TestToken", configToken: "TestToken"}, + 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.result, result, test.desc) + } + +} + // LoadCacheSpec reads and parses a file as a test case. If something goes wrong, it returns an error. func loadCacheSpec(filename string) (*cacheSpec, error) { specData, err := ioutil.ReadFile(filename) diff --git a/exchange/bidder.go b/exchange/bidder.go index 413e6bb67c5..8dcc9b5b856 100644 --- a/exchange/bidder.go +++ b/exchange/bidder.go @@ -49,7 +49,7 @@ type adaptedBidder interface { // // Any errors will be user-facing in the API. // Error messages should help publishers understand what might account for "bad" bids. - requestBid(ctx context.Context, request *openrtb2.BidRequest, name openrtb_ext.BidderName, bidAdjustment float64, conversions currency.Conversions, reqInfo *adapters.ExtraRequestInfo, accountDebugAllowed bool) (*pbsOrtbSeatBid, []error) + requestBid(ctx context.Context, request *openrtb2.BidRequest, name openrtb_ext.BidderName, bidAdjustment float64, conversions currency.Conversions, reqInfo *adapters.ExtraRequestInfo, accountDebugAllowed, headerDebugAllowed bool) (*pbsOrtbSeatBid, []error) } // pbsOrtbBid is a Bid returned by an adaptedBidder. @@ -128,7 +128,7 @@ type bidderAdapterConfig struct { DebugInfo config.DebugInfo } -func (bidder *bidderAdapter) requestBid(ctx context.Context, request *openrtb2.BidRequest, name openrtb_ext.BidderName, bidAdjustment float64, conversions currency.Conversions, reqInfo *adapters.ExtraRequestInfo, accountDebugAllowed bool) (*pbsOrtbSeatBid, []error) { +func (bidder *bidderAdapter) requestBid(ctx context.Context, request *openrtb2.BidRequest, name openrtb_ext.BidderName, bidAdjustment float64, conversions currency.Conversions, reqInfo *adapters.ExtraRequestInfo, accountDebugAllowed, headerDebugAllowed bool) (*pbsOrtbSeatBid, []error) { reqData, errs := bidder.Bidder.MakeRequests(request, reqInfo) if len(reqData) == 0 { @@ -178,19 +178,25 @@ func (bidder *bidderAdapter) requestBid(ctx context.Context, request *openrtb2.B httpInfo := <-responseChannel // If this is a test bid, capture debugging info from the requests. // Write debug data to ext in case if: + // - headerDebugAllowed (debug override header specified correct) - it overrides all other debug restrictions // - debugContextKey (url param) in true // - account debug is allowed // - bidder debug is allowed - if debugInfo := ctx.Value(DebugContextKey); 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", + 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) } } } diff --git a/exchange/bidder_test.go b/exchange/bidder_test.go index 6c31865f8a4..39a5b992ea9 100644 --- a/exchange/bidder_test.go +++ b/exchange/bidder_test.go @@ -94,7 +94,7 @@ func TestSingleBidder(t *testing.T) { bidder := adaptBidder(bidderImpl, server.Client(), &config.Configuration{}, &metricsConfig.DummyMetricsEngine{}, openrtb_ext.BidderAppnexus, test.debugInfo) currencyConverter := currency.NewRateConverter(&http.Client{}, "", time.Duration(0)) - seatBid, errs := bidder.requestBid(ctx, &openrtb2.BidRequest{}, "test", bidAdjustment, currencyConverter.Rates(), &adapters.ExtraRequestInfo{}, true) + seatBid, errs := bidder.requestBid(ctx, &openrtb2.BidRequest{}, "test", bidAdjustment, currencyConverter.Rates(), &adapters.ExtraRequestInfo{}, true, false) // Make sure the goodSingleBidder was called with the expected arguments. if bidderImpl.httpResponse == nil { @@ -167,7 +167,7 @@ func TestRequestBidRemovesSensitiveHeaders(t *testing.T) { bidder := adaptBidder(bidderImpl, server.Client(), &config.Configuration{}, &metricsConfig.DummyMetricsEngine{}, openrtb_ext.BidderAppnexus, debugInfo) currencyConverter := currency.NewRateConverter(&http.Client{}, "", time.Duration(0)) - seatBid, errs := bidder.requestBid(ctx, &openrtb2.BidRequest{}, "test", 1, currencyConverter.Rates(), &adapters.ExtraRequestInfo{}, true) + seatBid, errs := bidder.requestBid(ctx, &openrtb2.BidRequest{}, "test", 1, currencyConverter.Rates(), &adapters.ExtraRequestInfo{}, true, false) expectedHttpCalls := []*openrtb_ext.ExtHttpCall{ { @@ -208,7 +208,7 @@ func TestSetGPCHeader(t *testing.T) { bidder := adaptBidder(bidderImpl, server.Client(), &config.Configuration{}, &metricsConfig.DummyMetricsEngine{}, openrtb_ext.BidderAppnexus, debugInfo) currencyConverter := currency.NewRateConverter(&http.Client{}, "", time.Duration(0)) - seatBid, errs := bidder.requestBid(ctx, &openrtb2.BidRequest{}, "test", 1, currencyConverter.Rates(), &adapters.ExtraRequestInfo{GlobalPrivacyControlHeader: "1"}, true) + seatBid, errs := bidder.requestBid(ctx, &openrtb2.BidRequest{}, "test", 1, currencyConverter.Rates(), &adapters.ExtraRequestInfo{GlobalPrivacyControlHeader: "1"}, true, false) expectedHttpCall := []*openrtb_ext.ExtHttpCall{ { @@ -246,7 +246,7 @@ func TestSetGPCHeaderNil(t *testing.T) { bidder := adaptBidder(bidderImpl, server.Client(), &config.Configuration{}, &metricsConfig.DummyMetricsEngine{}, openrtb_ext.BidderAppnexus, debugInfo) currencyConverter := currency.NewRateConverter(&http.Client{}, "", time.Duration(0)) - seatBid, errs := bidder.requestBid(ctx, &openrtb2.BidRequest{}, "test", 1, currencyConverter.Rates(), &adapters.ExtraRequestInfo{GlobalPrivacyControlHeader: "1"}, true) + seatBid, errs := bidder.requestBid(ctx, &openrtb2.BidRequest{}, "test", 1, currencyConverter.Rates(), &adapters.ExtraRequestInfo{GlobalPrivacyControlHeader: "1"}, true, false) expectedHttpCall := []*openrtb_ext.ExtHttpCall{ { @@ -304,7 +304,7 @@ func TestMultiBidder(t *testing.T) { } bidder := adaptBidder(bidderImpl, server.Client(), &config.Configuration{}, &metricsConfig.DummyMetricsEngine{}, openrtb_ext.BidderAppnexus, nil) currencyConverter := currency.NewRateConverter(&http.Client{}, "", time.Duration(0)) - seatBid, errs := bidder.requestBid(context.Background(), &openrtb2.BidRequest{}, "test", 1.0, currencyConverter.Rates(), &adapters.ExtraRequestInfo{}, true) + seatBid, errs := bidder.requestBid(context.Background(), &openrtb2.BidRequest{}, "test", 1.0, currencyConverter.Rates(), &adapters.ExtraRequestInfo{}, true, true) if seatBid == nil { t.Fatalf("SeatBid should exist, because bids exist.") @@ -681,6 +681,7 @@ func TestMultiCurrencies(t *testing.T) { currencyConverter.Rates(), &adapters.ExtraRequestInfo{}, true, + true, ) // Verify: @@ -826,6 +827,7 @@ func TestMultiCurrencies_RateConverterNotSet(t *testing.T) { currencyConverter.Rates(), &adapters.ExtraRequestInfo{}, true, + true, ) // Verify: @@ -999,6 +1001,7 @@ func TestMultiCurrencies_RequestCurrencyPick(t *testing.T) { currencyConverter.Rates(), &adapters.ExtraRequestInfo{}, true, + false, ) // Verify: @@ -1303,6 +1306,7 @@ func TestMobileNativeTypes(t *testing.T) { currencyConverter.Rates(), &adapters.ExtraRequestInfo{}, true, + true, ) var actualValue string @@ -1316,7 +1320,7 @@ func TestMobileNativeTypes(t *testing.T) { func TestErrorReporting(t *testing.T) { bidder := adaptBidder(&bidRejector{}, nil, &config.Configuration{}, &metricsConfig.DummyMetricsEngine{}, openrtb_ext.BidderAppnexus, nil) currencyConverter := currency.NewRateConverter(&http.Client{}, "", time.Duration(0)) - bids, errs := bidder.requestBid(context.Background(), &openrtb2.BidRequest{}, "test", 1.0, currencyConverter.Rates(), &adapters.ExtraRequestInfo{}, true) + bids, errs := bidder.requestBid(context.Background(), &openrtb2.BidRequest{}, "test", 1.0, currencyConverter.Rates(), &adapters.ExtraRequestInfo{}, true, false) if bids != nil { t.Errorf("There should be no seatbid if no http requests are returned.") } @@ -1537,7 +1541,7 @@ func TestCallRecordAdapterConnections(t *testing.T) { // Run requestBid using an http.Client with a mock handler bidder := adaptBidder(bidderImpl, server.Client(), &config.Configuration{}, metrics, openrtb_ext.BidderAppnexus, nil) currencyConverter := currency.NewRateConverter(&http.Client{}, "", time.Duration(0)) - _, errs := bidder.requestBid(context.Background(), &openrtb2.BidRequest{}, "test", bidAdjustment, currencyConverter.Rates(), &adapters.ExtraRequestInfo{}, true) + _, errs := bidder.requestBid(context.Background(), &openrtb2.BidRequest{}, "test", bidAdjustment, currencyConverter.Rates(), &adapters.ExtraRequestInfo{}, true, true) // Assert no errors assert.Equal(t, 0, len(errs), "bidder.requestBid returned errors %v \n", errs) diff --git a/exchange/bidder_validate_bids.go b/exchange/bidder_validate_bids.go index 3d2eb0b8e42..aec0948ddde 100644 --- a/exchange/bidder_validate_bids.go +++ b/exchange/bidder_validate_bids.go @@ -28,8 +28,8 @@ type validatedBidder struct { bidder adaptedBidder } -func (v *validatedBidder) requestBid(ctx context.Context, request *openrtb2.BidRequest, name openrtb_ext.BidderName, bidAdjustment float64, conversions currency.Conversions, reqInfo *adapters.ExtraRequestInfo, accountDebugAllowed bool) (*pbsOrtbSeatBid, []error) { - seatBid, errs := v.bidder.requestBid(ctx, request, name, bidAdjustment, conversions, reqInfo, accountDebugAllowed) +func (v *validatedBidder) requestBid(ctx context.Context, request *openrtb2.BidRequest, name openrtb_ext.BidderName, bidAdjustment float64, conversions currency.Conversions, reqInfo *adapters.ExtraRequestInfo, accountDebugAllowed, headerDebugAllowed bool) (*pbsOrtbSeatBid, []error) { + seatBid, errs := v.bidder.requestBid(ctx, request, name, bidAdjustment, conversions, reqInfo, accountDebugAllowed, headerDebugAllowed) if validationErrors := removeInvalidBids(request, seatBid); len(validationErrors) > 0 { errs = append(errs, validationErrors...) } diff --git a/exchange/bidder_validate_bids_test.go b/exchange/bidder_validate_bids_test.go index 3bb43559856..06973b837c2 100644 --- a/exchange/bidder_validate_bids_test.go +++ b/exchange/bidder_validate_bids_test.go @@ -42,7 +42,7 @@ func TestAllValidBids(t *testing.T) { }, }, }) - seatBid, errs := bidder.requestBid(context.Background(), &openrtb2.BidRequest{}, openrtb_ext.BidderAppnexus, 1.0, currency.NewConstantRates(), &adapters.ExtraRequestInfo{}, true) + seatBid, errs := bidder.requestBid(context.Background(), &openrtb2.BidRequest{}, openrtb_ext.BidderAppnexus, 1.0, currency.NewConstantRates(), &adapters.ExtraRequestInfo{}, true, false) assert.Len(t, seatBid.bids, 3) assert.Len(t, errs, 0) } @@ -83,7 +83,7 @@ func TestAllBadBids(t *testing.T) { }, }, }) - seatBid, errs := bidder.requestBid(context.Background(), &openrtb2.BidRequest{}, openrtb_ext.BidderAppnexus, 1.0, currency.NewConstantRates(), &adapters.ExtraRequestInfo{}, true) + seatBid, errs := bidder.requestBid(context.Background(), &openrtb2.BidRequest{}, openrtb_ext.BidderAppnexus, 1.0, currency.NewConstantRates(), &adapters.ExtraRequestInfo{}, true, false) assert.Len(t, seatBid.bids, 0) assert.Len(t, errs, 5) } @@ -126,7 +126,7 @@ func TestMixedBids(t *testing.T) { }, }, }) - seatBid, errs := bidder.requestBid(context.Background(), &openrtb2.BidRequest{}, openrtb_ext.BidderAppnexus, 1.0, currency.NewConstantRates(), &adapters.ExtraRequestInfo{}, true) + seatBid, errs := bidder.requestBid(context.Background(), &openrtb2.BidRequest{}, openrtb_ext.BidderAppnexus, 1.0, currency.NewConstantRates(), &adapters.ExtraRequestInfo{}, true, false) assert.Len(t, seatBid.bids, 2) assert.Len(t, errs, 3) } @@ -246,7 +246,7 @@ func TestCurrencyBids(t *testing.T) { Cur: tc.brqCur, } - seatBid, errs := bidder.requestBid(context.Background(), request, openrtb_ext.BidderAppnexus, 1.0, currency.NewConstantRates(), &adapters.ExtraRequestInfo{}, true) + seatBid, errs := bidder.requestBid(context.Background(), request, openrtb_ext.BidderAppnexus, 1.0, currency.NewConstantRates(), &adapters.ExtraRequestInfo{}, true, false) assert.Len(t, seatBid.bids, expectedValidBids) assert.Len(t, errs, expectedErrs) } @@ -257,6 +257,6 @@ type mockAdaptedBidder struct { errorResponse []error } -func (b *mockAdaptedBidder) requestBid(ctx context.Context, request *openrtb2.BidRequest, name openrtb_ext.BidderName, bidAdjustment float64, conversions currency.Conversions, reqInfo *adapters.ExtraRequestInfo, accountDebugAllowed bool) (*pbsOrtbSeatBid, []error) { +func (b *mockAdaptedBidder) requestBid(ctx context.Context, request *openrtb2.BidRequest, name openrtb_ext.BidderName, bidAdjustment float64, conversions currency.Conversions, reqInfo *adapters.ExtraRequestInfo, accountDebugAllowed, headerDebugAllowed bool) (*pbsOrtbSeatBid, []error) { return b.bidResponse, b.errorResponse } diff --git a/exchange/cachetest/debuglog_enabled.json b/exchange/cachetest/debuglog_enabled.json index e6c85c57055..faba3ed690d 100644 --- a/exchange/cachetest/debuglog_enabled.json +++ b/exchange/cachetest/debuglog_enabled.json @@ -1,6 +1,8 @@ { "debugLog": { "Enabled": true, + "DebugEnabledOrOverridden": true, + "DebugOverride": false, "CacheType": "xml", "TTL": 3600, "Data": { diff --git a/exchange/exchange.go b/exchange/exchange.go index 7ace5aad814..c5a747766e9 100644 --- a/exchange/exchange.go +++ b/exchange/exchange.go @@ -162,13 +162,13 @@ func (e *exchange) HoldAuction(ctx context.Context, r AuctionRequest, debugLog * } if debugLog == nil { - debugLog = &DebugLog{Enabled: false} + debugLog = &DebugLog{Enabled: false, DebugEnabledOrOverridden: false} } requestDebugInfo := getDebugInfo(r.BidRequest, requestExt) - debugInfo := requestDebugInfo && r.Account.DebugAllow - debugLog.Enabled = debugLog.Enabled && r.Account.DebugAllow + debugInfo := debugLog.DebugEnabledOrOverridden || (requestDebugInfo && r.Account.DebugAllow) + debugLog.Enabled = debugLog.DebugEnabledOrOverridden || r.Account.DebugAllow if debugInfo { ctx = e.makeDebugContext(ctx, debugInfo) @@ -197,7 +197,7 @@ func (e *exchange) HoldAuction(ctx context.Context, r AuctionRequest, debugLog * // Get currency rates conversions for the auction conversions := e.getAuctionCurrencyRates(requestExt.Prebid.CurrencyConversions) - adapterBids, adapterExtra, anyBidsReturned := e.getAllBids(auctionCtx, bidderRequests, bidAdjustmentFactors, conversions, r.Account.DebugAllow, r.GlobalPrivacyControlHeader) + adapterBids, adapterExtra, anyBidsReturned := e.getAllBids(auctionCtx, bidderRequests, bidAdjustmentFactors, conversions, r.Account.DebugAllow, r.GlobalPrivacyControlHeader, debugLog.DebugOverride) var auc *auction var cacheErrs []error @@ -248,7 +248,7 @@ func (e *exchange) HoldAuction(ctx context.Context, r AuctionRequest, debugLog * } bidResponseExt = e.makeExtBidResponse(adapterBids, adapterExtra, r, debugInfo, errs) - if debugLog.Enabled { + if debugLog.DebugEnabledOrOverridden { if bidRespExtBytes, err := json.Marshal(bidResponseExt); err == nil { debugLog.Data.Response = string(bidRespExtBytes) } else { @@ -269,7 +269,7 @@ func (e *exchange) HoldAuction(ctx context.Context, r AuctionRequest, debugLog * } else { bidResponseExt = e.makeExtBidResponse(adapterBids, adapterExtra, r, debugInfo, errs) - if debugLog.Enabled { + if debugLog.DebugEnabledOrOverridden { if bidRespExtBytes, err := json.Marshal(bidResponseExt); err == nil { debugLog.Data.Response = string(bidRespExtBytes) @@ -280,7 +280,7 @@ func (e *exchange) HoldAuction(ctx context.Context, r AuctionRequest, debugLog * } } - if !r.Account.DebugAllow && requestDebugInfo { + if !r.Account.DebugAllow && requestDebugInfo && !debugLog.DebugOverride { accountDebugDisabledWarning := openrtb_ext.ExtBidderMessage{ Code: errortypes.AccountLevelDebugDisabledWarningCode, Message: "debug turned off for account", @@ -413,7 +413,8 @@ func (e *exchange) getAllBids( bidAdjustments map[string]float64, conversions currency.Conversions, accountDebugAllowed bool, - globalPrivacyControlHeader string) ( + globalPrivacyControlHeader string, + headerDebugAllowed bool) ( map[openrtb_ext.BidderName]*pbsOrtbSeatBid, map[openrtb_ext.BidderName]*seatResponseExtra, bool) { // Set up pointers to the bid results @@ -446,7 +447,7 @@ func (e *exchange) getAllBids( var reqInfo adapters.ExtraRequestInfo reqInfo.PbsEntryPoint = bidderRequest.BidderLabels.RType reqInfo.GlobalPrivacyControlHeader = globalPrivacyControlHeader - bids, err := e.adapterMap[bidderRequest.BidderCoreName].requestBid(ctx, bidderRequest.BidRequest, bidderRequest.BidderName, adjustmentFactor, conversions, &reqInfo, accountDebugAllowed) + bids, err := e.adapterMap[bidderRequest.BidderCoreName].requestBid(ctx, bidderRequest.BidRequest, bidderRequest.BidderName, adjustmentFactor, conversions, &reqInfo, accountDebugAllowed, headerDebugAllowed) // Add in time reporting elapsed := time.Since(start) diff --git a/exchange/exchange_test.go b/exchange/exchange_test.go index 7a2020c1819..04072dd1a6e 100644 --- a/exchange/exchange_test.go +++ b/exchange/exchange_test.go @@ -177,8 +177,9 @@ func TestDebugBehaviour(t *testing.T) { } type debugData struct { - bidderLevelDebugAllowed bool - accountLevelDebugAllowed bool + bidderLevelDebugAllowed bool + accountLevelDebugAllowed bool + headerOverrideDebugAllowed bool } type aTest struct { @@ -193,57 +194,78 @@ func TestDebugBehaviour(t *testing.T) { desc: "test flag equals zero, ext debug flag false, no debug info expected", in: inTest{test: 0, debug: false}, out: outTest{debugInfoIncluded: false}, - debugData: debugData{true, true}, + debugData: debugData{true, true, false}, generateWarnings: false, }, { desc: "test flag equals zero, ext debug flag true, debug info expected", in: inTest{test: 0, debug: true}, out: outTest{debugInfoIncluded: true}, - debugData: debugData{true, true}, + debugData: debugData{true, true, false}, generateWarnings: false, }, { desc: "test flag equals 1, ext debug flag false, debug info expected", in: inTest{test: 1, debug: false}, out: outTest{debugInfoIncluded: true}, - debugData: debugData{true, true}, + debugData: debugData{true, true, false}, generateWarnings: false, }, { desc: "test flag equals 1, ext debug flag true, debug info expected", in: inTest{test: 1, debug: true}, out: outTest{debugInfoIncluded: true}, - debugData: debugData{true, true}, + debugData: debugData{true, true, false}, generateWarnings: false, }, { desc: "test flag not equal to 0 nor 1, ext debug flag false, no debug info expected", in: inTest{test: 2, debug: false}, out: outTest{debugInfoIncluded: false}, - debugData: debugData{true, true}, + debugData: debugData{true, true, false}, generateWarnings: false, }, { desc: "test flag not equal to 0 nor 1, ext debug flag true, debug info expected", in: inTest{test: -1, debug: true}, out: outTest{debugInfoIncluded: true}, - debugData: debugData{true, true}, + debugData: debugData{true, true, false}, generateWarnings: true, }, { desc: "test account level debug disabled", in: inTest{test: -1, debug: true}, out: outTest{debugInfoIncluded: false}, - debugData: debugData{true, false}, + debugData: debugData{true, false, false}, generateWarnings: true, }, { - desc: "test bidder level debug disabled", + desc: "test header override enabled when all other debug options are disabled", + in: inTest{test: -1, debug: false}, + out: outTest{debugInfoIncluded: true}, + debugData: debugData{false, false, true}, + generateWarnings: false, + }, + { + desc: "test header override and url debug options are enabled when all other debug options are disabled", in: inTest{test: -1, debug: true}, - out: outTest{debugInfoIncluded: false}, - debugData: debugData{false, true}, - generateWarnings: true, + out: outTest{debugInfoIncluded: true}, + debugData: debugData{false, false, true}, + generateWarnings: false, + }, + { + desc: "test header override and url and bidder debug options are enabled when account debug option is disabled", + in: inTest{test: -1, debug: true}, + out: outTest{debugInfoIncluded: true}, + debugData: debugData{true, false, true}, + generateWarnings: false, + }, + { + desc: "test all debug options are enabled", + in: inTest{test: -1, debug: true}, + out: outTest{debugInfoIncluded: true}, + debugData: debugData{true, true, true}, + generateWarnings: false, }, } @@ -323,9 +345,12 @@ func TestDebugBehaviour(t *testing.T) { WarningCode: errortypes.InvalidPrivacyConsentWarningCode}) auctionRequest.Warnings = errL } - + debugLog := &DebugLog{} + if test.debugData.headerOverrideDebugAllowed { + debugLog = &DebugLog{DebugOverride: true, DebugEnabledOrOverridden: true} + } // Run test - outBidResponse, err := e.HoldAuction(ctx, auctionRequest, nil) + outBidResponse, err := e.HoldAuction(ctx, auctionRequest, debugLog) // Assert no HoldAuction error assert.NoErrorf(t, err, "%s. ex.HoldAuction returned an error: %v \n", test.desc, err) @@ -339,6 +364,11 @@ func TestDebugBehaviour(t *testing.T) { assert.NotEmpty(t, actualExt.Prebid.AuctionTimestamp, "%s. ext.prebid.auctiontimestamp should not be empty when AuctionRequest.StartTime is set") assert.Equal(t, auctionRequest.StartTime.UnixNano()/1e+6, actualExt.Prebid.AuctionTimestamp, "%s. ext.prebid.auctiontimestamp has incorrect value") + if test.debugData.headerOverrideDebugAllowed { + assert.Empty(t, actualExt.Warnings, "warnings should be empty") + assert.Empty(t, actualExt.Errors, "errors should be empty") + } + if test.out.debugInfoIncluded { assert.NotNilf(t, actualExt, "%s. ext.debug field is expected to be included in this outBidResponse.Ext and not be nil. outBidResponse.Ext.Debug = %v \n", test.desc, actualExt.Debug) @@ -358,13 +388,13 @@ func TestDebugBehaviour(t *testing.T) { assert.Nil(t, actualExt.Debug, "%s. ext.debug.httpcalls array should not be empty", "With bidder level debug disable option http calls should be empty") } - if test.out.debugInfoIncluded && !test.debugData.accountLevelDebugAllowed { + if test.out.debugInfoIncluded && !test.debugData.accountLevelDebugAllowed && !test.debugData.headerOverrideDebugAllowed { assert.Len(t, actualExt.Warnings, 1, "warnings should have one warning") assert.NotNil(t, actualExt.Warnings["general"], "general warning should be present") assert.Equal(t, "debug turned off for account", actualExt.Warnings["general"][0].Message, "account debug disabled message should be present") } - if !test.out.debugInfoIncluded && test.in.debug && test.debugData.accountLevelDebugAllowed { + if !test.out.debugInfoIncluded && test.in.debug && test.debugData.accountLevelDebugAllowed && !test.debugData.headerOverrideDebugAllowed { if test.generateWarnings { assert.Len(t, actualExt.Warnings, 2, "warnings should have one warning") } else { @@ -2893,7 +2923,7 @@ type validatingBidder struct { mockResponses map[string]bidderResponse } -func (b *validatingBidder) requestBid(ctx context.Context, request *openrtb2.BidRequest, name openrtb_ext.BidderName, bidAdjustment float64, conversions currency.Conversions, reqInfo *adapters.ExtraRequestInfo, accountDebugAllowed bool) (seatBid *pbsOrtbSeatBid, errs []error) { +func (b *validatingBidder) requestBid(ctx context.Context, request *openrtb2.BidRequest, name openrtb_ext.BidderName, bidAdjustment float64, conversions currency.Conversions, reqInfo *adapters.ExtraRequestInfo, accountDebugAllowed, headerDebugAllowed bool) (seatBid *pbsOrtbSeatBid, errs []error) { if expectedRequest, ok := b.expectations[string(name)]; ok { if expectedRequest != nil { if expectedRequest.BidAdjustment != bidAdjustment { @@ -3072,7 +3102,7 @@ func (e *mockUsersync) LiveSyncCount() int { type panicingAdapter struct{} -func (panicingAdapter) requestBid(ctx context.Context, request *openrtb2.BidRequest, name openrtb_ext.BidderName, bidAdjustment float64, conversions currency.Conversions, reqInfo *adapters.ExtraRequestInfo, accountDebugAllowed bool) (posb *pbsOrtbSeatBid, errs []error) { +func (panicingAdapter) requestBid(ctx context.Context, request *openrtb2.BidRequest, name openrtb_ext.BidderName, bidAdjustment float64, conversions currency.Conversions, reqInfo *adapters.ExtraRequestInfo, accountDebugAllowed, headerDebugAllowed bool) (posb *pbsOrtbSeatBid, errs []error) { panic("Panic! Panic! The world is ending!") } diff --git a/exchange/exchangetest/debuglog_enabled.json b/exchange/exchangetest/debuglog_enabled.json index 851bda69097..8475482f35b 100644 --- a/exchange/exchangetest/debuglog_enabled.json +++ b/exchange/exchangetest/debuglog_enabled.json @@ -1,6 +1,8 @@ { "debugLog": { "Enabled": true, + "DebugEnabledOrOverridden": true, + "DebugOverride": false, "CacheType": "xml", "TTL": 3600, "Data": { diff --git a/exchange/exchangetest/debuglog_enabled_no_bids.json b/exchange/exchangetest/debuglog_enabled_no_bids.json index 4823acf8f16..b9bb15df7fb 100644 --- a/exchange/exchangetest/debuglog_enabled_no_bids.json +++ b/exchange/exchangetest/debuglog_enabled_no_bids.json @@ -1,6 +1,8 @@ { "debugLog": { "Enabled": true, + "DebugEnabledOrOverridden": true, + "DebugOverride": false, "CacheType": "xml", "TTL": 3600, "Data": {