forked from kamon-io/Kamon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
! statsd: allow custom statsd senders + add simple statsd sender whic…
…h doesn't batch stats
- Loading branch information
Showing
12 changed files
with
519 additions
and
232 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
16 changes: 16 additions & 0 deletions
16
kamon-statsd/src/main/scala/kamon/statsd/SimpleMetricKeyGenerator.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
62 changes: 62 additions & 0 deletions
62
kamon-statsd/src/main/scala/kamon/statsd/SimpleStatsDMetricsSender.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,62 @@ | ||
/* | ||
* ========================================================================================= | ||
* Copyright © 2013-2015 the kamon project <http://kamon.io/> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file | ||
* except in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the | ||
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. See the License for the specific language governing permissions | ||
* and limitations under the License. | ||
* ========================================================================================= | ||
*/ | ||
|
||
package kamon.statsd | ||
|
||
import akka.actor.Props | ||
import com.typesafe.config.Config | ||
import kamon.metric.SubscriptionsDispatcher.TickMetricSnapshot | ||
import kamon.metric.instrument.{ Counter, Histogram } | ||
|
||
/** | ||
* Factory for [[SimpleStatsDMetricsSender]]. | ||
* Use FQCN of the object in "kamon.statsd.statsd-metrics-sender" | ||
* to select [[SimpleStatsDMetricsSender]] as your sender | ||
*/ | ||
object SimpleStatsDMetricsSender extends StatsDMetricsSenderFactory { | ||
override def props(statsDConfig: Config, metricKeyGenerator: MetricKeyGenerator): Props = | ||
Props(new SimpleStatsDMetricsSender(statsDConfig, metricKeyGenerator)) | ||
} | ||
|
||
/** | ||
* "Traditional" StatsD sender which sends a UDP packet for each piece of data it receives. | ||
* @param statsDConfig Config to read settings specific to this sender | ||
* @param metricKeyGenerator Key generator for all metrics sent by this sender | ||
*/ | ||
class SimpleStatsDMetricsSender(statsDConfig: Config, metricKeyGenerator: MetricKeyGenerator) | ||
extends UDPBasedStatsDMetricsSender(statsDConfig, metricKeyGenerator) { | ||
|
||
def writeMetricsToRemote(tick: TickMetricSnapshot, flushToUDP: String ⇒ Unit): Unit = { | ||
|
||
for ( | ||
(entity, snapshot) ← tick.metrics; | ||
(metricKey, metricSnapshot) ← snapshot.metrics | ||
) { | ||
|
||
val keyPrefix = metricKeyGenerator.generateKey(entity, metricKey) + ":" | ||
|
||
metricSnapshot match { | ||
case hs: Histogram.Snapshot ⇒ | ||
hs.recordsIterator.foreach { record ⇒ | ||
flushToUDP(keyPrefix + encodeStatsDTimer(record.level, record.count)) | ||
} | ||
|
||
case cs: Counter.Snapshot ⇒ | ||
flushToUDP(keyPrefix + encodeStatsDCounter(cs.count)) | ||
} | ||
} | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
kamon-statsd/src/main/scala/kamon/statsd/StatsDMetricsSenderFactory.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,24 @@ | ||
/* | ||
* ========================================================================================= | ||
* Copyright © 2013-2015 the kamon project <http://kamon.io/> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file | ||
* except in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the | ||
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. See the License for the specific language governing permissions | ||
* and limitations under the License. | ||
* ========================================================================================= | ||
*/ | ||
|
||
package kamon.statsd | ||
|
||
import akka.actor.Props | ||
import com.typesafe.config.Config | ||
|
||
trait StatsDMetricsSenderFactory { | ||
def props(statsDConfig: Config, metricKeyGenerator: MetricKeyGenerator): Props | ||
} |
76 changes: 76 additions & 0 deletions
76
kamon-statsd/src/main/scala/kamon/statsd/UDPBasedStatsDMetricsSender.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,76 @@ | ||
/* | ||
* ========================================================================================= | ||
* Copyright © 2013-2015 the kamon project <http://kamon.io/> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file | ||
* except in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the | ||
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. See the License for the specific language governing permissions | ||
* and limitations under the License. | ||
* ========================================================================================= | ||
*/ | ||
|
||
package kamon.statsd | ||
|
||
import java.net.InetSocketAddress | ||
import java.text.{ DecimalFormat, DecimalFormatSymbols } | ||
import java.util.Locale | ||
import akka.actor.{ Actor, ActorRef, ActorSystem } | ||
import akka.io.{ IO, Udp } | ||
import akka.util.ByteString | ||
import com.typesafe.config.Config | ||
import kamon.metric.SubscriptionsDispatcher.TickMetricSnapshot | ||
|
||
/** | ||
* Base class for different StatsD senders utilizing UDP protocol. It implies use of one statsd server. | ||
* @param statsDConfig Config to read settings specific to this sender | ||
* @param metricKeyGenerator Key generator for all metrics sent by this sender | ||
*/ | ||
abstract class UDPBasedStatsDMetricsSender(statsDConfig: Config, metricKeyGenerator: MetricKeyGenerator) | ||
extends Actor with UdpExtensionProvider { | ||
|
||
import context.system | ||
|
||
val statsDHost = statsDConfig.getString("hostname") | ||
val statsDPort = statsDConfig.getInt("port") | ||
|
||
val symbols = DecimalFormatSymbols.getInstance(Locale.US) | ||
symbols.setDecimalSeparator('.') // Just in case there is some weird locale config we are not aware of. | ||
|
||
// Absurdly high number of decimal digits, let the other end lose precision if it needs to. | ||
val samplingRateFormat = new DecimalFormat("#.################################################################", symbols) | ||
|
||
udpExtension ! Udp.SimpleSender | ||
|
||
lazy val socketAddress = new InetSocketAddress(statsDHost, statsDPort) | ||
|
||
def receive = { | ||
case Udp.SimpleSenderReady ⇒ | ||
context.become(ready(sender)) | ||
} | ||
|
||
def ready(udpSender: ActorRef): Receive = { | ||
case tick: TickMetricSnapshot ⇒ | ||
writeMetricsToRemote(tick, | ||
(data: String) ⇒ udpSender ! Udp.Send(ByteString(data), socketAddress)) | ||
} | ||
|
||
def writeMetricsToRemote(tick: TickMetricSnapshot, flushToUDP: String ⇒ Unit): Unit | ||
|
||
def encodeStatsDTimer(level: Long, count: Long): String = { | ||
val samplingRate: Double = 1D / count | ||
level.toString + "|ms" + (if (samplingRate != 1D) "|@" + samplingRateFormat.format(samplingRate) else "") | ||
} | ||
|
||
def encodeStatsDCounter(count: Long): String = count.toString + "|c" | ||
|
||
} | ||
|
||
trait UdpExtensionProvider { | ||
def udpExtension(implicit system: ActorSystem): ActorRef = IO(Udp) | ||
} | ||
|
Oops, something went wrong.