Skip to content

Add metrics for account cache #1543

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 1 commit into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions pbsmetrics/config/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ func (me *MultiMetricsEngine) RecordStoredImpCacheResult(cacheResult pbsmetrics.
}
}

// RecordAccountCacheResult across all engines
func (me *MultiMetricsEngine) RecordAccountCacheResult(cacheResult pbsmetrics.CacheResult, inc int) {
for _, thisME := range *me {
thisME.RecordAccountCacheResult(cacheResult, inc)
}
}

// RecordAdapterCookieSync across all engines
func (me *MultiMetricsEngine) RecordAdapterCookieSync(adapter openrtb_ext.BidderName, gdprBlocked bool) {
for _, thisME := range *me {
Expand Down Expand Up @@ -314,6 +321,10 @@ func (me *DummyMetricsEngine) RecordStoredReqCacheResult(cacheResult pbsmetrics.
func (me *DummyMetricsEngine) RecordStoredImpCacheResult(cacheResult pbsmetrics.CacheResult, inc int) {
}

// RecordAccountCacheResult as a noop
func (me *DummyMetricsEngine) RecordAccountCacheResult(cacheResult pbsmetrics.CacheResult, inc int) {
}

// RecordPrebidCacheRequestTime as a noop
func (me *DummyMetricsEngine) RecordPrebidCacheRequestTime(success bool, length time.Duration) {
}
Expand Down
14 changes: 14 additions & 0 deletions pbsmetrics/config/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ func TestMultiMetricsEngine(t *testing.T) {
metricsEngine.RecordImps(impTypeLabels)
}

metricsEngine.RecordStoredReqCacheResult(pbsmetrics.CacheMiss, 1)
metricsEngine.RecordStoredImpCacheResult(pbsmetrics.CacheMiss, 2)
metricsEngine.RecordAccountCacheResult(pbsmetrics.CacheMiss, 3)
metricsEngine.RecordStoredReqCacheResult(pbsmetrics.CacheHit, 4)
metricsEngine.RecordStoredImpCacheResult(pbsmetrics.CacheHit, 5)
metricsEngine.RecordAccountCacheResult(pbsmetrics.CacheHit, 6)

metricsEngine.RecordRequestQueueTime(false, pbsmetrics.ReqTypeVideo, time.Duration(1))

//Make the metrics engine, instantiated here with goEngine, fill its RequestStatuses[RequestType][pbsmetrics.RequestStatusXX] with the new boolean values added to pbsmetrics.Labels
Expand Down Expand Up @@ -154,6 +161,13 @@ func TestMultiMetricsEngine(t *testing.T) {

VerifyMetrics(t, "RecordRequestQueueTime.Video.Rejected", goEngine.RequestsQueueTimer[pbsmetrics.ReqTypeVideo][false].Count(), 1)
VerifyMetrics(t, "RecordRequestQueueTime.Video.Accepted", goEngine.RequestsQueueTimer[pbsmetrics.ReqTypeVideo][true].Count(), 0)

VerifyMetrics(t, "StoredReqCache.Miss", goEngine.StoredReqCacheMeter[pbsmetrics.CacheMiss].Count(), 1)
VerifyMetrics(t, "StoredImpCache.Miss", goEngine.StoredImpCacheMeter[pbsmetrics.CacheMiss].Count(), 2)
VerifyMetrics(t, "AccountCache.Miss", goEngine.AccountCacheMeter[pbsmetrics.CacheMiss].Count(), 3)
VerifyMetrics(t, "StoredReqCache.Hit", goEngine.StoredReqCacheMeter[pbsmetrics.CacheHit].Count(), 4)
VerifyMetrics(t, "StoredImpCache.Hit", goEngine.StoredImpCacheMeter[pbsmetrics.CacheHit].Count(), 5)
VerifyMetrics(t, "AccountCache.Hit", goEngine.AccountCacheMeter[pbsmetrics.CacheHit].Count(), 6)
}

func VerifyMetrics(t *testing.T, name string, actual int64, expected int64) {
Expand Down
10 changes: 10 additions & 0 deletions pbsmetrics/go_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Metrics struct {
StoredDataErrorMeter map[StoredDataType]map[StoredDataError]metrics.Meter
StoredReqCacheMeter map[CacheResult]metrics.Meter
StoredImpCacheMeter map[CacheResult]metrics.Meter
AccountCacheMeter map[CacheResult]metrics.Meter
DNSLookupTimer metrics.Timer

// Metrics for OpenRTB requests specifically. So we can track what % of RequestsMeter are OpenRTB
Expand Down Expand Up @@ -137,6 +138,7 @@ func NewBlankMetrics(registry metrics.Registry, exchanges []openrtb_ext.BidderNa
StoredDataErrorMeter: make(map[StoredDataType]map[StoredDataError]metrics.Meter),
StoredReqCacheMeter: make(map[CacheResult]metrics.Meter),
StoredImpCacheMeter: make(map[CacheResult]metrics.Meter),
AccountCacheMeter: make(map[CacheResult]metrics.Meter),
AmpNoCookieMeter: blankMeter,
CookieSyncMeter: blankMeter,
CookieSyncGen: make(map[openrtb_ext.BidderName]metrics.Meter),
Expand Down Expand Up @@ -181,6 +183,7 @@ func NewBlankMetrics(registry metrics.Registry, exchanges []openrtb_ext.BidderNa
for _, c := range CacheResults() {
newMetrics.StoredReqCacheMeter[c] = blankMeter
newMetrics.StoredImpCacheMeter[c] = blankMeter
newMetrics.AccountCacheMeter[c] = blankMeter
}

for _, v := range TCFVersions() {
Expand Down Expand Up @@ -264,6 +267,7 @@ func NewMetrics(registry metrics.Registry, exchanges []openrtb_ext.BidderName, d
for _, cacheRes := range CacheResults() {
newMetrics.StoredReqCacheMeter[cacheRes] = metrics.GetOrRegisterMeter(fmt.Sprintf("stored_request_cache_%s", string(cacheRes)), registry)
newMetrics.StoredImpCacheMeter[cacheRes] = metrics.GetOrRegisterMeter(fmt.Sprintf("stored_imp_cache_%s", string(cacheRes)), registry)
newMetrics.AccountCacheMeter[cacheRes] = metrics.GetOrRegisterMeter(fmt.Sprintf("account_cache_%s", string(cacheRes)), registry)
}

newMetrics.RequestsQueueTimer["video"][true] = metrics.GetOrRegisterTimer("queued_requests.video.accepted", registry)
Expand Down Expand Up @@ -647,6 +651,12 @@ func (me *Metrics) RecordStoredImpCacheResult(cacheResult CacheResult, inc int)
me.StoredImpCacheMeter[cacheResult].Mark(int64(inc))
}

// RecordAccountCacheResult implements a part of the MetricsEngine interface. Records the
// cache hits and misses when looking up accounts.
func (me *Metrics) RecordAccountCacheResult(cacheResult CacheResult, inc int) {
me.AccountCacheMeter[cacheResult].Mark(int64(inc))
}

// RecordPrebidCacheRequestTime implements a part of the MetricsEngine interface. Records the
// amount of time taken to store the auction result in Prebid Cache.
func (me *Metrics) RecordPrebidCacheRequestTime(success bool, length time.Duration) {
Expand Down
1 change: 1 addition & 0 deletions pbsmetrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ type MetricsEngine interface {
RecordUserIDSet(userLabels UserLabels) // Function should verify bidder values
RecordStoredReqCacheResult(cacheResult CacheResult, inc int)
RecordStoredImpCacheResult(cacheResult CacheResult, inc int)
RecordAccountCacheResult(cacheResult CacheResult, inc int)
RecordStoredDataFetchTime(labels StoredDataLabels, length time.Duration)
RecordStoredDataError(labels StoredDataLabels)
RecordPrebidCacheRequestTime(success bool, length time.Duration)
Expand Down
5 changes: 5 additions & 0 deletions pbsmetrics/metrics_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ func (me *MetricsEngineMock) RecordStoredImpCacheResult(cacheResult CacheResult,
me.Called(cacheResult, inc)
}

// RecordAccountCacheResult mock
func (me *MetricsEngineMock) RecordAccountCacheResult(cacheResult CacheResult, inc int) {
me.Called(cacheResult, inc)
}

// RecordPrebidCacheRequestTime mock
func (me *MetricsEngineMock) RecordPrebidCacheRequestTime(success bool, length time.Duration) {
me.Called(success, length)
Expand Down
4 changes: 4 additions & 0 deletions pbsmetrics/prometheus/preload.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ func preloadLabelValues(m *Metrics) {
cacheResultLabel: cacheResultValues,
})

preloadLabelValuesForCounter(m.accountCacheResult, map[string][]string{
cacheResultLabel: cacheResultValues,
})

preloadLabelValuesForCounter(m.adapterBids, map[string][]string{
adapterLabel: adapterValues,
markupDeliveryLabel: bidTypeValues,
Expand Down
12 changes: 12 additions & 0 deletions pbsmetrics/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Metrics struct {
requestsWithoutCookie *prometheus.CounterVec
storedImpressionsCacheResult *prometheus.CounterVec
storedRequestCacheResult *prometheus.CounterVec
accountCacheResult *prometheus.CounterVec
storedAccountFetchTimer *prometheus.HistogramVec
storedAccountErrors *prometheus.CounterVec
storedAMPFetchTimer *prometheus.HistogramVec
Expand Down Expand Up @@ -186,6 +187,11 @@ func NewMetrics(cfg config.PrometheusMetrics, disabledMetrics config.DisabledMet
"Count of stored request cache requests attempts by hits or miss.",
[]string{cacheResultLabel})

metrics.accountCacheResult = newCounter(cfg, metrics.Registry,
"account_cache_performance",
"Count of account cache lookups by hits or miss.",
[]string{cacheResultLabel})

metrics.storedAccountFetchTimer = newHistogramVec(cfg, metrics.Registry,
"stored_account_fetch_time_seconds",
"Seconds to fetch stored accounts labeled by fetch type",
Expand Down Expand Up @@ -613,6 +619,12 @@ func (m *Metrics) RecordStoredImpCacheResult(cacheResult pbsmetrics.CacheResult,
}).Add(float64(inc))
}

func (m *Metrics) RecordAccountCacheResult(cacheResult pbsmetrics.CacheResult, inc int) {
m.accountCacheResult.With(prometheus.Labels{
cacheResultLabel: string(cacheResult),
}).Add(float64(inc))
}

func (m *Metrics) RecordPrebidCacheRequestTime(success bool, length time.Duration) {
m.prebidCacheWriteTimer.With(prometheus.Labels{
successLabel: strconv.FormatBool(success),
Expand Down
24 changes: 22 additions & 2 deletions pbsmetrics/prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1013,8 +1013,8 @@ func TestStoredReqCacheResultMetric(t *testing.T) {
func TestStoredImpCacheResultMetric(t *testing.T) {
m := createMetricsForTesting()

hitCount := 42
missCount := 108
hitCount := 41
missCount := 107
m.RecordStoredImpCacheResult(pbsmetrics.CacheHit, hitCount)
m.RecordStoredImpCacheResult(pbsmetrics.CacheMiss, missCount)

Expand All @@ -1030,6 +1030,26 @@ func TestStoredImpCacheResultMetric(t *testing.T) {
})
}

func TestAccountCacheResultMetric(t *testing.T) {
m := createMetricsForTesting()

hitCount := 37
missCount := 92
m.RecordAccountCacheResult(pbsmetrics.CacheHit, hitCount)
m.RecordAccountCacheResult(pbsmetrics.CacheMiss, missCount)

assertCounterVecValue(t, "", "accountCacheResult:hit", m.accountCacheResult,
float64(hitCount),
prometheus.Labels{
cacheResultLabel: string(pbsmetrics.CacheHit),
})
assertCounterVecValue(t, "", "accountCacheResult:miss", m.accountCacheResult,
float64(missCount),
prometheus.Labels{
cacheResultLabel: string(pbsmetrics.CacheMiss),
})
}

func TestCookieMetric(t *testing.T) {
m := createMetricsForTesting()

Expand Down
3 changes: 3 additions & 0 deletions stored_requests/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ func (f *fetcherWithCache) FetchAccount(ctx context.Context, accountID string) (
accountData := f.cache.Accounts.Get(ctx, []string{accountID})
// TODO: add metrics
if account, ok := accountData[accountID]; ok {
f.metricsEngine.RecordAccountCacheResult(pbsmetrics.CacheHit, 1)
return account, errs
} else {
f.metricsEngine.RecordAccountCacheResult(pbsmetrics.CacheMiss, 1)
}
account, errs = f.fetcher.FetchAccount(ctx, accountID)
if len(errs) == 0 {
Expand Down
2 changes: 2 additions & 0 deletions stored_requests/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ func TestAccountCacheHit(t *testing.T) {
"known": json.RawMessage(`true`),
})

metricsEngine.On("RecordAccountCacheResult", pbsmetrics.CacheHit, 1)
account, errs := aFetcherWithCache.FetchAccount(ctx, "known")

accCache.AssertExpectations(t)
Expand All @@ -200,6 +201,7 @@ func TestAccountCacheMiss(t *testing.T) {
accCache.On("Get", ctx, uncachedAccounts).Return(map[string]json.RawMessage{})
accCache.On("Save", ctx, uncachedAccountsData)
fetcher.On("FetchAccount", ctx, "uncached").Return(uncachedAccountsData["uncached"], []error{})
metricsEngine.On("RecordAccountCacheResult", pbsmetrics.CacheMiss, 1)

account, errs := aFetcherWithCache.FetchAccount(ctx, "uncached")

Expand Down