Add a utility method to get OS, OS version, and libsignal version tags from UA additional specifiers

This commit is contained in:
Jon Chambers authored and Jon Chambers committed 2026-02-18 13:30:50 -05:00
1 parent 5ffcdb1c3f
commit 24029ef00c
2 files changed
+59 -5

No files matched your search

@@ -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();
}
}
@@ -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<Arguments> 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"))
);
}
}