Skip to content

Commit

Permalink
Test improvements (#368)
Browse files Browse the repository at this point in the history
* Using catalog from root project for tests

* Creating android-test catalog

* Providing agent version on tests

* Fixing tests

* Clean up

* Moving dependencies to the catalog

* Clean up

* Adding support for kotlin and junit5

* Setting up robolectric tests

* Initializing test instance

* Adding all resource attr names

* Rename .java to .kt

* Making device id configurable

* Setting android env values

* Setting android env values

* Passing agent version from build file

* Providing version code in tests

* Using latest sdk in tests

* Validating all signals' resources

* Renaming test file

* Adding global attributes test

* Making global attr tests pass

* Fixing tests

* Adding cell network attr to tests

* Enhancing tests

* Adding carrier info global attr tests

* Clean up

* Clean up

* Renaming packages

* Updating notice file
  • Loading branch information
LikeTheSalad authored Nov 22, 2024
1 parent 54b2b32 commit 54814cc
Show file tree
Hide file tree
Showing 30 changed files with 461 additions and 846 deletions.
21 changes: 20 additions & 1 deletion android-sdk/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
id 'co.elastic.apm.dependency.embedder'
}

Expand Down Expand Up @@ -27,6 +28,15 @@ android {
compileOptions {
sourceCompatibility jvmCompatibility
targetCompatibility jvmCompatibility
coreLibraryDesugaringEnabled = true
}
kotlinOptions {
jvmTarget = jvmCompatibility.toString()
}
testOptions {
unitTests {
includeAndroidResources = true
}
}
}

Expand All @@ -38,6 +48,11 @@ shadowJar {
relocate 'com.instacart.library.truetime', 'co.elastic.apm.android.truetime'
}

tasks.withType(Test).configureEach {
useJUnitPlatform()
systemProperty("agent_version", project.version)
}

dependencies {
api libs.opentelemetry.sdk
api libs.opentelemetry.android
Expand All @@ -58,7 +73,11 @@ dependencies {
annotationProcessor 'co.elastic.apm.compile:processor'
compileOnly 'co.elastic.apm.compile:processor'
testImplementation libs.bundles.mocking
testImplementation libs.junit
testImplementation libs.bundles.junit
testImplementation libs.mockwebserver
testImplementation libs.opentelemetry.testing
testImplementation libs.robolectric
testImplementation libs.assertj
testRuntimeOnly libs.junit5.vintage
coreLibraryDesugaring(libs.coreLib)
}
2 changes: 1 addition & 1 deletion android-sdk/metadata/notice.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
dependencies.hash=116B097450368CFACC99BF27198E0C94
dependencies.hash=FC395F744DC7E6D0DFA00982EDA59801
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ private PersistenceInitializer tryInitializePersistence(SignalConfiguration sign

private AttributesVisitor getResourceAttributesVisitor() {
return AttributesVisitor.compose(
new DeviceIdVisitor(),
new DeviceIdVisitor(configuration.deviceIdGenerator),
new DeviceInfoVisitor(),
new OsDescriptorVisitor(),
new RuntimeDescriptorVisitor(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.function.Supplier;

import co.elastic.apm.android.sdk.configuration.logging.LogLevel;
import co.elastic.apm.android.sdk.configuration.logging.LoggingPolicy;
Expand Down Expand Up @@ -54,6 +56,7 @@ public final class ElasticApmConfiguration {
public final List<LogFilter> logFilters;
public final List<MetricFilter> metricFilters;
public final LoggingPolicy libraryLoggingPolicy;
final Supplier<String> deviceIdGenerator;

public static Builder builder() {
return new Builder();
Expand All @@ -76,6 +79,7 @@ private ElasticApmConfiguration(Builder builder) {
exportProtocol = builder.exportProtocol;
libraryLoggingPolicy = builder.libraryLoggingPolicy;
resource = builder.resource;
deviceIdGenerator = builder.deviceIdGenerator;
spanFilters = Collections.unmodifiableList(new ArrayList<>(builder.spanFilters));
logFilters = Collections.unmodifiableList(new ArrayList<>(builder.logFilters));
metricFilters = Collections.unmodifiableList(new ArrayList<>(builder.metricFilters));
Expand All @@ -94,6 +98,7 @@ public static class Builder {
private ExportProtocol exportProtocol = ExportProtocol.GRPC;
private LoggingPolicy libraryLoggingPolicy = LoggingPolicy.getDefault();
private Resource resource = Resource.getDefault();
private Supplier<String> deviceIdGenerator;
private final Set<SpanFilter> spanFilters = new HashSet<>();
private final Set<LogFilter> logFilters = new HashSet<>();
private final Set<MetricFilter> metricFilters = new HashSet<>();
Expand Down Expand Up @@ -217,6 +222,11 @@ public Builder setResource(Resource resource) {
return this;
}

public Builder setDeviceIdGenerator(Supplier<String> deviceIdGenerator) {
this.deviceIdGenerator = deviceIdGenerator;
return this;
}

/**
* The span filter can be used to control which spans are exported and which shouldn't
* leave the device. An implementation that always excludes all spans is essentially a way
Expand Down Expand Up @@ -260,6 +270,9 @@ public ElasticApmConfiguration build() {
if (persistenceConfiguration == null) {
persistenceConfiguration = PersistenceConfiguration.builder().build();
}
if (deviceIdGenerator == null) {
deviceIdGenerator = () -> UUID.randomUUID().toString();
}
return new ElasticApmConfiguration(this);
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you 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 co.elastic.apm.android.sdk.attributes.resources

import co.elastic.apm.android.sdk.attributes.AttributesVisitor
import co.elastic.apm.android.sdk.internal.services.Service
import co.elastic.apm.android.sdk.internal.services.ServiceManager
import co.elastic.apm.android.sdk.internal.services.preferences.PreferencesService
import io.opentelemetry.api.common.AttributesBuilder
import io.opentelemetry.semconv.ResourceAttributes
import java.util.function.Supplier

class DeviceIdVisitor(private val deviceIdGenerator: Supplier<String>) : AttributesVisitor {

override fun visit(builder: AttributesBuilder) {
builder.put(ResourceAttributes.DEVICE_ID, getId())
}

private fun getId(): String {
val preferences =
ServiceManager.get()
.getService<PreferencesService>(
Service.Names.PREFERENCES
)
var deviceId =
preferences.retrieveString(DEVICE_ID_KEY)

if (deviceId == null) {
deviceId = deviceIdGenerator.get()
preferences.store(DEVICE_ID_KEY, deviceId)
}

return deviceId
}

companion object {
private const val DEVICE_ID_KEY = "device_id"
}
}
1 change: 1 addition & 0 deletions android-sdk/src/main/resources/META-INF/NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This product includes software licensed under the 'Apache License Version 2.0' l

- Android Lifecycle Process (https://developer.android.com/jetpack/androidx/releases/lifecycle#2.6.2)
- com.github.instacart.truetime-android:library:3.5
- Kotlin Stdlib (https://kotlinlang.org/)
- okhttp (https://square.github.io/okhttp/)
- OpenTelemetry Android (https://github.com/open-telemetry/opentelemetry-android)
- OpenTelemetry Java (https://github.com/open-telemetry/opentelemetry-java)
Expand Down
Loading

0 comments on commit 54814cc

Please sign in to comment.