-
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.
Add runtime details to crash span (#369)
* add runtime details to crash span * make field volatile instead of atomicreference * add nullable annotation * fix tests
- Loading branch information
1 parent
7f481fd
commit 7f73c98
Showing
7 changed files
with
201 additions
and
21 deletions.
There are no files selected for viewing
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
65 changes: 65 additions & 0 deletions
65
splunk-otel-android/src/main/java/com/splunk/rum/RuntimeDetails.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,65 @@ | ||
/* | ||
* 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.content.BroadcastReceiver; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.IntentFilter; | ||
import android.os.BatteryManager; | ||
import androidx.annotation.Nullable; | ||
import java.io.File; | ||
|
||
/** Represents details about the runtime environment at a time */ | ||
final class RuntimeDetails extends BroadcastReceiver { | ||
|
||
private @Nullable volatile Double batteryPercent = null; | ||
private final File filesDir; | ||
|
||
static RuntimeDetails create(Context context) { | ||
IntentFilter batteryChangedFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); | ||
File filesDir = context.getFilesDir(); | ||
RuntimeDetails runtimeDetails = new RuntimeDetails(filesDir); | ||
context.registerReceiver(runtimeDetails, batteryChangedFilter); | ||
return runtimeDetails; | ||
} | ||
|
||
private RuntimeDetails(File filesDir) { | ||
this.filesDir = filesDir; | ||
} | ||
|
||
long getCurrentStorageFreeSpaceInBytes() { | ||
return filesDir.getFreeSpace(); | ||
} | ||
|
||
long getCurrentFreeHeapInBytes() { | ||
Runtime runtime = Runtime.getRuntime(); | ||
return runtime.freeMemory(); | ||
} | ||
|
||
@Nullable | ||
Double getCurrentBatteryPercent() { | ||
return batteryPercent; | ||
} | ||
|
||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); | ||
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); | ||
batteryPercent = level * 100.0d / (float) scale; | ||
} | ||
} |
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
66 changes: 66 additions & 0 deletions
66
splunk-otel-android/src/test/java/com/splunk/rum/RuntimeDetailsTest.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,66 @@ | ||
/* | ||
* 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.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.os.BatteryManager; | ||
import java.io.File; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class RuntimeDetailsTest { | ||
|
||
private Context context; | ||
|
||
@Before | ||
public void setup() { | ||
context = mock(Context.class); | ||
} | ||
|
||
@Test | ||
public void testBattery() { | ||
Intent intent = mock(Intent.class); | ||
|
||
Integer level = 690; | ||
when(intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)).thenReturn(level); | ||
when(intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)).thenReturn(1000); | ||
|
||
RuntimeDetails details = RuntimeDetails.create(context); | ||
|
||
details.onReceive(context, intent); | ||
Double result = details.getCurrentBatteryPercent(); | ||
assertEquals(69.0d, result, 0.001); | ||
} | ||
|
||
@Test | ||
public void testFreeSpace() { | ||
File filesDir = mock(File.class); | ||
|
||
when(context.getFilesDir()).thenReturn(filesDir); | ||
when(filesDir.getFreeSpace()).thenReturn(4200L); | ||
|
||
RuntimeDetails details = RuntimeDetails.create(context); | ||
|
||
long result = details.getCurrentStorageFreeSpaceInBytes(); | ||
assertEquals(4200L, result); | ||
} | ||
} |