-
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.
Migrate the bulk of resource building to otel package. (#524)
* migrate the bulk of resource building to otel package. * guard against nulls and fix tests * add tests * spotless * javadoc
- Loading branch information
1 parent
cfcd0f3
commit 7505d19
Showing
9 changed files
with
231 additions
and
40 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
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
84 changes: 84 additions & 0 deletions
84
splunk-otel-android/src/main/java/io/opentelemetry/rum/internal/AndroidResource.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,84 @@ | ||
/* | ||
* 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 io.opentelemetry.rum.internal; | ||
|
||
import static io.opentelemetry.rum.internal.RumConstants.RUM_SDK_VERSION; | ||
import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.DEVICE_MODEL_IDENTIFIER; | ||
import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.DEVICE_MODEL_NAME; | ||
import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.OS_NAME; | ||
import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.OS_TYPE; | ||
import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.OS_VERSION; | ||
import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.SERVICE_NAME; | ||
|
||
import android.app.Application; | ||
import android.os.Build; | ||
|
||
import com.splunk.android.rum.R; | ||
|
||
import io.opentelemetry.sdk.resources.Resource; | ||
import io.opentelemetry.sdk.resources.ResourceBuilder; | ||
|
||
import java.util.function.Supplier; | ||
|
||
final class AndroidResource { | ||
|
||
static Resource createDefault(Application application) { | ||
String appName = readAppName(application); | ||
ResourceBuilder resourceBuilder = | ||
Resource.getDefault().toBuilder().put(SERVICE_NAME, appName); | ||
|
||
return resourceBuilder | ||
.put(RUM_SDK_VERSION, detectRumVersion(application)) | ||
.put(DEVICE_MODEL_NAME, Build.MODEL) | ||
.put(DEVICE_MODEL_IDENTIFIER, Build.MODEL) | ||
.put(OS_NAME, "Android") | ||
.put(OS_TYPE, "linux") | ||
.put(OS_VERSION, Build.VERSION.RELEASE) | ||
.build(); | ||
} | ||
|
||
private static String readAppName(Application application) { | ||
return trapTo( | ||
() -> { | ||
int stringId = | ||
application.getApplicationContext().getApplicationInfo().labelRes; | ||
return application.getApplicationContext().getString(stringId); | ||
}, | ||
"unknown_service:android"); | ||
} | ||
|
||
private static String detectRumVersion(Application application) { | ||
return trapTo( | ||
() -> { | ||
// TODO: Verify that this will be in the lib/jar at runtime. | ||
// TODO: After donation, package of R file will change | ||
return application | ||
.getApplicationContext() | ||
.getResources() | ||
.getString(R.string.rum_version); | ||
}, | ||
"unknown"); | ||
} | ||
|
||
private static String trapTo(Supplier<String> fn, String defaultValue) { | ||
try { | ||
return fn.get(); | ||
} catch (Exception e) { | ||
return defaultValue; | ||
} | ||
} | ||
} |
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
101 changes: 101 additions & 0 deletions
101
splunk-otel-android/src/test/java/io/opentelemetry/rum/internal/AndroidResourceTest.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,101 @@ | ||
/* | ||
* 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 io.opentelemetry.rum.internal; | ||
|
||
import static io.opentelemetry.rum.internal.RumConstants.RUM_SDK_VERSION; | ||
import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.DEVICE_MODEL_IDENTIFIER; | ||
import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.DEVICE_MODEL_NAME; | ||
import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.OS_NAME; | ||
import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.OS_TYPE; | ||
import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.OS_VERSION; | ||
import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.SERVICE_NAME; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.when; | ||
|
||
import android.app.Application; | ||
import android.content.pm.ApplicationInfo; | ||
import android.os.Build; | ||
|
||
import com.splunk.android.rum.R; | ||
|
||
import io.opentelemetry.sdk.resources.Resource; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Answers; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class AndroidResourceTest { | ||
|
||
String appName = "robotron"; | ||
String rumSdkVersion = "1.2.3"; | ||
|
||
@Mock(answer = Answers.RETURNS_DEEP_STUBS) | ||
Application app; | ||
|
||
@Test | ||
void testFullResource() { | ||
ApplicationInfo appInfo = new ApplicationInfo(); | ||
appInfo.labelRes = 12345; | ||
when(app.getApplicationContext().getApplicationInfo()).thenReturn(appInfo); | ||
when(app.getApplicationContext().getString(appInfo.labelRes)).thenReturn(appName); | ||
when(app.getApplicationContext().getResources().getString(R.string.rum_version)) | ||
.thenReturn(rumSdkVersion); | ||
|
||
Resource expected = | ||
Resource.getDefault() | ||
.merge( | ||
Resource.builder() | ||
.put(SERVICE_NAME, appName) | ||
.put(RUM_SDK_VERSION, rumSdkVersion) | ||
.put(DEVICE_MODEL_NAME, Build.MODEL) | ||
.put(DEVICE_MODEL_IDENTIFIER, Build.MODEL) | ||
.put(OS_NAME, "Android") | ||
.put(OS_TYPE, "linux") | ||
.put(OS_VERSION, Build.VERSION.RELEASE) | ||
.build()); | ||
|
||
Resource result = AndroidResource.createDefault(app); | ||
assertEquals(expected, result); | ||
} | ||
|
||
@Test | ||
void testProblematicContext() { | ||
when(app.getApplicationContext().getApplicationInfo()) | ||
.thenThrow(new SecurityException("cannot do that")); | ||
when(app.getApplicationContext().getResources()).thenThrow(new SecurityException("boom")); | ||
|
||
Resource expected = | ||
Resource.getDefault() | ||
.merge( | ||
Resource.builder() | ||
.put(SERVICE_NAME, "unknown_service:android") | ||
.put(RUM_SDK_VERSION, "unknown") | ||
.put(DEVICE_MODEL_NAME, Build.MODEL) | ||
.put(DEVICE_MODEL_IDENTIFIER, Build.MODEL) | ||
.put(OS_NAME, "Android") | ||
.put(OS_TYPE, "linux") | ||
.put(OS_VERSION, Build.VERSION.RELEASE) | ||
.build()); | ||
|
||
Resource result = AndroidResource.createDefault(app); | ||
assertEquals(expected, result); | ||
} | ||
} |
Oops, something went wrong.