Skip to content

Commit 4090d4e

Browse files
committed
ref(android): Report the network technology as its own attribute
device.connection_type is deprecated in favour of network.connection.type, and the conventions already have network.connection.effective_type for the generation, so the technology is no longer folded into the connection type. Device gains connection_effective_type with 2g, 3g, 4g or 5g, mirroring how connection_type aliases network.connection.type.
1 parent 68775a2 commit 4090d4e

6 files changed

Lines changed: 61 additions & 18 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120

121121
See the [Data Collection documentation](https://docs.sentry.io/platforms/java/configuration/options/#dataCollection) for all configuration keys, supported integrations, and migration guidance.
122122

123-
- Report the cellular network technology in `device.connection_type`, for example `cellular_4g` or `cellular_5g` ([#6146](https://github.com/getsentry/sentry-java/pull/6146))
123+
- Report the cellular network technology generation in `device.connection_effective_type`, for example `4g` or `5g` ([#6146](https://github.com/getsentry/sentry-java/pull/6146))
124124

125125
### Fixes
126126

‎sentry-android-core/src/main/java/io/sentry/android/core/DeviceInfoUtil.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
import android.os.SystemClock;
1616
import android.util.DisplayMetrics;
1717
import io.sentry.DateUtils;
18+
import io.sentry.IConnectionStatusProvider;
1819
import io.sentry.SentryLevel;
1920
import io.sentry.SentryOptions;
21+
import io.sentry.android.core.internal.util.AndroidConnectionStatusProvider;
2022
import io.sentry.android.core.internal.util.CpuInfoUtils;
2123
import io.sentry.android.core.internal.util.DeviceOrientations;
2224
import io.sentry.android.core.internal.util.RootChecker;
@@ -231,6 +233,17 @@ private void setDeviceIO(
231233
// wifi, ethernet or cellular, null if none
232234
device.setConnectionType(options.getConnectionStatusProvider().getConnectionType());
233235
}
236+
237+
if (device.getConnectionEffectiveType() == null) {
238+
final @NotNull IConnectionStatusProvider connectionStatusProvider =
239+
options.getConnectionStatusProvider();
240+
if (connectionStatusProvider instanceof AndroidConnectionStatusProvider) {
241+
// 2g, 3g, 4g or 5g, null unless the device is on a known cellular technology
242+
device.setConnectionEffectiveType(
243+
((AndroidConnectionStatusProvider) connectionStatusProvider)
244+
.getConnectionEffectiveType());
245+
}
246+
}
234247
}
235248

236249
@SuppressWarnings("NewApi")

‎sentry-android-core/src/main/java/io/sentry/android/core/internal/util/AndroidConnectionStatusProvider.java‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -170,28 +170,26 @@ private boolean isNetworkEffectivelyConnected(
170170
private @Nullable String getConnectionTypeFromCache() {
171171
final NetworkCapabilities capabilities = cachedNetworkCapabilities;
172172
if (capabilities != null) {
173-
return withCellularNetworkTechnology(getConnectionType(capabilities));
173+
return getConnectionType(capabilities);
174174
}
175175

176176
// Fallback to legacy method when NetworkCapabilities not available
177-
return withCellularNetworkTechnology(
178-
getConnectionType(context, options.getLogger(), buildInfoProvider));
177+
return getConnectionType(context, options.getLogger(), buildInfoProvider);
179178
}
180179

181180
/**
182-
* Refines a cellular connection type with the network technology, for example {@code
183-
* cellular_5g}. Other connection types and unknown technologies are returned unchanged.
181+
* The generation of the cellular network technology currently used for data, for example {@code
182+
* 5g}, or {@code null} when the connection is not cellular or the technology is unknown. Maps to
183+
* the {@code network.connection.effective_type} attribute.
184184
*/
185-
private @Nullable String withCellularNetworkTechnology(final @Nullable String connectionType) {
186-
if (!"cellular".equals(connectionType)) {
187-
return connectionType;
185+
public @Nullable String getConnectionEffectiveType() {
186+
if (!isCacheValid()) {
187+
updateCache(null);
188188
}
189-
final @Nullable String technology =
190-
cellularNetworkTechnologyProvider.getCellularNetworkTechnology();
191-
if (technology == null) {
192-
return connectionType;
189+
if (!"cellular".equals(getConnectionTypeFromCache())) {
190+
return null;
193191
}
194-
return connectionType + "_" + technology;
192+
return cellularNetworkTechnologyProvider.getCellularNetworkTechnology();
195193
}
196194

197195
private void ensureNetworkCallbackRegistered() {

‎sentry-android-core/src/test/java/io/sentry/android/core/internal/util/AndroidConnectionStatusProviderTest.kt‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ class AndroidConnectionStatusProviderTest {
275275
}
276276

277277
@Test
278-
fun `When the cellular network technology is known, it refines the connection type`() {
278+
fun `When on cellular with a known technology, the effective type is the generation`() {
279279
whenever(networkCapabilities.hasTransport(eq(TRANSPORT_WIFI))).thenReturn(false)
280280
whenever(networkCapabilities.hasTransport(eq(TRANSPORT_ETHERNET))).thenReturn(false)
281281
whenever(networkCapabilities.hasTransport(eq(TRANSPORT_CELLULAR))).thenReturn(true)
@@ -286,11 +286,13 @@ class AndroidConnectionStatusProviderTest {
286286
.thenReturn(PERMISSION_GRANTED)
287287
whenever(telephonyManager.dataNetworkType).thenReturn(TelephonyManager.NETWORK_TYPE_NR)
288288

289-
assertEquals("cellular_5g", connectionStatusProvider.connectionType)
289+
// The connection type keeps its documented values, the generation is reported separately.
290+
assertEquals("cellular", connectionStatusProvider.connectionType)
291+
assertEquals("5g", connectionStatusProvider.connectionEffectiveType)
290292
}
291293

292294
@Test
293-
fun `When the cellular network technology is unknown, the connection type stays cellular`() {
295+
fun `When the cellular network technology is unknown, the effective type is null`() {
294296
whenever(networkCapabilities.hasTransport(eq(TRANSPORT_WIFI))).thenReturn(false)
295297
whenever(networkCapabilities.hasTransport(eq(TRANSPORT_ETHERNET))).thenReturn(false)
296298
whenever(networkCapabilities.hasTransport(eq(TRANSPORT_CELLULAR))).thenReturn(true)
@@ -305,11 +307,12 @@ class AndroidConnectionStatusProviderTest {
305307
.thenReturn(PERMISSION_DENIED)
306308

307309
assertEquals("cellular", connectionStatusProvider.connectionType)
310+
assertNull(connectionStatusProvider.connectionEffectiveType)
308311
verify(telephonyManager, never()).dataNetworkType
309312
}
310313

311314
@Test
312-
fun `The cellular network technology does not refine other connection types`() {
315+
fun `When not on cellular, the effective type is null`() {
313316
whenever(networkCapabilities.hasTransport(eq(TRANSPORT_WIFI))).thenReturn(true)
314317
val telephonyManager = mock<TelephonyManager>()
315318
whenever(contextMock.getSystemService(eq(Context.TELEPHONY_SERVICE)))
@@ -319,6 +322,7 @@ class AndroidConnectionStatusProviderTest {
319322
whenever(telephonyManager.dataNetworkType).thenReturn(TelephonyManager.NETWORK_TYPE_NR)
320323

321324
assertEquals("wifi", connectionStatusProvider.connectionType)
325+
assertNull(connectionStatusProvider.connectionEffectiveType)
322326
}
323327

324328
@Test

‎sentry/api/sentry.api‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6082,6 +6082,7 @@ public final class io/sentry/protocol/Device : io/sentry/JsonSerializable, io/se
60826082
public fun getBootTime ()Ljava/util/Date;
60836083
public fun getBrand ()Ljava/lang/String;
60846084
public fun getChipset ()Ljava/lang/String;
6085+
public fun getConnectionEffectiveType ()Ljava/lang/String;
60856086
public fun getConnectionType ()Ljava/lang/String;
60866087
public fun getCpuDescription ()Ljava/lang/String;
60876088
public fun getExternalFreeStorage ()Ljava/lang/Long;
@@ -6120,6 +6121,7 @@ public final class io/sentry/protocol/Device : io/sentry/JsonSerializable, io/se
61206121
public fun setBrand (Ljava/lang/String;)V
61216122
public fun setCharging (Ljava/lang/Boolean;)V
61226123
public fun setChipset (Ljava/lang/String;)V
6124+
public fun setConnectionEffectiveType (Ljava/lang/String;)V
61236125
public fun setConnectionType (Ljava/lang/String;)V
61246126
public fun setCpuDescription (Ljava/lang/String;)V
61256127
public fun setExternalFreeStorage (Ljava/lang/Long;)V
@@ -6178,6 +6180,7 @@ public final class io/sentry/protocol/Device$JsonKeys {
61786180
public static final field BRAND Ljava/lang/String;
61796181
public static final field CHARGING Ljava/lang/String;
61806182
public static final field CHIPSET Ljava/lang/String;
6183+
public static final field CONNECTION_EFFECTIVE_TYPE Ljava/lang/String;
61816184
public static final field CONNECTION_TYPE Ljava/lang/String;
61826185
public static final field CPU_DESCRIPTION Ljava/lang/String;
61836186
public static final field EXTERNAL_FREE_STORAGE Ljava/lang/String;

‎sentry/src/main/java/io/sentry/protocol/Device.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ public final class Device implements JsonUnknown, JsonSerializable {
129129
private @Nullable String locale;
130130

131131
private @Nullable String connectionType;
132+
private @Nullable String connectionEffectiveType;
132133

133134
/** battery's temperature in celsius */
134135
private @Nullable Float batteryTemperature;
@@ -179,6 +180,7 @@ public Device() {}
179180
this.bootTime = device.bootTime;
180181
this.id = device.id;
181182
this.connectionType = device.connectionType;
183+
this.connectionEffectiveType = device.connectionEffectiveType;
182184
this.batteryTemperature = device.batteryTemperature;
183185
this.batteryLevel = device.batteryLevel;
184186
final String[] archsRef = device.archs;
@@ -421,6 +423,20 @@ public void setConnectionType(final @Nullable String connectionType) {
421423
this.connectionType = connectionType;
422424
}
423425

426+
/**
427+
* The generation of the cellular network technology, for example {@code 4g} or {@code 5g}. Maps
428+
* to the {@code network.connection.effective_type} attribute.
429+
*
430+
* @return the effective connection type or {@code null} if not set
431+
*/
432+
public @Nullable String getConnectionEffectiveType() {
433+
return connectionEffectiveType;
434+
}
435+
436+
public void setConnectionEffectiveType(final @Nullable String connectionEffectiveType) {
437+
this.connectionEffectiveType = connectionEffectiveType;
438+
}
439+
424440
public @Nullable Float getBatteryTemperature() {
425441
return batteryTemperature;
426442
}
@@ -494,6 +510,7 @@ public boolean equals(Object o) {
494510
&& Objects.equals(id, device.id)
495511
&& Objects.equals(locale, device.locale)
496512
&& Objects.equals(connectionType, device.connectionType)
513+
&& Objects.equals(connectionEffectiveType, device.connectionEffectiveType)
497514
&& Objects.equals(batteryTemperature, device.batteryTemperature)
498515
&& Objects.equals(processorCount, device.processorCount)
499516
&& Objects.equals(processorFrequency, device.processorFrequency)
@@ -533,6 +550,7 @@ public int hashCode() {
533550
id,
534551
locale,
535552
connectionType,
553+
connectionEffectiveType,
536554
batteryTemperature,
537555
processorCount,
538556
processorFrequency,
@@ -596,6 +614,7 @@ public static final class JsonKeys {
596614
public static final String TIMEZONE = "timezone";
597615
public static final String ID = "id";
598616
public static final String CONNECTION_TYPE = "connection_type";
617+
public static final String CONNECTION_EFFECTIVE_TYPE = "connection_effective_type";
599618
public static final String BATTERY_TEMPERATURE = "battery_temperature";
600619
public static final String LOCALE = "locale";
601620
public static final String PROCESSOR_COUNT = "processor_count";
@@ -692,6 +711,9 @@ public void serialize(final @NotNull ObjectWriter writer, final @NotNull ILogger
692711
if (connectionType != null) {
693712
writer.name(JsonKeys.CONNECTION_TYPE).value(connectionType);
694713
}
714+
if (connectionEffectiveType != null) {
715+
writer.name(JsonKeys.CONNECTION_EFFECTIVE_TYPE).value(connectionEffectiveType);
716+
}
695717
if (batteryTemperature != null) {
696718
writer.name(JsonKeys.BATTERY_TEMPERATURE).value(batteryTemperature);
697719
}
@@ -840,6 +862,9 @@ public static final class Deserializer implements JsonDeserializer<Device> {
840862
case JsonKeys.CONNECTION_TYPE:
841863
device.connectionType = reader.nextStringOrNull();
842864
break;
865+
case JsonKeys.CONNECTION_EFFECTIVE_TYPE:
866+
device.connectionEffectiveType = reader.nextStringOrNull();
867+
break;
843868
case JsonKeys.BATTERY_TEMPERATURE:
844869
device.batteryTemperature = reader.nextFloatOrNull();
845870
break;

0 commit comments

Comments
 (0)