Skip to content

Commit

Permalink
Core: Activity Infrastructure: Add trace log (#2568)
Browse files Browse the repository at this point in the history
  • Loading branch information
CTMBNara authored Nov 14, 2023
1 parent 599d473 commit 6ef0cab
Show file tree
Hide file tree
Showing 85 changed files with 2,651 additions and 424 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import io.vertx.core.Future;
import org.prebid.server.activity.Activity;
import org.prebid.server.activity.ComponentType;
import org.prebid.server.activity.infrastructure.payload.ActivityCallPayload;
import org.prebid.server.activity.infrastructure.payload.impl.ActivityCallPayloadImpl;
import org.prebid.server.activity.infrastructure.payload.impl.BidRequestActivityCallPayload;
import org.prebid.server.activity.infrastructure.payload.ActivityInvocationPayload;
import org.prebid.server.activity.infrastructure.payload.impl.ActivityInvocationPayloadImpl;
import org.prebid.server.activity.infrastructure.payload.impl.BidRequestActivityInvocationPayload;
import org.prebid.server.auction.PrivacyEnforcementService;
import org.prebid.server.auction.model.AuctionContext;
import org.prebid.server.auction.model.BidderResponse;
Expand Down Expand Up @@ -55,11 +55,11 @@ public Future<InvocationResult<AllProcessedBidResponsesPayload>> call(
private BidRequest getBidRequest(AuctionInvocationContext auctionInvocationContext) {
final AuctionContext auctionContext = auctionInvocationContext.auctionContext();
final BidRequest bidRequest = auctionContext.getBidRequest();
final ActivityCallPayload activityCallPayload = BidRequestActivityCallPayload.of(
ActivityCallPayloadImpl.of(ComponentType.GENERAL_MODULE, ConfiantAdQualityModule.CODE),
final ActivityInvocationPayload activityInvocationPayload = BidRequestActivityInvocationPayload.of(
ActivityInvocationPayloadImpl.of(ComponentType.GENERAL_MODULE, ConfiantAdQualityModule.CODE),
bidRequest);
final boolean disallowTransmitGeo = !auctionContext.getActivityInfrastructure()
.isAllowed(Activity.TRANSMIT_GEO, activityCallPayload);
.isAllowed(Activity.TRANSMIT_GEO, activityInvocationPayload);

final User maskedUser = privacyEnforcementService
.maskUserConsideringActivityRestrictions(bidRequest.getUser(), true, disallowTransmitGeo);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.prebid.server.activity.infrastructure;

import org.prebid.server.activity.infrastructure.payload.ActivityCallPayload;
import org.prebid.server.activity.infrastructure.debug.ActivityInfrastructureDebug;
import org.prebid.server.activity.infrastructure.payload.ActivityInvocationPayload;
import org.prebid.server.activity.infrastructure.rule.Rule;

import java.util.List;
Expand All @@ -10,30 +11,32 @@ public class ActivityController {

private final boolean allowByDefault;
private final List<Rule> rules;
private final ActivityInfrastructureDebug debug;

private ActivityController(boolean allowByDefault, List<Rule> rules) {
private ActivityController(boolean allowByDefault, List<Rule> rules, ActivityInfrastructureDebug debug) {
this.allowByDefault = allowByDefault;
this.rules = Objects.requireNonNull(rules);
this.debug = Objects.requireNonNull(debug);
}

public static ActivityController of(boolean allowByDefault, List<Rule> rules) {
return new ActivityController(allowByDefault, rules);
public static ActivityController of(boolean allowByDefault, List<Rule> rules, ActivityInfrastructureDebug debug) {
return new ActivityController(allowByDefault, rules, debug);
}

public ActivityCallResult isAllowed(ActivityCallPayload activityCallPayload) {
int processedRulesCount = 0;
public boolean isAllowed(ActivityInvocationPayload activityInvocationPayload) {
debug.emitActivityInvocationDefaultResult(allowByDefault);
boolean result = allowByDefault;

for (Rule rule : rules) {
processedRulesCount++;
final Rule.Result ruleResult = rule.proceed(activityInvocationPayload);
debug.emitProcessedRule(rule, ruleResult);

final Rule.Result ruleResult = rule.proceed(activityCallPayload);
if (ruleResult != Rule.Result.ABSTAIN) {
result = ruleResult == Rule.Result.ALLOW;
break;
}
}

return ActivityCallResult.of(result, processedRulesCount);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,28 @@
package org.prebid.server.activity.infrastructure;

import org.prebid.server.activity.Activity;
import org.prebid.server.activity.ComponentType;
import org.prebid.server.activity.infrastructure.payload.ActivityCallPayload;
import org.prebid.server.metric.Metrics;
import org.prebid.server.proto.openrtb.ext.request.TraceLevel;
import org.prebid.server.activity.infrastructure.debug.ActivityInfrastructureDebug;
import org.prebid.server.activity.infrastructure.payload.ActivityInvocationPayload;
import org.prebid.server.proto.openrtb.ext.response.ExtTraceActivityInfrastructure;

import java.util.List;
import java.util.Map;
import java.util.Objects;

public class ActivityInfrastructure {

public static final boolean ALLOW_ACTIVITY_BY_DEFAULT = true;

private final String accountId;
private final Map<Activity, ActivityController> activitiesControllers;
private final TraceLevel traceLevel;
private final Metrics metrics;
private final ActivityInfrastructureDebug debug;

public ActivityInfrastructure(String accountId,
Map<Activity, ActivityController> activitiesControllers,
TraceLevel traceLevel,
Metrics metrics) {
public ActivityInfrastructure(Map<Activity, ActivityController> activitiesControllers,
ActivityInfrastructureDebug debug) {

validate(activitiesControllers);

this.accountId = accountId;
this.activitiesControllers = activitiesControllers;
this.traceLevel = Objects.requireNonNull(traceLevel);
this.metrics = Objects.requireNonNull(metrics);
this.debug = Objects.requireNonNull(debug);
}

private static void validate(Map<Activity, ActivityController> activitiesControllers) {
Expand All @@ -38,29 +32,15 @@ private static void validate(Map<Activity, ActivityController> activitiesControl
}
}

public boolean isAllowed(Activity activity, ActivityCallPayload activityCallPayload) {
final ActivityCallResult result = activitiesControllers.get(activity).isAllowed(activityCallPayload);
updateMetrics(activity, activityCallPayload, result);
return result.isAllowed();
}
public boolean isAllowed(Activity activity, ActivityInvocationPayload activityInvocationPayload) {
debug.emitActivityInvocation(activity, activityInvocationPayload);
final boolean result = activitiesControllers.get(activity).isAllowed(activityInvocationPayload);
debug.emitActivityInvocationResult(activity, activityInvocationPayload, result);

private void updateMetrics(Activity activity, ActivityCallPayload activityCallPayload, ActivityCallResult result) {
final int processedRulesCount = result.getProcessedRulesCount();
if (processedRulesCount > 0) {
metrics.updateRequestsActivityProcessedRulesCount(processedRulesCount);
if (traceLevel == TraceLevel.verbose) {
metrics.updateAccountActivityProcessedRulesCount(accountId, processedRulesCount);
}
}
return result;
}

if (!result.isAllowed()) {
metrics.updateRequestsActivityDisallowedCount(activity);
if (traceLevel == TraceLevel.verbose) {
metrics.updateAccountActivityDisallowedCount(accountId, activity);
}
if (activityCallPayload.componentType() == ComponentType.BIDDER) {
metrics.updateAdapterActivityDisallowedCount(activityCallPayload.componentName(), activity);
}
}
public List<ExtTraceActivityInfrastructure> debugTrace() {
return debug.trace();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import org.prebid.server.activity.Activity;
import org.prebid.server.activity.infrastructure.ActivityController;
import org.prebid.server.activity.infrastructure.ActivityInfrastructure;
import org.prebid.server.activity.infrastructure.debug.ActivityInfrastructureDebug;
import org.prebid.server.activity.infrastructure.privacy.PrivacyModuleQualifier;
import org.prebid.server.activity.infrastructure.rule.Rule;
import org.prebid.server.auction.gpp.model.GppContext;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.metric.MetricName;
import org.prebid.server.metric.Metrics;
import org.prebid.server.proto.openrtb.ext.request.TraceLevel;
Expand All @@ -35,21 +37,27 @@ public class ActivityInfrastructureCreator {

private final ActivityRuleFactory activityRuleFactory;
private final Metrics metrics;
private final JacksonMapper jacksonMapper;

public ActivityInfrastructureCreator(ActivityRuleFactory activityRuleFactory,
Metrics metrics,
JacksonMapper jacksonMapper) {

public ActivityInfrastructureCreator(ActivityRuleFactory activityRuleFactory, Metrics metrics) {
this.activityRuleFactory = Objects.requireNonNull(activityRuleFactory);
this.metrics = Objects.requireNonNull(metrics);
this.jacksonMapper = Objects.requireNonNull(jacksonMapper);
}

public ActivityInfrastructure create(Account account, GppContext gppContext, TraceLevel traceLevel) {
return new ActivityInfrastructure(
account.getId(),
parse(account, gppContext),
traceLevel,
metrics);
final ActivityInfrastructureDebug debug = debugWheel(account, traceLevel);
return new ActivityInfrastructure(parse(account, gppContext, debug), debug);
}

private ActivityInfrastructureDebug debugWheel(Account account, TraceLevel traceLevel) {
return new ActivityInfrastructureDebug(account.getId(), traceLevel, metrics, jacksonMapper);
}

Map<Activity, ActivityController> parse(Account account, GppContext gppContext) {
Map<Activity, ActivityController> parse(Account account, GppContext gppContext, ActivityInfrastructureDebug debug) {
final Optional<AccountPrivacyConfig> accountPrivacyConfig = Optional.ofNullable(account.getPrivacy());

final Map<Activity, AccountActivityConfiguration> activitiesConfiguration = accountPrivacyConfig
Expand All @@ -64,12 +72,16 @@ Map<Activity, ActivityController> parse(Account account, GppContext gppContext)
UnaryOperator.identity(),
takeFirstAndLogDuplicates(account.getId())));

return Arrays.stream(Activity.values())
.collect(Collectors.toMap(
UnaryOperator.identity(),
activity -> from(activity, activitiesConfiguration.get(activity), modulesConfigs, gppContext),
(oldValue, newValue) -> newValue,
enumMapFactory()));
return Arrays.stream(Activity.values()).collect(Collectors.toMap(
UnaryOperator.identity(),
activity -> from(
activity,
activitiesConfiguration.get(activity),
modulesConfigs,
gppContext,
debug),
(oldValue, newValue) -> oldValue,
enumMapFactory()));
}

private BinaryOperator<AccountPrivacyModuleConfig> takeFirstAndLogDuplicates(String accountId) {
Expand All @@ -85,10 +97,14 @@ private BinaryOperator<AccountPrivacyModuleConfig> takeFirstAndLogDuplicates(Str
private ActivityController from(Activity activity,
AccountActivityConfiguration activityConfiguration,
Map<PrivacyModuleQualifier, AccountPrivacyModuleConfig> modulesConfigs,
GppContext gppContext) {
GppContext gppContext,
ActivityInfrastructureDebug debug) {

if (activityConfiguration == null) {
return ActivityController.of(ActivityInfrastructure.ALLOW_ACTIVITY_BY_DEFAULT, Collections.emptyList());
return ActivityController.of(
ActivityInfrastructure.ALLOW_ACTIVITY_BY_DEFAULT,
Collections.emptyList(),
debug);
}

final ActivityControllerCreationContext creationContext = ActivityControllerCreationContext.of(
Expand All @@ -102,7 +118,7 @@ private ActivityController from(Activity activity,
.map(ruleConfiguration -> activityRuleFactory.from(ruleConfiguration, creationContext))
.toList();

return ActivityController.of(allow, rules);
return ActivityController.of(allow, rules, debug);
}

private static boolean allowFromConfig(Boolean configValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
import org.prebid.server.activity.Activity;
import org.prebid.server.activity.infrastructure.creator.PrivacyModuleCreationContext;
import org.prebid.server.activity.infrastructure.creator.privacy.PrivacyModuleCreator;
import org.prebid.server.activity.infrastructure.privacy.AndPrivacyModules;
import org.prebid.server.activity.infrastructure.privacy.PrivacyModule;
import org.prebid.server.activity.infrastructure.privacy.PrivacyModuleQualifier;
import org.prebid.server.activity.infrastructure.privacy.PrivacySection;
import org.prebid.server.activity.infrastructure.privacy.uscustomlogic.USCustomLogicDataSupplier;
import org.prebid.server.activity.infrastructure.privacy.uscustomlogic.USCustomLogicModule;
import org.prebid.server.activity.infrastructure.rule.AndRule;
import org.prebid.server.auction.gpp.model.GppContext;
import org.prebid.server.exception.InvalidAccountConfigException;
import org.prebid.server.json.DecodeException;
Expand Down Expand Up @@ -79,8 +79,7 @@ public PrivacyModule from(PrivacyModuleCreationContext creationContext) {
.toList()
: Collections.emptyList();

final AndRule andRule = new AndRule(innerPrivacyModules);
return andRule::proceed;
return new AndPrivacyModules(innerPrivacyModules);
}

private static AccountUSCustomLogicModuleConfig moduleConfig(PrivacyModuleCreationContext creationContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import org.prebid.server.activity.Activity;
import org.prebid.server.activity.infrastructure.creator.PrivacyModuleCreationContext;
import org.prebid.server.activity.infrastructure.creator.privacy.PrivacyModuleCreator;
import org.prebid.server.activity.infrastructure.privacy.AndPrivacyModules;
import org.prebid.server.activity.infrastructure.privacy.PrivacyModule;
import org.prebid.server.activity.infrastructure.privacy.PrivacyModuleQualifier;
import org.prebid.server.activity.infrastructure.privacy.PrivacySection;
import org.prebid.server.activity.infrastructure.privacy.usnat.USNatModule;
import org.prebid.server.activity.infrastructure.rule.AndRule;
import org.prebid.server.auction.gpp.model.GppContext;
import org.prebid.server.settings.model.activity.privacy.AccountUSNatModuleConfig;

Expand Down Expand Up @@ -46,8 +46,7 @@ public PrivacyModule from(PrivacyModuleCreationContext creationContext) {
.map(sectionId -> forSection(creationContext.getActivity(), sectionId, scope.getGppModel()))
.toList();

final AndRule andRule = new AndRule(innerPrivacyModules);
return andRule::proceed;
return new AndPrivacyModules(innerPrivacyModules);
}

private static AccountUSNatModuleConfig moduleConfig(PrivacyModuleCreationContext creationContext) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.prebid.server.activity.infrastructure.debug;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.TextNode;

import java.util.List;

public class ActivityDebugUtils {

private ActivityDebugUtils() {
}

public static JsonNode asLogEntry(Object object, ObjectMapper mapper) {
return object instanceof Loggable loggable
? loggable.asLogEntry(mapper)
: TextNode.valueOf(object.toString());
}

public static ArrayNode asLogEntry(List<?> objects, ObjectMapper mapper) {
final ArrayNode arrayNode = mapper.createArrayNode();
objects.stream()
.map(object -> asLogEntry(object, mapper))
.forEach(arrayNode::add);

return arrayNode;
}
}
Loading

0 comments on commit 6ef0cab

Please sign in to comment.