Skip to content

Commit

Permalink
Modularity: Metrics infrastructure and collection (prebid#2484)
Browse files Browse the repository at this point in the history
Co-authored-by: 4lexvav <[email protected]>
  • Loading branch information
legendko and 4lexvav authored Dec 14, 2022
1 parent 83f4cc7 commit 4c9f779
Show file tree
Hide file tree
Showing 26 changed files with 973 additions and 119 deletions.
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,9 @@ type DisabledMetrics struct {

// True if we don't want to collect the per adapter GDPR request blocked metric
AdapterGDPRRequestBlocked bool `mapstructure:"adapter_gdpr_request_blocked"`

// True if we want to stop collecting account modules metrics
AccountModulesMetrics bool `mapstructure:"account_modules_metrics"`
}

func (cfg *Metrics) validate(errs []error) []error {
Expand Down
3 changes: 3 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func TestDefaults(t *testing.T) {
cmpNils(t, "host_schain_node", cfg.HostSChainNode)
cmpStrings(t, "datacenter", cfg.DataCenter, "")
cmpBools(t, "hooks.enabled", cfg.Hooks.Enabled, false)
cmpBools(t, "account_modules_metrics", cfg.Metrics.Disabled.AccountModulesMetrics, false)

//Assert purpose VendorExceptionMap hash tables were built correctly
expectedTCF2 := TCF2{
Expand Down Expand Up @@ -394,6 +395,7 @@ metrics:
account_stored_responses: false
adapter_connections_metrics: true
adapter_gdpr_request_blocked: true
account_modules_metrics: true
blacklisted_apps: ["spamAppID","sketchy-app-id"]
account_required: true
auto_gen_source_tid: false
Expand Down Expand Up @@ -666,6 +668,7 @@ func TestFullConfig(t *testing.T) {
cmpStrings(t, "experiment.adscert.remote.url", cfg.Experiment.AdCerts.Remote.Url, "")
cmpInts(t, "experiment.adscert.remote.signing_timeout_ms", cfg.Experiment.AdCerts.Remote.SigningTimeoutMs, 10)
cmpBools(t, "hooks.enabled", cfg.Hooks.Enabled, true)
cmpBools(t, "account_modules_metrics", cfg.Metrics.Disabled.AccountModulesMetrics, true)
}

func TestValidateConfig(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions endpoints/openrtb2/amp_auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewAmpEndpoint(
requestsById stored_requests.Fetcher,
accounts stored_requests.AccountFetcher,
cfg *config.Configuration,
met metrics.MetricsEngine,
metricsEngine metrics.MetricsEngine,
pbsAnalytics analytics.PBSAnalyticsModule,
disabledBidders map[string]string,
defReqJSON []byte,
Expand All @@ -66,7 +66,7 @@ func NewAmpEndpoint(
hookExecutionPlanBuilder hooks.ExecutionPlanBuilder,
) (httprouter.Handle, error) {

if ex == nil || validator == nil || requestsById == nil || accounts == nil || cfg == nil || met == nil {
if ex == nil || validator == nil || requestsById == nil || accounts == nil || cfg == nil || metricsEngine == nil {
return nil, errors.New("NewAmpEndpoint requires non-nil arguments.")
}

Expand All @@ -77,7 +77,7 @@ func NewAmpEndpoint(
IPv6PrivateNetworks: cfg.RequestValidation.IPv6PrivateNetworksParsed,
}

hookExecutor := hookexecution.NewHookExecutor(hookExecutionPlanBuilder, hookexecution.EndpointAmp)
hookExecutor := hookexecution.NewHookExecutor(hookExecutionPlanBuilder, hookexecution.EndpointAmp, metricsEngine)

return httprouter.Handle((&endpointDeps{
uuidGenerator,
Expand All @@ -87,7 +87,7 @@ func NewAmpEndpoint(
empty_fetcher.EmptyFetcher{},
accounts,
cfg,
met,
metricsEngine,
pbsAnalytics,
disabledBidders,
defRequest,
Expand Down
8 changes: 4 additions & 4 deletions endpoints/openrtb2/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ func NewEndpoint(
requestsById stored_requests.Fetcher,
accounts stored_requests.AccountFetcher,
cfg *config.Configuration,
met metrics.MetricsEngine,
metricsEngine metrics.MetricsEngine,
pbsAnalytics analytics.PBSAnalyticsModule,
disabledBidders map[string]string,
defReqJSON []byte,
bidderMap map[string]openrtb_ext.BidderName,
storedRespFetcher stored_requests.Fetcher,
hookExecutionPlanBuilder hooks.ExecutionPlanBuilder,
) (httprouter.Handle, error) {
if ex == nil || validator == nil || requestsById == nil || accounts == nil || cfg == nil || met == nil {
if ex == nil || validator == nil || requestsById == nil || accounts == nil || cfg == nil || metricsEngine == nil {
return nil, errors.New("NewEndpoint requires non-nil arguments.")
}

Expand All @@ -85,7 +85,7 @@ func NewEndpoint(
IPv6PrivateNetworks: cfg.RequestValidation.IPv6PrivateNetworksParsed,
}

hookExecutor := hookexecution.NewHookExecutor(hookExecutionPlanBuilder, hookexecution.EndpointAuction)
hookExecutor := hookexecution.NewHookExecutor(hookExecutionPlanBuilder, hookexecution.EndpointAuction, metricsEngine)

return httprouter.Handle((&endpointDeps{
uuidGenerator,
Expand All @@ -95,7 +95,7 @@ func NewEndpoint(
empty_fetcher.EmptyFetcher{},
accounts,
cfg,
met,
metricsEngine,
pbsAnalytics,
disabledBidders,
defRequest,
Expand Down
38 changes: 19 additions & 19 deletions endpoints/openrtb2/auction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ func TestStoredRequests(t *testing.T) {
nil,
hardcodedResponseIPValidator{response: true},
empty_fetcher.EmptyFetcher{},
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction),
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction, &metricsConfig.NilMetricsEngine{}),
}

testStoreVideoAttr := []bool{true, true, false, false, false}
Expand Down Expand Up @@ -1464,7 +1464,7 @@ func TestValidateRequest(t *testing.T) {
nil,
hardcodedResponseIPValidator{response: true},
empty_fetcher.EmptyFetcher{},
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction),
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction, &metricsConfig.NilMetricsEngine{}),
}

testCases := []struct {
Expand Down Expand Up @@ -1837,7 +1837,7 @@ func TestSetIntegrationType(t *testing.T) {
nil,
hardcodedResponseIPValidator{response: true},
empty_fetcher.EmptyFetcher{},
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction),
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction, &metricsConfig.NilMetricsEngine{}),
}

testCases := []struct {
Expand Down Expand Up @@ -1902,7 +1902,7 @@ func TestStoredRequestGenerateUuid(t *testing.T) {
nil,
hardcodedResponseIPValidator{response: true},
empty_fetcher.EmptyFetcher{},
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction),
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction, &metricsConfig.NilMetricsEngine{}),
}

req := &openrtb2.BidRequest{}
Expand Down Expand Up @@ -2003,7 +2003,7 @@ func TestOversizedRequest(t *testing.T) {
nil,
hardcodedResponseIPValidator{response: true},
empty_fetcher.EmptyFetcher{},
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction),
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction, &metricsConfig.NilMetricsEngine{}),
}

req := httptest.NewRequest("POST", "/openrtb2/auction", strings.NewReader(reqBody))
Expand Down Expand Up @@ -2041,7 +2041,7 @@ func TestRequestSizeEdgeCase(t *testing.T) {
nil,
hardcodedResponseIPValidator{response: true},
empty_fetcher.EmptyFetcher{},
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction),
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction, &metricsConfig.NilMetricsEngine{}),
}

req := httptest.NewRequest("POST", "/openrtb2/auction", strings.NewReader(reqBody))
Expand Down Expand Up @@ -2377,7 +2377,7 @@ func TestValidateImpExt(t *testing.T) {
nil,
hardcodedResponseIPValidator{response: true},
empty_fetcher.EmptyFetcher{},
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction),
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction, &metricsConfig.NilMetricsEngine{}),
}

for _, group := range testGroups {
Expand Down Expand Up @@ -2429,7 +2429,7 @@ func TestCurrencyTrunc(t *testing.T) {
nil,
hardcodedResponseIPValidator{response: true},
empty_fetcher.EmptyFetcher{},
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction),
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction, &metricsConfig.NilMetricsEngine{}),
}

ui := int64(1)
Expand Down Expand Up @@ -2476,7 +2476,7 @@ func TestCCPAInvalid(t *testing.T) {
nil,
hardcodedResponseIPValidator{response: true},
empty_fetcher.EmptyFetcher{},
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction),
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction, &metricsConfig.NilMetricsEngine{}),
}

ui := int64(1)
Expand Down Expand Up @@ -2527,7 +2527,7 @@ func TestNoSaleInvalid(t *testing.T) {
nil,
hardcodedResponseIPValidator{response: true},
empty_fetcher.EmptyFetcher{},
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction),
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction, &metricsConfig.NilMetricsEngine{}),
}

ui := int64(1)
Expand Down Expand Up @@ -2581,7 +2581,7 @@ func TestValidateSourceTID(t *testing.T) {
nil,
hardcodedResponseIPValidator{response: true},
empty_fetcher.EmptyFetcher{},
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction),
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction, &metricsConfig.NilMetricsEngine{}),
}

