Skip to content

Commit

Permalink
feat(android): expose API to get current session ID
Browse files Browse the repository at this point in the history
  • Loading branch information
abhaysood committed Dec 18, 2024
1 parent 16d55c1 commit 223cd90
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
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

0 comments on commit 223cd90

Please sign in to comment.