-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add SessionIdEventSender and wire up into lifecycle
- Loading branch information
1 parent
4bb6a10
commit 96d5fc1
Showing
4 changed files
with
149 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
40 changes: 40 additions & 0 deletions
40
core/src/main/java/io/opentelemetry/android/SessionIdEventSender.kt
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,40 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.android | ||
|
||
import io.opentelemetry.android.common.RumConstants.Events.EVENT_SESSION_END | ||
import io.opentelemetry.android.common.RumConstants.Events.EVENT_SESSION_START | ||
import io.opentelemetry.android.session.Session | ||
import io.opentelemetry.android.session.SessionObserver | ||
import io.opentelemetry.api.incubator.events.EventLogger | ||
import io.opentelemetry.semconv.incubating.SessionIncubatingAttributes.SESSION_ID | ||
import io.opentelemetry.semconv.incubating.SessionIncubatingAttributes.SESSION_PREVIOUS_ID | ||
|
||
internal class SessionIdEventSender(private val eventLogger: EventLogger) : SessionObserver { | ||
override fun onSessionStarted( | ||
newSession: Session, | ||
previousSession: Session, | ||
) { | ||
val eventBuilder = | ||
eventLogger | ||
.builder(EVENT_SESSION_START) | ||
.put(SESSION_ID, newSession.getId()) | ||
val previousSessionId = previousSession.getId() | ||
if (previousSessionId.isNotEmpty()) { | ||
eventBuilder.put(SESSION_PREVIOUS_ID, previousSessionId) | ||
} | ||
eventBuilder.emit() | ||
} | ||
|
||
override fun onSessionEnded(session: Session) { | ||
if (session.getId().isEmpty()) { | ||
return | ||
} | ||
eventLogger.builder(EVENT_SESSION_END) | ||
.put(SESSION_ID, session.getId()) | ||
.emit() | ||
} | ||
} |
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
77 changes: 77 additions & 0 deletions
77
core/src/test/java/io/opentelemetry/android/SessionIdEventSenderTest.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,77 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.android; | ||
|
||
import static io.opentelemetry.api.common.AttributeKey.stringKey; | ||
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; | ||
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import io.opentelemetry.android.session.Session; | ||
import io.opentelemetry.api.incubator.events.EventLogger; | ||
import io.opentelemetry.api.incubator.logs.KeyAnyValue; | ||
import io.opentelemetry.api.logs.LoggerProvider; | ||
import io.opentelemetry.sdk.logs.data.LogRecordData; | ||
import io.opentelemetry.sdk.logs.internal.AnyValueBody; | ||
import io.opentelemetry.sdk.logs.internal.SdkEventLoggerProvider; | ||
import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension; | ||
import java.util.List; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
class SessionIdEventSenderTest { | ||
@RegisterExtension | ||
static final OpenTelemetryExtension otelTesting = OpenTelemetryExtension.create(); | ||
|
||
private SessionIdEventSender underTest; | ||
|
||
@BeforeEach | ||
void setup() { | ||
LoggerProvider loggerProvider = otelTesting.getOpenTelemetry().getLogsBridge(); | ||
EventLogger eventLogger = SdkEventLoggerProvider.create(loggerProvider).get("testLogging"); | ||
underTest = new SessionIdEventSender(eventLogger); | ||
} | ||
|
||
@Test | ||
void shouldEmitSessionStartEvent() { | ||
Session newSession = new Session.DefaultSession("123", 0); | ||
Session oldSession = new Session.DefaultSession("666", 0); | ||
underTest.onSessionStarted(newSession, oldSession); | ||
|
||
List<LogRecordData> logs = otelTesting.getLogRecords(); | ||
assertEquals(1, logs.size()); | ||
LogRecordData log = logs.get(0); | ||
// TODO: Use new event body assertions when available. | ||
assertThat(log) | ||
.hasAttributesSatisfyingExactly(equalTo(stringKey("event.name"), "session.start")); | ||
|
||
AnyValueBody body = (AnyValueBody) log.getBody(); | ||
List<KeyAnyValue> kvBody = (List<KeyAnyValue>) body.asAnyValue().getValue(); | ||
assertThat(kvBody.get(0).getKey()).isEqualTo("session.previous_id"); | ||
assertThat(kvBody.get(0).getAnyValue().asString()).isEqualTo("666"); | ||
assertThat(kvBody.get(1).getKey()).isEqualTo("session.id"); | ||
assertThat(kvBody.get(1).getAnyValue().asString()).isEqualTo("123"); | ||
} | ||
|
||
@Test | ||
void shouldEmitSessionEndEvent() { | ||
Session session = new Session.DefaultSession("123", 0); | ||
underTest.onSessionEnded(session); | ||
|
||
List<LogRecordData> logs = otelTesting.getLogRecords(); | ||
assertEquals(1, logs.size()); | ||
LogRecordData log = logs.get(0); | ||
// TODO: Use new event body assertions when available. | ||
assertThat(log) | ||
.hasAttributesSatisfyingExactly(equalTo(stringKey("event.name"), "session.end")); | ||
|
||
AnyValueBody body = (AnyValueBody) log.getBody(); | ||
List<KeyAnyValue> kvBody = (List<KeyAnyValue>) body.asAnyValue().getValue(); | ||
assertThat(kvBody.get(0).getKey()).isEqualTo("session.id"); | ||
assertThat(kvBody.get(0).getAnyValue().asString()).isEqualTo("123"); | ||
} | ||
} |