-
Notifications
You must be signed in to change notification settings - Fork 224
Core: Improve traceability for Prebid Cache stores #3757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
363a50f
9a8c6b6
6d4017b
b56fe4a
65cc1f3
69bf63d
4d9449a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,6 +63,8 @@ public class CoreCacheService { | |
| private static final Logger logger = LoggerFactory.getLogger(CoreCacheService.class); | ||
|
|
||
| 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 final HttpClient httpClient; | ||
| private final URL endpointUrl; | ||
|
|
@@ -78,13 +80,18 @@ public class CoreCacheService { | |
| private final MultiMap cacheHeaders; | ||
| private final Map<String, List<String>> debugHeaders; | ||
|
|
||
| private final boolean appendTraceInfoToCacheId; | ||
| private final String datacenterRegion; | ||
|
|
||
| public CoreCacheService( | ||
| HttpClient httpClient, | ||
| URL endpointUrl, | ||
| String cachedAssetUrlTemplate, | ||
| long expectedCacheTimeMs, | ||
| String apiKey, | ||
| boolean isApiKeySecured, | ||
| boolean appendTraceInfoToCacheId, | ||
| String datacenterRegion, | ||
| VastModifier vastModifier, | ||
| EventsService eventsService, | ||
| Metrics metrics, | ||
|
|
@@ -107,6 +114,9 @@ public CoreCacheService( | |
| ? HttpUtil.headers().add(HttpUtil.X_PBC_API_KEY_HEADER, Objects.requireNonNull(apiKey)) | ||
| : HttpUtil.headers(); | ||
| debugHeaders = HttpUtil.toDebugHeaders(cacheHeaders); | ||
|
|
||
| this.appendTraceInfoToCacheId = appendTraceInfoToCacheId; | ||
| this.datacenterRegion = normalizeDatacenterRegion(datacenterRegion); | ||
| } | ||
|
|
||
| public String getEndpointHost() { | ||
|
|
@@ -138,7 +148,8 @@ public String cacheVideoDebugLog(CachedDebugLog cachedDebugLog, Integer videoCac | |
| return cacheKey; | ||
| } | ||
|
|
||
| private CachedCreative makeDebugCacheCreative(CachedDebugLog videoCacheDebugLog, String hbCacheId, | ||
| private CachedCreative makeDebugCacheCreative(CachedDebugLog videoCacheDebugLog, | ||
| String hbCacheId, | ||
| Integer videoCacheTtl) { | ||
| final JsonNode value = mapper.mapper().valueToTree(videoCacheDebugLog.buildCacheBody()); | ||
| videoCacheDebugLog.setCacheKey(hbCacheId); | ||
|
|
@@ -211,6 +222,7 @@ private List<CachedCreative> updatePutObjects(List<BidPutObject> bidPutObjects, | |
| .bidid(null) | ||
| .bidder(null) | ||
| .timestamp(null) | ||
| .key(resolveCacheKey(accountId, putObject.getKey())) | ||
| .value(vastModifier.modifyVastXml(isEventsEnabled, | ||
| allowedBidders, | ||
| putObject, | ||
|
|
@@ -268,7 +280,8 @@ private Future<CacheServiceResult> doCacheOpenrtb(List<CacheBid> bids, | |
| final List<CachedCreative> cachedCreatives = Stream.concat( | ||
| bids.stream().map(cacheBid -> | ||
| createJsonPutObjectOpenrtb(cacheBid, accountId, eventsContext)), | ||
| videoBids.stream().map(videoBid -> createXmlPutObjectOpenrtb(videoBid, requestId, hbCacheId))) | ||
| videoBids.stream().map(videoBid -> | ||
| createXmlPutObjectOpenrtb(videoBid, requestId, hbCacheId, accountId))) | ||
| .collect(Collectors.toCollection(ArrayList::new)); | ||
|
|
||
| if (cachedCreatives.isEmpty()) { | ||
|
|
@@ -385,26 +398,34 @@ private CachedCreative createJsonPutObjectOpenrtb(CacheBid cacheBid, | |
| bidObjectNode.put(BID_WURL_ATTRIBUTE, eventUrl); | ||
| } | ||
|
|
||
| final String resolvedCacheKey = resolveCacheKey(accountId); | ||
|
|
||
| final BidPutObject payload = BidPutObject.builder() | ||
| .aid(eventsContext.getAuctionId()) | ||
| .type("json") | ||
| .key(resolvedCacheKey) | ||
| .value(bidObjectNode) | ||
| .ttlseconds(cacheBid.getTtl()) | ||
| .build(); | ||
|
|
||
| return CachedCreative.of(payload, creativeSizeFromAdm(bid.getAdm())); | ||
| } | ||
|
|
||
| private CachedCreative createXmlPutObjectOpenrtb(CacheBid cacheBid, String requestId, String hbCacheId) { | ||
| private CachedCreative createXmlPutObjectOpenrtb(CacheBid cacheBid, | ||
| String requestId, | ||
| String hbCacheId, | ||
| String accountId) { | ||
CTMBNara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| final BidInfo bidInfo = cacheBid.getBidInfo(); | ||
| final Bid bid = bidInfo.getBid(); | ||
| final String vastXml = bid.getAdm(); | ||
|
|
||
| final String customCacheKey = resolveCustomCacheKey(hbCacheId, bidInfo.getCategory()); | ||
| final String resolvedCacheKey = resolveCacheKey(accountId, hbCacheId); | ||
| final String customCacheKey = resolveCustomCacheKey(resolvedCacheKey, bidInfo.getCategory()); | ||
|
|
||
| final BidPutObject payload = BidPutObject.builder() | ||
| .aid(requestId) | ||
| .type("xml") | ||
| .key(customCacheKey != null ? customCacheKey : resolvedCacheKey) | ||
|
||
| .value(vastXml != null ? new TextNode(vastXml) : null) | ||
| .ttlseconds(cacheBid.getTtl()) | ||
| .key(customCacheKey) | ||
|
|
@@ -515,10 +536,21 @@ private static String resolveVideoBidUuid(String uuid, String hbCacheId) { | |
| } | ||
|
|
||
| private void updateCreativeMetrics(String accountId, List<CachedCreative> cachedCreatives) { | ||
| for (final CachedCreative cachedCreative : cachedCreatives) { | ||
| for (CachedCreative cachedCreative : cachedCreatives) { | ||
| final MetricName creativeType = resolveCreativeTypeName(cachedCreative.getPayload()); | ||
| final Integer creativeTtl = cachedCreative.getPayload().getTtlseconds() != null | ||
| ? cachedCreative.getPayload().getTtlseconds() | ||
| : cachedCreative.getPayload().getExpiry(); | ||
|
||
|
|
||
| if (creativeTtl != null) { | ||
| metrics.updateCacheCreativeTtl(accountId, | ||
|
||
| creativeTtl, | ||
| creativeType); | ||
| } | ||
|
|
||
| metrics.updateCacheCreativeSize(accountId, | ||
| cachedCreative.getSize(), | ||
| resolveCreativeTypeName(cachedCreative.getPayload())); | ||
| creativeType); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -553,4 +585,44 @@ private BidCacheRequest toBidCacheRequest(List<CachedCreative> cachedCreatives) | |
| .map(CachedCreative::getPayload) | ||
| .toList()); | ||
| } | ||
|
|
||
| private String resolveCacheKey(String accountId) { | ||
| return resolveCacheKey(accountId, null); | ||
| } | ||
|
|
||
| // hbCacheId is accepted here to have a backwards-compatibility with video category mapping | ||
|
||
| private String resolveCacheKey(String accountId, String hbCacheId) { | ||
| if (!appendTraceInfoToCacheId) { | ||
| return hbCacheId; | ||
| } | ||
|
|
||
| // no need to have additional separator if datacenter name won't be added | ||
| final boolean isDatacenterNamePopulated = StringUtils.isNotBlank(datacenterRegion); | ||
| final int separatorCount = isDatacenterNamePopulated ? 2 : 1; | ||
| final int accountIdLength = accountId.length(); | ||
| final int traceInfoLength = isDatacenterNamePopulated | ||
| ? accountIdLength + datacenterRegion.length() + separatorCount | ||
| : accountIdLength + separatorCount; | ||
|
|
||
| final String cacheKey = hbCacheId != null ? hbCacheId : idGenerator.generateId(); | ||
| if (cacheKey != null && traceInfoLength < cacheKey.length()) { | ||
|
||
| final String substring = cacheKey.substring(0, cacheKey.length() - traceInfoLength); | ||
| return isDatacenterNamePopulated | ||
| ? accountId + TRACE_INFO_SEPARATOR + datacenterRegion + TRACE_INFO_SEPARATOR + substring | ||
| : accountId + TRACE_INFO_SEPARATOR + substring; | ||
| } else { | ||
| return hbCacheId; | ||
| } | ||
| } | ||
|
|
||
| private String normalizeDatacenterRegion(String datacenterRegion) { | ||
|
||
| if (datacenterRegion == null) { | ||
| return null; | ||
| } | ||
|
|
||
| final String trimmedDatacenterRegion = datacenterRegion.trim(); | ||
| return trimmedDatacenterRegion.length() > MAX_DATACENTER_REGION_LENGTH | ||
| ? trimmedDatacenterRegion.substring(0, MAX_DATACENTER_REGION_LENGTH) | ||
| : trimmedDatacenterRegion; | ||
CTMBNara marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 CacheCreativeTtlMetrics extends UpdatableMetrics { | ||
|
|
||
| CacheCreativeTtlMetrics(MetricRegistry metricRegistry, CounterType counterType, String prefix) { | ||
| super(Objects.requireNonNull(metricRegistry), | ||
| Objects.requireNonNull(counterType), | ||
| nameCreator(Objects.requireNonNull(prefix))); | ||
| } | ||
|
|
||
| private static Function<MetricName, String> nameCreator(String prefix) { | ||
| return metricName -> "%s.creative_ttl.%s".formatted(prefix, metricName); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.