Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the current screen name to crash events #704

Merged
merged 5 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import io.opentelemetry.android.internal.features.persistence.SimpleTemporaryFileProvider;
import io.opentelemetry.android.internal.initialization.InitializationEvents;
import io.opentelemetry.android.internal.processors.GlobalAttributesLogRecordAppender;
import io.opentelemetry.android.internal.processors.ScreenAttributesLogRecordProcessor;
import io.opentelemetry.android.internal.processors.SessionIdLogRecordAppender;
import io.opentelemetry.android.internal.services.CacheStorage;
import io.opentelemetry.android.internal.services.Preferences;
import io.opentelemetry.android.internal.services.ServiceManager;
Expand Down Expand Up @@ -478,6 +480,9 @@ private SdkLoggerProvider buildLoggerProvider(
SdkLoggerProvider.builder()
.setResource(resource)
.addLogRecordProcessor(new SessionIdLogRecordAppender(sessionProvider))
.addLogRecordProcessor(
new ScreenAttributesLogRecordProcessor(
getServiceManager().getVisibleScreenService()))
.addLogRecordProcessor(
new GlobalAttributesLogRecordAppender(
config.getGlobalAttributesSupplier()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.android.internal.processors

import io.opentelemetry.android.common.RumConstants
import io.opentelemetry.android.internal.services.visiblescreen.VisibleScreenService
import io.opentelemetry.context.Context
import io.opentelemetry.sdk.logs.LogRecordProcessor
import io.opentelemetry.sdk.logs.ReadWriteLogRecord

class ScreenAttributesLogRecordProcessor(val visibleScreenService: VisibleScreenService) : LogRecordProcessor {
override fun onEmit(
context: Context,
logRecord: ReadWriteLogRecord,
) {
val currentScreen = visibleScreenService.currentlyVisibleScreen
logRecord.setAttribute(RumConstants.SCREEN_NAME_KEY, currentScreen)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.android
package io.opentelemetry.android.internal.processors

import io.opentelemetry.android.session.SessionProvider
import io.opentelemetry.context.Context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import io.opentelemetry.android.internal.services.ServiceManager;
import io.opentelemetry.android.internal.services.applifecycle.AppLifecycleService;
import io.opentelemetry.android.internal.services.applifecycle.ApplicationStateListener;
import io.opentelemetry.android.internal.services.visiblescreen.VisibleScreenService;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.KeyValue;
import io.opentelemetry.api.common.Value;
Expand Down Expand Up @@ -83,6 +84,7 @@
@RunWith(AndroidJUnit4.class)
public class OpenTelemetryRumBuilderTest {

public static final String CUR_SCREEN_NAME = "Celebratory Token";
final Resource resource =
Resource.getDefault().toBuilder().put("test.attribute", "abcdef").build();
final InMemorySpanExporter spanExporter = InMemorySpanExporter.create();
Expand Down Expand Up @@ -152,8 +154,10 @@ public void shouldBuildTracerProvider() {

@Test
public void shouldBuildLogRecordProvider() {
ServiceManager serviceManager = createServiceManager();
OpenTelemetryRum openTelemetryRum =
makeBuilder()
.setServiceManager(serviceManager)
.setResource(resource)
.addLoggerProviderCustomizer(
(logRecordProviderBuilder, app) ->
Expand All @@ -174,6 +178,7 @@ public void shouldBuildLogRecordProvider() {
.hasAttributesSatisfyingExactly(
equalTo(SESSION_ID, openTelemetryRum.getRumSessionId()),
equalTo(stringKey("event.name"), "test.event"),
equalTo(SCREEN_NAME_KEY, CUR_SCREEN_NAME),
equalTo(stringKey("mega"), "hit"))
.hasResource(resource);

Expand Down Expand Up @@ -288,13 +293,18 @@ public void setSpanExporterCustomizer() {

@Test
public void setLogRecordExporterCustomizer() {
ServiceManager serviceManager = createServiceManager();
AtomicBoolean wasCalled = new AtomicBoolean(false);
Function<LogRecordExporter, LogRecordExporter> customizer =
x -> {
wasCalled.set(true);
return logsExporter;
};
OpenTelemetryRum rum = makeBuilder().addLogRecordExporterCustomizer(customizer).build();
OpenTelemetryRum rum =
makeBuilder()
.setServiceManager(serviceManager)
.addLogRecordExporterCustomizer(customizer)
.build();

Logger logger = rum.getOpenTelemetry().getLogsBridge().loggerBuilder("LogScope").build();
logger.logRecordBuilder()
Expand All @@ -313,6 +323,7 @@ public void setLogRecordExporterCustomizer() {
.hasBody("foo")
.hasAttributesSatisfyingExactly(
equalTo(stringKey("bing"), "bang"),
equalTo(SCREEN_NAME_KEY, CUR_SCREEN_NAME),
equalTo(SESSION_ID, rum.getRumSessionId()))
.hasSeverity(Severity.FATAL3);
}
Expand Down Expand Up @@ -402,12 +413,14 @@ public void diskBufferingDisabled() {

@Test
public void verifyGlobalAttrsForLogs() {
ServiceManager serviceManager = createServiceManager();
OtelRumConfig otelRumConfig = buildConfig();
otelRumConfig.setGlobalAttributes(
() -> Attributes.of(stringKey("someGlobalKey"), "someGlobalValue"));

OpenTelemetryRum rum =
OpenTelemetryRum.builder(application, otelRumConfig)
.setServiceManager(serviceManager)
.addLoggerProviderCustomizer(
(sdkLoggerProviderBuilder, application) ->
sdkLoggerProviderBuilder.addLogRecordProcessor(
Expand All @@ -426,6 +439,7 @@ public void verifyGlobalAttrsForLogs() {
.put(SESSION_ID, rum.getRumSessionId())
.put("someGlobalKey", "someGlobalValue")
.put("localAttrKey", "localAttrValue")
.put(SCREEN_NAME_KEY, CUR_SCREEN_NAME)
.build());
}

Expand Down Expand Up @@ -459,6 +473,9 @@ private static ServiceManager createServiceManager() {
when(serviceManager.getAppLifecycleService()).thenReturn(mock(AppLifecycleService.class));
when(serviceManager.getCacheStorage()).thenReturn(mock(CacheStorage.class));
when(serviceManager.getPreferences()).thenReturn(mock(Preferences.class));
VisibleScreenService screenService = mock(VisibleScreenService.class);
when(screenService.getCurrentlyVisibleScreen()).thenReturn(CUR_SCREEN_NAME);
when(serviceManager.getVisibleScreenService()).thenReturn(screenService);
return serviceManager;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.android.internal.processors

import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import io.opentelemetry.android.common.RumConstants.SCREEN_NAME_KEY
import io.opentelemetry.android.internal.services.visiblescreen.VisibleScreenService
import io.opentelemetry.api.common.AttributeKey
import io.opentelemetry.sdk.logs.ReadWriteLogRecord
import org.junit.Test

private const val CURRENT_SCREEN = "party favors"

class ScreenAttributesLogRecordProcessorTest {
@Test
fun `current screen name is appended`() {
val visibleScreenService: VisibleScreenService = mockk()
val logRecord: ReadWriteLogRecord = mockk()
every { visibleScreenService.currentlyVisibleScreen }.returns(CURRENT_SCREEN)
every { logRecord.setAttribute(any<AttributeKey<String>>(), any<String>()) } returns logRecord
val testClass = ScreenAttributesLogRecordProcessor(visibleScreenService)
testClass.onEmit(mockk(), logRecord)
verify { logRecord.setAttribute(SCREEN_NAME_KEY, CURRENT_SCREEN) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.android
package io.opentelemetry.android.internal.processors

import io.mockk.MockKAnnotations
import io.mockk.every
Expand All @@ -13,7 +13,7 @@ import io.opentelemetry.android.session.SessionProvider
import io.opentelemetry.api.common.AttributeKey
import io.opentelemetry.context.Context
import io.opentelemetry.sdk.logs.ReadWriteLogRecord
import io.opentelemetry.semconv.incubating.SessionIncubatingAttributes.SESSION_ID
import io.opentelemetry.semconv.incubating.SessionIncubatingAttributes
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

Expand All @@ -39,6 +39,6 @@ class SessionIdLogRecordAppenderTest {

underTest.onEmit(Context.root(), log)

verify { log.setAttribute(SESSION_ID, SESSION_ID_VALUE) }
verify { log.setAttribute(SessionIncubatingAttributes.SESSION_ID, SESSION_ID_VALUE) }
}
}