Skip to content

Commit

Permalink
support msp ab (#6)
Browse files Browse the repository at this point in the history
* support msp ab

* rename ab buckets name
  • Loading branch information
shunj-nb authored Apr 14, 2023
1 parent 402c775 commit 2290db8
Show file tree
Hide file tree
Showing 22 changed files with 256 additions and 55 deletions.
7 changes: 6 additions & 1 deletion endpoints/openrtb2/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (deps *endpointDeps) Auction(w http.ResponseWriter, r *http.Request, _ http
return
}

mspUpdateByImpInfo(&labels, mspLogInfo, mspExtOutput.StoredImp)
mspUpdateLogLabelsByExtOutput(&labels, mspLogInfo, mspExtOutput)
mspUpdateLogMsgByReq(mspLogInfo, req)

if rejectErr := hookexecution.FindFirstRejectOrNil(errL); rejectErr != nil {
Expand Down Expand Up @@ -235,6 +235,7 @@ func (deps *endpointDeps) Auction(w http.ResponseWriter, r *http.Request, _ http
PubID: labels.PubID,
HookExecutor: deps.hookExecutor,
MspStoredImp: mspExtOutput.StoredImp,
MspAbList: mspExtOutput.MspLayerAbBuckets,
}
response, err := deps.ex.HoldAuction(ctx, auctionRequest, nil)
ao.Request = req.BidRequest
Expand Down Expand Up @@ -435,6 +436,10 @@ func (deps *endpointDeps) parseRequest(httpRequest *http.Request, labels *metric
return
}

if requestJson, errs = mspExtOutput.processStoredAb(requestJson, deps.storedReqFetcher, ctx); len(errs) > 0 {
return
}

//Stored auction responses should be processed after stored requests due to possible impression modification
storedAuctionResponses, storedBidResponses, bidderImpReplaceImpId, errs = stored_responses.ProcessStoredResponses(ctx, requestJson, deps.storedRespFetcher, deps.bidderMap)
if len(errs) > 0 {
Expand Down
4 changes: 4 additions & 0 deletions endpoints/openrtb2/auction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5293,6 +5293,10 @@ func (cf *mockStoredResponseFetcher) FetchRequests(ctx context.Context, requestI
return nil, nil, nil
}

func (cf *mockStoredResponseFetcher) FetchABs(ctx context.Context, bucketList []string) (bucketData map[string]json.RawMessage, errs []error) {
return nil, nil
}

func (cf *mockStoredResponseFetcher) FetchResponses(ctx context.Context, ids []string) (data map[string]json.RawMessage, errs []error) {
return cf.data, nil
}
Expand Down
56 changes: 52 additions & 4 deletions endpoints/openrtb2/msp_ext.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
package openrtb2

import (
"context"
"encoding/json"
"fmt"

"github.com/prebid/openrtb/v17/openrtb2"
"github.com/prebid/prebid-server/metrics"
"github.com/prebid/prebid-server/msp/ab"
"github.com/prebid/prebid-server/openrtb_ext"
"github.com/prebid/prebid-server/stored_requests"
u "github.com/rjNemo/underscore"
jsonpatch "gopkg.in/evanphx/json-patch.v4"
)

const Unknown = "unknown"

// This struct holds the extra output parsed out from request and needed by MSP
type mspExtOutput struct {
StoredImp string
// ab buckets configured at ads layer
AdsLayerAbBuckets []string
// ab buckets configured at msp layer(server side)
MspLayerAbBuckets []string
}

func newMspExtOutput() *mspExtOutput {
Expand All @@ -32,6 +40,41 @@ func (m *mspExtOutput) extractStoredImp(impInfo []ImpExtPrebidData) {
}
}

func (m *mspExtOutput) processStoredAb(rawReq []byte, storedReqFetcher stored_requests.Fetcher, ctx context.Context) ([]byte, []error) {
resolvedReq := rawReq
req := &openrtb2.BidRequest{}
if err := json.Unmarshal(rawReq, req); err != nil {
return nil, []error{err}
}

if ab.Init && req.Device != nil && req.App != nil {
bucketList, err := ab.GetMspAB().GetBucket(req.User.ID, req.Device.OS, req.App.Ver)
if err != nil {
return nil, []error{err}
}

m.AdsLayerAbBuckets = bucketList
bucketList = append(bucketList, "all")
storedABs, _ := storedReqFetcher.FetchABs(ctx, bucketList)

for _, bucketName := range bucketList {
// only use configed experiments on server
val, ok := storedABs[bucketName]
if ok {
if len(m.MspLayerAbBuckets) > 0 && bucketName == "all" {
continue
}
resolvedReq, err = jsonpatch.MergePatch(resolvedReq, val)
m.MspLayerAbBuckets = append(m.MspLayerAbBuckets, bucketName)
if err != nil {
return nil, []error{err}
}
}
}
}
return resolvedReq, nil
}

type mspLogInfo struct {
Os string
Idfa string
Expand All @@ -50,6 +93,7 @@ type mspLogInfo struct {
HasBuyerId bool
Seat string
Adomain []string
AbBuckets []string
}

func newMSPLogInfo() *mspLogInfo {
Expand All @@ -71,16 +115,20 @@ func newMSPLogInfo() *mspLogInfo {
HasBuyerId: false,
Seat: Unknown,
Adomain: []string{Unknown},
AbBuckets: []string{Unknown},
}
}

func mspUpdateByImpInfo(labels *metrics.Labels, logInfo *mspLogInfo, storedImp string) {
if storedImp != "" {
logInfo.AdUnit = storedImp
labels.MSPStoredImp = storedImp
func mspUpdateLogLabelsByExtOutput(labels *metrics.Labels, logInfo *mspLogInfo, extOut *mspExtOutput) {
if extOut.StoredImp != "" {
logInfo.AdUnit = extOut.StoredImp
labels.MSPStoredImp = extOut.StoredImp
} else {
labels.MSPStoredImp = Unknown
}

logInfo.AbBuckets = extOut.AdsLayerAbBuckets
labels.MspAbBuckets = extOut.MspLayerAbBuckets
}

func mspUpdateLogMsgByResponse(logInfo *mspLogInfo, response *openrtb2.BidResponse) {
Expand Down
12 changes: 12 additions & 0 deletions endpoints/openrtb2/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,10 @@ func (cf mockStoredReqFetcher) FetchResponses(ctx context.Context, ids []string)
return nil, nil
}

func (cf *mockStoredReqFetcher) FetchABs(ctx context.Context, bucketList []string) (bucketData map[string]json.RawMessage, errs []error) {
return nil, nil
}

// mockExchange implements the Exchange interface
type mockExchange struct {
lastRequest *openrtb2.BidRequest
Expand Down Expand Up @@ -1360,6 +1364,14 @@ func (cf *mockAmpStoredReqFetcher) FetchResponses(ctx context.Context, ids []str
return nil, nil
}

func (cf *mockAmpStoredReqFetcher) FetchABs(ctx context.Context, bucketList []string) (bucketData map[string]json.RawMessage, errs []error) {
return nil, nil
}

func (cf *mockAmpStoredResponseFetcher) FetchABs(ctx context.Context, bucketList []string) (bucketData map[string]json.RawMessage, errs []error) {
return nil, nil
}

type mockAmpStoredResponseFetcher struct {
data map[string]json.RawMessage
}
Expand Down
4 changes: 4 additions & 0 deletions endpoints/openrtb2/video_auction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,10 @@ func (cf mockVideoStoredReqFetcher) FetchResponses(ctx context.Context, ids []st
return nil, nil
}

func (cf mockVideoStoredReqFetcher) FetchABs(ctx context.Context, bucketList []string) (data map[string]json.RawMessage, errs []error) {
return nil, nil
}

type mockExchangeVideo struct {
lastRequest *openrtb2.BidRequest
cache *mockCacheClient
Expand Down
2 changes: 2 additions & 0 deletions exchange/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ type AuctionRequest struct {

// msp stored Imp
MspStoredImp string
// msp configed abs
MspAbList []string
}

// BidderRequest holds the bidder specific request and all other
Expand Down
2 changes: 2 additions & 0 deletions exchange/msp_exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
func addMspInfoToBidderRequest(bidderRequest []BidderRequest, r AuctionRequest) []BidderRequest {
return u.Map(bidderRequest, func(req BidderRequest) BidderRequest {
req.BidderLabels.MSPStoredImp = r.MspStoredImp
req.BidderLabels.MspAbBuckets = r.MspAbList
return req
})
}
Expand All @@ -19,6 +20,7 @@ func (e *exchange) recordAdapterWinningInfo(auc *auction, r AuctionRequest) {
topBidderLabel := metrics.AdapterLabels{
Adapter: bidderName,
MSPStoredImp: r.MspStoredImp,
MspAbBuckets: r.MspAbList,
}

if len(topBidPerBidder) > 0 {
Expand Down
2 changes: 2 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Labels struct {

// Following Fields are for MSP only.
MSPStoredImp string
MspAbBuckets []string
}

// AdapterLabels defines the labels that can be attached to the adapter metrics.
Expand All @@ -30,6 +31,7 @@ type AdapterLabels struct {

// Following Fields are for MSP only.
MSPStoredImp string
MspAbBuckets []string
}

// ImpLabels defines metric labels describing the impression type.
Expand Down
26 changes: 17 additions & 9 deletions metrics/prometheus/msp_prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
const (
mspStoredImpUnknown = "unknown"
mspStoredImpLabel = "stored_imp"
mspAbBucketUnknown = "unknown"
mspAbLabel = "ab_bucket"
)

func (m *Metrics) RecordAdapterWinningBidReceived(labels metrics.AdapterLabels, bidType openrtb_ext.BidType, hasAdm bool) {
Expand All @@ -17,16 +19,22 @@ func (m *Metrics) RecordAdapterWinningBidReceived(labels metrics.AdapterLabels,
markupDelivery = markupDeliveryAdm
}

m.mspAdapterWinningBids.With(prometheus.Labels{
adapterLabel: string(labels.Adapter),
markupDeliveryLabel: markupDelivery,
mspStoredImpLabel: labels.MSPStoredImp,
}).Inc()
for _, bucket := range labels.MspAbBuckets {
m.mspAdapterWinningBids.With(prometheus.Labels{
adapterLabel: string(labels.Adapter),
markupDeliveryLabel: markupDelivery,
mspStoredImpLabel: labels.MSPStoredImp,
mspAbLabel: bucket,
}).Inc()
}
}

func (m *Metrics) RecordAdapterWinningPrice(labels metrics.AdapterLabels, cpm float64) {
m.mspAdapterWinningPrices.With(prometheus.Labels{
adapterLabel: string(labels.Adapter),
mspStoredImpLabel: labels.MSPStoredImp,
}).Observe(cpm)
for _, bucket := range labels.MspAbBuckets {
m.mspAdapterWinningPrices.With(prometheus.Labels{
adapterLabel: string(labels.Adapter),
mspStoredImpLabel: labels.MSPStoredImp,
mspAbLabel: bucket,
}).Observe(cpm)
}
}
8 changes: 8 additions & 0 deletions metrics/prometheus/preload.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func preloadLabelValues(m *Metrics, syncerKeys []string, moduleStageNames map[st
syncerSetsStatusValues = syncerSetStatusesAsString()
sourceValues = []string{sourceRequest}
mspStoredImpValues = []string{mspStoredImpUnknown}
mspAbBucketValues = []string{mspAbBucketUnknown}
)

preloadLabelValuesForCounter(m.connectionsError, map[string][]string{
Expand Down Expand Up @@ -53,11 +54,13 @@ func preloadLabelValues(m *Metrics, syncerKeys []string, moduleStageNames map[st
requestTypeLabel: requestTypeValues,
requestStatusLabel: requestStatusValues,
mspStoredImpLabel: mspStoredImpValues,
mspAbLabel: mspAbBucketValues,
})

preloadLabelValuesForHistogram(m.requestsTimer, map[string][]string{
requestTypeLabel: requestTypeValues,
mspStoredImpLabel: mspStoredImpValues,
mspAbLabel: mspAbBucketValues,
})

preloadLabelValuesForHistogram(m.storedAccountFetchTimer, map[string][]string{
Expand Down Expand Up @@ -128,6 +131,7 @@ func preloadLabelValues(m *Metrics, syncerKeys []string, moduleStageNames map[st
adapterLabel: adapterValues,
markupDeliveryLabel: bidTypeValues,
mspStoredImpLabel: mspStoredImpValues,
mspAbLabel: mspAbBucketValues,
})

preloadLabelValuesForCounter(m.adapterErrors, map[string][]string{
Expand Down Expand Up @@ -162,13 +166,15 @@ func preloadLabelValues(m *Metrics, syncerKeys []string, moduleStageNames map[st
preloadLabelValuesForHistogram(m.adapterPrices, map[string][]string{
adapterLabel: adapterValues,
mspStoredImpLabel: mspStoredImpValues,
mspAbLabel: mspAbBucketValues,
})

preloadLabelValuesForCounter(m.adapterRequests, map[string][]string{
adapterLabel: adapterValues,
cookieLabel: cookieValues,
hasBidsLabel: boolValues,
mspStoredImpLabel: mspStoredImpValues,
mspAbLabel: mspAbBucketValues,
})

preloadLabelValuesForCounter(m.adsCertRequests, map[string][]string{
Expand Down Expand Up @@ -273,11 +279,13 @@ func preloadLabelValues(m *Metrics, syncerKeys []string, moduleStageNames map[st
adapterLabel: adapterValues,
markupDeliveryLabel: bidTypeValues,
mspStoredImpLabel: mspStoredImpValues,
mspAbLabel: mspAbBucketValues,
})

preloadLabelValuesForHistogram(m.mspAdapterWinningPrices, map[string][]string{
adapterLabel: adapterValues,
mspStoredImpLabel: mspStoredImpValues,
mspAbLabel: mspAbBucketValues,
})
}

Expand Down
Loading

0 comments on commit 2290db8

Please sign in to comment.