Skip to content
This repository has been archived by the owner on Dec 22, 2022. It is now read-only.

Commit

Permalink
Add metrics to capture stored data fetch all/delta durations with fet…
Browse files Browse the repository at this point in the history
…ch status (prebid#1515)
  • Loading branch information
bsardo authored Oct 19, 2020
1 parent f852179 commit 37779aa
Show file tree
Hide file tree
Showing 13 changed files with 844 additions and 114 deletions.
22 changes: 22 additions & 0 deletions pbsmetrics/config/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,20 @@ func (me *MultiMetricsEngine) RecordRequestTime(labels pbsmetrics.Labels, length
}
}

// RecordStoredDataFetchTime across all engines
func (me *MultiMetricsEngine) RecordStoredDataFetchTime(labels pbsmetrics.StoredDataLabels, length time.Duration) {
for _, thisME := range *me {
thisME.RecordStoredDataFetchTime(labels, length)
}
}

// RecordStoredDataError across all engines
func (me *MultiMetricsEngine) RecordStoredDataError(labels pbsmetrics.StoredDataLabels) {
for _, thisME := range *me {
thisME.RecordStoredDataError(labels)
}
}

// RecordAdapterPanic across all engines
func (me *MultiMetricsEngine) RecordAdapterPanic(labels pbsmetrics.AdapterLabels) {
for _, thisME := range *me {
Expand Down Expand Up @@ -244,6 +258,14 @@ func (me *DummyMetricsEngine) RecordLegacyImps(labels pbsmetrics.Labels, numImps
func (me *DummyMetricsEngine) RecordRequestTime(labels pbsmetrics.Labels, length time.Duration) {
}

// RecordStoredDataFetchTime as a noop
func (me *DummyMetricsEngine) RecordStoredDataFetchTime(labels pbsmetrics.StoredDataLabels, length time.Duration) {
}

// RecordStoredDataError as a noop
func (me *DummyMetricsEngine) RecordStoredDataError(labels pbsmetrics.StoredDataLabels) {
}

// RecordAdapterPanic as a noop
func (me *DummyMetricsEngine) RecordAdapterPanic(labels pbsmetrics.AdapterLabels) {
}
Expand Down
36 changes: 36 additions & 0 deletions pbsmetrics/go_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type Metrics struct {
RequestsQueueTimer map[RequestType]map[bool]metrics.Timer
PrebidCacheRequestTimerSuccess metrics.Timer
PrebidCacheRequestTimerError metrics.Timer
StoredDataFetchTimer map[StoredDataType]map[StoredDataFetchType]metrics.Timer
StoredDataErrorMeter map[StoredDataType]map[StoredDataError]metrics.Meter
StoredReqCacheMeter map[CacheResult]metrics.Meter
StoredImpCacheMeter map[CacheResult]metrics.Meter
DNSLookupTimer metrics.Timer
Expand Down Expand Up @@ -131,6 +133,8 @@ func NewBlankMetrics(registry metrics.Registry, exchanges []openrtb_ext.BidderNa
RequestsQueueTimer: make(map[RequestType]map[bool]metrics.Timer),
PrebidCacheRequestTimerSuccess: blankTimer,
PrebidCacheRequestTimerError: blankTimer,
StoredDataFetchTimer: make(map[StoredDataType]map[StoredDataFetchType]metrics.Timer),
StoredDataErrorMeter: make(map[StoredDataType]map[StoredDataError]metrics.Meter),
StoredReqCacheMeter: make(map[CacheResult]metrics.Meter),
StoredImpCacheMeter: make(map[CacheResult]metrics.Meter),
AmpNoCookieMeter: blankMeter,
Expand Down Expand Up @@ -183,6 +187,17 @@ func NewBlankMetrics(registry metrics.Registry, exchanges []openrtb_ext.BidderNa
newMetrics.PrivacyTCFRequestVersion[v] = blankMeter
}

for _, dt := range StoredDataTypes() {
newMetrics.StoredDataFetchTimer[dt] = make(map[StoredDataFetchType]metrics.Timer)
newMetrics.StoredDataErrorMeter[dt] = make(map[StoredDataError]metrics.Meter)
for _, ft := range StoredDataFetchTypes() {
newMetrics.StoredDataFetchTimer[dt][ft] = blankTimer
}
for _, e := range StoredDataErrors() {
newMetrics.StoredDataErrorMeter[dt][e] = blankMeter
}
}

//to minimize memory usage, queuedTimeout metric is now supported for video endpoint only
//boolean value represents 2 general request statuses: accepted and rejected
newMetrics.RequestsQueueTimer["video"] = make(map[bool]metrics.Timer)
Expand Down Expand Up @@ -218,6 +233,17 @@ func NewMetrics(registry metrics.Registry, exchanges []openrtb_ext.BidderName, d
newMetrics.PrebidCacheRequestTimerSuccess = metrics.GetOrRegisterTimer("prebid_cache_request_time.ok", registry)
newMetrics.PrebidCacheRequestTimerError = metrics.GetOrRegisterTimer("prebid_cache_request_time.err", registry)

for _, dt := range StoredDataTypes() {
for _, ft := range StoredDataFetchTypes() {
timerName := fmt.Sprintf("stored_%s_fetch_time.%s", string(dt), string(ft))
newMetrics.StoredDataFetchTimer[dt][ft] = metrics.GetOrRegisterTimer(timerName, registry)
}
for _, e := range StoredDataErrors() {
meterName := fmt.Sprintf("stored_%s_error.%s", string(dt), string(e))
newMetrics.StoredDataErrorMeter[dt][e] = metrics.GetOrRegisterMeter(meterName, registry)
}
}

newMetrics.AmpNoCookieMeter = metrics.GetOrRegisterMeter("amp_no_cookie_requests", registry)
newMetrics.CookieSyncMeter = metrics.GetOrRegisterMeter("cookie_sync_requests", registry)
newMetrics.userSyncBadRequest = metrics.GetOrRegisterMeter("usersync.bad_requests", registry)
Expand Down Expand Up @@ -444,6 +470,16 @@ func (me *Metrics) RecordRequestTime(labels Labels, length time.Duration) {
}
}

// RecordStoredDataFetchTime implements a part of the MetricsEngine interface
func (me *Metrics) RecordStoredDataFetchTime(labels StoredDataLabels, length time.Duration) {
me.StoredDataFetchTimer[labels.DataType][labels.DataFetchType].Update(length)
}

// RecordStoredDataError implements a part of the MetricsEngine interface
func (me *Metrics) RecordStoredDataError(labels StoredDataLabels) {
me.StoredDataErrorMeter[labels.DataType][labels.Error].Mark(1)
}

// RecordAdapterPanic implements a part of the MetricsEngine interface
func (me *Metrics) RecordAdapterPanic(labels AdapterLabels) {
am, ok := me.AdapterMetrics[labels.Adapter]
Expand Down
145 changes: 145 additions & 0 deletions pbsmetrics/go_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,151 @@ func TestRecordPrebidCacheRequestTimeWithNotSuccess(t *testing.T) {
assert.Equal(t, m.PrebidCacheRequestTimerError.Count(), int64(1))
}

func TestRecordStoredDataFetchTime(t *testing.T) {
tests := []struct {
description string
dataType StoredDataType
fetchType StoredDataFetchType
}{
{
description: "Update stored_account_fetch_time.all timer",
dataType: AccountDataType,
fetchType: FetchAll,
},
{
description: "Update stored_amp_fetch_time.all timer",
dataType: AMPDataType,
fetchType: FetchAll,
},
{
description: "Update stored_category_fetch_time.all timer",
dataType: CategoryDataType,
fetchType: FetchAll,
},
{
description: "Update stored_request_fetch_time.all timer",
dataType: RequestDataType,
fetchType: FetchAll,
},
{
description: "Update stored_video_fetch_time.all timer",
dataType: VideoDataType,
fetchType: FetchAll,
},
{
description: "Update stored_account_fetch_time.delta timer",
dataType: AccountDataType,
fetchType: FetchDelta,
},
{
description: "Update stored_amp_fetch_time.delta timer",
dataType: AMPDataType,
fetchType: FetchDelta,
},
{
description: "Update stored_category_fetch_time.delta timer",
dataType: CategoryDataType,
fetchType: FetchDelta,
},
{
description: "Update stored_request_fetch_time.delta timer",
dataType: RequestDataType,
fetchType: FetchDelta,
},
{
description: "Update stored_video_fetch_time.delta timer",
dataType: VideoDataType,
fetchType: FetchDelta,
},
}

for _, tt := range tests {
registry := metrics.NewRegistry()
m := NewMetrics(registry, []openrtb_ext.BidderName{openrtb_ext.BidderAppnexus, openrtb_ext.BidderRubicon}, config.DisabledMetrics{AccountAdapterDetails: true})
m.RecordStoredDataFetchTime(StoredDataLabels{
DataType: tt.dataType,
DataFetchType: tt.fetchType,
}, time.Duration(500))

actualCount := m.StoredDataFetchTimer[tt.dataType][tt.fetchType].Count()
assert.Equal(t, int64(1), actualCount, tt.description)

actualDuration := m.StoredDataFetchTimer[tt.dataType][tt.fetchType].Sum()
assert.Equal(t, int64(500), actualDuration, tt.description)
}
}

func TestRecordStoredDataError(t *testing.T) {
tests := []struct {
description string
dataType StoredDataType
errorType StoredDataError
}{
{
description: "Increment stored_account_error.network meter",
dataType: AccountDataType,
errorType: StoredDataErrorNetwork,
},
{
description: "Increment stored_amp_error.network meter",
dataType: AMPDataType,
errorType: StoredDataErrorNetwork,
},
{
description: "Increment stored_category_error.network meter",
dataType: CategoryDataType,
errorType: StoredDataErrorNetwork,
},
{
description: "Increment stored_request_error.network meter",
dataType: RequestDataType,
errorType: StoredDataErrorNetwork,
},
{
description: "Increment stored_video_error.network meter",
dataType: VideoDataType,
errorType: StoredDataErrorNetwork,
},
{
description: "Increment stored_account_error.undefined meter",
dataType: AccountDataType,
errorType: StoredDataErrorUndefined,
},
{
description: "Increment stored_amp_error.undefined meter",
dataType: AMPDataType,
errorType: StoredDataErrorUndefined,
},
{
description: "Increment stored_category_error.undefined meter",
dataType: CategoryDataType,
errorType: StoredDataErrorUndefined,
},
{
description: "Increment stored_request_error.undefined meter",
dataType: RequestDataType,
errorType: StoredDataErrorUndefined,
},
{
description: "Increment stored_video_error.undefined meter",
dataType: VideoDataType,
errorType: StoredDataErrorUndefined,
},
}

for _, tt := range tests {
registry := metrics.NewRegistry()
m := NewMetrics(registry, []openrtb_ext.BidderName{openrtb_ext.BidderAppnexus, openrtb_ext.BidderRubicon}, config.DisabledMetrics{AccountAdapterDetails: true})
m.RecordStoredDataError(StoredDataLabels{
DataType: tt.dataType,
Error: tt.errorType,
})

actualCount := m.StoredDataErrorMeter[tt.dataType][tt.errorType].Count()
assert.Equal(t, int64(1), actualCount, tt.description)
}
}

func TestRecordRequestPrivacy(t *testing.T) {
registry := metrics.NewRegistry()
m := NewMetrics(registry, []openrtb_ext.BidderName{openrtb_ext.BidderAppnexus, openrtb_ext.BidderRubicon}, config.DisabledMetrics{AccountAdapterDetails: true})
Expand Down
56 changes: 56 additions & 0 deletions pbsmetrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,60 @@ type PrivacyLabels struct {
LMTEnforced bool
}

type StoredDataType string

const (
AccountDataType StoredDataType = "account"
AMPDataType StoredDataType = "amp"
CategoryDataType StoredDataType = "category"
RequestDataType StoredDataType = "request"
VideoDataType StoredDataType = "video"
)

func StoredDataTypes() []StoredDataType {
return []StoredDataType{
AccountDataType,
AMPDataType,
CategoryDataType,
RequestDataType,
VideoDataType,
}
}

type StoredDataFetchType string

const (
FetchAll StoredDataFetchType = "all"
FetchDelta StoredDataFetchType = "delta"
)

func StoredDataFetchTypes() []StoredDataFetchType {
return []StoredDataFetchType{
FetchAll,
FetchDelta,
}
}

type StoredDataLabels struct {
DataType StoredDataType
DataFetchType StoredDataFetchType
Error StoredDataError
}

type StoredDataError string

const (
StoredDataErrorNetwork StoredDataError = "network"
StoredDataErrorUndefined StoredDataError = "undefined"
)

func StoredDataErrors() []StoredDataError {
return []StoredDataError{
StoredDataErrorNetwork,
StoredDataErrorUndefined,
}
}

// Label typecasting. Se below the type definitions for possible values

// DemandSource : Demand source enumeration
Expand Down Expand Up @@ -314,6 +368,8 @@ type MetricsEngine interface {
RecordUserIDSet(userLabels UserLabels) // Function should verify bidder values
RecordStoredReqCacheResult(cacheResult CacheResult, inc int)
RecordStoredImpCacheResult(cacheResult CacheResult, inc int)
RecordStoredDataFetchTime(labels StoredDataLabels, length time.Duration)
RecordStoredDataError(labels StoredDataLabels)
RecordPrebidCacheRequestTime(success bool, length time.Duration)
RecordRequestQueueTime(success bool, requestType RequestType, length time.Duration)
RecordTimeoutNotice(sucess bool)
Expand Down
10 changes: 10 additions & 0 deletions pbsmetrics/metrics_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ func (me *MetricsEngineMock) RecordRequestTime(labels Labels, length time.Durati
me.Called(labels, length)
}

// RecordStoredDataFetchTime mock
func (me *MetricsEngineMock) RecordStoredDataFetchTime(labels StoredDataLabels, length time.Duration) {
me.Called(labels, length)
}

// RecordStoredDataError mock
func (me *MetricsEngineMock) RecordStoredDataError(labels StoredDataLabels) {
me.Called(labels)
}

// RecordAdapterPanic mock
func (me *MetricsEngineMock) RecordAdapterPanic(labels AdapterLabels) {
me.Called(labels)
Expand Down
Loading

0 comments on commit 37779aa

Please sign in to comment.