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

feat(android): expose API to get current session ID #1634

Merged
merged 1 commit into from
Dec 18, 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
1 change: 1 addition & 0 deletions android/measure/api/measure.api
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public final class sh/measure/android/Measure {
public static final fun clearUserId ()V
public final fun createSpanBuilder (Ljava/lang/String;)Lsh/measure/android/tracing/SpanBuilder;
public final fun getCurrentTime ()J
public final fun getSessionId ()Ljava/lang/String;
public final fun getTraceParentHeaderKey ()Ljava/lang/String;
public final fun getTraceParentHeaderValue (Lsh/measure/android/tracing/Span;)Ljava/lang/String;
public static final fun init (Landroid/content/Context;)V
Expand Down
17 changes: 17 additions & 0 deletions android/measure/src/main/java/sh/measure/android/Measure.kt
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,23 @@ object Measure {
}
}

/**
* Returns the session ID for the current session, or null if the SDK has not been initialized.
*
* A session represents a continuous period of activity in the app. A new session begins
* when an app is launched for the first time, or when there's been no activity for a
* 20-minute period. A single session can continue across multiple app background and
* foreground events; brief interruptions will not cause a new session to be created.
*
* @return session ID if the SDK is initialized, null otherwise.
*/
fun getSessionId(): String? {
if (isInitialized.get()) {
return measure.getSessionId()
}
return null
}

internal fun getOkHttpEventCollector(): OkHttpEventCollector? {
if (isInitialized.get()) {
return try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ internal class MeasureInternal(measureInitializer: MeasureInitializer) : AppLife
return spanCollector.getTraceParentHeaderKey()
}

fun getSessionId(): String? {
return try {
sessionManager.getSessionId()
} catch (e: IllegalArgumentException) {
return null
}
}

private fun unregisterCollectors() {
unhandledExceptionCollector.unregister()
anrCollector.unregister()
Expand Down
6 changes: 6 additions & 0 deletions docs/android/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ or when there's been no activity for a 20-minute period. A single session can co
foreground events; brief interruptions will not cause a new session to be created. This approach is helpful when reviewing
session replays, as it shows the app switching between background and foreground states within the same session.

The current session can be retrived by using `getSessionId` method.

```kotlin
val sessionId = Measure.getSessionId()
```


# Performance Impact

Expand Down
Loading