-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move session instrumentation into its own instrumentation module.
- Loading branch information
1 parent
dd58005
commit ce8fc85
Showing
19 changed files
with
307 additions
and
195 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
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
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
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
80 changes: 80 additions & 0 deletions
80
core/src/main/java/io/opentelemetry/android/session/SessionManagerImpl.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,80 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.android.session | ||
|
||
import io.opentelemetry.android.SessionIdTimeoutHandler | ||
import io.opentelemetry.sdk.common.Clock | ||
import java.util.Collections.synchronizedList | ||
import java.util.concurrent.TimeUnit | ||
|
||
internal class SessionManagerImpl( | ||
private val clock: Clock = Clock.getDefault(), | ||
private val sessionStorage: SessionStorage = SessionStorage.InMemory(), | ||
private val timeoutHandler: SessionIdTimeoutHandler, | ||
private val idGenerator: SessionIdGenerator = SessionIdGenerator.DEFAULT, | ||
private val sessionLifetimeNanos: Long = TimeUnit.HOURS.toNanos(4), | ||
) : SessionManager { | ||
// TODO: Make thread safe / wrap with AtomicReference? | ||
private var session: Session = Session.NONE | ||
private val observers = synchronizedList(ArrayList<SessionObserver>()) | ||
|
||
init { | ||
sessionStorage.save(session) | ||
} | ||
|
||
override fun addObserver(observer: SessionObserver) { | ||
observers.add(observer) | ||
} | ||
|
||
override fun getSessionId(): String { | ||
// value will never be null | ||
var newSession = session | ||
|
||
if (sessionHasExpired() || timeoutHandler.hasTimedOut()) { | ||
val newId = idGenerator.generateSessionId() | ||
|
||
// TODO FIXME: This is not threadsafe -- if two threads call getSessionId() | ||
// at the same time while timed out, two new sessions are created | ||
// Could require SessionStorage impls to be atomic/threadsafe or | ||
// do the locking in this class? | ||
|
||
newSession = Session.DefaultSession(newId, clock.now()) | ||
sessionStorage.save(newSession) | ||
} | ||
|
||
timeoutHandler.bump() | ||
|
||
// observers need to be called after bumping the timer because it may | ||
// create a new span | ||
if (newSession != session) { | ||
val oldSession = session | ||
session = newSession | ||
observers.forEach { | ||
it.onSessionEnded(oldSession) | ||
it.onSessionStarted(session, oldSession) | ||
} | ||
} | ||
return session.getId() | ||
} | ||
|
||
private fun sessionHasExpired(): Boolean { | ||
val elapsedTime = clock.now() - session.getStartTimestamp() | ||
return elapsedTime >= sessionLifetimeNanos | ||
} | ||
|
||
companion object { | ||
@JvmStatic | ||
fun create( | ||
timeoutHandler: SessionIdTimeoutHandler, | ||
sessionLifetimeNanos: Long, | ||
): SessionManagerImpl { | ||
return SessionManagerImpl( | ||
timeoutHandler = timeoutHandler, | ||
sessionLifetimeNanos = sessionLifetimeNanos, | ||
) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.