From 24029ef00c431f2c04b871578676003626c49b63 Mon Sep 17 00:00:00 2001 From: Jon Chambers Date: Tue, 17 Feb 2026 17:51:45 -0500 Subject: [PATCH] Add a utility method to get OS, OS version, and libsignal version tags from UA additional specifiers --- .../metrics/UserAgentTagUtil.java | 36 +++++++++++++++++++ .../metrics/UserAgentTagUtilTest.java | 28 ++++++++++++--- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/metrics/UserAgentTagUtil.java b/service/src/main/java/org/whispersystems/textsecuregcm/metrics/UserAgentTagUtil.java index aedc9cba7..ca9079583 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/metrics/UserAgentTagUtil.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/metrics/UserAgentTagUtil.java @@ -6,11 +6,15 @@ package org.whispersystems.textsecuregcm.metrics; import io.micrometer.core.instrument.Tag; +import io.micrometer.core.instrument.Tags; import jakarta.ws.rs.container.ContainerRequestContext; import jakarta.ws.rs.core.HttpHeaders; import java.util.Optional; import java.util.UUID; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.annotation.Nullable; +import org.apache.commons.lang3.StringUtils; import org.whispersystems.textsecuregcm.WhisperServerVersion; import org.whispersystems.textsecuregcm.storage.ClientReleaseManager; import org.whispersystems.textsecuregcm.util.ua.UnrecognizedUserAgentException; @@ -24,10 +28,16 @@ public class UserAgentTagUtil { public static final String PLATFORM_TAG = "platform"; public static final String VERSION_TAG = "clientVersion"; + public static final String OPERATING_SYSTEM_TAG = "operatingSystem"; + public static final String OPERATING_SYSTEM_VERSION_TAG = "operatingSystemVersion"; + public static final String LIBSIGNAL_VERSION_TAG = "libsignalVersion"; public static final String SERVER_UA = String.format("Signal-Server/%s (%s)", WhisperServerVersion.getServerVersion(), UUID.randomUUID()); + private static final Pattern STANDARD_ADDITIONAL_SPECIFIERS_PATTERN = + Pattern.compile("^(Windows|macOS|Linux) (\\S+) libsignal/([\\d.]+).*$", Pattern.CASE_INSENSITIVE); + private UserAgentTagUtil() { } @@ -76,4 +86,30 @@ public class UserAgentTagUtil { ? Optional.of(Tag.of(VERSION_TAG, userAgent.version().toString())) : Optional.empty(); } + + public static Tags getAdditionalSpecifierTags(@Nullable final String userAgentString) { + UserAgent userAgent = null; + + try { + userAgent = UserAgentUtil.parseUserAgentString(userAgentString); + } catch (final UnrecognizedUserAgentException ignored) { + } + + return getAdditionalSpecifierTags(userAgent); + } + + public static Tags getAdditionalSpecifierTags(@Nullable final UserAgent userAgent) { + if (userAgent == null || StringUtils.isBlank(userAgent.additionalSpecifiers())) { + return Tags.empty(); + } + + final Matcher matcher = STANDARD_ADDITIONAL_SPECIFIERS_PATTERN.matcher(userAgent.additionalSpecifiers()); + + return matcher.matches() + ? Tags.of( + OPERATING_SYSTEM_TAG, matcher.group(1), + OPERATING_SYSTEM_VERSION_TAG, matcher.group(2), + LIBSIGNAL_VERSION_TAG, matcher.group(3)) + : Tags.empty(); + } } diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/metrics/UserAgentTagUtilTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/metrics/UserAgentTagUtilTest.java index 4b03a7281..535f699bb 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/metrics/UserAgentTagUtilTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/metrics/UserAgentTagUtilTest.java @@ -10,19 +10,17 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import com.vdurmont.semver4j.Semver; import io.micrometer.core.instrument.Tag; -import java.util.Collections; -import java.util.Map; +import io.micrometer.core.instrument.Tags; +import java.util.List; import java.util.Optional; -import java.util.Set; import java.util.UUID; import java.util.stream.Stream; +import javax.annotation.Nullable; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; import org.whispersystems.textsecuregcm.storage.ClientReleaseManager; -import org.whispersystems.textsecuregcm.util.ua.ClientPlatform; class UserAgentTagUtilTest { @@ -74,4 +72,24 @@ class UserAgentTagUtilTest { Optional.empty()) ); } + + @ParameterizedTest + @MethodSource + void getAdditionalSpecifierTags(@Nullable final String userAgent, final Tags expectedTags) { + assertEquals(expectedTags, UserAgentTagUtil.getAdditionalSpecifierTags(userAgent)); + } + + private static List getAdditionalSpecifierTags() { + return List.of( + Arguments.argumentSet("null UA", null, Tags.empty()), + Arguments.argumentSet("nonsense UA", "This is not a valid User-Agent string", Tags.empty()), + Arguments.argumentSet("no additional specifiers", "Signal-Desktop/7.84.0", Tags.empty()), + Arguments.argumentSet("nonstandard additional specifiers", "Signal-Desktop/7.84.0 superfluous information", Tags.empty()), + Arguments.argumentSet("standard additional specifiers", "Signal-Desktop/7.84.0 macOS 21.6.0 libsignal/0.86.3", + Tags.of( + UserAgentTagUtil.OPERATING_SYSTEM_TAG, "macOS", + UserAgentTagUtil.OPERATING_SYSTEM_VERSION_TAG, "21.6.0", + UserAgentTagUtil.LIBSIGNAL_VERSION_TAG, "0.86.3")) + ); + } }