Hi, I was directed to ask questions about session duration to this pull request:
Situation: I have a CLI command that I wish to track duration between commands, one command opens something, the other closes something. In this case, the longer period between session is better. Unlike tracking durations of web request fulfillment, I need to track how long the user has a session open, where "open" means they are using the product. Short durations mean that they aren't really using the product.
What I have tried and would like validation of is the following:
const timestamp = new Date()
if (params.clean) { // this is the end of a session, log the duration to sentry
const startTimestamp: string = await session.get('timestamp')
const duration = (timestamp as any) - (new Date(startTimestamp) as any) // in ms
if (duration) {
Sentry.getCurrentHub().startSession({ duration, ignoreDuration: false })
Sentry.getCurrentHub().endSession()
}
} else { // This is the start of the session, set the session timestamp.
Sentry.getCurrentHub().startSession({ ignoreDuration: true })
await session.set('timestamp', timestamp.toISOString())
Sentry.getCurrentHub().endSession()
}
Here, 'clean' means the user is closing a session, otherwise, the session is being opened. We track the start timestamp in our own session object and then calculate the duration from this when the 'clean' is given.
I have also tried:
Sentry.captureMessage(util.format('%O', { duration: durationSeconds, params, session }))
But I am not sure how this would be useful.
Perhaps this is what I need to do:
if (params.clean) { // this is the end of a session, log the duration to sentry
const started: string = await session.get('timestamp')
if (started) {
const duration = (new Date() as any) - (new Date(started) as any) // in ms
const sentrySession: SessionContext = {
sid: randomUUID(),
init: true,
//@ts-ignore
started: started,
duration,
status: SessionStatus.Exited,
ignoreDuration: false,
//@ts-ignore
attrs: {
release: '@c6o/cli'
}
}
Sentry.getCurrentHub().startSession(sentrySession)
Sentry.getCurrentHub().endSession()
}
} else { // This is the start of the session, set the session timestamp.
await session.set('timestamp', new Date().toISOString())
}
Will this work to record the duration for our open/clean cycles of usage so that we can visualize the results in a dashboard or in the performance area of the Sentry website?
How should this be done?
Originally posted by @robblovell in #323 (comment)