Skip to content

Commit

Permalink
Exitpoint Stage (#3564)
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoxaAntoxic authored Dec 4, 2024
1 parent d74e387 commit ae475a5
Show file tree
Hide file tree
Showing 31 changed files with 2,122 additions and 371 deletions.
65 changes: 1 addition & 64 deletions src/main/java/org/prebid/server/auction/ExchangeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,7 @@
import org.prebid.server.floors.PriceFloorAdjuster;
import org.prebid.server.floors.PriceFloorProcessor;
import org.prebid.server.hooks.execution.HookStageExecutor;
import org.prebid.server.hooks.execution.model.ExecutionAction;
import org.prebid.server.hooks.execution.model.ExecutionStatus;
import org.prebid.server.hooks.execution.model.GroupExecutionOutcome;
import org.prebid.server.hooks.execution.model.HookExecutionOutcome;
import org.prebid.server.hooks.execution.model.HookId;
import org.prebid.server.hooks.execution.model.HookStageExecutionResult;
import org.prebid.server.hooks.execution.model.Stage;
import org.prebid.server.hooks.execution.model.StageExecutionOutcome;
import org.prebid.server.hooks.v1.bidder.BidderRequestPayload;
import org.prebid.server.hooks.v1.bidder.BidderResponsePayload;
import org.prebid.server.json.JacksonMapper;
Expand Down Expand Up @@ -110,7 +103,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -221,8 +213,7 @@ public Future<AuctionContext> holdAuction(AuctionContext context) {
return processAuctionRequest(context)
.compose(this::invokeResponseHooks)
.map(AnalyticsTagsEnricher::enrichWithAnalyticsTags)
.map(HookDebugInfoEnricher::enrichWithHooksDebugInfo)
.map(this::updateHooksMetrics);
.map(HookDebugInfoEnricher::enrichWithHooksDebugInfo);
}

private Future<AuctionContext> processAuctionRequest(AuctionContext context) {
Expand Down Expand Up @@ -1378,58 +1369,4 @@ private static MetricName bidderErrorTypeToMetric(BidderError.Type errorType) {
case rejected_ipf, generic -> MetricName.unknown_error;
};
}

private AuctionContext updateHooksMetrics(AuctionContext context) {
final EnumMap<Stage, List<StageExecutionOutcome>> stageOutcomes =
context.getHookExecutionContext().getStageOutcomes();

final Account account = context.getAccount();

stageOutcomes.forEach((stage, outcomes) -> updateHooksStageMetrics(account, stage, outcomes));

// account might be null if request is rejected by the entrypoint hook
if (account != null) {
stageOutcomes.values().stream()
.flatMap(Collection::stream)
.map(StageExecutionOutcome::getGroups)
.flatMap(Collection::stream)
.map(GroupExecutionOutcome::getHooks)
.flatMap(Collection::stream)
.filter(hookOutcome -> hookOutcome.getAction() != ExecutionAction.no_invocation)
.collect(Collectors.groupingBy(
outcome -> outcome.getHookId().getModuleCode(),
Collectors.summingLong(HookExecutionOutcome::getExecutionTime)))
.forEach((moduleCode, executionTime) ->
metrics.updateAccountModuleDurationMetric(account, moduleCode, executionTime));
}

return context;
}

private void updateHooksStageMetrics(Account account, Stage stage, List<StageExecutionOutcome> stageOutcomes) {
stageOutcomes.stream()
.flatMap(stageOutcome -> stageOutcome.getGroups().stream())
.flatMap(groupOutcome -> groupOutcome.getHooks().stream())
.forEach(hookOutcome -> updateHookInvocationMetrics(account, stage, hookOutcome));
}

private void updateHookInvocationMetrics(Account account, Stage stage, HookExecutionOutcome hookOutcome) {
final HookId hookId = hookOutcome.getHookId();
final ExecutionStatus status = hookOutcome.getStatus();
final ExecutionAction action = hookOutcome.getAction();
final String moduleCode = hookId.getModuleCode();

metrics.updateHooksMetrics(
moduleCode,
stage,
hookId.getHookImplCode(),
status,
hookOutcome.getExecutionTime(),
action);

// account might be null if request is rejected by the entrypoint hook
if (account != null) {
metrics.updateAccountHooksMetrics(account, moduleCode, status, action);
}
}
}
81 changes: 81 additions & 0 deletions src/main/java/org/prebid/server/auction/HooksMetricsService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.prebid.server.auction;

