-
Notifications
You must be signed in to change notification settings - Fork 580
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
util-stats: Add a TranslatingStatsReceiver that will add a label and …
…also prepend the label value as a scope Problem We want to be able to define metrics that can be identified by both a hierarchical name and a dimensional name, but are otherwise the same metric. However, we normally use "scoped" StatsReceivers which are fundamentally hierarchical only. Solution Add a new way of scoping a StatsReceiver that will allow us to add both the hierarchical prefix and a label at the same time. This will let us progressively migrate our stats by changing the scoping of StatsReceivers. JIRA Issues: CSL-11565 Differential Revision: https://phabricator.twitter.biz/D864997
- Loading branch information
Bryce Anderson
authored and
jenkins
committed
Apr 7, 2022
1 parent
a82980a
commit 67b7055
Showing
4 changed files
with
86 additions
and
0 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
34 changes: 34 additions & 0 deletions
34
util-stats/src/main/scala/com/twitter/finagle/stats/ScopeTranslatingStatsReceiver.scala
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,34 @@ | ||
package com.twitter.finagle.stats | ||
|
||
import com.twitter.finagle.stats.MetricBuilder.Identity | ||
|
||
/** | ||
* A [[TranslatingStatsReceiver]] for working with both dimensional and hierarchical metrics. | ||
* | ||
* Translates the [[MetricBuilder]] to prepend the label value as a scope in addition to adding | ||
* it to the labels map. | ||
*/ | ||
private class ScopeTranslatingStatsReceiver( | ||
sr: StatsReceiver, | ||
labelName: String, | ||
labelValue: String) | ||
extends TranslatingStatsReceiver(sr) { | ||
|
||
require(labelName.nonEmpty) | ||
require(labelValue.nonEmpty) | ||
|
||
private[this] val labelPair = labelName -> labelValue | ||
|
||
protected def translate(builder: MetricBuilder): MetricBuilder = | ||
builder.withIdentity(newIdentity(builder.identity)) | ||
|
||
private[this] def newIdentity(identity: Identity): Identity = identity match { | ||
case Identity.Full(name, labels) => | ||
Identity.Full(labelValue +: name, labels + labelPair) | ||
case Identity.Hierarchical(name, labels) => | ||
Identity.Hierarchical(labelValue +: name, labels + labelPair) | ||
} | ||
|
||
// We preserve this because unfortunately it is sometimes parsed | ||
override def toString: String = s"$self/$labelValue" | ||
} |
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