Skip to content
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

Account Metrics Locking improvements #4

Merged
merged 3 commits into from
May 2, 2017
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 36 additions & 18 deletions pbs_light.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import (
"encoding/json"
"flag"
"fmt"
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/cache"
"github.com/prebid/prebid-server/pbs"
"io/ioutil"
"math/rand"
"net"
Expand All @@ -16,6 +13,10 @@ import (
"sync"
"time"

"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/cache"
"github.com/prebid/prebid-server/pbs"

"github.com/cloudfoundry/gosigar"
"github.com/golang/glog"
"github.com/julienschmidt/httprouter"
Expand Down Expand Up @@ -58,10 +59,9 @@ var (
mRequestTimer metrics.Timer

adapterMetrics map[string]*AdapterMetrics
accountMetrics map[string]*AccountMetrics // FIXME -- this seems like an unbounded queue

adapterMetricsMutex sync.Mutex
accountMetricsMutex sync.Mutex
accountMetrics map[string]*AccountMetrics // FIXME -- this seems like an unbounded queue
accountMetricsRWMutex sync.RWMutex

requireUUID2 bool
cookieDomain string
Expand Down Expand Up @@ -91,6 +91,32 @@ func writeAuctionError(w http.ResponseWriter, s string, err error) {
}
}

func getAccountMetrics(id string) *AccountMetrics {
var am *AccountMetrics
var ok bool

accountMetricsRWMutex.RLock()
am, ok = accountMetrics[id]
accountMetricsRWMutex.RUnlock()

if ok {
return am
}

accountMetricsRWMutex.Lock()
am, ok = accountMetrics[id]
if !ok {
am = &AccountMetrics{}
am.RequestMeter = metrics.GetOrRegisterMeter(fmt.Sprintf("account.%s.requests", id), metricsRegistry)
am.BidsReceivedMeter = metrics.GetOrRegisterMeter(fmt.Sprintf("account.%s.bids_received", id), metricsRegistry)
am.PriceHistogram = metrics.GetOrRegisterHistogram(fmt.Sprintf("account.%s.prices", id), metricsRegistry, metrics.NewExpDecaySample(1028, 0.015))
accountMetrics[id] = am
}
accountMetricsRWMutex.Unlock()

return am
}

func auction(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Header().Add("Content-Type", "application/json")

Expand Down Expand Up @@ -147,16 +173,8 @@ func auction(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
return
}

accountMetricsMutex.Lock()
if _, ok := accountMetrics[pbs_req.AccountID]; !ok {
am := AccountMetrics{}
am.RequestMeter = metrics.GetOrRegisterMeter(fmt.Sprintf("account.%s.requests", pbs_req.AccountID), metricsRegistry)
am.BidsReceivedMeter = metrics.GetOrRegisterMeter(fmt.Sprintf("account.%s.bids_received", pbs_req.AccountID), metricsRegistry)
am.PriceHistogram = metrics.GetOrRegisterHistogram(fmt.Sprintf("account.%s.prices", pbs_req.AccountID), metricsRegistry, metrics.NewExpDecaySample(1028, 0.015))
accountMetrics[pbs_req.AccountID] = &am
}
accountMetrics[pbs_req.AccountID].RequestMeter.Mark(1)
accountMetricsMutex.Unlock()
am := getAccountMetrics(pbs_req.AccountID)
am.RequestMeter.Mark(1)

pbs_resp := pbs.PBSResponse{
Status: "OK",
Expand Down Expand Up @@ -195,10 +213,10 @@ func auction(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
}
} else if bid_list != nil {
bidder.NumBids = len(bid_list)
accountMetrics[pbs_req.AccountID].BidsReceivedMeter.Mark(int64(bidder.NumBids))
am.BidsReceivedMeter.Mark(int64(bidder.NumBids))
for _, bid := range bid_list {
ametrics.PriceHistogram.Update(int64(bid.Price * 1000))
accountMetrics[pbs_req.AccountID].PriceHistogram.Update(int64(bid.Price * 1000))
am.PriceHistogram.Update(int64(bid.Price * 1000))
}
} else {
bidder.NoBid = true
Expand Down