Skip to content

Commit

Permalink
Finagle Toggles - Export toggle value metric
Browse files Browse the repository at this point in the history
Adding toggle value to observability data to enable the
Integration between [[ https://docbird.twitter.biz/precog/interfaces/instigator/FTInstigator.html |  Precog FT ]] and  [[ https://docs.google.com/document/d/1NwnQMOWeZp7Ys3wUePTdDrx3fQ3vTtMaHo22T9zBYz4/edit#heading=h.bz9cvnszgqss | Core Entity Service: Graceful Degradation
 ]]
 probing the toggle value using Observability metrics to avoid calling admin point for every shard

Differential Revision: https://phabricator.twitter.biz/D1138842
  • Loading branch information
Ahmed Abdelhady authored and jenkins committed Apr 24, 2024
1 parent 9002aca commit d1468ba
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import java.util.{function => juf}
import java.{lang => jl}
import scala.annotation.varargs
import scala.collection.JavaConverters._
import scala.collection.{immutable, mutable}
import scala.collection.immutable
import scala.collection.mutable
import scala.util.hashing.MurmurHash3

/**
Expand Down Expand Up @@ -170,6 +171,9 @@ object ToggleMap {
}
crc32.getValue.toFloat
}
private[this] val toggleValue = statsReceiver.addGauge("fraction") {
iterator.toIndexedSeq.headOption.map(_.fraction.toFloat).getOrElse(0)
}

def underlying: ToggleMap = toggleMap

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.twitter.finagle.toggle

import com.twitter.finagle.stats.{InMemoryStatsReceiver, NullStatsReceiver}
import com.twitter.logging.{BareFormatter, Level, Logger, StringHandler}
import com.twitter.finagle.stats.InMemoryStatsReceiver
import com.twitter.finagle.stats.NullStatsReceiver
import com.twitter.logging.BareFormatter
import com.twitter.logging.Level
import com.twitter.logging.Logger
import com.twitter.logging.StringHandler
import org.scalacheck.Arbitrary.arbitrary
import org.scalacheck.Gen
import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks
Expand Down Expand Up @@ -44,6 +48,32 @@ class ToggleMapTest extends AnyFunSuite with ScalaCheckDrivenPropertyChecks with
assert(state2 == gauge())
}

test("ToggleMap.observed produces a toggleValue summary") {
val stats = new InMemoryStatsReceiver()
val inMem = ToggleMap.newMutable()
val map = ToggleMap.observed(inMem, stats)

val gauge: () => Float = stats.gauges(Seq("fraction"))

val initial = 0f
val state1 = 0.5f
val state2 = 0.1f

// without changes
assert(initial == gauge())

val toggleName = "com.id"

// validate fraction changes after an add
inMem.put(toggleName, 0.5)
assert(state1 == gauge())

// validate fraction changes after an update
inMem.put(toggleName, 0.1)
assert(state2 == gauge())

}

test("ToggleMap.observed produces Toggle.Captured") {
val inMem = ToggleMap.newMutable()
val tm = ToggleMap.observed(inMem, NullStatsReceiver)
Expand Down

0 comments on commit d1468ba

Please sign in to comment.