@@ -15,35 +15,36 @@ import (
1515 "github.com/prebid/go-gdpr/vendorlist"
1616 "github.com/prebid/go-gdpr/vendorlist2"
1717 "github.com/prebid/prebid-server/v3/config"
18+ "github.com/prebid/prebid-server/v3/metrics"
1819 "golang.org/x/net/context/ctxhttp"
1920)
2021
2122type saveVendors func (uint16 , uint16 , api.VendorList )
22- type VendorListFetcher func (ctx context.Context , specVersion uint16 , listVersion uint16 ) (vendorlist.VendorList , error )
23+ type VendorListFetcher func (ctx context.Context , specVersion uint16 , listVersion uint16 , metricsEngine metrics. MetricsEngine ) (vendorlist.VendorList , error )
2324
2425// This file provides the vendorlist-fetching function for Prebid Server.
2526//
2627// For more info, see https://github.com/prebid/prebid-server/issues/504
2728//
2829// Nothing in this file is exported. Public APIs can be found in gdpr.go
2930
30- func NewVendorListFetcher (initCtx context.Context , cfg config.GDPR , client * http.Client , urlMaker func (uint16 , uint16 ) string ) VendorListFetcher {
31+ func NewVendorListFetcher (initCtx context.Context , cfg config.GDPR , client * http.Client , metricsEngine metrics. MetricsEngine , urlMaker func (uint16 , uint16 ) string ) VendorListFetcher {
3132 cacheSave , cacheLoad := newVendorListCache ()
3233
3334 preloadContext , cancel := context .WithTimeout (initCtx , cfg .Timeouts .InitTimeout ())
3435 defer cancel ()
35- preloadCache (preloadContext , client , urlMaker , cacheSave )
36+ preloadCache (preloadContext , client , urlMaker , cacheSave , metricsEngine )
3637
3738 saveOneRateLimited := newOccasionalSaver (cfg .Timeouts .ActiveTimeout ())
38- return func (ctx context.Context , specVersion , listVersion uint16 ) (vendorlist.VendorList , error ) {
39+ return func (ctx context.Context , specVersion , listVersion uint16 , metricsEngine metrics. MetricsEngine ) (vendorlist.VendorList , error ) {
3940 // Attempt To Load From Cache
4041 if list := cacheLoad (specVersion , listVersion ); list != nil {
4142 return list , nil
4243 }
4344
4445 // Attempt To Download
4546 // - May not add to cache immediately.
46- saveOneRateLimited (ctx , client , urlMaker (specVersion , listVersion ), cacheSave )
47+ saveOneRateLimited (ctx , client , urlMaker (specVersion , listVersion ), cacheSave , metricsEngine )
4748
4849 // Attempt To Load From Cache Again
4950 // - May have been added by the call to saveOneRateLimited.
@@ -61,7 +62,7 @@ func makeVendorListNotFoundError(specVersion, listVersion uint16) error {
6162}
6263
6364// preloadCache saves all the known versions of the vendor list for future use.
64- func preloadCache (ctx context.Context , client * http.Client , urlMaker func (uint16 , uint16 ) string , saver saveVendors ) {
65+ func preloadCache (ctx context.Context , client * http.Client , urlMaker func (uint16 , uint16 ) string , saver saveVendors , metricsEngine metrics. MetricsEngine ) {
6566 versions := [2 ]struct {
6667 specVersion uint16
6768 firstListVersion uint16
@@ -76,10 +77,10 @@ func preloadCache(ctx context.Context, client *http.Client, urlMaker func(uint16
7677 },
7778 }
7879 for _ , v := range versions {
79- latestVersion := saveOne (ctx , client , urlMaker (v .specVersion , 0 ), saver )
80+ latestVersion := saveOne (ctx , client , urlMaker (v .specVersion , 0 ), saver , metricsEngine )
8081
8182 for i := v .firstListVersion ; i < latestVersion ; i ++ {
82- saveOne (ctx , client , urlMaker (v .specVersion , i ), saver )
83+ saveOne (ctx , client , urlMaker (v .specVersion , i ), saver , metricsEngine )
8384 }
8485 }
8586}
@@ -98,24 +99,24 @@ func VendorListURLMaker(specVersion, listVersion uint16) string {
9899// The goal here is to update quickly when new versions of the VendorList are released, but not wreck
99100// server performance if a bad CMP starts sending us malformed consent strings that advertize a version
100101// that doesn't exist yet.
101- func newOccasionalSaver (timeout time.Duration ) func (ctx context.Context , client * http.Client , url string , saver saveVendors ) {
102+ func newOccasionalSaver (timeout time.Duration ) func (ctx context.Context , client * http.Client , url string , saver saveVendors , metricsEngine metrics. MetricsEngine ) {
102103 lastSaved := & atomic.Value {}
103104 lastSaved .Store (time.Time {})
104105
105- return func (ctx context.Context , client * http.Client , url string , saver saveVendors ) {
106+ return func (ctx context.Context , client * http.Client , url string , saver saveVendors , metricsEngine metrics. MetricsEngine ) {
106107 now := time .Now ()
107108 timeSinceLastSave := now .Sub (lastSaved .Load ().(time.Time ))
108109
109110 if timeSinceLastSave .Minutes () > 10 {
110111 withTimeout , cancel := context .WithTimeout (ctx , timeout )
111112 defer cancel ()
112- saveOne (withTimeout , client , url , saver )
113+ saveOne (withTimeout , client , url , saver , metricsEngine )
113114 lastSaved .Store (now )
114115 }
115116 }
116117}
117118
118- func saveOne (ctx context.Context , client * http.Client , url string , saver saveVendors ) uint16 {
119+ func saveOne (ctx context.Context , client * http.Client , url string , saver saveVendors , me metrics. MetricsEngine ) uint16 {
119120 req , err := http .NewRequest ("GET" , url , nil )
120121 if err != nil {
121122 glog .Errorf ("Failed to build GET %s request. Cookie syncs may be affected: %v" , url , err )
@@ -136,8 +137,11 @@ func saveOne(ctx context.Context, client *http.Client, url string, saver saveVen
136137 }
137138 if resp .StatusCode != http .StatusOK {
138139 glog .Errorf ("GET %s returned %d. Cookie syncs may be affected." , url , resp .StatusCode )
140+ me .RecordGvlListRequest ()
139141 return 0
140142 }
143+ me .RecordGvlListRequest ()
144+
141145 var newList api.VendorList
142146 newList , err = vendorlist2 .ParseEagerly (respBody )
143147 if err != nil {
0 commit comments