ui := int64(1)
Expand Down Expand Up @@ -2625,7 +2625,7 @@ func TestSChainInvalid(t *testing.T) {
nil,
hardcodedResponseIPValidator{response: true},
empty_fetcher.EmptyFetcher{},
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction),
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction, &metricsConfig.NilMetricsEngine{}),
}

ui := int64(1)
Expand Down Expand Up @@ -2977,7 +2977,7 @@ func TestEidPermissionsInvalid(t *testing.T) {
nil,
hardcodedResponseIPValidator{response: true},
empty_fetcher.EmptyFetcher{},
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction),
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction, &metricsConfig.NilMetricsEngine{}),
}

ui := int64(1)
Expand Down Expand Up @@ -3261,7 +3261,7 @@ func TestAuctionWarnings(t *testing.T) {
nil,
hardcodedResponseIPValidator{response: true},
empty_fetcher.EmptyFetcher{},
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction),
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction, &metricsConfig.NilMetricsEngine{}),
}

req := httptest.NewRequest("POST", "/openrtb2/auction", strings.NewReader(reqBody))
Expand Down Expand Up @@ -3303,7 +3303,7 @@ func TestParseRequestParseImpInfoError(t *testing.T) {
nil,
hardcodedResponseIPValidator{response: true},
empty_fetcher.EmptyFetcher{},
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction),
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction, &metricsConfig.NilMetricsEngine{}),
}

