Skip to content

Commit

Permalink
OTT-1168: Introduce SeatNonBid in hookoutcome (#pbs-3416) (#676)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishshinde-pubm authored May 17, 2024
1 parent eed034c commit d812366
Show file tree
Hide file tree
Showing 41 changed files with 2,533 additions and 1,254 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ analytics/filesystem/testFiles/
*.swp
*.swo
pbsimage
manual_build
manual_build
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ all: deps test build-modules build
# deps will clean out the vendor directory and use go mod for a fresh install
deps:
GOPROXY="https://proxy.golang.org" go mod vendor -v && go mod tidy -v

# test will ensure that all of our dependencies are available and run validate.sh
test: deps
# If there is no indentation, Make will treat it as a directive for itself; otherwise, it's regarded as a shell script.
Expand Down Expand Up @@ -37,10 +37,10 @@ format:
# formatcheck runs format for diagnostics, without modifying the code
formatcheck:
./scripts/format.sh -f false

mockgen: mockgeninstall mockgendb mockgencache mockgenmetrics mockgenlogger mockgenpublisherfeature

# export GOPATH=~/go ; GOBIN=~/go/bin; export PATH=$PATH:$GOBIN
# export GOPATH=~/go ; GOBIN=~/go/bin; export PATH=$PATH:$GOBIN
mockgeninstall:
go install github.com/golang/mock/[email protected]

Expand Down
86 changes: 45 additions & 41 deletions analytics/pubmatic/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/openrtb/v20/openrtb3"
"github.com/prebid/prebid-server/v2/analytics"
"github.com/prebid/prebid-server/v2/exchange"
"github.com/prebid/prebid-server/v2/hooks/hookanalytics"
Expand Down Expand Up @@ -114,9 +115,9 @@ func TestConvertNonBidToBid(t *testing.T) {
nonBid: openrtb_ext.NonBid{
StatusCode: int(exchange.ResponseRejectedBelowFloor),
ImpId: "imp1",
Ext: openrtb_ext.NonBidExt{
Prebid: openrtb_ext.ExtResponseNonBidPrebid{
Bid: openrtb_ext.NonBidObject{
Ext: openrtb_ext.ExtNonBid{
Prebid: openrtb_ext.ExtNonBidPrebid{
Bid: openrtb_ext.ExtNonBidPrebidBid{
Price: 10,
ADomain: []string{"abc.com"},
DealID: "d1",
Expand Down Expand Up @@ -1059,9 +1060,9 @@ func TestGetPartnerRecordsByImpForDefaultBids(t *testing.T) {
{
ImpId: "imp1",
StatusCode: int(exchange.ResponseRejectedBelowFloor),
Ext: openrtb_ext.NonBidExt{
Prebid: openrtb_ext.ExtResponseNonBidPrebid{
Bid: openrtb_ext.NonBidObject{
Ext: openrtb_ext.ExtNonBid{
Prebid: openrtb_ext.ExtNonBidPrebid{
Bid: openrtb_ext.ExtNonBidPrebidBid{
ID: "bid-id-2",
},
},
Expand Down Expand Up @@ -1141,9 +1142,9 @@ func TestGetPartnerRecordsByImpForDefaultBids(t *testing.T) {
{
ImpId: "imp1",
StatusCode: int(exchange.ResponseRejectedBelowFloor),
Ext: openrtb_ext.NonBidExt{
Prebid: openrtb_ext.ExtResponseNonBidPrebid{
Bid: openrtb_ext.NonBidObject{
Ext: openrtb_ext.ExtNonBid{
Prebid: openrtb_ext.ExtNonBidPrebid{
Bid: openrtb_ext.ExtNonBidPrebidBid{
ID: "bid-id-1",
},
},
Expand Down Expand Up @@ -1313,9 +1314,9 @@ func TestGetPartnerRecordsByImpForSeatNonBid(t *testing.T) {
{
ImpId: "imp1",
StatusCode: int(exchange.ResponseRejectedBelowDealFloor),
Ext: openrtb_ext.NonBidExt{
Prebid: openrtb_ext.ExtResponseNonBidPrebid{
Bid: openrtb_ext.NonBidObject{
Ext: openrtb_ext.ExtNonBid{
Prebid: openrtb_ext.ExtNonBidPrebid{
Bid: openrtb_ext.ExtNonBidPrebidBid{
Price: 10,
},
},
Expand Down Expand Up @@ -1343,9 +1344,9 @@ func TestGetPartnerRecordsByImpForSeatNonBid(t *testing.T) {
{
ImpId: "imp1",
StatusCode: int(exchange.ResponseRejectedBelowFloor),
Ext: openrtb_ext.NonBidExt{
Prebid: openrtb_ext.ExtResponseNonBidPrebid{
Bid: openrtb_ext.NonBidObject{
Ext: openrtb_ext.ExtNonBid{
Prebid: openrtb_ext.ExtNonBidPrebid{
Bid: openrtb_ext.ExtNonBidPrebidBid{
Price: 10,
ID: "bid-id-1",
W: 10,
Expand Down Expand Up @@ -1444,9 +1445,9 @@ func TestGetPartnerRecordsByImpForSeatNonBidForFloors(t *testing.T) {
{
ImpId: "imp1",
StatusCode: int(exchange.ResponseRejectedBelowFloor),
Ext: openrtb_ext.NonBidExt{
Prebid: openrtb_ext.ExtResponseNonBidPrebid{
Bid: openrtb_ext.NonBidObject{
Ext: openrtb_ext.ExtNonBid{
Prebid: openrtb_ext.ExtNonBidPrebid{
Bid: openrtb_ext.ExtNonBidPrebidBid{
Price: 10,
ID: "bid-id-1",
Floors: &openrtb_ext.ExtBidPrebidFloors{
Expand Down Expand Up @@ -1509,10 +1510,10 @@ func TestGetPartnerRecordsByImpForSeatNonBidForFloors(t *testing.T) {
NonBid: []openrtb_ext.NonBid{
{
ImpId: "imp1",
StatusCode: int((exchange.ResponseRejectedBelowFloor)),
Ext: openrtb_ext.NonBidExt{
Prebid: openrtb_ext.ExtResponseNonBidPrebid{
Bid: openrtb_ext.NonBidObject{
StatusCode: int(exchange.ResponseRejectedBelowFloor),
Ext: openrtb_ext.ExtNonBid{
Prebid: openrtb_ext.ExtNonBidPrebid{
Bid: openrtb_ext.ExtNonBidPrebidBid{
Price: 10,
ID: "bid-id-1",
Floors: &openrtb_ext.ExtBidPrebidFloors{
Expand Down Expand Up @@ -1575,10 +1576,10 @@ func TestGetPartnerRecordsByImpForSeatNonBidForFloors(t *testing.T) {
NonBid: []openrtb_ext.NonBid{
{
ImpId: "imp1",
StatusCode: int((exchange.ResponseRejectedBelowFloor)),
Ext: openrtb_ext.NonBidExt{
Prebid: openrtb_ext.ExtResponseNonBidPrebid{
Bid: openrtb_ext.NonBidObject{
StatusCode: int(exchange.ResponseRejectedBelowFloor),
Ext: openrtb_ext.ExtNonBid{
Prebid: openrtb_ext.ExtNonBidPrebid{
Bid: openrtb_ext.ExtNonBidPrebidBid{
Price: 10,
ID: "bid-id-1",
Floors: &openrtb_ext.ExtBidPrebidFloors{
Expand Down Expand Up @@ -1642,9 +1643,9 @@ func TestGetPartnerRecordsByImpForSeatNonBidForFloors(t *testing.T) {
{
ImpId: "imp1",
StatusCode: int(exchange.ResponseRejectedBelowFloor),
Ext: openrtb_ext.NonBidExt{
Prebid: openrtb_ext.ExtResponseNonBidPrebid{
Bid: openrtb_ext.NonBidObject{
Ext: openrtb_ext.ExtNonBid{
Prebid: openrtb_ext.ExtNonBidPrebid{
Bid: openrtb_ext.ExtNonBidPrebidBid{
Price: 10,
ID: "bid-id-1",
},
Expand Down Expand Up @@ -1702,9 +1703,9 @@ func TestGetPartnerRecordsByImpForSeatNonBidForFloors(t *testing.T) {
{
ImpId: "imp1",
StatusCode: int(exchange.ResponseRejectedBelowFloor),
Ext: openrtb_ext.NonBidExt{
Prebid: openrtb_ext.ExtResponseNonBidPrebid{
Bid: openrtb_ext.NonBidObject{
Ext: openrtb_ext.ExtNonBid{
Prebid: openrtb_ext.ExtNonBidPrebid{
Bid: openrtb_ext.ExtNonBidPrebidBid{
Price: 10,
ID: "bid-id-1",
},
Expand Down Expand Up @@ -1765,9 +1766,9 @@ func TestGetPartnerRecordsByImpForSeatNonBidForFloors(t *testing.T) {
{
ImpId: "imp1",
StatusCode: int(exchange.ResponseRejectedBelowFloor),
Ext: openrtb_ext.NonBidExt{
Prebid: openrtb_ext.ExtResponseNonBidPrebid{
Bid: openrtb_ext.NonBidObject{
Ext: openrtb_ext.ExtNonBid{
Prebid: openrtb_ext.ExtNonBidPrebid{
Bid: openrtb_ext.ExtNonBidPrebidBid{
Price: 10,
ID: "bid-id-1",
Floors: &openrtb_ext.ExtBidPrebidFloors{
Expand Down Expand Up @@ -2186,9 +2187,9 @@ func TestGetPartnerRecordsByImpForBidIDCollisions(t *testing.T) {
{
ImpId: "imp1",
StatusCode: int(nbr.LossBidLostToDealBid),
Ext: openrtb_ext.NonBidExt{
Prebid: openrtb_ext.ExtResponseNonBidPrebid{
Bid: openrtb_ext.NonBidObject{
Ext: openrtb_ext.ExtNonBid{
Prebid: openrtb_ext.ExtNonBidPrebid{
Bid: openrtb_ext.ExtNonBidPrebidBid{
Price: 10,
ID: "bid-id-1",
BidId: "uuid",
Expand Down Expand Up @@ -2485,8 +2486,8 @@ func TestGetPartnerRecordsByImpForBidExtFailure(t *testing.T) {
NonBid: []openrtb_ext.NonBid{
{
ImpId: "imp1",
StatusCode: int(nbr.LossBidLostToDealBid),
Ext: openrtb_ext.NonBidExt{},
StatusCode: int(exchange.ResponseRejectedBelowDealFloor),
Ext: openrtb_ext.ExtNonBid{},
},
},
},
Expand All @@ -2511,7 +2512,10 @@ func TestGetPartnerRecordsByImpForBidExtFailure(t *testing.T) {
NetECPM: 0,
GrossECPM: 0,
OriginalCur: models.USD,
Nbr: nbr.LossBidLostToDealBid.Ptr(),
Nbr: func() *openrtb3.NoBidReason {
a := exchange.ResponseRejectedBelowDealFloor
return &a
}(),
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion analytics/pubstack/pubstack_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestNewModuleSuccess(t *testing.T) {
{
ImpId: "123",
StatusCode: 34,
Ext: openrtb_ext.NonBidExt{Prebid: openrtb_ext.ExtResponseNonBidPrebid{Bid: openrtb_ext.NonBidObject{}}},
Ext: openrtb_ext.ExtNonBid{Prebid: openrtb_ext.ExtNonBidPrebid{Bid: openrtb_ext.ExtNonBidPrebidBid{}}},
},
},
},
Expand Down
60 changes: 31 additions & 29 deletions endpoints/openrtb2/amp_auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func (deps *endpointDeps) AmpAuction(w http.ResponseWriter, r *http.Request, _ h
// We can respect timeouts more accurately if we note the *real* start time, and use it
// to compute the auction timeout.
start := time.Now()
seatNonBid := &openrtb_ext.NonBidCollection{}

hookExecutor := hookexecution.NewHookExecutor(deps.hookExecutionPlanBuilder, hookexecution.EndpointAmp, deps.metricsEngine)

Expand All @@ -137,6 +138,12 @@ func (deps *endpointDeps) AmpAuction(w http.ResponseWriter, r *http.Request, _ h
activityControl := privacy.ActivityControl{}

defer func() {
// if AmpObject.AuctionResponse is nil then collect nonbids from all stage outcomes and set it in the AmpObject.SeatNonBid
// Nil AmpObject.AuctionResponse indicates the occurrence of a fatal error.
if ao.AuctionResponse == nil {
seatNonBid.Append(getNonBidsFromStageOutcomes(hookExecutor.GetOutcomes()))
ao.SeatNonBid = seatNonBid.Get()
}
deps.metricsEngine.RecordRequest(labels)
deps.metricsEngine.RecordRequestTime(labels, time.Since(start))
deps.analytics.LogAmpObject(&ao, activityControl)
Expand All @@ -163,7 +170,7 @@ func (deps *endpointDeps) AmpAuction(w http.ResponseWriter, r *http.Request, _ h
// Process reject after parsing amp request, so we can use reqWrapper.
// There is no body for AMP requests, so we pass a nil body and ignore the return value.
if rejectErr != nil {
labels, ao = rejectAmpRequest(*rejectErr, w, hookExecutor, reqWrapper, nil, labels, ao, nil)
labels, ao = rejectAmpRequest(*rejectErr, w, hookExecutor, reqWrapper, nil, labels, ao, nil, *seatNonBid)
return
}

Expand Down Expand Up @@ -279,8 +286,8 @@ func (deps *endpointDeps) AmpAuction(w http.ResponseWriter, r *http.Request, _ h
var response *openrtb2.BidResponse
if auctionResponse != nil {
response = auctionResponse.BidResponse
seatNonBid.Append(auctionResponse.SeatNonBid)
}
ao.SeatNonBid = auctionResponse.GetSeatNonBid()
ao.AuctionResponse = response
rejectErr, isRejectErr := hookexecution.CastRejectErr(err)
if err != nil && !isRejectErr {
Expand All @@ -300,15 +307,21 @@ func (deps *endpointDeps) AmpAuction(w http.ResponseWriter, r *http.Request, _ h
glog.Errorf("/openrtb2/amp Critical error: %v", err)
ao.Status = http.StatusInternalServerError
ao.Errors = append(ao.Errors, err)
if ao.AuctionResponse != nil {
// this check ensures that we collect nonBids from stageOutcomes only once.
// there could be a case where ao.AuctionResponse nil and reqWrapper.RebuildRequest returns error
seatNonBid.Append(getNonBidsFromStageOutcomes(hookExecutor.GetOutcomes()))
ao.SeatNonBid = seatNonBid.Get()
}
return
}

if isRejectErr {
labels, ao = rejectAmpRequest(*rejectErr, w, hookExecutor, reqWrapper, account, labels, ao, errL)
labels, ao = rejectAmpRequest(*rejectErr, w, hookExecutor, reqWrapper, account, labels, ao, errL, *seatNonBid)
return
}

labels, ao = sendAmpResponse(w, hookExecutor, auctionResponse, reqWrapper, account, labels, ao, errL)
labels, ao = sendAmpResponse(w, hookExecutor, auctionResponse, reqWrapper, account, labels, ao, errL, *seatNonBid)
}

func rejectAmpRequest(
Expand All @@ -320,12 +333,13 @@ func rejectAmpRequest(
labels metrics.Labels,
ao analytics.AmpObject,
errs []error,
seatNonBid openrtb_ext.NonBidCollection,
) (metrics.Labels, analytics.AmpObject) {
response := &openrtb2.BidResponse{NBR: openrtb3.NoBidReason(rejectErr.NBR).Ptr()}
ao.AuctionResponse = response
ao.Errors = append(ao.Errors, rejectErr)

return sendAmpResponse(w, hookExecutor, &exchange.AuctionResponse{BidResponse: response}, reqWrapper, account, labels, ao, errs)
return sendAmpResponse(w, hookExecutor, &exchange.AuctionResponse{BidResponse: response}, reqWrapper, account, labels, ao, errs, seatNonBid)
}

func sendAmpResponse(
Expand All @@ -337,6 +351,7 @@ func sendAmpResponse(
labels metrics.Labels,
ao analytics.AmpObject,
errs []error,
seatNonBid openrtb_ext.NonBidCollection,
) (metrics.Labels, analytics.AmpObject) {
var response *openrtb2.BidResponse
if auctionResponse != nil {
Expand Down Expand Up @@ -364,6 +379,8 @@ func sendAmpResponse(
glog.Errorf("/openrtb2/amp Critical error unpacking targets: %v", err)
ao.Errors = append(ao.Errors, fmt.Errorf("Critical error while unpacking AMP targets: %v", err))
ao.Status = http.StatusInternalServerError
seatNonBid.Append(getNonBidsFromStageOutcomes(hookExecutor.GetOutcomes()))
ao.SeatNonBid = seatNonBid.Get()
return labels, ao
}
for key, value := range bidExt.Prebid.Targeting {
Expand Down Expand Up @@ -392,7 +409,7 @@ func sendAmpResponse(
}
// Now JSONify the targets for the AMP response.
ampResponse := AmpResponse{Targeting: targets}
ao, ampResponse.ORTB2.Ext = getExtBidResponse(hookExecutor, auctionResponse, reqWrapper, account, ao, errs)
ao, ampResponse.ORTB2.Ext = getExtBidResponse(hookExecutor, auctionResponse, reqWrapper, account, ao, errs, seatNonBid)

ao.AmpTargetingValues = targets

Expand All @@ -418,6 +435,7 @@ func getExtBidResponse(
account *config.Account,
ao analytics.AmpObject,
errs []error,
seatNonBid openrtb_ext.NonBidCollection,
) (analytics.AmpObject, openrtb_ext.ExtBidResponse) {
var response *openrtb2.BidResponse
if auctionResponse != nil {
Expand Down Expand Up @@ -447,6 +465,7 @@ func getExtBidResponse(
Warnings: warnings,
}

stageOutcomes := hookExecutor.GetOutcomes()
// add debug information if requested
if reqWrapper != nil {
if reqWrapper.Test == 1 && eRErr == nil {
Expand All @@ -458,7 +477,6 @@ func getExtBidResponse(
}
}

stageOutcomes := hookExecutor.GetOutcomes()
ao.HookExecutionOutcome = stageOutcomes
modules, warns, err := hookexecution.GetModulesJSON(stageOutcomes, reqWrapper.BidRequest, account)
if err != nil {
Expand All @@ -474,8 +492,12 @@ func getExtBidResponse(
}
}

setSeatNonBid(&extBidResponse, reqWrapper, auctionResponse)

// collect seatNonBid from all stage-outcomes and set in the response.ext.prebid
seatNonBid.Append(getNonBidsFromStageOutcomes(stageOutcomes))
ao.SeatNonBid = seatNonBid.Get()
if returnAllBidStatus(reqWrapper) {
setSeatNonBid(&extBidResponse, ao.SeatNonBid)
}
return ao, extBidResponse
}

Expand Down Expand Up @@ -851,23 +873,3 @@ func setTrace(req *openrtb2.BidRequest, value string) error {

return nil
}

// setSeatNonBid populates bidresponse.ext.prebid.seatnonbid if bidrequest.ext.prebid.returnallbidstatus is true
func setSeatNonBid(finalExtBidResponse *openrtb_ext.ExtBidResponse, request *openrtb_ext.RequestWrapper, auctionResponse *exchange.AuctionResponse) bool {
if finalExtBidResponse == nil || auctionResponse == nil || request == nil {
return false
}
reqExt, err := request.GetRequestExt()
if err != nil {
return false
}
prebid := reqExt.GetPrebid()
if prebid == nil || !prebid.ReturnAllBidStatus {
return false
}
if finalExtBidResponse.Prebid == nil {
finalExtBidResponse.Prebid = &openrtb_ext.ExtResponsePrebid{}
}
finalExtBidResponse.Prebid.SeatNonBid = auctionResponse.GetSeatNonBid()
return true
}
Loading

0 comments on commit d812366

Please sign in to comment.