the-algorithm/follow-recommendations-service/common/src/main/scala/com/twitter/follow_recommendations/common/utils/RescueWithStatsUtils.scala
twitter-team ef4c5eb65e Twitter Recommendation Algorithm
Please note we have force-pushed a new initial commit in order to remove some publicly-available Twitter user information. Note that this process may be required in the future.
2023-03-31 17:36:31 -05:00

51 lines
1.4 KiB
Scala

package com.twitter.follow_recommendations.common.utils
import com.twitter.finagle.stats.StatsReceiver
import com.twitter.follow_recommendations.common.base.StatsUtil
import com.twitter.stitch.Stitch
import com.twitter.util.Duration
import com.twitter.util.TimeoutException
object RescueWithStatsUtils {
def rescueWithStats[T](
s: Stitch[Seq[T]],
stats: StatsReceiver,
source: String
): Stitch[Seq[T]] = {
StatsUtil.profileStitchSeqResults(s, stats.scope(source)).rescue {
case _: Exception => Stitch.Nil
}
}
def rescueOptionalWithStats[T](
s: Stitch[Option[T]],
stats: StatsReceiver,
source: String
): Stitch[Option[T]] = {
StatsUtil.profileStitchOptionalResults(s, stats.scope(source)).rescue {
case _: Exception => Stitch.None
}
}
def rescueWithStatsWithin[T](
s: Stitch[Seq[T]],
stats: StatsReceiver,
source: String,
timeout: Duration
): Stitch[Seq[T]] = {
val hydratedScopeSource = stats.scope(source)
StatsUtil
.profileStitchSeqResults(
s.within(timeout)(com.twitter.finagle.util.DefaultTimer),
hydratedScopeSource)
.rescue {
case _: TimeoutException =>
hydratedScopeSource.counter("timeout").incr()
Stitch.Nil
case _: Exception =>
hydratedScopeSource.counter("exception").incr()
Stitch.Nil
}
}
}