req := httptest.NewRequest("POST", "/openrtb2/auction", strings.NewReader(reqBody))
Expand Down Expand Up @@ -3878,7 +3878,7 @@ func TestParseRequestMergeBidderParams(t *testing.T) {
nil,
hardcodedResponseIPValidator{response: true},
empty_fetcher.EmptyFetcher{},
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction),
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction, &metricsConfig.NilMetricsEngine{}),
}

req := httptest.NewRequest("POST", "/openrtb2/auction", strings.NewReader(test.givenRequestBody))
Expand Down Expand Up @@ -3981,7 +3981,7 @@ func TestParseRequestStoredResponses(t *testing.T) {
nil,
hardcodedResponseIPValidator{response: true},
&mockStoredResponseFetcher{mockStoredResponses},
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction),
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction, &metricsConfig.NilMetricsEngine{}),
}

req := httptest.NewRequest("POST", "/openrtb2/auction", strings.NewReader(test.givenRequestBody))
Expand Down Expand Up @@ -4069,7 +4069,7 @@ func TestParseRequestStoredBidResponses(t *testing.T) {
nil,
hardcodedResponseIPValidator{response: true},
&mockStoredResponseFetcher{mockStoredBidResponses},
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction),
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction, &metricsConfig.NilMetricsEngine{}),
}

req := httptest.NewRequest("POST", "/openrtb2/auction", strings.NewReader(test.givenRequestBody))
Expand Down Expand Up @@ -4103,7 +4103,7 @@ func TestValidateStoredResp(t *testing.T) {
nil,
hardcodedResponseIPValidator{response: true},
&mockStoredResponseFetcher{},
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction),
hookexecution.NewHookExecutor(hooks.EmptyPlanBuilder{}, hookexecution.EndpointAuction, &metricsConfig.NilMetricsEngine{}),
}

testCases := []struct {
Expand Down
2 changes: 1 addition & 1 deletion endpoints/openrtb2/video_auction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,7 @@ func TestVideoAuctionResponseHeaders(t *testing.T) {
func mockDepsWithMetrics(t *testing.T, ex *mockExchangeVideo) (*endpointDeps, *metrics.Metrics, *mockAnalyticsModule) {
mockModule := &mockAnalyticsModule{}

metrics := metrics.NewMetrics(gometrics.NewRegistry(), openrtb_ext.CoreBidderNames(), config.DisabledMetrics{}, nil)
metrics := metrics.NewMetrics(gometrics.NewRegistry(), openrtb_ext.CoreBidderNames(), config.DisabledMetrics{}, nil, nil)

deps := &endpointDeps{
fakeUUIDGenerator{},
Expand Down
5 changes: 3 additions & 2 deletions exchange/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,8 @@ func TestGetBidCacheInfoEndToEnd(t *testing.T) {

adapterList := make([]openrtb_ext.BidderName, 0, 2)
syncerKeys := []string{}
testEngine := metricsConf.NewMetricsEngine(cfg, adapterList, syncerKeys)
var moduleStageNames map[string][]string
testEngine := metricsConf.NewMetricsEngine(cfg, adapterList, syncerKeys, moduleStageNames)
// 2) Init new exchange with said configuration
handlerNoBidServer := func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(204) }
server := httptest.NewServer(http.HandlerFunc(handlerNoBidServer))
Expand Down Expand Up @@ -2382,7 +2383,7 @@ func newExchangeForTests(t *testing.T, filename string, expectations map[string]

return &exchange{
adapterMap: bidderAdapters,
me: metricsConf.NewMetricsEngine(&config.Configuration{}, openrtb_ext.CoreBidderNames(), nil),
me: metricsConf.NewMetricsEngine(&config.Configuration{}, openrtb_ext.CoreBidderNames(), nil, nil),
cache: &wellBehavedCache{},
cacheTime: 0,
currencyConverter: currency.NewRateConverter(&http.Client{}, "", time.Duration(0)),
Expand Down
1 change: 1 addition & 0 deletions hooks/hookexecution/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func (e TimeoutError) Error() string {
}

// FailureError indicates expected error occurred during hook execution on the module-side.
// A moduleFailed metric will be sent in such case.
type FailureError struct {
Message string
}
Expand Down
Loading

0 comments on commit 4c9f779

Please sign in to comment.