-
Notifications
You must be signed in to change notification settings - Fork 805
Simplifying exchange module: bidResponseExt gets built anyway #1518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,6 +95,10 @@ func NewExchange(client *http.Client, cache prebid_cache_client.Client, cfg *con | |
} | ||
|
||
func (e *exchange) HoldAuction(ctx context.Context, bidRequest *openrtb.BidRequest, usersyncs IdFetcher, labels pbsmetrics.Labels, account *config.Account, categoriesFetcher *stored_requests.CategoryFetcher, debugLog *DebugLog) (*openrtb.BidResponse, error) { | ||
var auc *auction = nil | ||
var bidResponseExt *openrtb_ext.ExtBidResponse = nil | ||
var cacheErrs []error = nil | ||
var err error | ||
|
||
requestExt, err := extractBidRequestExt(bidRequest) | ||
if err != nil { | ||
|
@@ -114,34 +118,11 @@ func (e *exchange) HoldAuction(ctx context.Context, bidRequest *openrtb.BidReque | |
|
||
bidAdjustmentFactors := getExtBidAdjustmentFactors(requestExt) | ||
|
||
for _, impInRequest := range bidRequest.Imp { | ||
var impLabels pbsmetrics.ImpLabels = pbsmetrics.ImpLabels{ | ||
BannerImps: impInRequest.Banner != nil, | ||
VideoImps: impInRequest.Video != nil, | ||
AudioImps: impInRequest.Audio != nil, | ||
NativeImps: impInRequest.Native != nil, | ||
} | ||
e.me.RecordImps(impLabels) | ||
} | ||
e.recordImpMetrics(bidRequest) | ||
|
||
// Make our best guess if GDPR applies | ||
usersyncIfAmbiguous := e.UsersyncIfAmbiguous | ||
var geo *openrtb.Geo = nil | ||
if bidRequest.User != nil && bidRequest.User.Geo != nil { | ||
geo = bidRequest.User.Geo | ||
} else if bidRequest.Device != nil && bidRequest.Device.Geo != nil { | ||
geo = bidRequest.Device.Geo | ||
} | ||
if geo != nil { | ||
// If we have a country set, and it is on the list, we assume GDPR applies if not set on the request. | ||
// Otherwise we assume it does not apply as long as it appears "valid" (is 3 characters long). | ||
if _, found := e.privacyConfig.GDPR.EEACountriesMap[strings.ToUpper(geo.Country)]; found { | ||
usersyncIfAmbiguous = false | ||
} else if len(geo.Country) == 3 { | ||
// The country field is formatted properly as a three character country code | ||
usersyncIfAmbiguous = true | ||
} | ||
} | ||
usersyncIfAmbiguous := e.parseUsersyncIfAmbiguous(bidRequest) | ||
|
||
// Slice of BidRequests, each a copy of the original cleaned to only contain bidder data for the named bidder | ||
blabels := make(map[openrtb_ext.BidderName]*pbsmetrics.AdapterLabels) | ||
cleanRequests, aliases, privacyLabels, errs := cleanOpenRTBRequests(ctx, bidRequest, requestExt, usersyncs, blabels, labels, e.gDPR, usersyncIfAmbiguous, e.privacyConfig) | ||
|
@@ -161,14 +142,11 @@ func (e *exchange) HoldAuction(ctx context.Context, bidRequest *openrtb.BidReque | |
|
||
adapterBids, adapterExtra, anyBidsReturned := e.getAllBids(auctionCtx, cleanRequests, aliases, bidAdjustmentFactors, blabels, conversions) | ||
|
||
var auc *auction = nil | ||
var bidResponseExt *openrtb_ext.ExtBidResponse = nil | ||
if anyBidsReturned { | ||
|
||
var bidCategory map[string]string | ||
//If includebrandcategory is present in ext then CE feature is on. | ||
if requestExt.Prebid.Targeting != nil && requestExt.Prebid.Targeting.IncludeBrandCategory != nil { | ||
var err error | ||
var rejections []string | ||
bidCategory, adapterBids, rejections, err = applyCategoryMapping(ctx, requestExt, adapterBids, *categoriesFetcher, targData) | ||
if err != nil { | ||
|
@@ -189,52 +167,79 @@ func (e *exchange) HoldAuction(ctx context.Context, bidRequest *openrtb.BidReque | |
errs = append(errs, dealErrs...) | ||
} | ||
|
||
if debugLog != nil && debugLog.Enabled { | ||
SyntaxNode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
bidResponseExt = e.makeExtBidResponse(adapterBids, adapterExtra, bidRequest, debugInfo, errs) | ||
if bidRespExtBytes, err := json.Marshal(bidResponseExt); err == nil { | ||
debugLog.Data.Response = string(bidRespExtBytes) | ||
} else { | ||
debugLog.Data.Response = "Unable to marshal response ext for debugging" | ||
errs = append(errs, errors.New(debugLog.Data.Response)) | ||
} | ||
} | ||
|
||
cacheErrs := auc.doCache(ctx, e.cache, targData, bidRequest, 60, &account.CacheTTL, bidCategory, debugLog) | ||
if len(cacheErrs) > 0 { | ||
errs = append(errs, cacheErrs...) | ||
} | ||
targData.setTargeting(auc, bidRequest.App != nil, bidCategory) | ||
|
||
// Ensure caching errors are added if the bid response ext has already been created | ||
if bidResponseExt != nil && len(cacheErrs) > 0 { | ||
bidderCacheErrs := errsToBidderErrors(cacheErrs) | ||
bidResponseExt.Errors[openrtb_ext.PrebidExtKey] = append(bidResponseExt.Errors[openrtb_ext.PrebidExtKey], bidderCacheErrs...) | ||
} | ||
} | ||
|
||
} | ||
|
||
if !anyBidsReturned { | ||
if debugLog != nil && debugLog.Enabled { | ||
bidResponseExt = e.makeExtBidResponse(adapterBids, adapterExtra, bidRequest, debugInfo, errs) | ||
if debugLog != nil && debugLog.Enabled { | ||
if bidRespExtBytes, err := json.Marshal(bidResponseExt); err == nil { | ||
debugLog.Data.Response = string(bidRespExtBytes) | ||
} else { | ||
debugLog.Data.Response = "Unable to marshal response ext for debugging" | ||
errs = append(errs, err) | ||
} | ||
if !anyBidsReturned { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm still not sure why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm guessing there's an assumption that the CacheKey will set elsewhere if there are bids. There seems to be a lot of code checking if it's length is 0. I'd be afraid to change that assumption in this PR. If you're up for it, a separate PR would be appropriate to fix that concept. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe a non-nil In either case, it'd be better that @camrice or someone from his team takes a look to this refactoring PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The cache key creation can be removed here so only the response so far is added to the |
||
if rawUUID, err := uuid.NewV4(); err == nil { | ||
debugLog.CacheKey = rawUUID.String() | ||
|
||
bidResponseExt = e.makeExtBidResponse(adapterBids, adapterExtra, bidRequest, debugInfo, errs) | ||
if bidRespExtBytes, err := json.Marshal(bidResponseExt); err == nil { | ||
debugLog.Data.Response = string(bidRespExtBytes) | ||
} else { | ||
debugLog.Data.Response = "Unable to marshal response ext for debugging" | ||
} | ||
} else { | ||
errs = append(errs, err) | ||
} | ||
} | ||
} | ||
|
||
// Ensure caching errors are added if the bid response ext has already been created | ||
if len(cacheErrs) > 0 { | ||
bidderCacheErrs := errsToBidderErrors(cacheErrs) | ||
bidResponseExt.Errors[openrtb_ext.PrebidExtKey] = append(bidResponseExt.Errors[openrtb_ext.PrebidExtKey], bidderCacheErrs...) | ||
} | ||
|
||
// Build the response | ||
return e.buildBidResponse(ctx, liveAdapters, adapterBids, bidRequest, adapterExtra, auc, bidResponseExt, cacheInstructions.returnCreative, errs) | ||
} | ||
|
||
func (e *exchange) parseUsersyncIfAmbiguous(bidRequest *openrtb.BidRequest) bool { | ||
usersyncIfAmbiguous := e.UsersyncIfAmbiguous | ||
var geo *openrtb.Geo = nil | ||
|
||
if bidRequest.User != nil && bidRequest.User.Geo != nil { | ||
geo = bidRequest.User.Geo | ||
} else if bidRequest.Device != nil && bidRequest.Device.Geo != nil { | ||
geo = bidRequest.Device.Geo | ||
} | ||
if geo != nil { | ||
// If we have a country set, and it is on the list, we assume GDPR applies if not set on the request. | ||
// Otherwise we assume it does not apply as long as it appears "valid" (is 3 characters long). | ||
if _, found := e.privacyConfig.GDPR.EEACountriesMap[strings.ToUpper(geo.Country)]; found { | ||
usersyncIfAmbiguous = false | ||
} else if len(geo.Country) == 3 { | ||
// The country field is formatted properly as a three character country code | ||
usersyncIfAmbiguous = true | ||
} | ||
} | ||
|
||
return usersyncIfAmbiguous | ||
} | ||
|
||
func (e *exchange) recordImpMetrics(bidRequest *openrtb.BidRequest) { | ||
SyntaxNode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
SyntaxNode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for _, impInRequest := range bidRequest.Imp { | ||
var impLabels pbsmetrics.ImpLabels = pbsmetrics.ImpLabels{ | ||
BannerImps: impInRequest.Banner != nil, | ||
VideoImps: impInRequest.Video != nil, | ||
AudioImps: impInRequest.Audio != nil, | ||
NativeImps: impInRequest.Native != nil, | ||
} | ||
e.me.RecordImps(impLabels) | ||
} | ||
|
||
} | ||
|
||
type DealTierInfo struct { | ||
Prefix string `json:"prefix"` | ||
MinDealTier int `json:"minDealTier"` | ||
|
@@ -479,6 +484,7 @@ func errsToBidderErrors(errs []error) []openrtb_ext.ExtBidderError { | |
// This piece takes all the bids supplied by the adapters and crafts an openRTB response to send back to the requester | ||
func (e *exchange) buildBidResponse(ctx context.Context, liveAdapters []openrtb_ext.BidderName, adapterBids map[openrtb_ext.BidderName]*pbsOrtbSeatBid, bidRequest *openrtb.BidRequest, adapterExtra map[openrtb_ext.BidderName]*seatResponseExtra, auc *auction, bidResponseExt *openrtb_ext.ExtBidResponse, returnCreative bool, errList []error) (*openrtb.BidResponse, error) { | ||
bidResponse := new(openrtb.BidResponse) | ||
var err error | ||
|
||
bidResponse.ID = bidRequest.ID | ||
if len(liveAdapters) == 0 { | ||
|
@@ -500,21 +506,19 @@ func (e *exchange) buildBidResponse(ctx context.Context, liveAdapters []openrtb_ | |
|
||
bidResponse.SeatBid = seatBids | ||
|
||
if bidResponseExt == nil { | ||
contextDebugValue := ctx.Value(DebugContextKey) | ||
SyntaxNode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var debugInfo bool | ||
if contextDebugValue != nil { | ||
debugInfo = contextDebugValue.(bool) | ||
} | ||
bidResponseExt = e.makeExtBidResponse(adapterBids, adapterExtra, bidRequest, debugInfo, errList) | ||
} | ||
bidResponse.Ext, err = encodeBidResponseExt(bidResponseExt) | ||
|
||
return bidResponse, err | ||
} | ||
|
||
func encodeBidResponseExt(bidResponseExt *openrtb_ext.ExtBidResponse) ([]byte, error) { | ||
buffer := &bytes.Buffer{} | ||
enc := json.NewEncoder(buffer) | ||
|
||
enc.SetEscapeHTML(false) | ||
err := enc.Encode(bidResponseExt) | ||
bidResponse.Ext = buffer.Bytes() | ||
|
||
return bidResponse, err | ||
return buffer.Bytes(), err | ||
} | ||
|
||
func applyCategoryMapping(ctx context.Context, requestExt *openrtb_ext.ExtRequest, seatBids map[openrtb_ext.BidderName]*pbsOrtbSeatBid, categoriesFetcher stored_requests.CategoryFetcher, targData *targetData) (map[string]string, map[openrtb_ext.BidderName]*pbsOrtbSeatBid, []string, error) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.