Use an explicit-allow model for tagging client versions in metrics

This commit is contained in:
Jon Chambers
2023-07-10 12:20:02 -04:00
committed by Jon Chambers
parent c315b34395
commit adf6c751ee
2 changed files with 46 additions and 99 deletions

View File

@@ -22,47 +22,10 @@ public class UserAgentTagUtil {
public static final String PLATFORM_TAG = "platform";
public static final String VERSION_TAG = "clientVersion";
static final List<Tag> OVERFLOW_TAGS = List.of(Tag.of(PLATFORM_TAG, "overflow"), Tag.of(VERSION_TAG, "overflow"));
static final List<Tag> UNRECOGNIZED_TAGS = List.of(Tag.of(PLATFORM_TAG, "unrecognized"),
Tag.of(VERSION_TAG, "unrecognized"));
private static final Map<ClientPlatform, Semver> MINIMUM_VERSION_BY_PLATFORM = new EnumMap<>(ClientPlatform.class);
static {
MINIMUM_VERSION_BY_PLATFORM.put(ClientPlatform.ANDROID, new Semver("4.0.0"));
MINIMUM_VERSION_BY_PLATFORM.put(ClientPlatform.DESKTOP, new Semver("1.0.0"));
MINIMUM_VERSION_BY_PLATFORM.put(ClientPlatform.IOS, new Semver("3.0.0"));
}
static final int MAX_VERSIONS = 1_000;
private static final Set<Pair<ClientPlatform, Semver>> SEEN_VERSIONS = new HashSet<>();
private UserAgentTagUtil() {
}
public static List<Tag> getUserAgentTags(final String userAgentString) {
try {
final UserAgent userAgent = UserAgentUtil.parseUserAgentString(userAgentString);
final List<Tag> tags;
if (userAgent.getVersion().isStable() && userAgent.getVersion()
.isGreaterThanOrEqualTo(MINIMUM_VERSION_BY_PLATFORM.get(userAgent.getPlatform()))) {
if (allowVersion(userAgent.getPlatform(), userAgent.getVersion())) {
tags = List.of(Tag.of(PLATFORM_TAG, userAgent.getPlatform().name().toLowerCase()),
Tag.of(VERSION_TAG, userAgent.getVersion().toString()));
} else {
tags = OVERFLOW_TAGS;
}
} else {
tags = UNRECOGNIZED_TAGS;
}
return tags;
} catch (final UnrecognizedUserAgentException e) {
return UNRECOGNIZED_TAGS;
}
}
public static Tag getPlatformTag(final String userAgentString) {
String platform;
@@ -75,12 +38,16 @@ public class UserAgentTagUtil {
return Tag.of(PLATFORM_TAG, platform);
}
private static boolean allowVersion(final ClientPlatform platform, final Semver version) {
final Pair<ClientPlatform, Semver> platformAndVersion = new Pair<>(platform, version);
public static Optional<Tag> getClientVersionTag(final String userAgentString, final Map<ClientPlatform, Set<Semver>> taggedVersions) {
try {
final UserAgent userAgent = UserAgentUtil.parseUserAgentString(userAgentString);
synchronized (SEEN_VERSIONS) {
return SEEN_VERSIONS.contains(platformAndVersion) || (SEEN_VERSIONS.size() < MAX_VERSIONS && SEEN_VERSIONS.add(
platformAndVersion));
if (taggedVersions.getOrDefault(userAgent.getPlatform(), Collections.emptySet()).contains(userAgent.getVersion())) {
return Optional.of(Tag.of(VERSION_TAG, userAgent.getVersion().toString()));
}
} catch (final UnrecognizedUserAgentException ignored) {
}
return Optional.empty();
}
}