-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor CurrentNetwork: add a method that returns all network Attrib… (
#410) * Refactor CurrentNetwork: add a method that returns all network Attributes * code review comments * Improved NetworkMonitor assertions * fix crash reporter tests?
- Loading branch information
Mateusz Rzeszutek
authored
Nov 17, 2022
1 parent
7893159
commit 2f74525
Showing
8 changed files
with
199 additions
and
70 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
splunk-otel-android/src/main/java/com/splunk/rum/CurrentNetworkAttributesExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright Splunk Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.splunk.rum; | ||
|
||
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NET_HOST_CARRIER_ICC; | ||
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NET_HOST_CARRIER_MCC; | ||
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NET_HOST_CARRIER_MNC; | ||
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NET_HOST_CARRIER_NAME; | ||
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NET_HOST_CONNECTION_SUBTYPE; | ||
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NET_HOST_CONNECTION_TYPE; | ||
|
||
import androidx.annotation.Nullable; | ||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.common.AttributesBuilder; | ||
|
||
class CurrentNetworkAttributesExtractor { | ||
|
||
Attributes extract(CurrentNetwork network) { | ||
AttributesBuilder builder = | ||
Attributes.builder() | ||
.put(NET_HOST_CONNECTION_TYPE, network.getState().getHumanName()); | ||
|
||
setIfNotNull(builder, NET_HOST_CONNECTION_SUBTYPE, network.getSubType()); | ||
setIfNotNull(builder, NET_HOST_CARRIER_NAME, network.getCarrierName()); | ||
setIfNotNull(builder, NET_HOST_CARRIER_MCC, network.getCarrierCountryCode()); | ||
setIfNotNull(builder, NET_HOST_CARRIER_MNC, network.getCarrierNetworkCode()); | ||
setIfNotNull(builder, NET_HOST_CARRIER_ICC, network.getCarrierIsoCountryCode()); | ||
|
||
return builder.build(); | ||
} | ||
|
||
private static void setIfNotNull( | ||
AttributesBuilder builder, AttributeKey<String> key, @Nullable String value) { | ||
if (value != null) { | ||
builder.put(key, value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
splunk-otel-android/src/test/java/com/splunk/rum/CurrentNetworkAttributesExtractorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright Splunk Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.splunk.rum; | ||
|
||
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; | ||
import static org.assertj.core.api.Assertions.entry; | ||
|
||
import android.os.Build; | ||
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RobolectricTestRunner; | ||
import org.robolectric.annotation.Config; | ||
|
||
@RunWith(RobolectricTestRunner.class) | ||
public class CurrentNetworkAttributesExtractorTest { | ||
|
||
final CurrentNetworkAttributesExtractor underTest = new CurrentNetworkAttributesExtractor(); | ||
|
||
@Config(sdk = Build.VERSION_CODES.P) | ||
@Test | ||
public void getNetworkAttributes_withCarrier() { | ||
CurrentNetwork currentNetwork = | ||
CurrentNetwork.builder(NetworkState.TRANSPORT_CELLULAR) | ||
.subType("aaa") | ||
.carrier( | ||
Carrier.builder() | ||
.id(206) | ||
.name("ShadyTel") | ||
.isoCountryCode("US") | ||
.mobileCountryCode("usa") | ||
.mobileNetworkCode("omg") | ||
.build()) | ||
.build(); | ||
|
||
assertThat(underTest.extract(currentNetwork)) | ||
.containsOnly( | ||
entry(SemanticAttributes.NET_HOST_CONNECTION_TYPE, "cell"), | ||
entry(SemanticAttributes.NET_HOST_CONNECTION_SUBTYPE, "aaa"), | ||
entry(SemanticAttributes.NET_HOST_CARRIER_NAME, "ShadyTel"), | ||
entry(SemanticAttributes.NET_HOST_CARRIER_ICC, "US"), | ||
entry(SemanticAttributes.NET_HOST_CARRIER_MCC, "usa"), | ||
entry(SemanticAttributes.NET_HOST_CARRIER_MNC, "omg")); | ||
} | ||
|
||
@Config(sdk = Build.VERSION_CODES.O) | ||
@Test | ||
public void getNetworkAttributes_withoutCarrier() { | ||
CurrentNetwork currentNetwork = | ||
CurrentNetwork.builder(NetworkState.TRANSPORT_CELLULAR) | ||
.subType("aaa") | ||
.carrier(Carrier.builder().id(42).name("ShadyTel").build()) | ||
.build(); | ||
|
||
assertThat(underTest.extract(currentNetwork)) | ||
.containsOnly( | ||
entry(SemanticAttributes.NET_HOST_CONNECTION_TYPE, "cell"), | ||
entry(SemanticAttributes.NET_HOST_CONNECTION_SUBTYPE, "aaa")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters