Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bidder cardinality metric #953

Merged
merged 1 commit into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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