Skip to content

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

Merged
merged 6 commits into from
Oct 15, 2020

Conversation

guscarreon
Copy link
Contributor

According to the current logic in exchange/exchange.go, function makeExtBidResponse(adapterBids, adapterExtra, bidRequest, debugInfo, errs) gets called under every scenario either from HoldAuction() or from buildBidResponse(). This opens the possibility of slightly simplifying these couple of functions. Please let me know what you think of this small refactoring PR.

debugLog.Data.Response = "Unable to marshal response ext for debugging"
errs = append(errs, err)
}
if !anyBidsReturned {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not sure why debugLog.CacheKey = rawUUID.String() only happens when !anyBidsReturned but I refactored to keep the exact same logic (please let me know if I you think I missed something).

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe a non-nil debugLog object can only come from the video_auction.go endpoint and also see that when an error occurs in the video endpoint, a rawUUID, err := uuid.NewV4(); also gets generated just before exiting execution. My guess is that only when an error happens, or no bids are returned, the UUID to a debug log is returned.

In either case, it'd be better that @camrice or someone from his team takes a look to this refactoring PR.

Copy link
Contributor

Choose a reason for hiding this comment

The 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 debugLog. Before, I believe I didn't have the cache key created in PutDebugLogError or handleError, so this case would be missed.

debugLog.Data.Response = "Unable to marshal response ext for debugging"
errs = append(errs, err)
}
if !anyBidsReturned {
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Comment on lines 195 to 199
// Ensure caching errors are added in case auc.doCache was called and errors were returned
if len(cacheErrs) > 0 {
bidderCacheErrs := errsToBidderErrors(cacheErrs)
bidResponseExt.Errors[openrtb_ext.PrebidExtKey] = append(bidResponseExt.Errors[openrtb_ext.PrebidExtKey], bidderCacheErrs...)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this block be between lines 178 and 179 where we call makeExtBidResponse and marshal the ext if the debugging is enabled? In that case a cache error would be in the marshaled ext.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you have a point. The current version of the code places this block after we make and marshal the bidResponseExt, therefore, I respected that order. But it'd probably be a good idea to reconsider this in its own PR.

 97 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) {
 98
 99 *-- 92 lines: requestExt, err := extractBidRequestExt(bidRequest)---------------------------------------------------------------------------
191
192             if debugLog != nil && debugLog.Enabled {
193                 bidResponseExt = e.makeExtBidResponse(adapterBids, adapterExtra, bidRequest, debugInfo, errs)
194                 if bidRespExtBytes, err := json.Marshal(bidResponseExt); err == nil {
195                     debugLog.Data.Response = string(bidRespExtBytes)
196                 } else {
197                     debugLog.Data.Response = "Unable to marshal response ext for debugging"
198                     errs = append(errs, errors.New(debugLog.Data.Response))
199                 }
200             }
201
202             cacheErrs := auc.doCache(ctx, e.cache, targData, bidRequest, 60, &account.CacheTTL, bidCategory, debugLog)
203             if len(cacheErrs) > 0 {
204                 errs = append(errs, cacheErrs...)
205             }
206             targData.setTargeting(auc, bidRequest.App != nil, bidCategory)
207
208             // Ensure caching errors are added if the bid response ext has already been created
209             if bidResponseExt != nil && len(cacheErrs) > 0 {
210                 bidderCacheErrs := errsToBidderErrors(cacheErrs)
211                 bidResponseExt.Errors[openrtb_ext.PrebidExtKey] = append(bidResponseExt.Errors[openrtb_ext.PrebidExtKey], bidderCacheErrs...)
212             }
213         }
214
215     }
216
217 *-- 17 lines: if !anyBidsReturned {---------------------------------------------------------------------------------------------------------
234     // Build the response
235     return e.buildBidResponse(ctx, liveAdapters, adapterBids, bidRequest, adapterExtra, auc, bidResponseExt, cacheInstructions.returnCreative, errs)
236 }
exchange/exchange.go

What do you think?

Copy link
Collaborator

@bsardo bsardo Oct 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A separate PR would be fine but I don't see the change I'm suggesting.

I figured it might be best for the following steps to happen in this order:

  1. Make the ext bid response:
    bidResponseExt = e.makeExtBidResponse(adapterBids, adapterExtra, bidRequest, debugInfo, errs)
  2. Set any cache errors on the bid response:
    bidResponseExt.Errors[openrtb_ext.PrebidExtKey] = append(bidResponseExt.Errors[openrtb_ext.PrebidExtKey], bidderCacheErrs...)
  3. Marshal the bid response and then store it in the debug log data response:
    if bidRespExtBytes, err := json.Marshal(bidResponseExt); err == nil { debugLog.Data.Response = string(bidRespExtBytes)

This way any cache errors that are supposed to be added to the bid response are added to the debug log data response.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected. Pretty good catch

bsardo
bsardo previously approved these changes Oct 15, 2020
@hhhjort hhhjort merged commit 27ec65d into prebid:master Oct 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants