diff --git a/docs/metrics.md b/docs/metrics.md index 7360cdb0d04..189a5b242c8 100644 --- a/docs/metrics.md +++ b/docs/metrics.md @@ -89,10 +89,10 @@ Following metrics are collected and submitted if account is configured with `bas Following metrics are collected and submitted if account is configured with `detailed` verbosity: - `account..requests.type.(openrtb2-web,openrtb-app,amp,legacy)` - number of requests received from account with `` broken down by type of incoming request -- `account...request_time` - timer tracking how long did it take to make a request to `` when incoming request was from `` -- `account...bids_received` - number of bids received from `` when incoming request was from `` -- `account...requests.(gotbids|nobid)` - number of requests made to `` broken down by result status when incoming request was from `` - `account..requests.rejected` - number of rejected requests caused by incorrect `accountId` +- `account..adapter..request_time` - timer tracking how long did it take to make a request to `` when incoming request was from `` +- `account..adapter..bids_received` - number of bids received from `` when incoming request was from `` +- `account..adapter..requests.(gotbids|nobid)` - number of requests made to `` broken down by result status when incoming request was from `` ## General Prebid Cache metrics - `prebid_cache.requests.ok` - timer tracking how long did successful cache requests take diff --git a/src/main/java/org/prebid/server/metric/AccountMetrics.java b/src/main/java/org/prebid/server/metric/AccountMetrics.java index 80ae7853dd6..622e08f96a9 100644 --- a/src/main/java/org/prebid/server/metric/AccountMetrics.java +++ b/src/main/java/org/prebid/server/metric/AccountMetrics.java @@ -12,13 +12,12 @@ */ class AccountMetrics extends UpdatableMetrics { - private final Function adapterMetricsCreator; // not thread-safe maps are intentionally used here because it's harmless in this particular case - eventually // this all boils down to metrics lookup by underlying metric registry and that operation is guaranteed to be // thread-safe - private final Map adapterMetrics; private final Function requestTypeMetricsCreator; private final Map requestTypeMetrics; + private final AdapterMetrics adapterMetrics; private final RequestMetrics requestsMetrics; private final CacheMetrics cacheMetrics; private final ResponseMetrics responseMetrics; @@ -26,10 +25,9 @@ class AccountMetrics extends UpdatableMetrics { AccountMetrics(MetricRegistry metricRegistry, CounterType counterType, String account) { super(Objects.requireNonNull(metricRegistry), Objects.requireNonNull(counterType), nameCreator(createPrefix(Objects.requireNonNull(account)))); - adapterMetricsCreator = adapterType -> new AdapterMetrics(metricRegistry, counterType, account, adapterType); - adapterMetrics = new HashMap<>(); requestTypeMetricsCreator = requestType -> new RequestTypeMetrics(metricRegistry, counterType, createPrefix(account), requestType); + adapterMetrics = new AdapterMetrics(metricRegistry, counterType, createPrefix(account)); requestTypeMetrics = new HashMap<>(); requestsMetrics = new RequestMetrics(metricRegistry, counterType, createPrefix(account)); cacheMetrics = new CacheMetrics(metricRegistry, counterType, createPrefix(account)); @@ -44,8 +42,8 @@ private static Function nameCreator(String prefix) { return metricName -> String.format("%s.%s", prefix, metricName.toString()); } - AdapterMetrics forAdapter(String adapterType) { - return adapterMetrics.computeIfAbsent(adapterType, adapterMetricsCreator); + AdapterMetrics adapter() { + return adapterMetrics; } RequestTypeMetrics requestType(MetricName requestType) { diff --git a/src/main/java/org/prebid/server/metric/AdapterMetrics.java b/src/main/java/org/prebid/server/metric/AdapterMetrics.java index 2f98a32cdb1..96c3621e431 100644 --- a/src/main/java/org/prebid/server/metric/AdapterMetrics.java +++ b/src/main/java/org/prebid/server/metric/AdapterMetrics.java @@ -12,68 +12,27 @@ */ class AdapterMetrics extends UpdatableMetrics { - private final Function requestTypeMetricsCreator; - private final Map requestTypeMetrics; - private final RequestMetrics requestMetrics; - private final Function bidTypeMetricsCreator; - private final Map bidTypeMetrics; - private final ResponseMetrics responseMetrics; + private final Function adapterMetricsCreator; + private final Map adapterMetrics; - AdapterMetrics(MetricRegistry metricRegistry, CounterType counterType, String adapterType) { + AdapterMetrics(MetricRegistry metricRegistry, CounterType counterType, String accountPrefix) { super(Objects.requireNonNull(metricRegistry), Objects.requireNonNull(counterType), - nameCreator(createAdapterPrefix(Objects.requireNonNull(adapterType)))); + nameCreator(createAdapterSuffix(Objects.requireNonNull(accountPrefix)))); - bidTypeMetricsCreator = bidType -> - new BidTypeMetrics(metricRegistry, counterType, createAdapterPrefix(adapterType), bidType); - requestTypeMetricsCreator = requestType -> - new RequestTypeMetrics(metricRegistry, counterType, createAdapterPrefix(adapterType), requestType); - requestTypeMetrics = new HashMap<>(); - requestMetrics = new RequestMetrics(metricRegistry, counterType, createAdapterPrefix(adapterType)); - bidTypeMetrics = new HashMap<>(); - responseMetrics = new ResponseMetrics(metricRegistry, counterType, createAdapterPrefix(adapterType)); + adapterMetrics = new HashMap<>(); + adapterMetricsCreator = adapterType -> new AdapterTypeMetrics(metricRegistry, counterType, + createAdapterSuffix(Objects.requireNonNull(accountPrefix)), adapterType); } - AdapterMetrics(MetricRegistry metricRegistry, CounterType counterType, String account, String adapterType) { - super(Objects.requireNonNull(metricRegistry), Objects.requireNonNull(counterType), - nameCreator(createAccountAdapterPrefix(Objects.requireNonNull(account), - Objects.requireNonNull(adapterType)))); - - requestMetrics = new RequestMetrics(metricRegistry, counterType, - createAccountAdapterPrefix(account, adapterType)); - - // not used for account.adapter metrics - requestTypeMetricsCreator = null; - requestTypeMetrics = null; - bidTypeMetricsCreator = null; - bidTypeMetrics = null; - responseMetrics = null; - } - - private static String createAdapterPrefix(String adapterType) { - return String.format("adapter.%s", adapterType); - } - - private static String createAccountAdapterPrefix(String account, String adapterType) { - return String.format("account.%s.%s", account, adapterType); + private static String createAdapterSuffix(String prefix) { + return String.format("%s.adapter", prefix); } private static Function nameCreator(String prefix) { return metricName -> String.format("%s.%s", prefix, metricName.toString()); } - RequestTypeMetrics requestType(MetricName requestType) { - return requestTypeMetrics.computeIfAbsent(requestType, requestTypeMetricsCreator); - } - - RequestMetrics request() { - return requestMetrics; - } - - BidTypeMetrics forBidType(String bidType) { - return bidTypeMetrics.computeIfAbsent(bidType, bidTypeMetricsCreator); - } - - ResponseMetrics response() { - return responseMetrics; + AdapterTypeMetrics forAdapter(String adapterType) { + return adapterMetrics.computeIfAbsent(adapterType, adapterMetricsCreator); } } diff --git a/src/main/java/org/prebid/server/metric/AdapterTypeMetrics.java b/src/main/java/org/prebid/server/metric/AdapterTypeMetrics.java new file mode 100644 index 00000000000..5c4bf1ae1d2 --- /dev/null +++ b/src/main/java/org/prebid/server/metric/AdapterTypeMetrics.java @@ -0,0 +1,82 @@ +package org.prebid.server.metric; + +import com.codahale.metrics.MetricRegistry; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.function.Function; + +/** + * AdapterType metrics support. + */ +class AdapterTypeMetrics extends UpdatableMetrics { + + private final Function requestTypeMetricsCreator; + private final Map requestTypeMetrics; + private final RequestMetrics requestMetrics; + private final Function bidTypeMetricsCreator; + private final Map bidTypeMetrics; + private final ResponseMetrics responseMetrics; + + AdapterTypeMetrics(MetricRegistry metricRegistry, CounterType counterType, String adapterType) { + super(Objects.requireNonNull(metricRegistry), Objects.requireNonNull(counterType), + nameCreator(createAdapterPrefix(Objects.requireNonNull(adapterType)))); + + bidTypeMetricsCreator = bidType -> + new BidTypeMetrics(metricRegistry, counterType, createAdapterPrefix(adapterType), bidType); + requestTypeMetricsCreator = requestType -> + new RequestTypeMetrics(metricRegistry, counterType, createAdapterPrefix(adapterType), requestType); + requestTypeMetrics = new HashMap<>(); + requestMetrics = new RequestMetrics(metricRegistry, counterType, createAdapterPrefix(adapterType)); + bidTypeMetrics = new HashMap<>(); + responseMetrics = new ResponseMetrics(metricRegistry, counterType, createAdapterPrefix(adapterType)); + } + + AdapterTypeMetrics(MetricRegistry metricRegistry, + CounterType counterType, + String accountAdapterPrefix, + String adapterType) { + super(Objects.requireNonNull(metricRegistry), Objects.requireNonNull(counterType), + nameCreator(createAdapterPrefix(Objects.requireNonNull(accountAdapterPrefix), + Objects.requireNonNull(adapterType)))); + + requestMetrics = new RequestMetrics(metricRegistry, counterType, + createAdapterPrefix(accountAdapterPrefix, adapterType)); + + // not used for account.adapter.adapters metrics + requestTypeMetricsCreator = null; + requestTypeMetrics = null; + bidTypeMetricsCreator = null; + bidTypeMetrics = null; + responseMetrics = null; + } + + private static String createAdapterPrefix(String adapterType) { + return String.format("adapter.%s", adapterType); + } + + private static String createAdapterPrefix(String adapterPrefix, String adapterType) { + return String.format("%s.%s", adapterPrefix, adapterType); + } + + private static Function nameCreator(String prefix) { + return metricName -> String.format("%s.%s", prefix, metricName.toString()); + } + + RequestTypeMetrics requestType(MetricName requestType) { + return requestTypeMetrics.computeIfAbsent(requestType, requestTypeMetricsCreator); + } + + RequestMetrics request() { + return requestMetrics; + } + + BidTypeMetrics forBidType(String bidType) { + return bidTypeMetrics.computeIfAbsent(bidType, bidTypeMetricsCreator); + } + + ResponseMetrics response() { + return responseMetrics; + } +} diff --git a/src/main/java/org/prebid/server/metric/Metrics.java b/src/main/java/org/prebid/server/metric/Metrics.java index 5980206f0a4..4eefaea1c80 100644 --- a/src/main/java/org/prebid/server/metric/Metrics.java +++ b/src/main/java/org/prebid/server/metric/Metrics.java @@ -29,7 +29,7 @@ public class Metrics extends UpdatableMetrics { private final Function requestMetricsCreator; private final Function accountMetricsCreator; - private final Function adapterMetricsCreator; + private final Function adapterMetricsCreator; private final Function bidderCardinalityMetricsCreator; private final Function circuitBreakerMetricsCreator; private final Function settingsCacheMetricsCreator; @@ -38,7 +38,7 @@ public class Metrics extends UpdatableMetrics { // thread-safe private final Map requestMetrics; private final Map accountMetrics; - private final Map adapterMetrics; + private final Map adapterMetrics; private final Map bidderCardinailtyMetrics; private final UserSyncMetrics userSyncMetrics; private final CookieSyncMetrics cookieSyncMetrics; @@ -58,7 +58,7 @@ public Metrics(MetricRegistry metricRegistry, CounterType counterType, AccountMe requestMetricsCreator = requestType -> new RequestStatusMetrics(metricRegistry, counterType, requestType); accountMetricsCreator = account -> new AccountMetrics(metricRegistry, counterType, account); - adapterMetricsCreator = adapterType -> new AdapterMetrics(metricRegistry, counterType, adapterType); + adapterMetricsCreator = adapterType -> new AdapterTypeMetrics(metricRegistry, counterType, adapterType); bidderCardinalityMetricsCreator = cardinality -> new BidderCardinalityMetrics( metricRegistry, counterType, cardinality); circuitBreakerMetricsCreator = type -> new CircuitBreakerMetrics(metricRegistry, counterType, type); @@ -89,7 +89,7 @@ AccountMetrics forAccount(String account) { return accountMetrics.computeIfAbsent(account, accountMetricsCreator); } - AdapterMetrics forAdapter(String adapterType) { + AdapterTypeMetrics forAdapter(String adapterType) { return adapterMetrics.computeIfAbsent(adapterType, adapterMetricsCreator); } @@ -211,22 +211,23 @@ public void updateAccountRequestRejectedMetrics(String accountId) { } public void updateAdapterRequestTypeAndNoCookieMetrics(String bidder, MetricName requestType, boolean noCookie) { - final AdapterMetrics adapterMetrics = forAdapter(resolveMetricsBidderName(bidder)); + final AdapterTypeMetrics adapterTypeMetrics = forAdapter(resolveMetricsBidderName(bidder)); - adapterMetrics.requestType(requestType).incCounter(MetricName.requests); + adapterTypeMetrics.requestType(requestType).incCounter(MetricName.requests); if (noCookie) { - adapterMetrics.incCounter(MetricName.no_cookie_requests); + adapterTypeMetrics.incCounter(MetricName.no_cookie_requests); } } public void updateAdapterResponseTime(String bidder, String accountId, int responseTime) { final String metricsBidderName = resolveMetricsBidderName(bidder); - final AdapterMetrics adapterMetrics = forAdapter(metricsBidderName); - adapterMetrics.updateTimer(MetricName.request_time, responseTime); + final AdapterTypeMetrics adapterTypeMetrics = forAdapter(metricsBidderName); + adapterTypeMetrics.updateTimer(MetricName.request_time, responseTime); if (accountMetricsVerbosity.forAccount(accountId).isAtLeast(AccountMetricsVerbosityLevel.detailed)) { - final AdapterMetrics accountAdapterMetrics = forAccount(accountId).forAdapter(metricsBidderName); + final AdapterTypeMetrics accountAdapterMetrics = + forAccount(accountId).adapter().forAdapter(metricsBidderName); accountAdapterMetrics.updateTimer(MetricName.request_time, responseTime); } } @@ -235,7 +236,7 @@ public void updateAdapterRequestNobidMetrics(String bidder, String accountId) { final String metricsBidderName = resolveMetricsBidderName(bidder); forAdapter(metricsBidderName).request().incCounter(MetricName.nobid); if (accountMetricsVerbosity.forAccount(accountId).isAtLeast(AccountMetricsVerbosityLevel.detailed)) { - forAccount(accountId).forAdapter(metricsBidderName).request().incCounter(MetricName.nobid); + forAccount(accountId).adapter().forAdapter(metricsBidderName).request().incCounter(MetricName.nobid); } } @@ -243,20 +244,21 @@ public void updateAdapterRequestGotbidsMetrics(String bidder, String accountId) final String metricsBidderName = resolveMetricsBidderName(bidder); forAdapter(metricsBidderName).request().incCounter(MetricName.gotbids); if (accountMetricsVerbosity.forAccount(accountId).isAtLeast(AccountMetricsVerbosityLevel.detailed)) { - forAccount(accountId).forAdapter(metricsBidderName).request().incCounter(MetricName.gotbids); + forAccount(accountId).adapter().forAdapter(metricsBidderName).request().incCounter(MetricName.gotbids); } } public void updateAdapterBidMetrics(String bidder, String accountId, long cpm, boolean isAdm, String bidType) { final String metricsBidderName = resolveMetricsBidderName(bidder); - final AdapterMetrics adapterMetrics = forAdapter(metricsBidderName); - adapterMetrics.updateHistogram(MetricName.prices, cpm); - adapterMetrics.incCounter(MetricName.bids_received); - adapterMetrics.forBidType(bidType) + final AdapterTypeMetrics adapterTypeMetrics = forAdapter(metricsBidderName); + adapterTypeMetrics.updateHistogram(MetricName.prices, cpm); + adapterTypeMetrics.incCounter(MetricName.bids_received); + adapterTypeMetrics.forBidType(bidType) .incCounter(isAdm ? MetricName.adm_bids_received : MetricName.nurl_bids_received); if (accountMetricsVerbosity.forAccount(accountId).isAtLeast(AccountMetricsVerbosityLevel.detailed)) { - final AdapterMetrics accountAdapterMetrics = forAccount(accountId).forAdapter(metricsBidderName); + final AdapterTypeMetrics accountAdapterMetrics = + forAccount(accountId).adapter().forAdapter(metricsBidderName); accountAdapterMetrics.updateHistogram(MetricName.prices, cpm); accountAdapterMetrics.incCounter(MetricName.bids_received); } diff --git a/src/test/java/org/prebid/server/metric/MetricsTest.java b/src/test/java/org/prebid/server/metric/MetricsTest.java index a47b0dea119..630ac19da8f 100644 --- a/src/test/java/org/prebid/server/metric/MetricsTest.java +++ b/src/test/java/org/prebid/server/metric/MetricsTest.java @@ -153,14 +153,15 @@ public void shouldReturnAdapterRequestMetricsConfiguredWithAdapterType() { @Test public void shouldReturnSameAccountAdapterMetricsOnSuccessiveCalls() { - assertThat(metrics.forAccount(ACCOUNT_ID).forAdapter(RUBICON)) - .isSameAs(metrics.forAccount(ACCOUNT_ID).forAdapter(RUBICON)); + assertThat(metrics.forAccount(ACCOUNT_ID).adapter().forAdapter(RUBICON)) + .isSameAs(metrics.forAccount(ACCOUNT_ID).adapter().forAdapter(RUBICON)); } @Test public void shouldReturnAccountAdapterMetricsConfiguredWithCounterType() { verifyCreatesConfiguredCounterType(metrics -> metrics .forAccount(ACCOUNT_ID) + .adapter() .forAdapter(RUBICON) .incCounter(MetricName.bids_received)); } @@ -168,22 +169,23 @@ public void shouldReturnAccountAdapterMetricsConfiguredWithCounterType() { @Test public void shouldReturnAccountAdapterMetricsConfiguredWithAccountAndAdapterType() { // when - metrics.forAccount(ACCOUNT_ID).forAdapter(RUBICON).incCounter(MetricName.bids_received); + metrics.forAccount(ACCOUNT_ID).adapter().forAdapter(RUBICON).incCounter(MetricName.bids_received); // then - assertThat(metricRegistry.counter("account.accountId.rubicon.bids_received").getCount()).isOne(); + assertThat(metricRegistry.counter("account.accountId.adapter.rubicon.bids_received").getCount()).isOne(); } @Test public void shouldReturnSameAccountAdapterRequestMetricsOnSuccessiveCalls() { - assertThat(metrics.forAccount(ACCOUNT_ID).forAdapter(RUBICON).request()) - .isSameAs(metrics.forAccount(ACCOUNT_ID).forAdapter(RUBICON).request()); + assertThat(metrics.forAccount(ACCOUNT_ID).adapter().forAdapter(RUBICON).request()) + .isSameAs(metrics.forAccount(ACCOUNT_ID).adapter().forAdapter(RUBICON).request()); } @Test public void shouldReturnAccountAdapterRequestMetricsConfiguredWithCounterType() { verifyCreatesConfiguredCounterType(metrics -> metrics .forAccount(ACCOUNT_ID) + .adapter() .forAdapter(RUBICON) .request() .incCounter(MetricName.gotbids)); @@ -192,10 +194,11 @@ public void shouldReturnAccountAdapterRequestMetricsConfiguredWithCounterType() @Test public void shouldReturnAccountAdapterRequestMetricsConfiguredWithAccountAndAdapterType() { // when - metrics.forAccount(ACCOUNT_ID).forAdapter(RUBICON).request().incCounter(MetricName.gotbids); + metrics.forAccount(ACCOUNT_ID).adapter().forAdapter(RUBICON).request().incCounter(MetricName.gotbids); // then - assertThat(metricRegistry.counter("account.accountId.rubicon.requests.gotbids").getCount()).isOne(); + assertThat(metricRegistry.counter("account.accountId.adapter.rubicon.requests.gotbids").getCount()) + .isOne(); } @Test @@ -481,9 +484,9 @@ public void updateAdapterResponseTimeShouldUpdateMetrics() { // then assertThat(metricRegistry.timer("adapter.rubicon.request_time").getCount()).isOne(); - assertThat(metricRegistry.timer("account.accountId.rubicon.request_time").getCount()).isOne(); + assertThat(metricRegistry.timer("account.accountId.adapter.rubicon.request_time").getCount()).isOne(); assertThat(metricRegistry.timer("adapter.UNKNOWN.request_time").getCount()).isEqualTo(2); - assertThat(metricRegistry.timer("account.accountId.UNKNOWN.request_time").getCount()).isEqualTo(2); + assertThat(metricRegistry.timer("account.accountId.adapter.UNKNOWN.request_time").getCount()).isEqualTo(2); } @Test @@ -498,9 +501,9 @@ public void updateAdapterRequestNobidMetricsShouldIncrementMetrics() { // then assertThat(metricRegistry.counter("adapter.rubicon.requests.nobid").getCount()).isOne(); - assertThat(metricRegistry.counter("account.accountId.rubicon.requests.nobid").getCount()).isOne(); + assertThat(metricRegistry.counter("account.accountId.adapter.rubicon.requests.nobid").getCount()).isOne(); assertThat(metricRegistry.counter("adapter.UNKNOWN.requests.nobid").getCount()).isEqualTo(2); - assertThat(metricRegistry.counter("account.accountId.UNKNOWN.requests.nobid").getCount()).isEqualTo(2); + assertThat(metricRegistry.counter("account.accountId.adapter.UNKNOWN.requests.nobid").getCount()).isEqualTo(2); } @Test @@ -515,9 +518,11 @@ public void updateAdapterRequestGotbidsMetricsShouldIncrementMetrics() { // then assertThat(metricRegistry.counter("adapter.rubicon.requests.gotbids").getCount()).isOne(); - assertThat(metricRegistry.counter("account.accountId.rubicon.requests.gotbids").getCount()).isOne(); + assertThat(metricRegistry.counter("account.accountId.adapter.rubicon.requests.gotbids").getCount()) + .isOne(); assertThat(metricRegistry.counter("adapter.UNKNOWN.requests.gotbids").getCount()).isEqualTo(2); - assertThat(metricRegistry.counter("account.accountId.UNKNOWN.requests.gotbids").getCount()).isEqualTo(2); + assertThat(metricRegistry.counter("account.accountId.adapter.UNKNOWN.requests.gotbids").getCount()) + .isEqualTo(2); } @Test @@ -533,15 +538,15 @@ public void updateAdapterBidMetricsShouldUpdateMetrics() { // then assertThat(metricRegistry.histogram("adapter.rubicon.prices").getCount()).isEqualTo(2); - assertThat(metricRegistry.histogram("account.accountId.rubicon.prices").getCount()).isEqualTo(2); + assertThat(metricRegistry.histogram("account.accountId.adapter.rubicon.prices").getCount()).isEqualTo(2); assertThat(metricRegistry.counter("adapter.rubicon.bids_received").getCount()).isEqualTo(2); - assertThat(metricRegistry.counter("account.accountId.rubicon.bids_received").getCount()).isEqualTo(2); + assertThat(metricRegistry.counter("account.accountId.adapter.rubicon.bids_received").getCount()).isEqualTo(2); assertThat(metricRegistry.counter("adapter.rubicon.banner.adm_bids_received").getCount()).isOne(); assertThat(metricRegistry.counter("adapter.rubicon.video.nurl_bids_received").getCount()).isOne(); assertThat(metricRegistry.histogram("adapter.UNKNOWN.prices").getCount()).isEqualTo(2); - assertThat(metricRegistry.histogram("account.accountId.UNKNOWN.prices").getCount()).isEqualTo(2); + assertThat(metricRegistry.histogram("account.accountId.adapter.UNKNOWN.prices").getCount()).isEqualTo(2); assertThat(metricRegistry.counter("adapter.UNKNOWN.bids_received").getCount()).isEqualTo(2); - assertThat(metricRegistry.counter("account.accountId.UNKNOWN.bids_received").getCount()).isEqualTo(2); + assertThat(metricRegistry.counter("account.accountId.adapter.UNKNOWN.bids_received").getCount()).isEqualTo(2); assertThat(metricRegistry.counter("adapter.UNKNOWN.banner.nurl_bids_received").getCount()).isEqualTo(2); }