Skip to content

Commit

Permalink
Add bidder cardinality metric (#953)
Browse files Browse the repository at this point in the history
  • Loading branch information
schernysh authored Oct 20, 2020
1 parent c1c94b7 commit be56e41
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Other available metrics can found at [Vert.x Dropwizard Metrics](https://vertx.i
- `imps_native` - number of native impressions
- `imps_audio` - number of audio impressions
- `requests.(ok|badinput|err|networkerr|blacklisted_account|blacklisted_app).(openrtb2-web|openrtb-app|amp|legacy)` - number of requests broken down by status and type
- `bidder-cardinality.<cardinality>.requests` - number of requests targeting `<cardinality>` of bidders
- `connection_accept_errors` - number of errors occurred while establishing HTTP connection
- `db_circuitbreaker_opened` - number of times database circuit breaker was opened (database is unavailable)
- `db_circuitbreaker_closed` - number of times database circuit breaker was closed (database is available again)
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/org/prebid/server/auction/ExchangeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -780,9 +780,13 @@ private ExtRequest prepareExt(String bidder,
/**
* Updates 'account.*.request', 'request' and 'no_cookie_requests' metrics for each {@link BidderRequest}.
*/
private List<BidderRequest> updateRequestMetric(List<BidderRequest> bidderRequests, UidsCookie uidsCookie,
BidderAliases aliases, String publisherId,
private List<BidderRequest> updateRequestMetric(List<BidderRequest> bidderRequests,
UidsCookie uidsCookie,
BidderAliases aliases,
String publisherId,
MetricName requestTypeMetric) {

metrics.updateRequestBidderCardinalityMetric(bidderRequests.size());
metrics.updateAccountRequestMetrics(publisherId, requestTypeMetric);

for (BidderRequest bidderRequest : bidderRequests) {
Expand All @@ -793,6 +797,7 @@ private List<BidderRequest> updateRequestMetric(List<BidderRequest> bidderReques

metrics.updateAdapterRequestTypeAndNoCookieMetrics(bidder, requestTypeMetric, !isApp && noBuyerId);
}

return bidderRequests;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.prebid.server.metric;

import com.codahale.metrics.MetricRegistry;

import java.util.Objects;
import java.util.function.Function;

public class BidderCardinalityMetrics extends UpdatableMetrics {

BidderCardinalityMetrics(MetricRegistry metricRegistry, CounterType counterType, Integer cardinality) {
super(metricRegistry, counterType, nameCreator(Objects.requireNonNull(cardinality)));
}

private static Function<MetricName, String> nameCreator(Integer cardinality) {
return metricName -> String.format("bidder-cardinality.%d.%s", cardinality, metricName.toString());
}
}
13 changes: 13 additions & 0 deletions src/main/java/org/prebid/server/metric/Metrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ public class Metrics extends UpdatableMetrics {
private final Function<MetricName, RequestStatusMetrics> requestMetricsCreator;
private final Function<String, AccountMetrics> accountMetricsCreator;
private final Function<String, AdapterMetrics> adapterMetricsCreator;
private final Function<Integer, BidderCardinalityMetrics> bidderCardinalityMetricsCreator;
private final Function<String, CircuitBreakerMetrics> circuitBreakerMetricsCreator;
// 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<MetricName, RequestStatusMetrics> requestMetrics;
private final Map<String, AccountMetrics> accountMetrics;
private final Map<String, AdapterMetrics> adapterMetrics;
private final Map<Integer, BidderCardinalityMetrics> bidderCardinailtyMetrics;
private final UserSyncMetrics userSyncMetrics;
private final CookieSyncMetrics cookieSyncMetrics;
private final PrivacyMetrics privacyMetrics;
Expand All @@ -51,10 +53,13 @@ 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);
bidderCardinalityMetricsCreator = cardinality -> new BidderCardinalityMetrics(
metricRegistry, counterType, cardinality);
circuitBreakerMetricsCreator = id -> new CircuitBreakerMetrics(metricRegistry, counterType, id);
requestMetrics = new EnumMap<>(MetricName.class);
accountMetrics = new HashMap<>();
adapterMetrics = new HashMap<>();
bidderCardinailtyMetrics = new HashMap<>();
userSyncMetrics = new UserSyncMetrics(metricRegistry, counterType);
cookieSyncMetrics = new CookieSyncMetrics(metricRegistry, counterType);
privacyMetrics = new PrivacyMetrics(metricRegistry, counterType);
Expand All @@ -66,6 +71,10 @@ RequestStatusMetrics forRequestType(MetricName requestType) {
return requestMetrics.computeIfAbsent(requestType, requestMetricsCreator);
}

BidderCardinalityMetrics forBidderCardinality(int cardinality) {
return bidderCardinailtyMetrics.computeIfAbsent(cardinality, bidderCardinalityMetricsCreator);
}

AccountMetrics forAccount(String account) {
return accountMetrics.computeIfAbsent(account, accountMetricsCreator);
}
Expand Down Expand Up @@ -172,6 +181,10 @@ public void updateRequestTypeMetric(MetricName requestType, MetricName requestSt
forRequestType(requestType).incCounter(requestStatus);
}

public void updateRequestBidderCardinalityMetric(int bidderCardinality) {
forBidderCardinality(bidderCardinality).incCounter(MetricName.requests);
}

public void updateAccountRequestMetrics(String accountId, MetricName requestType) {
final AccountMetricsVerbosityLevel verbosityLevel = accountMetricsVerbosity.forAccount(accountId);
if (verbosityLevel.isAtLeast(AccountMetricsVerbosityLevel.basic)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2107,6 +2107,7 @@ public void shouldIncrementCommonMetrics() {
exchangeService.holdAuction(givenRequestContext(bidRequest));

// then
verify(metrics).updateRequestBidderCardinalityMetric(1);
verify(metrics).updateAccountRequestMetrics(eq("accountId"), eq(MetricName.openrtb2web));
verify(metrics)
.updateAdapterRequestTypeAndNoCookieMetrics(eq("someBidder"), eq(MetricName.openrtb2web), eq(true));
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/org/prebid/server/metric/MetricsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,15 @@ public void updateRequestTypeMetricShouldIncrementMetric() {
assertThat(metricRegistry.counter("requests.networkerr.amp").getCount()).isEqualTo(1);
}

@Test
public void uupdateRequestBidderCardinalityMetricShouldIncrementMetrics() {
// when
metrics.updateRequestBidderCardinalityMetric(3);

// then
assertThat(metricRegistry.counter("bidder-cardinality.3.requests").getCount()).isEqualTo(1);
}

@Test
public void updateAccountRequestMetricsShouldIncrementMetrics() {
// when
Expand Down

0 comments on commit be56e41

Please sign in to comment.