github.com/prebid/prebid-server/v2@v2.18.0/metrics/config/metrics_test.go (about) 1 package config 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 "time" 8 9 mainConfig "github.com/prebid/prebid-server/v2/config" 10 "github.com/prebid/prebid-server/v2/metrics" 11 "github.com/prebid/prebid-server/v2/openrtb_ext" 12 13 gometrics "github.com/rcrowley/go-metrics" 14 ) 15 16 var modulesStages = map[string][]string{"foobar": {"entry", "raw"}, "another_module": {"raw", "auction"}} 17 18 // Start a simple test to insure we get valid MetricsEngines for various configurations 19 func TestNilMetricsEngine(t *testing.T) { 20 cfg := mainConfig.Configuration{} 21 adapterList := make([]openrtb_ext.BidderName, 0, 2) 22 syncerKeys := []string{"keyA", "keyB"} 23 testEngine := NewMetricsEngine(&cfg, adapterList, syncerKeys, modulesStages) 24 _, ok := testEngine.MetricsEngine.(*NilMetricsEngine) 25 if !ok { 26 t.Error("Expected a NilMetricsEngine, but didn't get it") 27 } 28 } 29 30 func TestGoMetricsEngine(t *testing.T) { 31 cfg := mainConfig.Configuration{} 32 cfg.Metrics.Influxdb.Host = "localhost" 33 adapterList := make([]openrtb_ext.BidderName, 0, 2) 34 syncerKeys := []string{"keyA", "keyB"} 35 testEngine := NewMetricsEngine(&cfg, adapterList, syncerKeys, modulesStages) 36 _, ok := testEngine.MetricsEngine.(*metrics.Metrics) 37 if !ok { 38 t.Error("Expected a Metrics as MetricsEngine, but didn't get it") 39 } 40 } 41 42 // Test the multiengine 43 func TestMultiMetricsEngine(t *testing.T) { 44 cfg := mainConfig.Configuration{} 45 cfg.Metrics.Influxdb.Host = "localhost" 46 adapterList := openrtb_ext.CoreBidderNames() 47 goEngine := metrics.NewMetrics(gometrics.NewPrefixedRegistry("prebidserver."), adapterList, mainConfig.DisabledMetrics{}, nil, modulesStages) 48 engineList := make(MultiMetricsEngine, 2) 49 engineList[0] = goEngine 50 engineList[1] = &NilMetricsEngine{} 51 var metricsEngine metrics.MetricsEngine 52 metricsEngine = &engineList 53 labels := metrics.Labels{ 54 Source: metrics.DemandWeb, 55 RType: metrics.ReqTypeORTB2Web, 56 PubID: "test1", 57 CookieFlag: metrics.CookieFlagYes, 58 RequestStatus: metrics.RequestStatusOK, 59 } 60 apnLabels := metrics.AdapterLabels{ 61 Source: metrics.DemandWeb, 62 RType: metrics.ReqTypeORTB2Web, 63 Adapter: openrtb_ext.BidderAppnexus, 64 PubID: "test1", 65 CookieFlag: metrics.CookieFlagYes, 66 AdapterBids: metrics.AdapterBidNone, 67 } 68 pubLabels := metrics.AdapterLabels{ 69 Source: metrics.DemandWeb, 70 RType: metrics.ReqTypeORTB2Web, 71 Adapter: openrtb_ext.BidderPubmatic, 72 PubID: "test1", 73 CookieFlag: metrics.CookieFlagYes, 74 AdapterBids: metrics.AdapterBidPresent, 75 } 76 impTypeLabels := metrics.ImpLabels{ 77 BannerImps: true, 78 VideoImps: false, 79 AudioImps: true, 80 NativeImps: true, 81 } 82 moduleLabels := make([]metrics.ModuleLabels, 0) 83 for module, stages := range modulesStages { 84 for _, stage := range stages { 85 moduleLabels = append(moduleLabels, metrics.ModuleLabels{ 86 Module: module, 87 Stage: stage, 88 AccountID: "test1", 89 }) 90 } 91 } 92 for i := 0; i < 5; i++ { 93 metricsEngine.RecordRequest(labels) 94 metricsEngine.RecordImps(impTypeLabels) 95 metricsEngine.RecordRequestTime(labels, time.Millisecond*20) 96 metricsEngine.RecordAdapterRequest(pubLabels) 97 metricsEngine.RecordAdapterRequest(apnLabels) 98 metricsEngine.RecordAdapterPrice(pubLabels, 1.34) 99 metricsEngine.RecordAdapterBidReceived(pubLabels, openrtb_ext.BidTypeBanner, true) 100 metricsEngine.RecordAdapterTime(pubLabels, time.Millisecond*20) 101 metricsEngine.RecordPrebidCacheRequestTime(true, time.Millisecond*20) 102 } 103 for _, module := range moduleLabels { 104 metricsEngine.RecordModuleCalled(module, time.Millisecond*1) 105 metricsEngine.RecordModuleFailed(module) 106 metricsEngine.RecordModuleSuccessNooped(module) 107 metricsEngine.RecordModuleSuccessUpdated(module) 108 metricsEngine.RecordModuleSuccessRejected(module) 109 metricsEngine.RecordModuleExecutionError(module) 110 metricsEngine.RecordModuleTimeout(module) 111 } 112 labelsBlacklist := []metrics.Labels{ 113 { 114 Source: metrics.DemandWeb, 115 RType: metrics.ReqTypeAMP, 116 PubID: "test2", 117 CookieFlag: metrics.CookieFlagYes, 118 RequestStatus: metrics.RequestStatusBlacklisted, 119 }, 120 { 121 Source: metrics.DemandWeb, 122 RType: metrics.ReqTypeVideo, 123 PubID: "test2", 124 CookieFlag: metrics.CookieFlagYes, 125 RequestStatus: metrics.RequestStatusBlacklisted, 126 }, 127 } 128 for _, label := range labelsBlacklist { 129 metricsEngine.RecordRequest(label) 130 } 131 impTypeLabels.BannerImps = false 132 impTypeLabels.VideoImps = true 133 impTypeLabels.AudioImps = false 134 impTypeLabels.NativeImps = false 135 for i := 0; i < 3; i++ { 136 metricsEngine.RecordImps(impTypeLabels) 137 } 138 139 metricsEngine.RecordStoredReqCacheResult(metrics.CacheMiss, 1) 140 metricsEngine.RecordStoredImpCacheResult(metrics.CacheMiss, 2) 141 metricsEngine.RecordAccountCacheResult(metrics.CacheMiss, 3) 142 metricsEngine.RecordStoredReqCacheResult(metrics.CacheHit, 4) 143 metricsEngine.RecordStoredImpCacheResult(metrics.CacheHit, 5) 144 metricsEngine.RecordAccountCacheResult(metrics.CacheHit, 6) 145 146 metricsEngine.RecordAdapterBuyerUIDScrubbed(openrtb_ext.BidderAppnexus) 147 metricsEngine.RecordAdapterGDPRRequestBlocked(openrtb_ext.BidderAppnexus) 148 149 metricsEngine.RecordRequestQueueTime(false, metrics.ReqTypeVideo, time.Duration(1)) 150 151 //Make the metrics engine, instantiated here with goEngine, fill its RequestStatuses[RequestType][metrics.RequestStatusXX] with the new boolean values added to metrics.Labels 152 VerifyMetrics(t, "RequestStatuses.OpenRTB2.OK", goEngine.RequestStatuses[metrics.ReqTypeORTB2Web][metrics.RequestStatusOK].Count(), 5) 153 VerifyMetrics(t, "RequestStatuses.AMP.OK", goEngine.RequestStatuses[metrics.ReqTypeAMP][metrics.RequestStatusOK].Count(), 0) 154 VerifyMetrics(t, "RequestStatuses.AMP.BlacklistedAcctOrApp", goEngine.RequestStatuses[metrics.ReqTypeAMP][metrics.RequestStatusBlacklisted].Count(), 1) 155 VerifyMetrics(t, "RequestStatuses.Video.OK", goEngine.RequestStatuses[metrics.ReqTypeVideo][metrics.RequestStatusOK].Count(), 0) 156 VerifyMetrics(t, "RequestStatuses.Video.Error", goEngine.RequestStatuses[metrics.ReqTypeVideo][metrics.RequestStatusErr].Count(), 0) 157 VerifyMetrics(t, "RequestStatuses.Video.BadInput", goEngine.RequestStatuses[metrics.ReqTypeVideo][metrics.RequestStatusBadInput].Count(), 0) 158 VerifyMetrics(t, "RequestStatuses.Video.BlacklistedAcctOrApp", goEngine.RequestStatuses[metrics.ReqTypeVideo][metrics.RequestStatusBlacklisted].Count(), 1) 159 VerifyMetrics(t, "RequestStatuses.OpenRTB2.Error", goEngine.RequestStatuses[metrics.ReqTypeORTB2Web][metrics.RequestStatusErr].Count(), 0) 160 VerifyMetrics(t, "RequestStatuses.OpenRTB2.BadInput", goEngine.RequestStatuses[metrics.ReqTypeORTB2Web][metrics.RequestStatusBadInput].Count(), 0) 161 VerifyMetrics(t, "RequestStatuses.OpenRTB2.BlacklistedAcctOrApp", goEngine.RequestStatuses[metrics.ReqTypeORTB2Web][metrics.RequestStatusBlacklisted].Count(), 0) 162 163 VerifyMetrics(t, "ImpsTypeBanner", goEngine.ImpsTypeBanner.Count(), 5) 164 VerifyMetrics(t, "ImpsTypeVideo", goEngine.ImpsTypeVideo.Count(), 3) 165 VerifyMetrics(t, "ImpsTypeAudio", goEngine.ImpsTypeAudio.Count(), 5) 166 VerifyMetrics(t, "ImpsTypeNative", goEngine.ImpsTypeNative.Count(), 5) 167 168 VerifyMetrics(t, "RecordPrebidCacheRequestTime", goEngine.PrebidCacheRequestTimerSuccess.Count(), 5) 169 170 VerifyMetrics(t, "Request", goEngine.RequestStatuses[metrics.ReqTypeORTB2Web][metrics.RequestStatusOK].Count(), 5) 171 VerifyMetrics(t, "ImpMeter", goEngine.ImpMeter.Count(), 8) 172 VerifyMetrics(t, "NoCookieMeter", goEngine.NoCookieMeter.Count(), 0) 173 174 VerifyMetrics(t, "AdapterMetrics.pubmatic.GotBidsMeter", goEngine.AdapterMetrics[strings.ToLower(string(openrtb_ext.BidderPubmatic))].GotBidsMeter.Count(), 5) 175 VerifyMetrics(t, "AdapterMetrics.pubmatic.NoBidMeter", goEngine.AdapterMetrics[strings.ToLower(string(openrtb_ext.BidderPubmatic))].NoBidMeter.Count(), 0) 176 for _, err := range metrics.AdapterErrors() { 177 VerifyMetrics(t, "AdapterMetrics.pubmatic.Request.ErrorMeter."+string(err), goEngine.AdapterMetrics[strings.ToLower(string(openrtb_ext.BidderPubmatic))].ErrorMeters[err].Count(), 0) 178 } 179 VerifyMetrics(t, "AdapterMetrics.appnexus.GotBidsMeter", goEngine.AdapterMetrics[strings.ToLower(string(openrtb_ext.BidderAppnexus))].GotBidsMeter.Count(), 0) 180 VerifyMetrics(t, "AdapterMetrics.appnexus.NoBidMeter", goEngine.AdapterMetrics[strings.ToLower(string(openrtb_ext.BidderAppnexus))].NoBidMeter.Count(), 5) 181 182 VerifyMetrics(t, "RecordRequestQueueTime.Video.Rejected", goEngine.RequestsQueueTimer[metrics.ReqTypeVideo][false].Count(), 1) 183 VerifyMetrics(t, "RecordRequestQueueTime.Video.Accepted", goEngine.RequestsQueueTimer[metrics.ReqTypeVideo][true].Count(), 0) 184 185 VerifyMetrics(t, "StoredReqCache.Miss", goEngine.StoredReqCacheMeter[metrics.CacheMiss].Count(), 1) 186 VerifyMetrics(t, "StoredImpCache.Miss", goEngine.StoredImpCacheMeter[metrics.CacheMiss].Count(), 2) 187 VerifyMetrics(t, "AccountCache.Miss", goEngine.AccountCacheMeter[metrics.CacheMiss].Count(), 3) 188 VerifyMetrics(t, "StoredReqCache.Hit", goEngine.StoredReqCacheMeter[metrics.CacheHit].Count(), 4) 189 VerifyMetrics(t, "StoredImpCache.Hit", goEngine.StoredImpCacheMeter[metrics.CacheHit].Count(), 5) 190 VerifyMetrics(t, "AccountCache.Hit", goEngine.AccountCacheMeter[metrics.CacheHit].Count(), 6) 191 192 VerifyMetrics(t, "AdapterMetrics.appNexus.BuyerUIDScrubbed", goEngine.AdapterMetrics[strings.ToLower(string(openrtb_ext.BidderAppnexus))].BuyerUIDScrubbed.Count(), 1) 193 VerifyMetrics(t, "AdapterMetrics.appNexus.GDPRRequestBlocked", goEngine.AdapterMetrics[strings.ToLower(string(openrtb_ext.BidderAppnexus))].GDPRRequestBlocked.Count(), 1) 194 195 // verify that each module has its own metric recorded 196 for module, stages := range modulesStages { 197 for _, stage := range stages { 198 VerifyMetrics(t, fmt.Sprintf("ModuleMetrics.%s.%s.Duration", module, stage), goEngine.ModuleMetrics[module][stage].DurationTimer.Count(), 1) 199 VerifyMetrics(t, fmt.Sprintf("ModuleMetrics.%s.%s.Call", module, stage), goEngine.ModuleMetrics[module][stage].CallCounter.Count(), 1) 200 VerifyMetrics(t, fmt.Sprintf("ModuleMetrics.%s.%s.Fail", module, stage), goEngine.ModuleMetrics[module][stage].FailureCounter.Count(), 1) 201 VerifyMetrics(t, fmt.Sprintf("ModuleMetrics.%s.%s.SuccessNoop", module, stage), goEngine.ModuleMetrics[module][stage].SuccessNoopCounter.Count(), 1) 202 VerifyMetrics(t, fmt.Sprintf("ModuleMetrics.%s.%s.SuccessUpdate", module, stage), goEngine.ModuleMetrics[module][stage].SuccessUpdateCounter.Count(), 1) 203 VerifyMetrics(t, fmt.Sprintf("ModuleMetrics.%s.%s.SuccessReject", module, stage), goEngine.ModuleMetrics[module][stage].SuccessRejectCounter.Count(), 1) 204 VerifyMetrics(t, fmt.Sprintf("ModuleMetrics.%s.%s.ExecutionError", module, stage), goEngine.ModuleMetrics[module][stage].ExecutionErrorCounter.Count(), 1) 205 VerifyMetrics(t, fmt.Sprintf("ModuleMetrics.%s.%s.Timeout", module, stage), goEngine.ModuleMetrics[module][stage].TimeoutCounter.Count(), 1) 206 } 207 } 208 } 209 210 func VerifyMetrics(t *testing.T, name string, actual int64, expected int64) { 211 if expected != actual { 212 t.Errorf("Error in metric %s: got %d, expected %d.", name, actual, expected) 213 } 214 }