-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added versionCode, applicationID, and customUUID from AndroidManifest…
….XML (if it exists) to crash and ANR spans (#993)
- Loading branch information
Showing
5 changed files
with
318 additions
and
22 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
splunk-otel-android/src/main/java/com/splunk/rum/ErrorIdentifierExtractor.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,92 @@ | ||
/* | ||
* 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 android.app.Application; | ||
import android.content.pm.ApplicationInfo; | ||
import android.content.pm.PackageInfo; | ||
import android.content.pm.PackageManager; | ||
import android.os.Bundle; | ||
import android.util.Log; | ||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
public class ErrorIdentifierExtractor { | ||
|
||
private static final String SPLUNK_UUID_MANIFEST_KEY = "SPLUNK_O11Y_CUSTOM_UUID"; | ||
private final Application application; | ||
private final PackageManager packageManager; | ||
@Nullable private final ApplicationInfo applicationInfo; | ||
|
||
public ErrorIdentifierExtractor(@NonNull Application application) { | ||
this.application = application; | ||
this.packageManager = application.getPackageManager(); | ||
ApplicationInfo appInfo; | ||
try { | ||
appInfo = | ||
packageManager.getApplicationInfo( | ||
application.getPackageName(), PackageManager.GET_META_DATA); | ||
} catch (Exception e) { | ||
Log.e( | ||
SplunkRum.LOG_TAG, | ||
"Failed to initialize ErrorIdentifierExtractor: " + e.getMessage()); | ||
appInfo = null; | ||
} | ||
this.applicationInfo = appInfo; | ||
} | ||
|
||
public ErrorIdentifierInfo extractInfo() { | ||
String applicationId = null; | ||
String versionCode = retrieveVersionCode(); | ||
String customUUID = retrieveCustomUUID(); | ||
|
||
if (applicationInfo != null) { | ||
applicationId = applicationInfo.packageName; | ||
} else { | ||
Log.e(SplunkRum.LOG_TAG, "ApplicationInfo is null, cannot extract applicationId"); | ||
} | ||
|
||
return new ErrorIdentifierInfo(applicationId, versionCode, customUUID); | ||
} | ||
|
||
@Nullable | ||
private String retrieveVersionCode() { | ||
try { | ||
PackageInfo packageInfo = | ||
packageManager.getPackageInfo(application.getPackageName(), 0); | ||
return String.valueOf(packageInfo.versionCode); | ||
} catch (Exception e) { | ||
Log.e(SplunkRum.LOG_TAG, "Failed to get application version code", e); | ||
return null; | ||
} | ||
} | ||
|
||
@Nullable | ||
private String retrieveCustomUUID() { | ||
if (applicationInfo == null) { | ||
Log.e(SplunkRum.LOG_TAG, "ApplicationInfo is null; cannot retrieve Custom UUID."); | ||
return null; | ||
} | ||
Bundle bundle = applicationInfo.metaData; | ||
if (bundle != null) { | ||
return bundle.getString(SPLUNK_UUID_MANIFEST_KEY); | ||
} else { | ||
Log.e(SplunkRum.LOG_TAG, "Application MetaData bundle is null"); | ||
return null; | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
splunk-otel-android/src/main/java/com/splunk/rum/ErrorIdentifierInfo.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,49 @@ | ||
/* | ||
* 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 androidx.annotation.Nullable; | ||
|
||
public class ErrorIdentifierInfo { | ||
@Nullable private final String applicationId; | ||
@Nullable private final String versionCode; | ||
@Nullable private final String customUUID; | ||
|
||
public ErrorIdentifierInfo( | ||
@Nullable String applicationId, | ||
@Nullable String versionCode, | ||
@Nullable String customUUID) { | ||
this.applicationId = applicationId; | ||
this.versionCode = versionCode; | ||
this.customUUID = customUUID; | ||
} | ||
|
||
@Nullable | ||
public String getApplicationId() { | ||
return applicationId; | ||
} | ||
|
||
@Nullable | ||
public String getVersionCode() { | ||
return versionCode; | ||
} | ||
|
||
@Nullable | ||
public String getCustomUUID() { | ||
return customUUID; | ||
} | ||
} |
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
122 changes: 122 additions & 0 deletions
122
splunk-otel-android/src/test/java/com/splunk/rum/ErrorIdentifierExtractorTest.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,122 @@ | ||
/* | ||
* 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 org.junit.Assert.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import android.app.Application; | ||
import android.content.pm.ApplicationInfo; | ||
import android.content.pm.PackageInfo; | ||
import android.content.pm.PackageManager; | ||
import android.os.Bundle; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
public class ErrorIdentifierExtractorTest { | ||
private static final String SPLUNK_UUID_MANIFEST_KEY = "SPLUNK_O11Y_CUSTOM_UUID"; | ||
private static final String TEST_PACKAGE_NAME = "splunk.test.package.name"; | ||
private static final String TEST_VERSION_CODE = "123"; | ||
private static final String TEST_UUID = "test-uuid"; | ||
|
||
@Mock private Application mockApplication; | ||
@Mock private PackageManager mockPackageManager; | ||
@Mock private PackageInfo mockPackageInfo; | ||
@Mock private ApplicationInfo mockApplicationInfo; | ||
@Mock private Bundle mockMetadata; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
MockitoAnnotations.openMocks(this); | ||
|
||
when(mockApplication.getApplicationContext()).thenReturn(mockApplication); | ||
when(mockApplication.getPackageManager()).thenReturn(mockPackageManager); | ||
when(mockApplication.getPackageName()).thenReturn(TEST_PACKAGE_NAME); | ||
|
||
mockApplicationInfo.packageName = TEST_PACKAGE_NAME; | ||
mockApplicationInfo.metaData = mockMetadata; | ||
|
||
when(mockPackageManager.getApplicationInfo(TEST_PACKAGE_NAME, PackageManager.GET_META_DATA)) | ||
.thenReturn(mockApplicationInfo); | ||
when(mockMetadata.getString(SPLUNK_UUID_MANIFEST_KEY)).thenReturn(TEST_UUID); | ||
|
||
mockPackageInfo.versionCode = 123; | ||
when(mockPackageManager.getPackageInfo(TEST_PACKAGE_NAME, 0)).thenReturn(mockPackageInfo); | ||
} | ||
|
||
@Test | ||
public void testGetApplicationId() { | ||
ErrorIdentifierExtractor extractor = new ErrorIdentifierExtractor(mockApplication); | ||
assertEquals(TEST_PACKAGE_NAME, extractor.extractInfo().getApplicationId()); | ||
} | ||
|
||
@Test | ||
public void testGetVersionCode() { | ||
ErrorIdentifierExtractor extractor = new ErrorIdentifierExtractor(mockApplication); | ||
assertEquals(TEST_VERSION_CODE, extractor.extractInfo().getVersionCode()); | ||
} | ||
|
||
@Test | ||
public void testGetCustomUUID() { | ||
ErrorIdentifierExtractor extractor = new ErrorIdentifierExtractor(mockApplication); | ||
assertEquals(TEST_UUID, extractor.extractInfo().getCustomUUID()); | ||
} | ||
|
||
@Test | ||
public void testCustomUUIDButDoesNotExist() { | ||
when(mockMetadata.getString(SPLUNK_UUID_MANIFEST_KEY)).thenReturn(null); | ||
ErrorIdentifierExtractor extractor = new ErrorIdentifierExtractor(mockApplication); | ||
assertNull(extractor.extractInfo().getCustomUUID()); | ||
} | ||
|
||
@Test | ||
public void testApplicationInfoMetaDataIsNull() throws PackageManager.NameNotFoundException { | ||
ApplicationInfo applicationInfoWithNullMetaData = new ApplicationInfo(); | ||
applicationInfoWithNullMetaData.packageName = TEST_PACKAGE_NAME; | ||
|
||
when(mockPackageManager.getApplicationInfo(TEST_PACKAGE_NAME, PackageManager.GET_META_DATA)) | ||
.thenReturn(applicationInfoWithNullMetaData); | ||
|
||
ErrorIdentifierExtractor extractor = new ErrorIdentifierExtractor(mockApplication); | ||
assertNull(extractor.extractInfo().getCustomUUID()); | ||
} | ||
|
||
@Test | ||
public void testRetrieveVersionCodeIsNull() throws PackageManager.NameNotFoundException { | ||
when(mockPackageManager.getPackageInfo(TEST_PACKAGE_NAME, 0)) | ||
.thenThrow(new PackageManager.NameNotFoundException()); | ||
|
||
ErrorIdentifierExtractor extractor = new ErrorIdentifierExtractor(mockApplication); | ||
assertNull(extractor.extractInfo().getVersionCode()); | ||
} | ||
|
||
@Test | ||
public void testExtractInfoWhenApplicationInfoIsNull() | ||
throws PackageManager.NameNotFoundException { | ||
when(mockPackageManager.getApplicationInfo(TEST_PACKAGE_NAME, PackageManager.GET_META_DATA)) | ||
.thenThrow(new PackageManager.NameNotFoundException()); | ||
|
||
ErrorIdentifierExtractor extractor = new ErrorIdentifierExtractor(mockApplication); | ||
|
||
ErrorIdentifierInfo info = extractor.extractInfo(); | ||
assertNull(info.getApplicationId()); | ||
assertEquals(TEST_VERSION_CODE, info.getVersionCode()); | ||
assertNull(info.getCustomUUID()); | ||
} | ||
} |