diff --git a/src/main/java/org/prebid/server/cache/CoreCacheService.java b/src/main/java/org/prebid/server/cache/CoreCacheService.java index 863c25ee38b..908fe9e2905 100644 --- a/src/main/java/org/prebid/server/cache/CoreCacheService.java +++ b/src/main/java/org/prebid/server/cache/CoreCacheService.java @@ -9,6 +9,7 @@ import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.http.client.utils.URIBuilder; import org.prebid.server.auction.model.AuctionContext; import org.prebid.server.auction.model.BidInfo; import org.prebid.server.auction.model.CachedDebugLog; @@ -22,6 +23,7 @@ import org.prebid.server.cache.model.DebugHttpCall; import org.prebid.server.cache.proto.request.bid.BidCacheRequest; import org.prebid.server.cache.proto.request.bid.BidPutObject; +import org.prebid.server.cache.proto.response.CacheErrorResponse; import org.prebid.server.cache.proto.response.bid.BidCacheResponse; import org.prebid.server.cache.proto.response.bid.CacheObject; import org.prebid.server.cache.utils.CacheServiceUtil; @@ -45,6 +47,7 @@ import org.prebid.server.vertx.httpclient.HttpClient; import org.prebid.server.vertx.httpclient.model.HttpClientResponse; +import java.net.URISyntaxException; import java.net.URL; import java.time.Clock; import java.util.ArrayList; @@ -55,6 +58,7 @@ import java.util.Objects; import java.util.Set; import java.util.concurrent.TimeoutException; +import java.util.function.BiConsumer; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -66,6 +70,8 @@ public class CoreCacheService { private static final String BID_WURL_ATTRIBUTE = "wurl"; private static final String TRACE_INFO_SEPARATOR = "-"; private static final int MAX_DATACENTER_REGION_LENGTH = 4; + private static final String UUID_QUERY_PARAMETER = "uuid"; + private static final String CH_QUERY_PARAMETER = "ch"; private final HttpClient httpClient; private final URL externalEndpointUrl; @@ -186,18 +192,27 @@ private Future makeRequest(BidCacheRequest bidCacheRequest, cacheHeaders, mapper.encodeToString(bidCacheRequest), remainingTimeout) - .map(response -> toBidCacheResponse( + .map(response -> processVtrackWriteCacheResponse( response.getStatusCode(), response.getBody(), bidCount, accountId, startTime)) - .recover(exception -> failResponse(exception, accountId, startTime)); + .recover(exception -> failVtrackCacheWriteResponse(exception, accountId, startTime)); } - private Future failResponse(Throwable exception, String accountId, long startTime) { - metrics.updateCacheRequestFailedTime(accountId, clock.millis() - startTime); + private BidCacheResponse processVtrackWriteCacheResponse(int statusCode, + String responseBody, + int bidCount, + String accountId, + long startTime) { - logger.warn("Error occurred while interacting with cache service: {}", exception.getMessage()); - logger.debug("Error occurred while interacting with cache service", exception); + final BidCacheResponse bidCacheResponse = toBidCacheResponse(statusCode, responseBody, bidCount); + metrics.updateVtrackCacheWriteRequestTime(accountId, clock.millis() - startTime, MetricName.ok); + return bidCacheResponse; + } - return Future.failedFuture(exception); + private Future failVtrackCacheWriteResponse(Throwable exception, String accountId, long startTime) { + if (exception instanceof PreBidException) { + metrics.updateVtrackCacheWriteRequestTime(accountId, clock.millis() - startTime, MetricName.err); + } + return failResponse(exception); } public Future cachePutObjects(List bidPutObjects, @@ -210,7 +225,10 @@ public Future cachePutObjects(List bidPutObjects final List cachedCreatives = updatePutObjects(bidPutObjects, isEventsEnabled, biddersAllowingVastUpdate, accountId, integration); - updateCreativeMetrics(accountId, cachedCreatives); + updateCreativeMetrics( + cachedCreatives, + (ttl, type) -> metrics.updateVtrackCacheCreativeTtl(accountId, ttl, type), + (size, type) -> metrics.updateVtrackCacheCreativeSize(accountId, size, type)); return makeRequest(toBidCacheRequest(cachedCreatives), cachedCreatives.size(), timeout, accountId); } @@ -309,7 +327,10 @@ private Future doCacheOpenrtb(List bids, final BidCacheRequest bidCacheRequest = toBidCacheRequest(cachedCreatives); - updateCreativeMetrics(accountId, cachedCreatives); + updateCreativeMetrics( + cachedCreatives, + (ttl, type) -> metrics.updateCacheCreativeTtl(accountId, ttl, type), + (size, type) -> metrics.updateCacheCreativeSize(accountId, size, type)); final String url = ObjectUtils.firstNonNull(internalEndpointUrl, externalEndpointUrl).toString(); final String body = mapper.encodeToString(bidCacheRequest); @@ -343,8 +364,8 @@ private CacheServiceResult processResponseOpenrtb(HttpClientResponse response, externalEndpointUrl.toString(), httpRequest, httpResponse, startTime); final BidCacheResponse bidCacheResponse; try { - bidCacheResponse = toBidCacheResponse( - responseStatusCode, response.getBody(), bidCount, accountId, startTime); + bidCacheResponse = toBidCacheResponse(responseStatusCode, response.getBody(), bidCount); + metrics.updateAuctionCacheRequestTime(accountId, clock.millis() - startTime, MetricName.ok); } catch (PreBidException e) { return CacheServiceResult.of(httpCall, e, Collections.emptyMap()); } @@ -361,7 +382,7 @@ private CacheServiceResult failResponseOpenrtb(Throwable exception, logger.warn("Error occurred while interacting with cache service: {}", exception.getMessage()); logger.debug("Error occurred while interacting with cache service", exception); - metrics.updateCacheRequestFailedTime(accountId, clock.millis() - startTime); + metrics.updateAuctionCacheRequestTime(accountId, clock.millis() - startTime, MetricName.err); final DebugHttpCall httpCall = makeDebugHttpCall(externalEndpointUrl.toString(), request, null, startTime); return CacheServiceResult.of(httpCall, exception, Collections.emptyMap()); @@ -460,9 +481,7 @@ private String generateWinUrl(String bidId, private BidCacheResponse toBidCacheResponse(int statusCode, String responseBody, - int bidCount, - String accountId, - long startTime) { + int bidCount) { if (statusCode != 200) { throw new PreBidException("HTTP status code " + statusCode); @@ -480,7 +499,6 @@ private BidCacheResponse toBidCacheResponse(int statusCode, throw new PreBidException("The number of response cache objects doesn't match with bids"); } - metrics.updateCacheRequestSuccessTime(accountId, clock.millis() - startTime); return bidCacheResponse; } @@ -538,17 +556,20 @@ private static String resolveVideoBidUuid(String uuid, String hbCacheId) { return hbCacheId != null && uuid.endsWith(hbCacheId) ? hbCacheId : uuid; } - private void updateCreativeMetrics(String accountId, List cachedCreatives) { + private void updateCreativeMetrics(List cachedCreatives, + BiConsumer updateCreativeTtlMetric, + BiConsumer updateCreativeSiseMetric) { + for (CachedCreative cachedCreative : cachedCreatives) { final BidPutObject payload = cachedCreative.getPayload(); final MetricName creativeType = resolveCreativeTypeName(payload); final Integer creativeTtl = ObjectUtils.defaultIfNull(payload.getTtlseconds(), payload.getExpiry()); if (creativeTtl != null) { - metrics.updateCacheCreativeTtl(accountId, creativeTtl, creativeType); + updateCreativeTtlMetric.accept(creativeTtl, creativeType); } - metrics.updateCacheCreativeSize(accountId, cachedCreative.getSize(), creativeType); + updateCreativeSiseMetric.accept(cachedCreative.getSize(), creativeType); } } @@ -627,4 +648,55 @@ private static String normalizeDatacenterRegion(String datacenterRegion) { ? trimmedDatacenterRegion.substring(0, MAX_DATACENTER_REGION_LENGTH) : trimmedDatacenterRegion; } + + public Future getCachedObject(String key, String ch, Timeout timeout) { + final long remainingTimeout = timeout.remaining(); + if (remainingTimeout <= 0) { + return Future.failedFuture(new TimeoutException("Timeout has been exceeded")); + } + + final URL endpointUrl = ObjectUtils.firstNonNull(internalEndpointUrl, externalEndpointUrl); + final String url; + try { + final URIBuilder uriBuilder = new URIBuilder(endpointUrl.toString()); + uriBuilder.addParameter(UUID_QUERY_PARAMETER, key); + if (StringUtils.isNotBlank(ch)) { + uriBuilder.addParameter(CH_QUERY_PARAMETER, ch); + } + url = uriBuilder.build().toString(); + } catch (URISyntaxException e) { + return Future.failedFuture(new IllegalArgumentException("Configured cache url is malformed", e)); + } + + final long startTime = clock.millis(); + return httpClient.get(url, cacheHeaders, remainingTimeout) + .map(response -> processVtrackReadResponse(response, startTime)) + .recover(CoreCacheService::failResponse); + } + + private HttpClientResponse processVtrackReadResponse(HttpClientResponse response, long startTime) { + final int statusCode = response.getStatusCode(); + final String body = response.getBody(); + + if (statusCode == 200) { + metrics.updateVtrackCacheReadRequestTime(clock.millis() - startTime, MetricName.ok); + return response; + } + + metrics.updateVtrackCacheReadRequestTime(clock.millis() - startTime, MetricName.err); + + try { + final CacheErrorResponse errorResponse = mapper.decodeValue(body, CacheErrorResponse.class); + return HttpClientResponse.of(statusCode, response.getHeaders(), errorResponse.getMessage()); + } catch (DecodeException e) { + throw new PreBidException("Cannot parse response: " + body, e); + } + } + + private static Future failResponse(Throwable exception) { + logger.warn("Error occurred while interacting with cache service: {}", exception.getMessage()); + logger.debug("Error occurred while interacting with cache service", exception); + + return Future.failedFuture(exception); + } } diff --git a/src/main/java/org/prebid/server/cache/proto/response/CacheErrorResponse.java b/src/main/java/org/prebid/server/cache/proto/response/CacheErrorResponse.java new file mode 100644 index 00000000000..cf535ac1181 --- /dev/null +++ b/src/main/java/org/prebid/server/cache/proto/response/CacheErrorResponse.java @@ -0,0 +1,19 @@ +package org.prebid.server.cache.proto.response; + +import lombok.Builder; +import lombok.Value; + +@Value +@Builder +public class CacheErrorResponse { + + String error; + + Integer status; + + String path; + + String message; + + Long timestamp; +} diff --git a/src/main/java/org/prebid/server/handler/GetVtrackHandler.java b/src/main/java/org/prebid/server/handler/GetVtrackHandler.java new file mode 100644 index 00000000000..e70e26627fc --- /dev/null +++ b/src/main/java/org/prebid/server/handler/GetVtrackHandler.java @@ -0,0 +1,108 @@ +package org.prebid.server.handler; + +import io.netty.handler.codec.http.HttpHeaderValues; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.vertx.core.AsyncResult; +import io.vertx.core.MultiMap; +import io.vertx.core.http.HttpMethod; +import io.vertx.ext.web.RoutingContext; +import org.apache.commons.lang3.StringUtils; +import org.prebid.server.cache.CoreCacheService; +import org.prebid.server.execution.timeout.Timeout; +import org.prebid.server.execution.timeout.TimeoutFactory; +import org.prebid.server.log.Logger; +import org.prebid.server.log.LoggerFactory; +import org.prebid.server.model.Endpoint; +import org.prebid.server.util.HttpUtil; +import org.prebid.server.vertx.httpclient.model.HttpClientResponse; +import org.prebid.server.vertx.verticles.server.HttpEndpoint; +import org.prebid.server.vertx.verticles.server.application.ApplicationResource; + +import java.util.Collections; +import java.util.List; +import java.util.Objects; + +public class GetVtrackHandler implements ApplicationResource { + + private static final Logger logger = LoggerFactory.getLogger(GetVtrackHandler.class); + + private static final String UUID_PARAMETER = "uuid"; + private static final String CH_PARAMETER = "ch"; + + private final long defaultTimeout; + private final CoreCacheService coreCacheService; + private final TimeoutFactory timeoutFactory; + + public GetVtrackHandler(long defaultTimeout, CoreCacheService coreCacheService, TimeoutFactory timeoutFactory) { + this.defaultTimeout = defaultTimeout; + this.coreCacheService = Objects.requireNonNull(coreCacheService); + this.timeoutFactory = Objects.requireNonNull(timeoutFactory); + } + + @Override + public List endpoints() { + return Collections.singletonList(HttpEndpoint.of(HttpMethod.GET, Endpoint.vtrack.value())); + } + + @Override + public void handle(RoutingContext routingContext) { + final String uuid = routingContext.request().getParam(UUID_PARAMETER); + final String ch = routingContext.request().getParam(CH_PARAMETER); + if (StringUtils.isBlank(uuid)) { + respondWith( + routingContext, + HttpResponseStatus.BAD_REQUEST, + "'%s' is a required query parameter and can't be empty".formatted(UUID_PARAMETER)); + return; + } + + final Timeout timeout = timeoutFactory.create(defaultTimeout); + + coreCacheService.getCachedObject(uuid, ch, timeout) + .onComplete(asyncCache -> handleCacheResult(asyncCache, routingContext)); + } + + private static void respondWithServerError(RoutingContext routingContext, Throwable exception) { + logger.error("Error occurred while sending request to cache", exception); + respondWith(routingContext, HttpResponseStatus.INTERNAL_SERVER_ERROR, + "%s: %s".formatted("Error occurred while sending request to cache", exception.getMessage())); + } + + private static void respondWith(RoutingContext routingContext, + HttpResponseStatus status, + MultiMap headers, + String body) { + + HttpUtil.executeSafely( + routingContext, + Endpoint.vtrack, + response -> { + headers.forEach(response::putHeader); + response.setStatusCode(status.code()) .end(body); + }); + } + + private static void respondWith(RoutingContext routingContext, HttpResponseStatus status, String body) { + HttpUtil.executeSafely( + routingContext, + Endpoint.vtrack, + response -> response + .putHeader(HttpUtil.CONTENT_TYPE_HEADER, HttpHeaderValues.APPLICATION_JSON) + .setStatusCode(status.code()) + .end(body)); + } + + private void handleCacheResult(AsyncResult async, RoutingContext routingContext) { + if (async.failed()) { + respondWithServerError(routingContext, async.cause()); + } else { + final HttpClientResponse response = async.result(); + final HttpResponseStatus status = HttpResponseStatus.valueOf(response.getStatusCode()); + if (status == HttpResponseStatus.OK) { + respondWith(routingContext, status, response.getHeaders(), response.getBody()); + } else { + respondWith(routingContext, status, response.getBody()); + } + } + } +} diff --git a/src/main/java/org/prebid/server/handler/VtrackHandler.java b/src/main/java/org/prebid/server/handler/PostVtrackHandler.java similarity index 94% rename from src/main/java/org/prebid/server/handler/VtrackHandler.java rename to src/main/java/org/prebid/server/handler/PostVtrackHandler.java index f8b67b50d8c..194d8a4f3fb 100644 --- a/src/main/java/org/prebid/server/handler/VtrackHandler.java +++ b/src/main/java/org/prebid/server/handler/PostVtrackHandler.java @@ -39,9 +39,9 @@ import java.util.Set; import java.util.stream.Collectors; -public class VtrackHandler implements ApplicationResource { +public class PostVtrackHandler implements ApplicationResource { - private static final Logger logger = LoggerFactory.getLogger(VtrackHandler.class); + private static final Logger logger = LoggerFactory.getLogger(PostVtrackHandler.class); private static final String ACCOUNT_PARAMETER = "a"; private static final String INTEGRATION_PARAMETER = "int"; @@ -56,14 +56,14 @@ public class VtrackHandler implements ApplicationResource { private final TimeoutFactory timeoutFactory; private final JacksonMapper mapper; - public VtrackHandler(long defaultTimeout, - boolean allowUnknownBidder, - boolean modifyVastForUnknownBidder, - ApplicationSettings applicationSettings, - BidderCatalog bidderCatalog, - CoreCacheService coreCacheService, - TimeoutFactory timeoutFactory, - JacksonMapper mapper) { + public PostVtrackHandler(long defaultTimeout, + boolean allowUnknownBidder, + boolean modifyVastForUnknownBidder, + ApplicationSettings applicationSettings, + BidderCatalog bidderCatalog, + CoreCacheService coreCacheService, + TimeoutFactory timeoutFactory, + JacksonMapper mapper) { this.defaultTimeout = defaultTimeout; this.allowUnknownBidder = allowUnknownBidder; diff --git a/src/main/java/org/prebid/server/metric/CacheMetrics.java b/src/main/java/org/prebid/server/metric/CacheMetrics.java index 5116e10e08f..4838c0848f6 100644 --- a/src/main/java/org/prebid/server/metric/CacheMetrics.java +++ b/src/main/java/org/prebid/server/metric/CacheMetrics.java @@ -13,6 +13,7 @@ class CacheMetrics extends UpdatableMetrics { private final RequestMetrics requestsMetrics; private final CacheCreativeSizeMetrics cacheCreativeSizeMetrics; private final CacheCreativeTtlMetrics cacheCreativeTtlMetrics; + private final CacheVtrackMetrics cacheVtrackMetrics; CacheMetrics(MetricRegistry metricRegistry, CounterType counterType) { super( @@ -23,6 +24,7 @@ class CacheMetrics extends UpdatableMetrics { requestsMetrics = new RequestMetrics(metricRegistry, counterType, createPrefix()); cacheCreativeSizeMetrics = new CacheCreativeSizeMetrics(metricRegistry, counterType, createPrefix()); cacheCreativeTtlMetrics = new CacheCreativeTtlMetrics(metricRegistry, counterType, createPrefix()); + cacheVtrackMetrics = new CacheVtrackMetrics(metricRegistry, counterType, createPrefix()); } CacheMetrics(MetricRegistry metricRegistry, CounterType counterType, String prefix) { @@ -34,6 +36,7 @@ class CacheMetrics extends UpdatableMetrics { requestsMetrics = new RequestMetrics(metricRegistry, counterType, createPrefix(prefix)); cacheCreativeSizeMetrics = new CacheCreativeSizeMetrics(metricRegistry, counterType, createPrefix(prefix)); cacheCreativeTtlMetrics = new CacheCreativeTtlMetrics(metricRegistry, counterType, createPrefix(prefix)); + cacheVtrackMetrics = new CacheVtrackMetrics(metricRegistry, counterType, createPrefix(prefix)); } private static String createPrefix(String prefix) { @@ -59,4 +62,8 @@ CacheCreativeSizeMetrics creativeSize() { CacheCreativeTtlMetrics creativeTtl() { return cacheCreativeTtlMetrics; } + + CacheVtrackMetrics vtrack() { + return cacheVtrackMetrics; + } } diff --git a/src/main/java/org/prebid/server/metric/CacheReadMetrics.java b/src/main/java/org/prebid/server/metric/CacheReadMetrics.java new file mode 100644 index 00000000000..356d96fb9d1 --- /dev/null +++ b/src/main/java/org/prebid/server/metric/CacheReadMetrics.java @@ -0,0 +1,20 @@ +package org.prebid.server.metric; + +import com.codahale.metrics.MetricRegistry; + +import java.util.Objects; +import java.util.function.Function; + +public class CacheReadMetrics extends UpdatableMetrics { + + CacheReadMetrics(MetricRegistry metricRegistry, CounterType counterType, String prefix) { + super(Objects.requireNonNull(metricRegistry), + Objects.requireNonNull(counterType), + nameCreator(Objects.requireNonNull(prefix))); + } + + private static Function nameCreator(String prefix) { + return metricName -> "%s.read.%s".formatted(prefix, metricName); + } + +} diff --git a/src/main/java/org/prebid/server/metric/CacheVtrackMetrics.java b/src/main/java/org/prebid/server/metric/CacheVtrackMetrics.java new file mode 100644 index 00000000000..ba1644fa136 --- /dev/null +++ b/src/main/java/org/prebid/server/metric/CacheVtrackMetrics.java @@ -0,0 +1,51 @@ +package org.prebid.server.metric; + +import com.codahale.metrics.MetricRegistry; + +import java.util.Objects; +import java.util.function.Function; + +class CacheVtrackMetrics extends UpdatableMetrics { + + private final CacheReadMetrics readMetrics; + private final CacheWriteMetrics writeMetrics; + private final CacheCreativeSizeMetrics creativeSizeMetrics; + private final CacheCreativeTtlMetrics creativeTtlMetrics; + + CacheVtrackMetrics(MetricRegistry metricRegistry, CounterType counterType, String prefix) { + super( + Objects.requireNonNull(metricRegistry), + Objects.requireNonNull(counterType), + nameCreator(createPrefix(Objects.requireNonNull(prefix)))); + + readMetrics = new CacheReadMetrics(metricRegistry, counterType, createPrefix(prefix)); + writeMetrics = new CacheWriteMetrics(metricRegistry, counterType, createPrefix(prefix)); + creativeSizeMetrics = new CacheCreativeSizeMetrics(metricRegistry, counterType, createPrefix(prefix)); + creativeTtlMetrics = new CacheCreativeTtlMetrics(metricRegistry, counterType, createPrefix(prefix)); + } + + private static Function nameCreator(String prefix) { + return metricName -> "%s.%s".formatted(prefix, metricName); + } + + private static String createPrefix(String prefix) { + return prefix + ".vtrack"; + } + + CacheReadMetrics read() { + return readMetrics; + } + + CacheWriteMetrics write() { + return writeMetrics; + } + + CacheCreativeSizeMetrics creativeSize() { + return creativeSizeMetrics; + } + + CacheCreativeTtlMetrics creativeTtl() { + return creativeTtlMetrics; + } + +} diff --git a/src/main/java/org/prebid/server/metric/CacheWriteMetrics.java b/src/main/java/org/prebid/server/metric/CacheWriteMetrics.java new file mode 100644 index 00000000000..d0d598d6ae5 --- /dev/null +++ b/src/main/java/org/prebid/server/metric/CacheWriteMetrics.java @@ -0,0 +1,19 @@ +package org.prebid.server.metric; + +import com.codahale.metrics.MetricRegistry; + +import java.util.Objects; +import java.util.function.Function; + +public class CacheWriteMetrics extends UpdatableMetrics { + + CacheWriteMetrics(MetricRegistry metricRegistry, CounterType counterType, String prefix) { + super(Objects.requireNonNull(metricRegistry), + Objects.requireNonNull(counterType), + nameCreator(Objects.requireNonNull(prefix))); + } + + private static Function nameCreator(String prefix) { + return metricName -> "%s.write.%s".formatted(prefix, metricName); + } +} diff --git a/src/main/java/org/prebid/server/metric/Metrics.java b/src/main/java/org/prebid/server/metric/Metrics.java index c10d3301391..5c57214ee4c 100644 --- a/src/main/java/org/prebid/server/metric/Metrics.java +++ b/src/main/java/org/prebid/server/metric/Metrics.java @@ -612,14 +612,28 @@ public void updateStoredImpsMetric(boolean found) { } } - public void updateCacheRequestSuccessTime(String accountId, long timeElapsed) { - cache().requests().updateTimer(MetricName.ok, timeElapsed); - forAccount(accountId).cache().requests().updateTimer(MetricName.ok, timeElapsed); + public void updateVtrackCacheReadRequestTime(long timeElapsed, MetricName metricName) { + cache().vtrack().read().updateTimer(metricName, timeElapsed); } - public void updateCacheRequestFailedTime(String accountId, long timeElapsed) { - cache().requests().updateTimer(MetricName.err, timeElapsed); - forAccount(accountId).cache().requests().updateTimer(MetricName.err, timeElapsed); + public void updateVtrackCacheWriteRequestTime(String accountId, long timeElapsed, MetricName metricName) { + cache().vtrack().write().updateTimer(metricName, timeElapsed); + forAccount(accountId).cache().vtrack().write().updateTimer(metricName, timeElapsed); + } + + public void updateVtrackCacheCreativeSize(String accountId, int creativeSize, MetricName creativeType) { + cache().vtrack().creativeSize().updateHistogram(creativeType, creativeSize); + forAccount(accountId).cache().vtrack().creativeSize().updateHistogram(creativeType, creativeSize); + } + + public void updateVtrackCacheCreativeTtl(String accountId, Integer creativeTtl, MetricName creativeType) { + cache().vtrack().creativeTtl().updateHistogram(creativeType, creativeTtl); + forAccount(accountId).cache().vtrack().creativeTtl().updateHistogram(creativeType, creativeTtl); + } + + public void updateAuctionCacheRequestTime(String accountId, long timeElapsed, MetricName metricName) { + cache().requests().updateTimer(metricName, timeElapsed); + forAccount(accountId).cache().requests().updateTimer(metricName, timeElapsed); } public void updateCacheCreativeSize(String accountId, int creativeSize, MetricName creativeType) { @@ -627,6 +641,11 @@ public void updateCacheCreativeSize(String accountId, int creativeSize, MetricNa forAccount(accountId).cache().creativeSize().updateHistogram(creativeType, creativeSize); } + public void updateCacheCreativeTtl(String accountId, Integer creativeTtl, MetricName creativeType) { + cache().creativeTtl().updateHistogram(creativeType, creativeTtl); + forAccount(accountId).cache().creativeTtl().updateHistogram(creativeType, creativeTtl); + } + public void updateTimeoutNotificationMetric(boolean success) { if (success) { timeoutNotificationMetrics.incCounter(MetricName.ok); @@ -704,11 +723,6 @@ public void updateAccountModuleDurationMetric(Account account, String moduleCode } } - public void updateCacheCreativeTtl(String accountId, Integer creativeTtl, MetricName creativeType) { - cache().creativeTtl().updateHistogram(creativeType, creativeTtl); - forAccount(accountId).cache().creativeTtl().updateHistogram(creativeType, creativeTtl); - } - public void updateRequestsActivityDisallowedCount(Activity activity) { requests().activities().forActivity(activity).incCounter(MetricName.disallowed_count); } diff --git a/src/main/java/org/prebid/server/spring/config/server/application/ApplicationServerConfiguration.java b/src/main/java/org/prebid/server/spring/config/server/application/ApplicationServerConfiguration.java index 88f1ef32f92..c6ae167ae94 100644 --- a/src/main/java/org/prebid/server/spring/config/server/application/ApplicationServerConfiguration.java +++ b/src/main/java/org/prebid/server/spring/config/server/application/ApplicationServerConfiguration.java @@ -34,13 +34,14 @@ import org.prebid.server.handler.BidderParamHandler; import org.prebid.server.handler.CookieSyncHandler; import org.prebid.server.handler.ExceptionHandler; +import org.prebid.server.handler.GetVtrackHandler; import org.prebid.server.handler.GetuidsHandler; import org.prebid.server.handler.NoCacheHandler; import org.prebid.server.handler.NotificationEventHandler; import org.prebid.server.handler.OptoutHandler; import org.prebid.server.handler.SetuidHandler; import org.prebid.server.handler.StatusHandler; -import org.prebid.server.handler.VtrackHandler; +import org.prebid.server.handler.PostVtrackHandler; import org.prebid.server.handler.info.BidderDetailsHandler; import org.prebid.server.handler.info.BiddersHandler; import org.prebid.server.handler.info.filters.BaseOnlyBidderInfoFilterStrategy; @@ -370,7 +371,7 @@ GetuidsHandler getuidsHandler(UidsCookieService uidsCookieService, JacksonMapper } @Bean - VtrackHandler vtrackHandler( + PostVtrackHandler postVtrackHandler( @Value("${vtrack.default-timeout-ms}") int defaultTimeoutMs, @Value("${vtrack.allow-unknown-bidder}") boolean allowUnknownBidder, @Value("${vtrack.modify-vast-for-unknown-bidder}") boolean modifyVastForUnknownBidder, @@ -380,7 +381,7 @@ VtrackHandler vtrackHandler( TimeoutFactory timeoutFactory, JacksonMapper mapper) { - return new VtrackHandler( + return new PostVtrackHandler( defaultTimeoutMs, allowUnknownBidder, modifyVastForUnknownBidder, @@ -391,6 +392,14 @@ VtrackHandler vtrackHandler( mapper); } + @Bean + GetVtrackHandler getVtrackHandler(@Value("${vtrack.default-timeout-ms}") int defaultTimeoutMs, + CoreCacheService coreCacheService, + TimeoutFactory timeoutFactory) { + + return new GetVtrackHandler(defaultTimeoutMs, coreCacheService, timeoutFactory); + } + @Bean OptoutHandler optoutHandler( @Value("${external-url}") String externalUrl, diff --git a/src/test/groovy/org/prebid/server/functional/model/response/vtrack/TransferValue.groovy b/src/test/groovy/org/prebid/server/functional/model/response/vtrack/TransferValue.groovy new file mode 100644 index 00000000000..1a7f24177dd --- /dev/null +++ b/src/test/groovy/org/prebid/server/functional/model/response/vtrack/TransferValue.groovy @@ -0,0 +1,22 @@ +package org.prebid.server.functional.model.response.vtrack + +import groovy.transform.EqualsAndHashCode +import groovy.transform.ToString +import org.prebid.server.functional.util.PBSUtils + +@ToString(includeNames = true, ignoreNulls = true) +@EqualsAndHashCode +class TransferValue { + + String adm + Integer width + Integer height + + static final TransferValue getTransferValue(){ + return new TransferValue().tap { + adm = PBSUtils.randomString + width = 300 + height = 250 + } + } +} diff --git a/src/test/groovy/org/prebid/server/functional/service/PrebidServerService.groovy b/src/test/groovy/org/prebid/server/functional/service/PrebidServerService.groovy index b9c173baa54..45a1138cf71 100644 --- a/src/test/groovy/org/prebid/server/functional/service/PrebidServerService.groovy +++ b/src/test/groovy/org/prebid/server/functional/service/PrebidServerService.groovy @@ -28,6 +28,7 @@ import org.prebid.server.functional.model.response.getuids.GetuidResponse import org.prebid.server.functional.model.response.infobidders.BidderInfoResponse import org.prebid.server.functional.model.response.setuid.SetuidResponse import org.prebid.server.functional.model.response.status.StatusResponse +import org.prebid.server.functional.model.response.vtrack.TransferValue import org.prebid.server.functional.testcontainers.container.PrebidServerContainer import org.prebid.server.functional.util.ObjectMapperWrapper import org.prebid.server.functional.util.PBSUtils @@ -73,7 +74,7 @@ class PrebidServerService implements ObjectMapperWrapper { authenticationScheme.password = pbsContainer.ADMIN_ENDPOINT_PASSWORD this.pbsContainer = pbsContainer requestSpecification = new RequestSpecBuilder().setBaseUri(pbsContainer.rootUri) - .build() + .build() adminRequestSpecification = buildAndGetRequestSpecification(pbsContainer.adminRootUri, authenticationScheme) prometheusRequestSpecification = buildAndGetRequestSpecification(pbsContainer.prometheusRootUri, authenticationScheme) } @@ -198,7 +199,7 @@ class PrebidServerService implements ObjectMapperWrapper { def uidsCookieAsEncodedJson = Base64.urlEncoder.encodeToString(uidsCookieAsJson.bytes) def response = given(requestSpecification).cookie(UIDS_COOKIE_NAME, uidsCookieAsEncodedJson) - .get(GET_UIDS_ENDPOINT) + .get(GET_UIDS_ENDPOINT) checkResponseStatusCode(response) decode(response.body.asString(), GetuidResponse) @@ -206,22 +207,31 @@ class PrebidServerService implements ObjectMapperWrapper { byte[] sendEventRequest(EventRequest eventRequest, Map headers = [:]) { def response = given(requestSpecification).headers(headers) - .queryParams(toMap(eventRequest)) - .get(EVENT_ENDPOINT) + .queryParams(toMap(eventRequest)) + .get(EVENT_ENDPOINT) checkResponseStatusCode(response) response.body.asByteArray() } - PrebidCacheResponse sendVtrackRequest(VtrackRequest request, String account) { + PrebidCacheResponse sendPostVtrackRequest(VtrackRequest request, String account) { def response = given(requestSpecification).queryParam("a", account) - .body(request) - .post(VTRACK_ENDPOINT) + .body(request) + .post(VTRACK_ENDPOINT) checkResponseStatusCode(response) decode(response.body.asString(), PrebidCacheResponse) } + TransferValue sendGetVtrackRequest(Map parameters) { + def response = given(requestSpecification) + .queryParams(parameters) + .get(VTRACK_ENDPOINT) + + checkResponseStatusCode(response) + decode(response.body.asString(), TransferValue) + } + StatusResponse sendStatusRequest() { def response = given(requestSpecification).get(STATUS_ENDPOINT) @@ -267,7 +277,7 @@ class PrebidServerService implements ObjectMapperWrapper { String sendLoggingHttpInteractionRequest(HttpInteractionRequest httpInteractionRequest) { def response = given(adminRequestSpecification).queryParams(toMap(httpInteractionRequest)) - .get(HTTP_INTERACTION_ENDPOINT) + .get(HTTP_INTERACTION_ENDPOINT) checkResponseStatusCode(response) response.body().asString() @@ -296,8 +306,8 @@ class PrebidServerService implements ObjectMapperWrapper { def payload = encode(bidRequest) given(requestSpecification).headers(headers) - .body(payload) - .post(AUCTION_ENDPOINT) + .body(payload) + .post(AUCTION_ENDPOINT) } private Response postCookieSync(CookieSyncRequest cookieSyncRequest, @@ -388,7 +398,7 @@ class PrebidServerService implements ObjectMapperWrapper { throw new IllegalArgumentException("The end time of the test is less than the start time") } def formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss") - .withZone(ZoneId.from(UTC)) + .withZone(ZoneId.from(UTC)) def logs = Arrays.asList(pbsContainer.logs.split("\n")) def filteredLogs = [] @@ -441,7 +451,7 @@ class PrebidServerService implements ObjectMapperWrapper { private static RequestSpecification buildAndGetRequestSpecification(String uri, AuthenticationScheme authScheme) { new RequestSpecBuilder().setBaseUri(uri) - .setAuth(authScheme) - .build() + .setAuth(authScheme) + .build() } } diff --git a/src/test/groovy/org/prebid/server/functional/testcontainers/scaffolding/NetworkScaffolding.groovy b/src/test/groovy/org/prebid/server/functional/testcontainers/scaffolding/NetworkScaffolding.groovy index 7eea7536dfb..8ac5ad41483 100644 --- a/src/test/groovy/org/prebid/server/functional/testcontainers/scaffolding/NetworkScaffolding.groovy +++ b/src/test/groovy/org/prebid/server/functional/testcontainers/scaffolding/NetworkScaffolding.groovy @@ -37,17 +37,17 @@ abstract class NetworkScaffolding implements ObjectMapperWrapper { int getRequestCount(HttpRequest httpRequest) { mockServerClient.retrieveRecordedRequests(httpRequest) - .size() + .size() } int getRequestCount(String value) { mockServerClient.retrieveRecordedRequests(getRequest(value)) - .size() + .size() } int getRequestCount() { mockServerClient.retrieveRecordedRequests(request) - .size() + .size() } void setResponse(HttpRequest httpRequest, @@ -56,8 +56,8 @@ abstract class NetworkScaffolding implements ObjectMapperWrapper { Times times = Times.exactly(1)) { def mockResponse = encode(responseModel) mockServerClient.when(httpRequest, times) - .respond(response().withStatusCode(statusCode.code()) - .withBody(mockResponse, APPLICATION_JSON)) + .respond(response().withStatusCode(statusCode.code()) + .withBody(mockResponse, APPLICATION_JSON)) } void setResponse(String value, @@ -73,9 +73,9 @@ abstract class NetworkScaffolding implements ObjectMapperWrapper { def responseHeaders = headers.collect { new Header(it.key, it.value) } def mockResponse = encode(responseModel) mockServerClient.when(getRequest(value), Times.unlimited()) - .respond(response().withStatusCode(statusCode.code()) - .withBody(mockResponse, APPLICATION_JSON) - .withHeaders(responseHeaders)) + .respond(response().withStatusCode(statusCode.code()) + .withBody(mockResponse, APPLICATION_JSON) + .withHeaders(responseHeaders)) } void setResponse(String value, @@ -86,39 +86,39 @@ abstract class NetworkScaffolding implements ObjectMapperWrapper { def responseHeaders = headers.collect { new Header(it.key, it.value) } def mockResponse = encode(responseModel) mockServerClient.when(getRequest(value), Times.unlimited()) - .respond(response().withStatusCode(statusCode.code()) - .withBody(mockResponse, APPLICATION_JSON) - .withHeaders(responseHeaders) - .withDelay(TimeUnit.MILLISECONDS, responseDelay)) + .respond(response().withStatusCode(statusCode.code()) + .withBody(mockResponse, APPLICATION_JSON) + .withHeaders(responseHeaders) + .withDelay(TimeUnit.MILLISECONDS, responseDelay)) } void setResponse(String value, String mockResponse) { mockServerClient.when(getRequest(value), Times.exactly(1)) - .respond(response().withStatusCode(OK_200.code()) - .withBody(mockResponse, APPLICATION_JSON)) + .respond(response().withStatusCode(OK_200.code()) + .withBody(mockResponse, APPLICATION_JSON)) } void setResponse(ResponseModel responseModel) { def mockResponse = encode(responseModel) mockServerClient.when(request().withPath(endpoint)) - .respond(response().withStatusCode(OK_200.code()) - .withBody(mockResponse, APPLICATION_JSON)) + .respond(response().withStatusCode(OK_200.code()) + .withBody(mockResponse, APPLICATION_JSON)) } void setResponse(String value, HttpStatusCode httpStatusCode) { mockServerClient.when(getRequest(value), Times.exactly(1)) - .respond(response().withStatusCode(httpStatusCode.code())) + .respond(response().withStatusCode(httpStatusCode.code())) } void setResponse(String value, HttpStatusCode httpStatusCode, String errorText) { mockServerClient.when(getRequest(value), Times.exactly(1)) - .respond(response().withStatusCode(httpStatusCode.code()) - .withBody(errorText, APPLICATION_JSON)) + .respond(response().withStatusCode(httpStatusCode.code()) + .withBody(errorText, APPLICATION_JSON)) } void setResponseWithTimeout(String value, int timeoutSec = 5) { mockServerClient.when(getRequest(value), Times.exactly(1)) - .respond(response().withDelay(SECONDS, timeoutSec)) + .respond(response().withDelay(SECONDS, timeoutSec)) } protected def getRequestAndResponse() { @@ -130,14 +130,19 @@ abstract class NetworkScaffolding implements ObjectMapperWrapper { .collect { it.body.toString() } } + String getRecordedRequestsQueryParameters(HttpRequest httpRequest) { + mockServerClient.retrieveRecordedRequests(httpRequest) + .collect { it -> it.queryStringParameters.multimap.toString()} + } + List getRecordedRequestsBody(String value) { mockServerClient.retrieveRecordedRequests(getRequest(value)) - .collect { it.body.toString() } + .collect { it.body.toString() } } List getRecordedRequestsBody() { mockServerClient.retrieveRecordedRequests(request) - .collect { it.body.toString() } + .collect { it.body.toString() } } Map> getLastRecordedRequestHeaders(HttpRequest httpRequest) { diff --git a/src/test/groovy/org/prebid/server/functional/testcontainers/scaffolding/PrebidCache.groovy b/src/test/groovy/org/prebid/server/functional/testcontainers/scaffolding/PrebidCache.groovy index 224f7c8b228..66ce54a9531 100644 --- a/src/test/groovy/org/prebid/server/functional/testcontainers/scaffolding/PrebidCache.groovy +++ b/src/test/groovy/org/prebid/server/functional/testcontainers/scaffolding/PrebidCache.groovy @@ -7,12 +7,15 @@ import org.mockserver.model.HttpResponse import org.prebid.server.functional.model.mock.services.prebidcache.response.CacheObject import org.prebid.server.functional.model.mock.services.prebidcache.response.PrebidCacheResponse import org.prebid.server.functional.model.request.cache.BidCacheRequest +import org.prebid.server.functional.model.response.vtrack.TransferValue +import org.prebid.server.functional.util.PBSUtils import org.testcontainers.containers.MockServerContainer import java.util.stream.Stream import static org.mockserver.model.HttpRequest.request import static org.mockserver.model.HttpResponse.response +import static org.mockserver.model.HttpStatusCode.INTERNAL_SERVER_ERROR_500 import static org.mockserver.model.HttpStatusCode.OK_200 import static org.mockserver.model.JsonPathBody.jsonPath @@ -24,6 +27,11 @@ class PrebidCache extends NetworkScaffolding { super(mockServerContainer, CACHE_ENDPOINT) } + String getVTracGetRequestParams() { + getRecordedRequestsQueryParameters(request().withMethod("GET") + .withPath(CACHE_ENDPOINT)) + } + void setXmlCacheResponse(String payload, PrebidCacheResponse prebidCacheResponse) { setResponse(getXmlCacheRequest(payload), prebidCacheResponse) } @@ -43,8 +51,8 @@ class PrebidCache extends NetworkScaffolding { @Override protected HttpRequest getRequest(String impId) { request().withMethod("POST") - .withPath(CACHE_ENDPOINT) - .withBody(jsonPath("\$.puts[?(@.value.impid == '$impId')]")) + .withPath(CACHE_ENDPOINT) + .withBody(jsonPath("\$.puts[?(@.value.impid == '$impId')]")) } List getRecordedRequests(String impId) { @@ -59,21 +67,52 @@ class PrebidCache extends NetworkScaffolding { @Override HttpRequest getRequest() { request().withMethod("POST") - .withPath(CACHE_ENDPOINT) + .withPath(CACHE_ENDPOINT) } @Override void setResponse() { - mockServerClient.when(request().withPath(endpoint), Times.unlimited(), TimeToLive.unlimited(), -10) - .respond{request -> request.withPath(endpoint) - ? response().withStatusCode(OK_200.code()).withBody(getBodyByRequest(request)) - : HttpResponse.notFoundResponse()} + mockServerClient.when(request() + .withMethod("POST") + .withPath(endpoint), Times.unlimited(), TimeToLive.unlimited(), -10) + .respond { request -> + request.withPath(endpoint) + ? response().withStatusCode(OK_200.code()).withBody(getBodyByRequest(request)) + : HttpResponse.notFoundResponse() + } + } + + void setGetResponse(TransferValue vTrackResponse) { + mockServerClient.when(request() + .withMethod("GET") + .withPath(endpoint), Times.unlimited(), TimeToLive.unlimited(), -10) + .respond { request -> + request.withPath(endpoint) + ? response().withStatusCode(OK_200.code()).withBody(encode(vTrackResponse)) + : HttpResponse.notFoundResponse() + } + } + + void setInvalidPostResponse() { + mockServerClient.when(request() + .withMethod("POST") + .withPath(endpoint), Times.unlimited(), TimeToLive.unlimited(), -10) + .respond { response().withStatusCode(INTERNAL_SERVER_ERROR_500.code()) } + } + + void setInvalidGetResponse(String uuid, String errorMessage = PBSUtils.randomString) { + mockServerClient.when(request() + .withMethod("GET") + .withPath(endpoint) + .withQueryStringParameter("uuid", uuid), Times.unlimited(), TimeToLive.unlimited(), -10) + .respond { response().withBody(errorMessage).withStatusCode(INTERNAL_SERVER_ERROR_500.code()) } + } private static HttpRequest getXmlCacheRequest(String payload) { request().withMethod("POST") - .withPath(CACHE_ENDPOINT) - .withBody(jsonPath("\$.puts[?(@.value =~/^.*$payload.*\$/)]")) + .withPath(CACHE_ENDPOINT) + .withBody(jsonPath("\$.puts[?(@.value =~/^.*$payload.*\$/)]")) } private String getBodyByRequest(HttpRequest request) { diff --git a/src/test/groovy/org/prebid/server/functional/tests/BidderParamsSpec.groovy b/src/test/groovy/org/prebid/server/functional/tests/BidderParamsSpec.groovy index 0db60125603..495650fe6e4 100644 --- a/src/test/groovy/org/prebid/server/functional/tests/BidderParamsSpec.groovy +++ b/src/test/groovy/org/prebid/server/functional/tests/BidderParamsSpec.groovy @@ -140,7 +140,7 @@ class BidderParamsSpec extends BaseSpec { accountDao.save(account) when: "PBS processes vtrack request" - pbsService.sendVtrackRequest(request, accountId.toString()) + pbsService.sendPostVtrackRequest(request, accountId.toString()) then: "vast xml is modified" def prebidCacheRequest = prebidCache.getXmlRecordedRequestsBody(payload) @@ -172,7 +172,7 @@ class BidderParamsSpec extends BaseSpec { accountDao.save(account) when: "PBS processes vtrack request" - pbsService.sendVtrackRequest(request, accountId.toString()) + pbsService.sendPostVtrackRequest(request, accountId.toString()) then: "vast xml is not modified" def prebidCacheRequest = prebidCache.getXmlRecordedRequestsBody(payload) diff --git a/src/test/groovy/org/prebid/server/functional/tests/CacheSpec.groovy b/src/test/groovy/org/prebid/server/functional/tests/CacheSpec.groovy index b745ae53a1f..d77f9601abc 100644 --- a/src/test/groovy/org/prebid/server/functional/tests/CacheSpec.groovy +++ b/src/test/groovy/org/prebid/server/functional/tests/CacheSpec.groovy @@ -3,14 +3,11 @@ package org.prebid.server.functional.tests import org.prebid.server.functional.model.config.AccountAuctionConfig import org.prebid.server.functional.model.config.AccountCacheConfig import org.prebid.server.functional.model.config.AccountConfig -import org.prebid.server.functional.model.config.AccountEventsConfig import org.prebid.server.functional.model.db.Account import org.prebid.server.functional.model.request.auction.Asset import org.prebid.server.functional.model.request.auction.BidRequest import org.prebid.server.functional.model.request.auction.Imp import org.prebid.server.functional.model.request.auction.Targeting -import org.prebid.server.functional.model.request.vtrack.VtrackRequest -import org.prebid.server.functional.model.request.vtrack.xml.Vast import org.prebid.server.functional.model.response.auction.Adm import org.prebid.server.functional.model.response.auction.BidResponse import org.prebid.server.functional.util.PBSUtils @@ -29,15 +26,18 @@ class CacheSpec extends BaseSpec { private static final Integer DEFAULT_UUID_LENGTH = 36 private static final Integer TARGETING_PARAM_NAME_MAX_LENGTH = 20 - private static final String XML_CREATIVE_SIZE_ACCOUNT_METRIC = "account.%s.prebid_cache.creative_size.xml" - private static final String JSON_CREATIVE_SIZE_ACCOUNT_METRIC = "account.%s.prebid_cache.creative_size.json" - private static final String XML_CREATIVE_TTL_ACCOUNT_METRIC = "account.%s.prebid_cache.creative_ttl.xml" - private static final String JSON_CREATIVE_TTL_ACCOUNT_METRIC = "account.%s.prebid_cache.creative_ttl.json" - private static final String CACHE_REQUEST_OK_ACCOUNT_METRIC = "account.%s.prebid_cache.requests.ok" + private static final String ACCOUNT_JSON_CREATIVE_SIZE_METRIC = "account.%s.prebid_cache.creative_size.json" + private static final String ACCOUNT_XML_CREATIVE_SIZE_METRIC = "account.%s.prebid_cache.creative_size.xml" + private static final String ACCOUNT_XML_CREATIVE_TTL_METRIC = "account.%s.prebid_cache.creative_ttl.xml" + private static final String ACCOUNT_JSON_CREATIVE_TTL_METRIC = "account.%s.prebid_cache.creative_ttl.json" + + private static final String ACCOUNT_REQUEST_OK_METRIC = "account.%s.prebid_cache.requests.ok" + private static final String REQUEST_OK_METRIC = "prebid_cache.requests.ok" - private static final String XML_CREATIVE_SIZE_GLOBAL_METRIC = "prebid_cache.creative_size.xml" private static final String JSON_CREATIVE_SIZE_GLOBAL_METRIC = "prebid_cache.creative_size.json" - private static final String CACHE_REQUEST_OK_GLOBAL_METRIC = "prebid_cache.requests.ok" + private static final String XML_CREATIVE_SIZE_GLOBAL_METRIC = "prebid_cache.creative_size.xml" + private static final String XML_CREATIVE_TTL_METRIC = "prebid_cache.creative_ttl.xml" + private static final String JSON_CREATIVE_TTL_METRIC = "prebid_cache.creative_ttl.json" private static final String CACHE_PATH = "/${PBSUtils.randomString}".toString() private static final String CACHE_HOST = "${PBSUtils.randomString}:${PBSUtils.getRandomNumber(0, 65535)}".toString() @@ -45,39 +45,49 @@ class CacheSpec extends BaseSpec { private static final String HTTP_SCHEME = 'http' private static final String HTTPS_SCHEME = 'https' - def "PBS should update prebid_cache.creative_size.xml metric when xml creative is received"() { + def "PBS should update prebid_cache.creative_size.json metric when json creative is received"() { given: "Current value of metric prebid_cache.requests.ok" - def initialValue = getCurrentMetricValue(defaultPbsService, CACHE_REQUEST_OK_GLOBAL_METRIC) + def initialValue = getCurrentMetricValue(defaultPbsService, REQUEST_OK_METRIC) - and: "Default VtrackRequest" - def accountId = PBSUtils.randomNumber.toString() - def creative = encodeXml(Vast.getDefaultVastModel(PBSUtils.randomString)) - def request = VtrackRequest.getDefaultVtrackRequest(creative) + and: "Default BidRequest with cache, targeting" + def bidRequest = BidRequest.defaultBidRequest.tap { + it.enableCache() + } - and: "Flush metrics" - flushMetrics(defaultPbsService) + and: "Default basic bid with banner creative" + def asset = new Asset(id: PBSUtils.randomNumber) + def bidResponse = BidResponse.getDefaultBidResponse(bidRequest).tap { + seatbid[0].bid[0].adm = new Adm(assets: [asset]) + } + + and: "Set bidder response" + bidder.setResponse(bidRequest.id, bidResponse) - when: "PBS processes vtrack request" - defaultPbsService.sendVtrackRequest(request, accountId) + when: "PBS processes auction request" + defaultPbsService.sendAuctionRequest(bidRequest) - then: "prebid_cache.creative_size.xml metric should be updated" + then: "prebid_cache.creative_size.json should be update" + def adm = bidResponse.seatbid[0].bid[0].getAdm() + def creativeSize = adm.bytes.length + + and: "prebid_cache.creative_size.json metric should be updated" def metrics = defaultPbsService.sendCollectedMetricsRequest() - def creativeSize = creative.bytes.length - assert metrics[CACHE_REQUEST_OK_GLOBAL_METRIC] == initialValue + 1 - assert metrics[XML_CREATIVE_SIZE_GLOBAL_METRIC] == creativeSize + assert metrics[REQUEST_OK_METRIC] == initialValue + 1 + assert metrics[JSON_CREATIVE_SIZE_GLOBAL_METRIC] == creativeSize - and: "account..prebid_cache.creative_size.xml should be updated" - assert metrics[CACHE_REQUEST_OK_ACCOUNT_METRIC.formatted(accountId)] == 1 - assert metrics[XML_CREATIVE_SIZE_ACCOUNT_METRIC.formatted(accountId)] == creativeSize + and: "account..prebid_cache.creative_size.json should be update" + assert metrics[ACCOUNT_REQUEST_OK_METRIC.formatted(bidRequest.accountId)] == 1 + assert metrics[ACCOUNT_JSON_CREATIVE_SIZE_METRIC.formatted(bidRequest.accountId)] == creativeSize } - def "PBS should update prebid_cache.creative_size.json metric when json creative is received"() { + def "PBS should update prebid_cache.creative_size.xml metric when video bid and xml creative is received"() { given: "Current value of metric prebid_cache.requests.ok" - def initialValue = getCurrentMetricValue(defaultPbsService, CACHE_REQUEST_OK_GLOBAL_METRIC) + def initialValue = getCurrentMetricValue(defaultPbsService, REQUEST_OK_METRIC) and: "Default BidRequest with cache, targeting" - def bidRequest = BidRequest.defaultBidRequest.tap { + def bidRequest = BidRequest.defaultVideoRequest.tap { it.enableCache() + it.ext.prebid.targeting = new Targeting() } and: "Default basic bid with banner creative" @@ -98,12 +108,12 @@ class CacheSpec extends BaseSpec { and: "prebid_cache.creative_size.json metric should be updated" def metrics = defaultPbsService.sendCollectedMetricsRequest() - assert metrics[CACHE_REQUEST_OK_GLOBAL_METRIC] == initialValue + 1 - assert metrics[JSON_CREATIVE_SIZE_GLOBAL_METRIC] == creativeSize + assert metrics[REQUEST_OK_METRIC] == initialValue + 1 + assert metrics[XML_CREATIVE_SIZE_GLOBAL_METRIC] == creativeSize and: "account..prebid_cache.creative_size.json should be update" - assert metrics[CACHE_REQUEST_OK_ACCOUNT_METRIC.formatted(bidRequest.accountId)] == 1 - assert metrics[JSON_CREATIVE_SIZE_ACCOUNT_METRIC.formatted(bidRequest.accountId)] == creativeSize + assert metrics[ACCOUNT_REQUEST_OK_METRIC.formatted(bidRequest.accountId)] == 1 + assert metrics[ACCOUNT_XML_CREATIVE_SIZE_METRIC.formatted(bidRequest.accountId)] == creativeSize } def "PBS should cache bids when targeting is specified"() { @@ -204,8 +214,8 @@ class CacheSpec extends BaseSpec { and: "PBS should include metrics for request" def metrics = pbsService.sendCollectedMetricsRequest() - assert metrics[JSON_CREATIVE_TTL_ACCOUNT_METRIC.formatted(bidRequest.accountId)] == bannerHostTtl - assert metrics[CACHE_REQUEST_OK_ACCOUNT_METRIC.formatted(bidRequest.accountId)] == 1 + assert metrics[ACCOUNT_JSON_CREATIVE_TTL_METRIC.formatted(bidRequest.accountId)] == bannerHostTtl + assert metrics[ACCOUNT_REQUEST_OK_METRIC.formatted(bidRequest.accountId)] == 1 cleanup: "Stop and remove pbs container" pbsServiceFactory.removeContainer(pbsConfig) @@ -244,11 +254,15 @@ class CacheSpec extends BaseSpec { and: "PBS cache key should have length equal to default UUID" assert cacheKey.length() == DEFAULT_UUID_LENGTH - and: "PBS should include metrics for request" + and: "PBS should include metrics for account" def metrics = pbsService.sendCollectedMetricsRequest() - assert metrics[JSON_CREATIVE_TTL_ACCOUNT_METRIC.formatted(bidRequest.accountId)] == videoHostTtl - assert metrics[XML_CREATIVE_TTL_ACCOUNT_METRIC.formatted(bidRequest.accountId)] == videoHostTtl - assert metrics[CACHE_REQUEST_OK_ACCOUNT_METRIC.formatted(bidRequest.accountId)] == 1 + assert metrics[ACCOUNT_JSON_CREATIVE_TTL_METRIC.formatted(bidRequest.accountId)] == videoHostTtl + assert metrics[ACCOUNT_XML_CREATIVE_TTL_METRIC.formatted(bidRequest.accountId)] == videoHostTtl + assert metrics[ACCOUNT_REQUEST_OK_METRIC.formatted(bidRequest.accountId)] == 1 + + and: "PBS should include metrics prebid_cache_creative.{xml,json}.creative.ttl for general" + assert metrics[JSON_CREATIVE_TTL_METRIC] == videoHostTtl + assert metrics[XML_CREATIVE_TTL_METRIC] == videoHostTtl cleanup: "Stop and remove pbs container" pbsServiceFactory.removeContainer(pbsConfig) @@ -284,8 +298,8 @@ class CacheSpec extends BaseSpec { and: "PBS should include metrics for request" def metrics = pbsService.sendCollectedMetricsRequest() - assert metrics[JSON_CREATIVE_TTL_ACCOUNT_METRIC.formatted(bidRequest.accountId)] == bannerHostTtl - assert metrics[CACHE_REQUEST_OK_ACCOUNT_METRIC.formatted(bidRequest.accountId)] == 1 + assert metrics[ACCOUNT_JSON_CREATIVE_TTL_METRIC.formatted(bidRequest.accountId)] == bannerHostTtl + assert metrics[ACCOUNT_REQUEST_OK_METRIC.formatted(bidRequest.accountId)] == 1 cleanup: "Stop and remove pbs container" pbsServiceFactory.removeContainer(pbsConfig) @@ -320,8 +334,8 @@ class CacheSpec extends BaseSpec { and: "PBS should include metrics for request" def metrics = pbsService.sendCollectedMetricsRequest() - assert metrics[JSON_CREATIVE_TTL_ACCOUNT_METRIC.formatted(bidRequest.accountId)] == bannerHostTtl - assert metrics[CACHE_REQUEST_OK_ACCOUNT_METRIC.formatted(bidRequest.accountId)] == 1 + assert metrics[ACCOUNT_JSON_CREATIVE_TTL_METRIC.formatted(bidRequest.accountId)] == bannerHostTtl + assert metrics[ACCOUNT_REQUEST_OK_METRIC.formatted(bidRequest.accountId)] == 1 cleanup: "Stop and remove pbs container" pbsServiceFactory.removeContainer(pbsConfig) @@ -354,8 +368,8 @@ class CacheSpec extends BaseSpec { and: "PBS should include metrics for request" def metrics = pbsService.sendCollectedMetricsRequest() - assert metrics[JSON_CREATIVE_TTL_ACCOUNT_METRIC.formatted(bidRequest.accountId)] == bannerHostTtl - assert metrics[CACHE_REQUEST_OK_ACCOUNT_METRIC.formatted(bidRequest.accountId)] == 1 + assert metrics[ACCOUNT_JSON_CREATIVE_TTL_METRIC.formatted(bidRequest.accountId)] == bannerHostTtl + assert metrics[ACCOUNT_REQUEST_OK_METRIC.formatted(bidRequest.accountId)] == 1 cleanup: "Stop and remove pbs container" pbsServiceFactory.removeContainer(pbsConfig) @@ -470,55 +484,6 @@ class CacheSpec extends BaseSpec { true | VIDEO } - def "PBS should update prebid_cache.creative_size.xml metric and adding tracking xml when xml creative contain #wrapper and impression are valid xml value"() { - given: "Current value of metric prebid_cache.requests.ok" - def initialValue = getCurrentMetricValue(defaultPbsService, CACHE_REQUEST_OK_GLOBAL_METRIC) - - and: "Create and save enabled events config in account" - def accountId = PBSUtils.randomNumber.toString() - def account = new Account().tap { - uuid = accountId - config = new AccountConfig().tap { - auction = new AccountAuctionConfig(events: new AccountEventsConfig(enabled: true)) - } - } - accountDao.save(account) - - and: "Vtrack request with custom tags" - def payload = PBSUtils.randomString - def creative = "<${wrapper}>prebid.org wrapper" + - "<![CDATA[//${payload}]]>" + - "<${impression}> <![CDATA[ ]]> " - def request = VtrackRequest.getDefaultVtrackRequest(creative) - - and: "Flush metrics" - flushMetrics(defaultPbsService) - - when: "PBS processes vtrack request" - defaultPbsService.sendVtrackRequest(request, accountId) - - then: "Vast xml is modified" - def prebidCacheRequest = prebidCache.getXmlRecordedRequestsBody(payload) - assert prebidCacheRequest.size() == 1 - assert prebidCacheRequest[0].contains("/event?t=imp&b=${request.puts[0].bidid}&a=$accountId&bidder=${request.puts[0].bidder}") - - and: "prebid_cache.creative_size.xml metric should be updated" - def metrics = defaultPbsService.sendCollectedMetricsRequest() - assert metrics[CACHE_REQUEST_OK_GLOBAL_METRIC] == initialValue + 1 - - and: "account..prebid_cache.creative_size.xml should be updated" - assert metrics[CACHE_REQUEST_OK_ACCOUNT_METRIC.formatted(accountId) as String] == 1 - - where: - wrapper | impression - " wrapper " | " impression " - PBSUtils.getRandomCase(" wrapper ") | PBSUtils.getRandomCase(" impression ") - " wraPPer ${PBSUtils.getRandomString()} " | " imPreSSion ${PBSUtils.getRandomString()}" - " inLine " | " ImpreSSion $PBSUtils.randomNumber" - PBSUtils.getRandomCase(" inline ") | " ${PBSUtils.getRandomCase(" impression ")} $PBSUtils.randomNumber " - " inline ${PBSUtils.getRandomString()} " | " ImpreSSion " - } - def "PBS shouldn't cache bids when targeting is specified and config cache is invalid"() { given: "Pbs config with cache" def INVALID_PREBID_CACHE_CONFIG = ["cache.path" : CACHE_PATH, @@ -607,7 +572,7 @@ class CacheSpec extends BaseSpec { assert prebidCache.getRequestCount(bidRequest.imp[0].id) == 1 and: "Bid response targeting should contain value" - verifyAll (bidResponse?.seatbid[0]?.bid[0]?.ext?.prebid?.targeting as Map) { + verifyAll(bidResponse?.seatbid[0]?.bid[0]?.ext?.prebid?.targeting as Map) { it.get("hb_cache_id") it.get("hb_cache_id_generic") it.get("hb_cache_path") == CACHE_PATH @@ -643,7 +608,7 @@ class CacheSpec extends BaseSpec { assert prebidCache.getRequestCount(bidRequest.imp[0].id) == 1 and: "Bid response targeting should contain value" - verifyAll (bidResponse.seatbid[0].bid[0].ext.prebid.targeting) { + verifyAll(bidResponse.seatbid[0].bid[0].ext.prebid.targeting) { it.get("hb_cache_id") it.get("hb_cache_id_generic") it.get("hb_cache_path") == INTERNAL_CACHE_PATH @@ -662,7 +627,7 @@ class CacheSpec extends BaseSpec { def "PBS should cache bids and add targeting values when account cache config #accountAuctionConfig"() { given: "Current value of metric prebid_cache.requests.ok" - def initialValue = getCurrentMetricValue(defaultPbsService, CACHE_REQUEST_OK_GLOBAL_METRIC) + def initialValue = getCurrentMetricValue(defaultPbsService, REQUEST_OK_METRIC) and: "Default BidRequest with cache, targeting" def bidRequest = BidRequest.getDefaultVideoRequest().tap { @@ -696,8 +661,8 @@ class CacheSpec extends BaseSpec { and: "Metrics should be updated" def metrics = defaultPbsService.sendCollectedMetricsRequest() - assert metrics[CACHE_REQUEST_OK_GLOBAL_METRIC] == initialValue + 1 - assert metrics[CACHE_REQUEST_OK_ACCOUNT_METRIC.formatted(bidRequest.accountId)] == 1 + assert metrics[REQUEST_OK_METRIC] == initialValue + 1 + assert metrics[ACCOUNT_REQUEST_OK_METRIC.formatted(bidRequest.accountId)] == 1 where: accountAuctionConfig << [ @@ -709,10 +674,7 @@ class CacheSpec extends BaseSpec { } def "PBS shouldn't cache bids and add targeting values when account cache config disabled"() { - given: "Current value of metric prebid_cache.requests.ok" - def initialValue = getCurrentMetricValue(defaultPbsService, CACHE_REQUEST_OK_GLOBAL_METRIC) - - and: "Default BidRequest with cache, targeting" + given: "Default BidRequest with cache, targeting" def bidRequest = BidRequest.getDefaultVideoRequest().tap { it.enableCache() } @@ -742,47 +704,5 @@ class CacheSpec extends BaseSpec { assert !targetingKeyMap.containsKey("hb_cache_id_${GENERIC}".toString()) assert !targetingKeyMap.containsKey('hb_uuid') assert !targetingKeyMap.containsKey("hb_uuid_${GENERIC}".toString()) - - and: "Metrics shouldn't be updated" - def metrics = defaultPbsService.sendCollectedMetricsRequest() - assert metrics[CACHE_REQUEST_OK_GLOBAL_METRIC] == initialValue - assert !metrics[CACHE_REQUEST_OK_ACCOUNT_METRIC.formatted(bidRequest.accountId)] - } - - def "PBS should update prebid_cache.creative_size.xml metric when account cache config #enabledCacheConcfig"() { - given: "Current value of metric prebid_cache.requests.ok" - def okInitialValue = getCurrentMetricValue(defaultPbsService, CACHE_REQUEST_OK_GLOBAL_METRIC) - - and: "Default VtrackRequest" - def accountId = PBSUtils.randomNumber.toString() - def creative = encodeXml(Vast.getDefaultVastModel(PBSUtils.randomString)) - def request = VtrackRequest.getDefaultVtrackRequest(creative) - - and: "Create and save enabled events config in account" - def account = new Account().tap { - it.uuid = accountId - it.config = new AccountConfig().tap { - it.auction = new AccountAuctionConfig(cache: new AccountCacheConfig(enabled: enabledCacheConcfig)) - } - } - accountDao.save(account) - - and: "Flush metrics" - flushMetrics(defaultPbsService) - - when: "PBS processes vtrack request" - defaultPbsService.sendVtrackRequest(request, accountId) - - then: "prebid_cache.creative_size.xml metric should be updated" - def metrics = defaultPbsService.sendCollectedMetricsRequest() - def creativeSize = creative.bytes.length - assert metrics[CACHE_REQUEST_OK_GLOBAL_METRIC] == okInitialValue + 1 - - and: "account..prebid_cache.creative_size.xml should be updated" - assert metrics[CACHE_REQUEST_OK_ACCOUNT_METRIC.formatted(accountId)] == 1 - assert metrics[XML_CREATIVE_SIZE_ACCOUNT_METRIC.formatted(accountId)] == creativeSize - - where: - enabledCacheConcfig << [null, false, true] } } diff --git a/src/test/groovy/org/prebid/server/functional/tests/CacheVtrackSpec.groovy b/src/test/groovy/org/prebid/server/functional/tests/CacheVtrackSpec.groovy new file mode 100644 index 00000000000..e96fc519fe5 --- /dev/null +++ b/src/test/groovy/org/prebid/server/functional/tests/CacheVtrackSpec.groovy @@ -0,0 +1,388 @@ +package org.prebid.server.functional.tests + +import org.prebid.server.functional.model.config.AccountAuctionConfig +import org.prebid.server.functional.model.config.AccountCacheConfig +import org.prebid.server.functional.model.config.AccountConfig +import org.prebid.server.functional.model.config.AccountEventsConfig +import org.prebid.server.functional.model.db.Account +import org.prebid.server.functional.model.request.vtrack.VtrackRequest +import org.prebid.server.functional.model.request.vtrack.xml.Vast +import org.prebid.server.functional.model.response.vtrack.TransferValue +import org.prebid.server.functional.service.PrebidServerException +import org.prebid.server.functional.service.PrebidServerService +import org.prebid.server.functional.util.PBSUtils + +import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST +import static io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR +import static org.prebid.server.functional.testcontainers.Dependencies.getNetworkServiceContainer + +class CacheVtrackSpec extends BaseSpec { + + private static final String ACCOUNT_VTRACK_XML_CREATIVE_SIZE_METRIC = "account.%s.prebid_cache.vtrack.creative_size.xml" + private static final String ACCOUNT_VTRACK_CREATIVE_TTL_XML_METRIC = "account.%s.prebid_cache.vtrack.creative_ttl.xml" + private static final String ACCOUNT_VTRACK_WRITE_ERR_METRIC = "account.%s.prebid_cache.vtrack.write.err" + private static final String ACCOUNT_VTRACK_WRITE_OK_METRIC = "account.%s.prebid_cache.vtrack.write.ok" + + private static final String VTRACK_XML_CREATIVE_SIZE_METRIC = "prebid_cache.vtrack.creative_size.xml" + private static final String VTRACK_XML_CREATIVE_TTL_METRIC = "prebid_cache.vtrack.creative_ttl.xml" + private static final String VTRACK_WRITE_OK_METRIC = "prebid_cache.vtrack.write.ok" + private static final String VTRACK_WRITE_ERROR_METRIC = "prebid_cache.vtrack.write.err" + private static final String VTRACK_READ_OK_METRIC = "prebid_cache.vtrack.read.ok" + private static final String VTRACK_READ_ERROR_METRIC = "prebid_cache.vtrack.read.err" + + private static final String CACHE_ENDPOINT = "/cache" + private static final String CACHE_PATH = "/${PBSUtils.randomString}".toString() + private static final String CACHE_HOST = "${PBSUtils.randomString}:${PBSUtils.getRandomNumber(0, 65535)}".toString() + private static final String HTTP_SCHEME = 'http' + + private static final Map INVALID_PREBID_CACHE_CONFIG = ["cache.path" : CACHE_PATH, + "cache.scheme": HTTP_SCHEME, + "cache.host" : CACHE_HOST] + private static final Map VALID_INTERNAL_CACHE = ["cache.internal.scheme": HTTP_SCHEME, + "cache.internal.host" : "$networkServiceContainer.hostAndPort".toString(), + "cache.internal.path" : CACHE_ENDPOINT] + private static PrebidServerService pbsServiceWithInternalCache + + def setupSpec() { + pbsServiceWithInternalCache = pbsServiceFactory.getService(VALID_INTERNAL_CACHE + INVALID_PREBID_CACHE_CONFIG) + } + + def cleanupSpec() { + pbsServiceFactory.removeContainer(VALID_INTERNAL_CACHE + INVALID_PREBID_CACHE_CONFIG) + } + + void cleanup() { + prebidCache.reset() + } + + def "PBS should return 400 status code when get vtrack request without uuid"() { + when: "PBS processes get vtrack request" + defaultPbsService.sendGetVtrackRequest(["uuid": null]) + + then: "Request should fail with an error" + def exception = thrown(PrebidServerException) + assert exception.statusCode == BAD_REQUEST.code() + assert exception.responseBody == "'uuid' is a required query parameter and can't be empty" + } + + def "PBS should return 200 status code when get vtrack request contain uuid"() { + given: "Clean up and set up successful response" + def responseBody = TransferValue.getTransferValue() + prebidCache.setGetResponse(responseBody) + + when: "PBS processes get vtrack request" + def response = defaultPbsService.sendGetVtrackRequest(["uuid": UUID.randomUUID().toString()]) + + then: "Response should contain response from pbc" + assert response == responseBody + + then: "Metrics should contain ok metric" + def metricsRequest = defaultPbsService.sendCollectedMetricsRequest() + assert metricsRequest[VTRACK_READ_OK_METRIC] == 1 + } + + def "PBS should return status code that came from pbc when get vtrack request and response from pbc invalid"() { + given: "Random uuid" + def uuid = UUID.randomUUID().toString() + + and: "Cache set up invalid response" + def randomErrorMessage = PBSUtils.randomString + prebidCache.setInvalidGetResponse(uuid, randomErrorMessage) + + when: "PBS processes get vtrack request" + defaultPbsService.sendGetVtrackRequest(["uuid": uuid]) + + then: "Request should fail with an error" + def exception = thrown(PrebidServerException) + assert exception.statusCode == INTERNAL_SERVER_ERROR.code() + assert exception.responseBody == "Error occurred while sending request to cache: Cannot parse response: $randomErrorMessage" + + and: "Metrics should contain error metric" + def metricsRequest = defaultPbsService.sendCollectedMetricsRequest() + assert metricsRequest[VTRACK_READ_ERROR_METRIC] == 1 + } + + def "PBS should return 200 status code and body when get vtrack request with uuid and ch"() { + given: "Current value of metric prebid_cache.vtrack.read.ok" + def initialValue = getCurrentMetricValue(defaultPbsService, VTRACK_READ_OK_METRIC) + + and: "Random uuid and cache host" + def uuid = UUID.randomUUID().toString() + def cacheHost = PBSUtils.randomString + + and: "Set up response body" + def responseBody = TransferValue.getTransferValue() + prebidCache.setGetResponse(responseBody) + + when: "PBS processes get vtrack request" + def response = defaultPbsService.sendGetVtrackRequest(["uuid": uuid, "ch": cacheHost]) + + then: "Response should contain response from pbc" + assert response == responseBody + + and: "Metrics should contain ok metrics" + def metricsRequest = defaultPbsService.sendCollectedMetricsRequest() + assert metricsRequest[VTRACK_READ_OK_METRIC] == initialValue + 1 + } + + def "PBS should return 200 status code and body when internal cache configured and get vtrack request with uuid and ch"() { + given: "Current value of metric prebid_cache.vtrack.read.ok" + def initialValue = getCurrentMetricValue(pbsServiceWithInternalCache, VTRACK_READ_OK_METRIC) + + and: "Flush metric" + flushMetrics(pbsServiceWithInternalCache) + + and: "Random uuid and cache host" + def uuid = UUID.randomUUID().toString() + def cacheHost = PBSUtils.randomString + + and: "Mock set up successful response" + def responseBody = TransferValue.getTransferValue() + prebidCache.setGetResponse(responseBody) + + when: "PBS processes get vtrack request" + def response = pbsServiceWithInternalCache.sendGetVtrackRequest(["uuid": uuid, "ch": cacheHost]) + + then: "Response should contain response from pbc" + assert response == responseBody + + and: "Metrics should contain ok metrics" + def metricsRequest = pbsServiceWithInternalCache.sendCollectedMetricsRequest() + assert metricsRequest[VTRACK_READ_OK_METRIC] == initialValue + 1 + + and: "Verify parameters that came to external cache services" + def requestParams = prebidCache.getVTracGetRequestParams() + assert requestParams == "[{ch=[$cacheHost], uuid=[$uuid]}]" + } + + def "PBS should return 200 status code when internal cache and get vtrack request contain uuid"() { + given: "Current value of metric prebid_cache.vtrack.read.ok" + def initialValue = getCurrentMetricValue(pbsServiceWithInternalCache, VTRACK_READ_OK_METRIC) + + and: "Random uuid" + def uuid = UUID.randomUUID().toString() + + and: "Set up response body" + def responseBody = TransferValue.getTransferValue() + prebidCache.setGetResponse(responseBody) + + and: "Flush metric" + flushMetrics(pbsServiceWithInternalCache) + + when: "PBS processes get vtrack request" + def response = pbsServiceWithInternalCache.sendGetVtrackRequest(["uuid": uuid]) + + then: "Response should contain response from pbc" + assert response == responseBody + + and: "Metrics should contain ok metrics" + def metricsRequest = pbsServiceWithInternalCache.sendCollectedMetricsRequest() + assert metricsRequest[VTRACK_READ_OK_METRIC] == initialValue + 1 + + and: "Verify parameters that came to external cache services" + def requestParams = prebidCache.getVTracGetRequestParams() + assert requestParams == "[{uuid=[$uuid]}]" + } + + def "PBS should return status code that came from pbc when internal cache and get vtrack request and response from pbc invalid"() { + given: "Random uuid" + def uuid = UUID.randomUUID().toString() + + and: "Cache set up invalid response" + def randomErrorMessage = PBSUtils.randomString + prebidCache.setInvalidGetResponse(uuid, randomErrorMessage) + + and: "Flush metric" + flushMetrics(pbsServiceWithInternalCache) + + when: "PBS processes get vtrack request" + pbsServiceWithInternalCache.sendGetVtrackRequest(["uuid": uuid]) + + then: "Request should fail with an error" + def exception = thrown(PrebidServerException) + assert exception.statusCode == INTERNAL_SERVER_ERROR.code() + assert exception.responseBody == "Error occurred while sending request to cache: Cannot parse response: $randomErrorMessage" + + and: "Metrics should contain error metric" + def metricsRequest = pbsServiceWithInternalCache.sendCollectedMetricsRequest() + assert metricsRequest[VTRACK_READ_ERROR_METRIC] == 1 + + and: "Verify parameters that came to external cache services" + def requestParams = prebidCache.getVTracGetRequestParams() + assert requestParams == "[{uuid=[$uuid]}]" + } + + def "PBS should return 400 status code when internal cache and get vtrack request without uuid"() { + when: "PBS processes get vtrack request" + pbsServiceWithInternalCache.sendGetVtrackRequest(["uuid": null]) + + then: "Request should fail with an error" + def exception = thrown(PrebidServerException) + assert exception.statusCode == BAD_REQUEST.code() + assert exception.responseBody == "'uuid' is a required query parameter and can't be empty" + } + + def "PBS should update prebid_cache.creative_size.xml metric when xml creative is received"() { + given: "Current value of metric prebid_cache.requests.ok" + def initialValue = getCurrentMetricValue(defaultPbsService, VTRACK_WRITE_OK_METRIC) + + and: "Cache set up response" + prebidCache.setResponse() + + and: "Default VtrackRequest" + def accountId = PBSUtils.randomNumber.toString() + def creative = encodeXml(Vast.getDefaultVastModel(PBSUtils.randomString)) + def request = VtrackRequest.getDefaultVtrackRequest(creative) + + and: "Flush metrics" + flushMetrics(defaultPbsService) + + when: "PBS processes vtrack request" + defaultPbsService.sendPostVtrackRequest(request, accountId) + + then: "prebid_cache.vtrack.creative_size.xml metric should be updated" + def metrics = defaultPbsService.sendCollectedMetricsRequest() + def creativeSize = creative.bytes.length + assert metrics[VTRACK_WRITE_OK_METRIC] == initialValue + 1 + assert metrics[VTRACK_XML_CREATIVE_SIZE_METRIC] == creativeSize + + and: "account..prebid_cache.creative_size.xml should be updated" + assert metrics[ACCOUNT_VTRACK_WRITE_OK_METRIC.formatted(accountId)] == 1 + assert metrics[ACCOUNT_VTRACK_XML_CREATIVE_SIZE_METRIC.formatted(accountId)] == creativeSize + } + + def "PBS should update prebid_cache.creative_size.xml metric and adding tracking xml when xml creative contain #wrapper and impression are valid xml value"() { + given: "Current value of metric prebid_cache.vtrack.write.ok" + def initialOkVTrackValue = getCurrentMetricValue(defaultPbsService, VTRACK_WRITE_OK_METRIC) + + and: "Create and save enabled events config in account" + def accountId = PBSUtils.randomNumber.toString() + def account = new Account().tap { + uuid = accountId + config = new AccountConfig().tap { + auction = new AccountAuctionConfig(events: new AccountEventsConfig(enabled: true)) + } + } + accountDao.save(account) + + and: "Set up prebid cache" + prebidCache.setResponse() + + and: "Vtrack request with custom tags" + def payload = PBSUtils.randomString + def creative = "<${wrapper}>prebid.org wrapper" + + "<![CDATA[//${payload}]]>" + + "<${impression}> <![CDATA[ ]]> " + def request = VtrackRequest.getDefaultVtrackRequest(creative) + + and: "Flush metrics" + flushMetrics(defaultPbsService) + + when: "PBS processes vtrack request" + defaultPbsService.sendPostVtrackRequest(request, accountId) + + then: "Vast xml is modified" + def prebidCacheRequest = prebidCache.getXmlRecordedRequestsBody(payload) + assert prebidCacheRequest.size() == 1 + assert prebidCacheRequest[0].contains("/event?t=imp&b=${request.puts[0].bidid}&a=$accountId&bidder=${request.puts[0].bidder}") + + and: "prebid_cache.creative_size.xml metric should be updated" + def metrics = defaultPbsService.sendCollectedMetricsRequest() + def ttlSeconds = request.puts[0].ttlseconds + assert metrics[VTRACK_WRITE_OK_METRIC] == initialOkVTrackValue + 1 + assert metrics[VTRACK_XML_CREATIVE_TTL_METRIC] == ttlSeconds + + and: "account..prebid_cache.vtrack.creative_size.xml should be updated" + assert metrics[ACCOUNT_VTRACK_WRITE_OK_METRIC.formatted(accountId) as String] == 1 + assert metrics[ACCOUNT_VTRACK_CREATIVE_TTL_XML_METRIC.formatted(accountId) as String] == ttlSeconds + + where: + wrapper | impression + " wrapper " | " impression " + PBSUtils.getRandomCase(" wrapper ") | PBSUtils.getRandomCase(" impression ") + " wraPPer ${PBSUtils.getRandomString()} " | " imPreSSion ${PBSUtils.getRandomString()}" + " inLine " | " ImpreSSion $PBSUtils.randomNumber" + PBSUtils.getRandomCase(" inline ") | " ${PBSUtils.getRandomCase(" impression ")} $PBSUtils.randomNumber " + " inline ${PBSUtils.getRandomString()} " | " ImpreSSion " + } + + def "PBS should update prebid_cache.creative_size.xml metric when account cache config #enabledCacheConcfig"() { + given: "Current value of metric prebid_cache.requests.ok" + def okInitialValue = getCurrentMetricValue(defaultPbsService, VTRACK_WRITE_OK_METRIC) + + and: "Default VtrackRequest" + def accountId = PBSUtils.randomNumber.toString() + def creative = encodeXml(Vast.getDefaultVastModel(PBSUtils.randomString)) + def request = VtrackRequest.getDefaultVtrackRequest(creative) + + and: "Create and save enabled events config in account" + def account = new Account().tap { + it.uuid = accountId + it.config = new AccountConfig().tap { + it.auction = new AccountAuctionConfig(cache: new AccountCacheConfig(enabled: enabledCacheConcfig)) + } + } + accountDao.save(account) + + and: "Flush metrics" + flushMetrics(defaultPbsService) + + and: "Set up prebid cache" + prebidCache.setResponse() + + when: "PBS processes vtrack request" + defaultPbsService.sendPostVtrackRequest(request, accountId) + + then: "prebid_cache.creative_size.xml metric should be updated" + def metrics = defaultPbsService.sendCollectedMetricsRequest() + def creativeSize = creative.bytes.length + assert metrics[VTRACK_WRITE_OK_METRIC] == okInitialValue + 1 + + and: "account..prebid_cache.creative_size.xml should be updated" + assert metrics[ACCOUNT_VTRACK_WRITE_OK_METRIC.formatted(accountId)] == 1 + assert metrics[ACCOUNT_VTRACK_XML_CREATIVE_SIZE_METRIC.formatted(accountId)] == creativeSize + + where: + enabledCacheConcfig << [null, false, true] + } + + def "PBS should failed cache and update prebid_cache.vtrack.write.err metric when cache service respond with invalid status code"() { + given: "Current value of metric prebid_cache.requests.ok" + def okInitialValue = getCurrentMetricValue(defaultPbsService, VTRACK_WRITE_ERROR_METRIC) + + and: "Default VtrackRequest" + def accountId = PBSUtils.randomNumber.toString() + def creative = encodeXml(Vast.getDefaultVastModel(PBSUtils.randomString)) + def request = VtrackRequest.getDefaultVtrackRequest(creative) + + and: "Create and save enabled events config in account" + def account = new Account().tap { + it.uuid = accountId + it.config = new AccountConfig().tap { + it.auction = new AccountAuctionConfig(cache: new AccountCacheConfig(enabled: true)) + } + } + accountDao.save(account) + + and: "Flush metrics" + flushMetrics(defaultPbsService) + + and: "Reset cache and set up invalid response" + prebidCache.setInvalidPostResponse() + + when: "PBS processes vtrack request" + defaultPbsService.sendPostVtrackRequest(request, accountId) + + then: "PBS throws an exception" + def exception = thrown(PrebidServerException) + assert exception.statusCode == 500 + assert exception.responseBody.contains("Error occurred while sending request to cache: HTTP status code 500") + + then: "prebid_cache.vtrack.write.err metric should be updated" + def metrics = defaultPbsService.sendCollectedMetricsRequest() + assert metrics[VTRACK_WRITE_ERROR_METRIC] == okInitialValue + 1 + + and: "account..prebid_cache.vtrack.write.err should be updated" + assert metrics[ACCOUNT_VTRACK_WRITE_ERR_METRIC.formatted(accountId)] == 1 + } +} diff --git a/src/test/groovy/org/prebid/server/functional/tests/HttpSettingsSpec.groovy b/src/test/groovy/org/prebid/server/functional/tests/HttpSettingsSpec.groovy index 01f60ac2808..4a0229122b6 100644 --- a/src/test/groovy/org/prebid/server/functional/tests/HttpSettingsSpec.groovy +++ b/src/test/groovy/org/prebid/server/functional/tests/HttpSettingsSpec.groovy @@ -271,7 +271,7 @@ class HttpSettingsSpec extends BaseSpec { httpSettings.setResponse(accountId, httpSettingsResponse) when: "PBS processes vtrack request" - def response = prebidServerService.sendVtrackRequest(request, accountId) + def response = prebidServerService.sendPostVtrackRequest(request, accountId) then: "Response should contain uid" assert response.responses[0]?.uuid @@ -296,7 +296,7 @@ class HttpSettingsSpec extends BaseSpec { httpSettings.setRfcResponse(accountId, httpSettingsResponse) when: "PBS processes vtrack request" - def response = prebidServerServiceWithRfc.sendVtrackRequest(request, accountId) + def response = prebidServerServiceWithRfc.sendPostVtrackRequest(request, accountId) then: "Response should contain uid" assert response.responses[0]?.uuid diff --git a/src/test/groovy/org/prebid/server/functional/tests/SmokeSpec.groovy b/src/test/groovy/org/prebid/server/functional/tests/SmokeSpec.groovy index c7cceae6d01..daa27b260fa 100644 --- a/src/test/groovy/org/prebid/server/functional/tests/SmokeSpec.groovy +++ b/src/test/groovy/org/prebid/server/functional/tests/SmokeSpec.groovy @@ -98,7 +98,7 @@ class SmokeSpec extends BaseSpec { def accountId = PBSUtils.randomNumber.toString() when: "PBS processes vtrack request" - def response = defaultPbsService.sendVtrackRequest(request, accountId) + def response = defaultPbsService.sendPostVtrackRequest(request, accountId) then: "Response should contain uid" assert response.responses[0]?.uuid diff --git a/src/test/java/org/prebid/server/cache/CoreCacheServiceTest.java b/src/test/java/org/prebid/server/cache/CoreCacheServiceTest.java index 6ce3836a695..4a9fbabc15a 100644 --- a/src/test/java/org/prebid/server/cache/CoreCacheServiceTest.java +++ b/src/test/java/org/prebid/server/cache/CoreCacheServiceTest.java @@ -27,6 +27,7 @@ import org.prebid.server.cache.model.DebugHttpCall; import org.prebid.server.cache.proto.request.bid.BidCacheRequest; import org.prebid.server.cache.proto.request.bid.BidPutObject; +import org.prebid.server.cache.proto.response.CacheErrorResponse; import org.prebid.server.cache.proto.response.bid.BidCacheResponse; import org.prebid.server.cache.proto.response.bid.CacheObject; import org.prebid.server.events.EventsContext; @@ -56,6 +57,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeoutException; import java.util.function.UnaryOperator; import static java.util.Arrays.asList; @@ -233,7 +235,7 @@ public void cacheBidsOpenrtbShouldTolerateReadingHttpResponseFails() throws Json eventsContext); // then - verify(metrics).updateCacheRequestFailedTime(eq("accountId"), anyLong()); + verify(metrics).updateAuctionCacheRequestTime(eq("accountId"), anyLong(), eq(MetricName.err)); verify(httpClient).post(eq("http://cache-service/cache"), any(), any(), anyLong()); final CacheServiceResult result = future.result(); @@ -286,7 +288,7 @@ public void cacheBidsOpenrtbShouldTryCallingInternalEndpointAndTolerateReadingHt eventsContext); // then - verify(metrics).updateCacheRequestFailedTime(eq("accountId"), anyLong()); + verify(metrics).updateAuctionCacheRequestTime(eq("accountId"), anyLong(), eq(MetricName.err)); verify(httpClient).post(eq("http://cache-service-internal/cache"), any(), any(), anyLong()); final CacheServiceResult result = future.result(); @@ -507,6 +509,10 @@ public void cacheBidsOpenrtbShouldPerformHttpRequestWithExpectedBody() throws IO .auctionTimestamp(1000L) .build(); + given(httpClient.post(anyString(), any(), any(), anyLong())).willReturn(Future.succeededFuture( + HttpClientResponse.of(200, null, mapper.writeValueAsString(BidCacheResponse.of( + List.of(CacheObject.of("uuid1"), CacheObject.of("uuid2"), CacheObject.of("uuid3"))))))); + // when target.cacheBidsOpenrtb( asList(bidInfo1, bidInfo2), @@ -526,6 +532,8 @@ public void cacheBidsOpenrtbShouldPerformHttpRequestWithExpectedBody() throws IO verify(metrics).updateCacheCreativeTtl(eq("accountId"), eq(1), eq(MetricName.json)); verify(metrics).updateCacheCreativeTtl(eq("accountId"), eq(2), eq(MetricName.json)); + verify(metrics).updateAuctionCacheRequestTime(eq("accountId"), anyLong(), eq(MetricName.ok)); + final Bid bid1 = bidInfo1.getBid(); final Bid bid2 = bidInfo2.getBid(); @@ -797,7 +805,7 @@ public void cachePutObjectsShouldReturnResultWithEmptyListWhenPutObjectsIsEmpty( } @Test - public void cachePutObjectsShould() throws IOException { + public void cachePutObjectsShouldCacheObjects() throws IOException { // given final BidPutObject firstBidPutObject = BidPutObject.builder() .type("json") @@ -829,6 +837,10 @@ public void cachePutObjectsShould() throws IOException { .willReturn(new TextNode("VAST")) .willReturn(new TextNode("updatedVast")); + given(httpClient.post(anyString(), any(), any(), anyLong())).willReturn(Future.succeededFuture( + HttpClientResponse.of(200, null, mapper.writeValueAsString(BidCacheResponse.of( + List.of(CacheObject.of("uuid1"), CacheObject.of("uuid2"), CacheObject.of("uuid3"))))))); + // when target.cachePutObjects( asList(firstBidPutObject, secondBidPutObject, thirdBidPutObject), @@ -841,13 +853,15 @@ public void cachePutObjectsShould() throws IOException { // then verify(httpClient).post(eq("http://cache-service/cache"), any(), any(), anyLong()); - verify(metrics).updateCacheCreativeSize(eq("account"), eq(12), eq(MetricName.json)); - verify(metrics).updateCacheCreativeSize(eq("account"), eq(4), eq(MetricName.xml)); - verify(metrics).updateCacheCreativeSize(eq("account"), eq(11), eq(MetricName.unknown)); + verify(metrics).updateVtrackCacheCreativeSize(eq("account"), eq(12), eq(MetricName.json)); + verify(metrics).updateVtrackCacheCreativeSize(eq("account"), eq(4), eq(MetricName.xml)); + verify(metrics).updateVtrackCacheCreativeSize(eq("account"), eq(11), eq(MetricName.unknown)); - verify(metrics).updateCacheCreativeTtl(eq("account"), eq(1), eq(MetricName.json)); - verify(metrics).updateCacheCreativeTtl(eq("account"), eq(2), eq(MetricName.xml)); - verify(metrics).updateCacheCreativeTtl(eq("account"), eq(3), eq(MetricName.unknown)); + verify(metrics).updateVtrackCacheCreativeTtl(eq("account"), eq(1), eq(MetricName.json)); + verify(metrics).updateVtrackCacheCreativeTtl(eq("account"), eq(2), eq(MetricName.xml)); + verify(metrics).updateVtrackCacheCreativeTtl(eq("account"), eq(3), eq(MetricName.unknown)); + + verify(metrics).updateVtrackCacheWriteRequestTime(eq("account"), anyLong(), eq(MetricName.ok)); verify(vastModifier).modifyVastXml(true, singleton("bidder1"), firstBidPutObject, "account", "pbjs"); verify(vastModifier).modifyVastXml(true, singleton("bidder1"), secondBidPutObject, "account", "pbjs"); @@ -874,6 +888,78 @@ public void cachePutObjectsShould() throws IOException { .containsExactly(modifiedFirstBidPutObject, modifiedSecondBidPutObject, modifiedThirdBidPutObject); } + @Test + public void cachePutObjectsShouldLogErrorMetricsWhenStatusCodeIsNotOk() { + // given + final BidPutObject bidObject = BidPutObject.builder() + .type("json") + .bidid("bidId1") + .bidder("bidder1") + .timestamp(1L) + .value(new TextNode("vast")) + .ttlseconds(1) + .build(); + + given(vastModifier.modifyVastXml(any(), any(), any(), any(), anyString())) + .willReturn(new TextNode("modifiedVast")) + .willReturn(new TextNode("VAST")) + .willReturn(new TextNode("updatedVast")); + + given(httpClient.post(eq("http://cache-service/cache"), any(), any(), anyLong())) + .willReturn(Future.succeededFuture(HttpClientResponse.of(404, null, null))); + + // when + target.cachePutObjects( + singletonList(bidObject), + true, + singleton("bidder1"), + "account", + "pbjs", + timeout); + + // then + verify(metrics).updateVtrackCacheCreativeSize(eq("account"), eq(12), eq(MetricName.json)); + verify(metrics).updateVtrackCacheCreativeTtl(eq("account"), eq(1), eq(MetricName.json)); + verify(metrics).updateVtrackCacheWriteRequestTime(eq("account"), anyLong(), eq(MetricName.err)); + verify(vastModifier).modifyVastXml(true, singleton("bidder1"), bidObject, "account", "pbjs"); + } + + @Test + public void cachePutObjectsShouldNotLogErrorMetricsWhenCacheServiceIsNotConnected() { + // given + final BidPutObject bidObject = BidPutObject.builder() + .type("json") + .bidid("bidId1") + .bidder("bidder1") + .timestamp(1L) + .value(new TextNode("vast")) + .ttlseconds(1) + .build(); + + given(vastModifier.modifyVastXml(any(), any(), any(), any(), anyString())) + .willReturn(new TextNode("modifiedVast")) + .willReturn(new TextNode("VAST")) + .willReturn(new TextNode("updatedVast")); + + given(httpClient.post(eq("http://cache-service/cache"), any(), any(), anyLong())) + .willReturn(Future.failedFuture(new TimeoutException("Timeout"))); + + // when + target.cachePutObjects( + singletonList(bidObject), + true, + singleton("bidder1"), + "account", + "pbjs", + timeout); + + // then + verify(metrics, never()).updateVtrackCacheWriteRequestTime(eq("account"), anyLong(), any()); + verify(metrics).updateVtrackCacheCreativeSize(eq("account"), eq(12), eq(MetricName.json)); + verify(metrics).updateVtrackCacheCreativeTtl(eq("account"), eq(1), eq(MetricName.json)); + verify(vastModifier).modifyVastXml(true, singleton("bidder1"), bidObject, "account", "pbjs"); + } + @Test public void cachePutObjectsShouldCallInternalCacheEndpointWhenProvided() throws IOException { // given @@ -911,8 +997,8 @@ public void cachePutObjectsShouldCallInternalCacheEndpointWhenProvided() throws // then verify(httpClient).post(eq("http://cache-service-internal/cache"), any(), any(), anyLong()); - verify(metrics).updateCacheCreativeSize(eq("account"), eq(12), eq(MetricName.json)); - verify(metrics).updateCacheCreativeTtl(eq("account"), eq(1), eq(MetricName.json)); + verify(metrics).updateVtrackCacheCreativeSize(eq("account"), eq(12), eq(MetricName.json)); + verify(metrics).updateVtrackCacheCreativeTtl(eq("account"), eq(1), eq(MetricName.json)); verify(vastModifier).modifyVastXml(true, singleton("bidder1"), firstBidPutObject, "account", "pbjs"); @@ -1356,6 +1442,138 @@ public void cachePutObjectsShouldNotEmitEmptyTtlMetrics() { verify(metrics, never()).updateCacheCreativeTtl(any(), any(), any()); } + @Test + public void getCachedObjectShouldAddUuidAndChQueryParamsBeforeSendingWhenChIsPresent() { + // given + final HttpClientResponse response = HttpClientResponse.of( + 200, + MultiMap.caseInsensitiveMultiMap().add("Header", "Value"), + "body"); + + given(httpClient.get(eq("http://cache-service/cache?uuid=key&ch=ch"), any(), anyLong())) + .willReturn(Future.succeededFuture(response)); + + // when + final Future result = target.getCachedObject("key", "ch", timeout); + + // then + assertThat(result.result()).isEqualTo(response); + verify(metrics).updateVtrackCacheReadRequestTime(anyLong(), eq(MetricName.ok)); + } + + @Test + public void getCachedObjectShouldAddUuidQueryParamsBeforeSendingWhenChIsAbsent() { + // given + final HttpClientResponse response = HttpClientResponse.of( + 200, + MultiMap.caseInsensitiveMultiMap().add("Header", "Value"), + "body"); + + given(httpClient.get(eq("http://cache-service/cache?uuid=key"), any(), anyLong())) + .willReturn(Future.succeededFuture(response)); + + // when + final Future result = target.getCachedObject("key", null, timeout); + + // then + assertThat(result.result()).isEqualTo(response); + verify(metrics).updateVtrackCacheReadRequestTime(anyLong(), eq(MetricName.ok)); + } + + @Test + public void getCachedObjectShouldAddUuidQueryParamsToInternalBeforeSendingWhenChIsAbsent() + throws MalformedURLException { + + // given + target = new CoreCacheService( + httpClient, + new URL("http://cache-service/cache"), + new URL("http://internal-cache-service/cache"), + "http://cache-service-host/cache?uuid=", + 100L, + "ApiKey", + false, + true, + "apacific", + vastModifier, + eventsService, + metrics, + clock, + idGenerator, + jacksonMapper); + + final HttpClientResponse response = HttpClientResponse.of( + 200, + MultiMap.caseInsensitiveMultiMap().add("Header", "Value"), + "body"); + + given(httpClient.get(eq("http://internal-cache-service/cache?uuid=key"), any(), anyLong())) + .willReturn(Future.succeededFuture(response)); + + // when + final Future result = target.getCachedObject("key", null, timeout); + + // then + assertThat(result.result()).isEqualTo(response); + } + + @Test + public void getCachedObjectShouldNotLogErrorMetricsWhenCacheIsNotReached() { + // given + final HttpClientResponse response = HttpClientResponse.of( + 200, + MultiMap.caseInsensitiveMultiMap().add("Header", "Value"), + "body"); + + given(httpClient.get(eq("http://cache-service/cache?uuid=key&ch=ch"), any(), anyLong())) + .willReturn(Future.failedFuture(new TimeoutException("Timeout"))); + + // when + final Future result = target.getCachedObject("key", "ch", timeout); + + // then + assertThat(result.failed()).isTrue(); + verify(metrics, never()).updateVtrackCacheReadRequestTime(anyLong(), any()); + } + + @Test + public void getCachedObjectShouldHandleErrorResponse() { + // given + final HttpClientResponse response = HttpClientResponse.of( + 404, + null, + jacksonMapper.encodeToString(CacheErrorResponse.builder().message("Resource not found").build())); + + given(httpClient.get(eq("http://cache-service/cache?uuid=key&ch=ch"), any(), anyLong())) + .willReturn(Future.succeededFuture(response)); + + // when + final Future result = target.getCachedObject("key", "ch", timeout); + + // then + assertThat(result.result()).isEqualTo(HttpClientResponse.of(404, null, "Resource not found")); + verify(metrics).updateVtrackCacheReadRequestTime(anyLong(), eq(MetricName.err)); + } + + @Test + public void getCachedObjectShouldFailWhenErrorResponseCanNotBeParsed() { + // given + final HttpClientResponse response = HttpClientResponse.of(404, null, "Resource not found"); + + given(httpClient.get(eq("http://cache-service/cache?uuid=key&ch=ch"), any(), anyLong())) + .willReturn(Future.succeededFuture(response)); + + // when + final Future result = target.getCachedObject("key", "ch", timeout); + + // then + assertThat(result.failed()).isTrue(); + assertThat(result.cause()).hasMessage("Cannot parse response: Resource not found"); + assertThat(result.cause()).isInstanceOf(PreBidException.class); + + verify(metrics).updateVtrackCacheReadRequestTime(anyLong(), eq(MetricName.err)); + } + private AuctionContext givenAuctionContext(UnaryOperator accountCustomizer, UnaryOperator bidRequestCustomizer) { diff --git a/src/test/java/org/prebid/server/handler/GetVtrackHandlerTest.java b/src/test/java/org/prebid/server/handler/GetVtrackHandlerTest.java new file mode 100644 index 00000000000..f4d3bca3d3c --- /dev/null +++ b/src/test/java/org/prebid/server/handler/GetVtrackHandlerTest.java @@ -0,0 +1,123 @@ +package org.prebid.server.handler; + +import io.netty.handler.codec.http.HttpHeaderValues; +import io.netty.util.AsciiString; +import io.vertx.core.Future; +import io.vertx.core.http.HttpServerRequest; +import io.vertx.core.http.HttpServerResponse; +import io.vertx.ext.web.RoutingContext; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.prebid.server.VertxTest; +import org.prebid.server.cache.CoreCacheService; +import org.prebid.server.execution.timeout.TimeoutFactory; +import org.prebid.server.util.HttpUtil; +import org.prebid.server.vertx.httpclient.model.HttpClientResponse; + +import static io.vertx.core.MultiMap.caseInsensitiveMultiMap; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mock.Strictness.LENIENT; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; + +@ExtendWith(MockitoExtension.class) +public class GetVtrackHandlerTest extends VertxTest { + + @Mock + private CoreCacheService coreCacheService; + @Mock + private TimeoutFactory timeoutFactory; + + private GetVtrackHandler target; + + @Mock(strictness = LENIENT) + private RoutingContext routingContext; + @Mock(strictness = LENIENT) + private HttpServerRequest httpRequest; + @Mock(strictness = LENIENT) + private HttpServerResponse httpResponse; + + @BeforeEach + public void setUp() { + given(routingContext.request()).willReturn(httpRequest); + given(routingContext.response()).willReturn(httpResponse); + given(httpResponse.putHeader(any(CharSequence.class), any(AsciiString.class))).willReturn(httpResponse); + given(httpResponse.putHeader(anyString(), anyString())).willReturn(httpResponse); + + given(httpRequest.getParam("uuid")).willReturn("key"); + given(httpRequest.getParam("ch")).willReturn("test.com"); + + given(httpResponse.setStatusCode(anyInt())).willReturn(httpResponse); + + target = new GetVtrackHandler(2000, coreCacheService, timeoutFactory); + } + + @Test + public void shouldRespondWithBadRequestWhenAccountParameterIsMissing() { + // given + given(httpRequest.getParam("uuid")).willReturn(null); + + // when + target.handle(routingContext); + + // then + verifyNoInteractions(coreCacheService); + + verify(httpResponse).setStatusCode(400); + verify(httpResponse).end("'uuid' is a required query parameter and can't be empty"); + } + + @Test + public void shouldRespondWithInternalServerErrorWhenCacheServiceReturnFailure() { + // given + given(coreCacheService.getCachedObject(eq("key"), eq("test.com"), any())) + .willReturn(Future.failedFuture("error")); + + // when + target.handle(routingContext); + + // then + verify(httpResponse).setStatusCode(500); + verify(httpResponse).putHeader(HttpUtil.CONTENT_TYPE_HEADER, HttpHeaderValues.APPLICATION_JSON); + verify(httpResponse).end("Error occurred while sending request to cache: error"); + } + + @Test + public void shouldRespondWithBodyAndHeadersReturnedFromCacheWhenStatusCodeIsOk() { + // given + given(coreCacheService.getCachedObject(any(), any(), any())).willReturn(Future.succeededFuture( + HttpClientResponse.of(200, caseInsensitiveMultiMap().add("Header", "Value"), "body"))); + + // when + target.handle(routingContext); + + // then + verify(coreCacheService).getCachedObject(eq("key"), eq("test.com"), any()); + verify(httpResponse).setStatusCode(200); + verify(httpResponse).putHeader("Header", "Value"); + verify(httpResponse).end("body"); + } + + @Test + public void shouldRespondWithBodyAndDefaultHeadersReturnedFromCacheWhenStatusCodeIsNotOk() { + // given + given(coreCacheService.getCachedObject(any(), any(), any())).willReturn(Future.succeededFuture( + HttpClientResponse.of(404, caseInsensitiveMultiMap().add("Header", "Value"), "reason"))); + + // when + target.handle(routingContext); + + // then + verify(coreCacheService).getCachedObject(eq("key"), eq("test.com"), any()); + verify(httpResponse).setStatusCode(404); + verify(httpResponse).putHeader(HttpUtil.CONTENT_TYPE_HEADER, HttpHeaderValues.APPLICATION_JSON); + verify(httpResponse).end("reason"); + } +} diff --git a/src/test/java/org/prebid/server/handler/VtrackHandlerTest.java b/src/test/java/org/prebid/server/handler/PostVtrackHandlerTest.java similarity index 98% rename from src/test/java/org/prebid/server/handler/VtrackHandlerTest.java rename to src/test/java/org/prebid/server/handler/PostVtrackHandlerTest.java index a48af60aabf..98011db6ecf 100644 --- a/src/test/java/org/prebid/server/handler/VtrackHandlerTest.java +++ b/src/test/java/org/prebid/server/handler/PostVtrackHandlerTest.java @@ -51,7 +51,7 @@ import static org.mockito.Mockito.verifyNoInteractions; @ExtendWith(MockitoExtension.class) -public class VtrackHandlerTest extends VertxTest { +public class PostVtrackHandlerTest extends VertxTest { @Mock private ApplicationSettings applicationSettings; @@ -62,7 +62,7 @@ public class VtrackHandlerTest extends VertxTest { @Mock private TimeoutFactory timeoutFactory; - private VtrackHandler handler; + private PostVtrackHandler handler; @Mock(strictness = LENIENT) private RoutingContext routingContext; @Mock(strictness = LENIENT) @@ -84,7 +84,7 @@ public void setUp() { given(httpResponse.setStatusCode(anyInt())).willReturn(httpResponse); - handler = new VtrackHandler( + handler = new PostVtrackHandler( 2000, true, true, applicationSettings, bidderCatalog, coreCacheService, timeoutFactory, jacksonMapper); } @@ -314,7 +314,7 @@ public void shouldSendToCacheNullInAccountEnabledAndValidBiddersWhenAccountEvent public void shouldSendToCacheExpectedPutsAndUpdatableBiddersWhenBidderVastNotAllowed() throws JsonProcessingException { // given - handler = new VtrackHandler( + handler = new PostVtrackHandler( 2000, false, true, applicationSettings, bidderCatalog, coreCacheService, timeoutFactory, jacksonMapper); final List bidPutObjects = asList( @@ -359,7 +359,7 @@ public void shouldSendToCacheExpectedPutsAndUpdatableBiddersWhenBidderVastNotAll @Test public void shouldSendToCacheExpectedPutsAndUpdatableBiddersWhenBidderVastAllowed() throws JsonProcessingException { // given - handler = new VtrackHandler( + handler = new PostVtrackHandler( 2000, false, false, applicationSettings, bidderCatalog, coreCacheService, timeoutFactory, jacksonMapper); @@ -447,7 +447,7 @@ public void shouldSendToCacheExpectedPutsWhenModifyVastForUnknownBidderAndAllowU public void shouldSendToCacheWithEmptyBiddersAllowingVastUpdatePutsWhenAllowUnknownBidderIsFalse() throws JsonProcessingException { // given - handler = new VtrackHandler( + handler = new PostVtrackHandler( 2000, false, true, applicationSettings, bidderCatalog, coreCacheService, timeoutFactory, jacksonMapper); final List bidPutObjects = asList( @@ -486,7 +486,7 @@ public void shouldSendToCacheWithEmptyBiddersAllowingVastUpdatePutsWhenAllowUnkn public void shouldSendToCacheWithEmptyBiddersAllowingVastUpdatePutsWhenModifyVastForUnknownBidderIsFalse() throws JsonProcessingException { // given - handler = new VtrackHandler( + handler = new PostVtrackHandler( 2000, true, false, applicationSettings, bidderCatalog, coreCacheService, timeoutFactory, jacksonMapper); final List bidPutObjects = asList( diff --git a/src/test/java/org/prebid/server/it/ApplicationTest.java b/src/test/java/org/prebid/server/it/ApplicationTest.java index ca4414b8897..3a3710c6e6e 100644 --- a/src/test/java/org/prebid/server/it/ApplicationTest.java +++ b/src/test/java/org/prebid/server/it/ApplicationTest.java @@ -53,6 +53,7 @@ import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; import static com.github.tomakehurst.wiremock.client.WireMock.equalToIgnoreCase; import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson; +import static com.github.tomakehurst.wiremock.client.WireMock.get; import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; import static io.restassured.RestAssured.given; @@ -397,7 +398,7 @@ public void getuidsShouldReturnJsonWithUids() throws JSONException, IOException } @Test - public void vtrackShouldReturnJsonWithUids() throws JSONException, IOException { + public void vtrackShouldPutBidsAndReturnUids() throws JSONException, IOException { // given and when WIRE_MOCK_RULE.stubFor(post(urlPathEqualTo("/cache")) .withRequestBody(equalToBidCacheRequest(jsonFrom("vtrack/test-cache-request.json"))) @@ -413,6 +414,24 @@ public void vtrackShouldReturnJsonWithUids() throws JSONException, IOException { assertJsonEquals("vtrack/test-vtrack-response.json", response, emptyList()); } + @Test + public void vtrackShouldReturnCachedObjects() throws IOException { + // given and when + WIRE_MOCK_RULE.stubFor(get(urlPathEqualTo("/cache")) + .withQueryParam("uuid", equalTo("key")) + .withQueryParam("ch", equalTo("test.com")) + .willReturn(aResponse().withBody(xmlFrom("vtrack/test-vtrack-response.xml")))); + + final Response response = given(SPEC) + .when() + .queryParam("uuid", "key") + .queryParam("ch", "test.com") + .get("/vtrack"); + + // then + assertThat(response.asString()).isEqualTo(xmlFrom("vtrack/test-vtrack-response.xml")); + } + @Test public void optionsRequestShouldRespondWithOriginalPolicyHeaders() { // when diff --git a/src/test/java/org/prebid/server/it/IntegrationTest.java b/src/test/java/org/prebid/server/it/IntegrationTest.java index 7df8040f599..7b8d42e0dc0 100644 --- a/src/test/java/org/prebid/server/it/IntegrationTest.java +++ b/src/test/java/org/prebid/server/it/IntegrationTest.java @@ -40,6 +40,8 @@ import org.springframework.test.context.TestPropertySource; import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; @@ -118,6 +120,12 @@ protected static String jsonFrom(String file, PrebidVersionProvider prebidVersio .replace("{{ pbs.java.version }}", prebidVersionProvider.getNameVersionRecord()); } + protected static String xmlFrom(String file) throws IOException { + try (InputStream inputStream = IntegrationTest.class.getResourceAsStream(file)) { + return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8); + } + } + protected static String openrtbAuctionResponseFrom(String templatePath, Response response, List bidders) throws IOException { diff --git a/src/test/java/org/prebid/server/metric/MetricsTest.java b/src/test/java/org/prebid/server/metric/MetricsTest.java index fc6510e3529..1ee76608ede 100644 --- a/src/test/java/org/prebid/server/metric/MetricsTest.java +++ b/src/test/java/org/prebid/server/metric/MetricsTest.java @@ -1227,23 +1227,42 @@ public void shouldIncrementStoredImpMissingMetric() { } @Test - public void shouldIncrementPrebidCacheRequestSuccessTimer() { + public void shouldIncrementAuctionPrebidCacheRequestTimer() { // when - metrics.updateCacheRequestSuccessTime("accountId", 1424L); + metrics.updateAuctionCacheRequestTime("accountId", 1424L, MetricName.ok); + metrics.updateAuctionCacheRequestTime("accountId", 1424L, MetricName.err); // then assertThat(metricRegistry.timer("prebid_cache.requests.ok").getCount()).isEqualTo(1); assertThat(metricRegistry.timer("account.accountId.prebid_cache.requests.ok").getCount()).isOne(); + + assertThat(metricRegistry.timer("prebid_cache.requests.err").getCount()).isEqualTo(1); + assertThat(metricRegistry.timer("account.accountId.prebid_cache.requests.err").getCount()).isOne(); } @Test - public void shouldIncrementPrebidCacheRequestFailedTimer() { + public void shouldIncrementVtrackReadPrebidCacheRequestTimer() { // when - metrics.updateCacheRequestFailedTime("accountId", 1424L); + metrics.updateVtrackCacheReadRequestTime(1424L, MetricName.ok); + metrics.updateVtrackCacheReadRequestTime(1424L, MetricName.err); // then - assertThat(metricRegistry.timer("prebid_cache.requests.err").getCount()).isEqualTo(1); - assertThat(metricRegistry.timer("account.accountId.prebid_cache.requests.err").getCount()).isOne(); + assertThat(metricRegistry.timer("prebid_cache.vtrack.read.ok").getCount()).isEqualTo(1); + assertThat(metricRegistry.timer("prebid_cache.vtrack.read.err").getCount()).isEqualTo(1); + } + + @Test + public void shouldIncrementVtrackWritePrebidCacheRequestTimer() { + // when + metrics.updateVtrackCacheWriteRequestTime("accountId", 1424L, MetricName.ok); + metrics.updateVtrackCacheWriteRequestTime("accountId", 1424L, MetricName.err); + + // then + assertThat(metricRegistry.timer("prebid_cache.vtrack.write.ok").getCount()).isEqualTo(1); + assertThat(metricRegistry.timer("account.accountId.prebid_cache.vtrack.write.ok").getCount()).isOne(); + + assertThat(metricRegistry.timer("prebid_cache.vtrack.write.err").getCount()).isEqualTo(1); + assertThat(metricRegistry.timer("account.accountId.prebid_cache.vtrack.write.err").getCount()).isOne(); } @Test @@ -1265,6 +1284,63 @@ public void shouldIncrementPrebidCacheCreativeSizeHistogram() { .isEqualTo(1); } + @Test + public void shouldIncrementPrebidCacheVtrackCreativeSizeHistogram() { + // when + metrics.updateVtrackCacheCreativeSize("accountId", 123, MetricName.json); + metrics.updateVtrackCacheCreativeSize("accountId", 456, MetricName.xml); + metrics.updateVtrackCacheCreativeSize("accountId", 789, MetricName.unknown); + + // then + assertThat(metricRegistry.histogram("prebid_cache.vtrack.creative_size.json").getCount()).isEqualTo(1); + assertThat(metricRegistry.histogram("account.accountId.prebid_cache.vtrack.creative_size.json").getCount()) + .isEqualTo(1); + assertThat(metricRegistry.histogram("prebid_cache.vtrack.creative_size.xml").getCount()).isEqualTo(1); + assertThat(metricRegistry.histogram("account.accountId.prebid_cache.vtrack.creative_size.xml").getCount()) + .isEqualTo(1); + assertThat(metricRegistry.histogram("prebid_cache.vtrack.creative_size.unknown").getCount()).isEqualTo(1); + assertThat(metricRegistry.histogram("account.accountId.prebid_cache.vtrack.creative_size.unknown").getCount()) + .isEqualTo(1); + } + + @Test + public void shouldIncrementPrebidCacheCreativeTtlHistogram() { + // when + metrics.updateCacheCreativeTtl("accountId", 123, MetricName.json); + metrics.updateCacheCreativeTtl("accountId", 456, MetricName.xml); + metrics.updateCacheCreativeTtl("accountId", 789, MetricName.unknown); + + // then + assertThat(metricRegistry.histogram("prebid_cache.creative_ttl.json").getCount()).isEqualTo(1); + assertThat(metricRegistry.histogram("account.accountId.prebid_cache.creative_ttl.json").getCount()) + .isEqualTo(1); + assertThat(metricRegistry.histogram("prebid_cache.creative_ttl.xml").getCount()).isEqualTo(1); + assertThat(metricRegistry.histogram("account.accountId.prebid_cache.creative_ttl.xml").getCount()) + .isEqualTo(1); + assertThat(metricRegistry.histogram("prebid_cache.creative_ttl.unknown").getCount()).isEqualTo(1); + assertThat(metricRegistry.histogram("account.accountId.prebid_cache.creative_ttl.unknown").getCount()) + .isEqualTo(1); + } + + @Test + public void shouldIncrementPrebidCacheVtrackCreativeTtlHistogram() { + // when + metrics.updateVtrackCacheCreativeTtl("accountId", 123, MetricName.json); + metrics.updateVtrackCacheCreativeTtl("accountId", 456, MetricName.xml); + metrics.updateVtrackCacheCreativeTtl("accountId", 789, MetricName.unknown); + + // then + assertThat(metricRegistry.histogram("prebid_cache.vtrack.creative_ttl.json").getCount()).isEqualTo(1); + assertThat(metricRegistry.histogram("account.accountId.prebid_cache.vtrack.creative_ttl.json").getCount()) + .isEqualTo(1); + assertThat(metricRegistry.histogram("prebid_cache.vtrack.creative_ttl.xml").getCount()).isEqualTo(1); + assertThat(metricRegistry.histogram("account.accountId.prebid_cache.vtrack.creative_ttl.xml").getCount()) + .isEqualTo(1); + assertThat(metricRegistry.histogram("prebid_cache.vtrack.creative_ttl.unknown").getCount()).isEqualTo(1); + assertThat(metricRegistry.histogram("account.accountId.prebid_cache.vtrack.creative_ttl.unknown").getCount()) + .isEqualTo(1); + } + @Test public void shouldCreateCurrencyRatesGaugeMetric() { // when @@ -1552,25 +1628,6 @@ public void shouldIncrementUpdateAccountRequestRejectedByFailedFetchCount() { .isEqualTo(1); } - @Test - public void shouldIncrementPrebidCacheCreativeTtlHistogram() { - // when - metrics.updateCacheCreativeTtl("accountId", 123, MetricName.json); - metrics.updateCacheCreativeTtl("accountId", 456, MetricName.xml); - metrics.updateCacheCreativeTtl("accountId", 789, MetricName.unknown); - - // then - assertThat(metricRegistry.histogram("prebid_cache.creative_ttl.json").getCount()).isEqualTo(1); - assertThat(metricRegistry.histogram("account.accountId.prebid_cache.creative_ttl.json").getCount()) - .isEqualTo(1); - assertThat(metricRegistry.histogram("prebid_cache.creative_ttl.xml").getCount()).isEqualTo(1); - assertThat(metricRegistry.histogram("account.accountId.prebid_cache.creative_ttl.xml").getCount()) - .isEqualTo(1); - assertThat(metricRegistry.histogram("prebid_cache.creative_ttl.unknown").getCount()).isEqualTo(1); - assertThat(metricRegistry.histogram("account.accountId.prebid_cache.creative_ttl.unknown").getCount()) - .isEqualTo(1); - } - @Test public void shouldIncrementUpdateProfileMetric() { // when diff --git a/src/test/resources/org/prebid/server/it/vtrack/test-vtrack-response.xml b/src/test/resources/org/prebid/server/it/vtrack/test-vtrack-response.xml new file mode 100644 index 00000000000..19e3ace7ba3 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/vtrack/test-vtrack-response.xml @@ -0,0 +1,12 @@ + + + + prebid.org wrapper + + + + + + + +