import org.prebid.server.auction.model.AuctionContext;
import org.prebid.server.hooks.execution.model.ExecutionAction;
import org.prebid.server.hooks.execution.model.ExecutionStatus;
import org.prebid.server.hooks.execution.model.GroupExecutionOutcome;
import org.prebid.server.hooks.execution.model.HookExecutionOutcome;
import org.prebid.server.hooks.execution.model.HookId;
import org.prebid.server.hooks.execution.model.Stage;
import org.prebid.server.hooks.execution.model.StageExecutionOutcome;
import org.prebid.server.metric.Metrics;
import org.prebid.server.settings.model.Account;

import java.util.Collection;
import java.util.EnumMap;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class HooksMetricsService {

private final Metrics metrics;

public HooksMetricsService(Metrics metrics) {
this.metrics = Objects.requireNonNull(metrics);
}

public AuctionContext updateHooksMetrics(AuctionContext context) {
final EnumMap<Stage, List<StageExecutionOutcome>> stageOutcomes =
context.getHookExecutionContext().getStageOutcomes();

final Account account = context.getAccount();

stageOutcomes.forEach((stage, outcomes) -> updateHooksStageMetrics(account, stage, outcomes));

// account might be null if request is rejected by the entrypoint hook
if (account != null) {
stageOutcomes.values().stream()
.flatMap(Collection::stream)
.map(StageExecutionOutcome::getGroups)
.flatMap(Collection::stream)
.map(GroupExecutionOutcome::getHooks)
.flatMap(Collection::stream)
.filter(hookOutcome -> hookOutcome.getAction() != ExecutionAction.no_invocation)
.collect(Collectors.groupingBy(
outcome -> outcome.getHookId().getModuleCode(),
Collectors.summingLong(HookExecutionOutcome::getExecutionTime)))
.forEach((moduleCode, executionTime) ->
metrics.updateAccountModuleDurationMetric(account, moduleCode, executionTime));
}

return context;
}

private void updateHooksStageMetrics(Account account, Stage stage, List<StageExecutionOutcome> stageOutcomes) {
stageOutcomes.stream()
.flatMap(stageOutcome -> stageOutcome.getGroups().stream())
.flatMap(groupOutcome -> groupOutcome.getHooks().stream())
.forEach(hookOutcome -> updateHookInvocationMetrics(account, stage, hookOutcome));
}

private void updateHookInvocationMetrics(Account account, Stage stage, HookExecutionOutcome hookOutcome) {
final HookId hookId = hookOutcome.getHookId();
final ExecutionStatus status = hookOutcome.getStatus();
final ExecutionAction action = hookOutcome.getAction();
final String moduleCode = hookId.getModuleCode();

metrics.updateHooksMetrics(
moduleCode,
stage,
hookId.getHookImplCode(),
status,
hookOutcome.getExecutionTime(),
action);

// account might be null if request is rejected by the entrypoint hook
if (account != null) {
metrics.updateAccountHooksMetrics(account, moduleCode, status, action);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ private static HttpRequestContext toHttpRequest(HookStageExecutionResult<Entrypo
}

return HttpRequestContext.builder()
.httpMethod(routingContext.request().method())
.absoluteUri(routingContext.request().absoluteURI())
.queryParams(stageResult.getPayload().queryParams())
.headers(stageResult.getPayload().headers())
Expand Down
Loading

0 comments on commit ae475a5

Please sign in to comment.