-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
165 additions
and
35 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
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
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
89 changes: 89 additions & 0 deletions
89
modules/loader/src/main/scala/com/snowplowanalytics/snowplow/rdbloader/dsl/StatsD.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,89 @@ | ||
/* | ||
* Copyright (c) 2021-2021 Snowplow Analytics Ltd. All rights reserved. | ||
* | ||
* This program is licensed to you under the Apache License Version 2.0, | ||
* and you may not use this file except in compliance with the Apache License Version 2.0. | ||
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the Apache License Version 2.0 is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. | ||
*/ | ||
package com.snowplowanalytics.snowplow.rdbloader.dsl | ||
|
||
import java.time.Duration | ||
import java.net.{DatagramPacket, DatagramSocket, InetAddress} | ||
import java.nio.charset.StandardCharsets.UTF_8 | ||
|
||
import cats.Monad | ||
import cats.implicits._ | ||
|
||
import cats.effect.{Blocker, ContextShift, Resource, Sync, Timer} | ||
|
||
import com.snowplowanalytics.snowplow.rdbloader.common.LoaderMessage | ||
import com.snowplowanalytics.snowplow.rdbloader.common.config.Config | ||
|
||
trait StatsD[F[_]] { | ||
def sendMetrics(loaded: LoaderMessage.ShreddingComplete): F[Unit] | ||
} | ||
|
||
object StatsD { | ||
|
||
final case class KVMetric(key: String, value: String) | ||
|
||
private val MinLatencyName = "latency.min" | ||
private val MaxLatencyName = "latency.max" | ||
private val CountGoodName = "count.good" | ||
|
||
def getInstance[F[_]: ContextShift: Monad: Sync: Timer](statsDConfig: Option[Config.StatsD], blocker: Blocker): StatsD[F] = | ||
statsDConfig match { | ||
case Some(config) => | ||
new StatsD[F] { | ||
def sendMetrics(loaded: LoaderMessage.ShreddingComplete): F[Unit] = | ||
mkSocket[F](blocker).use { socket => | ||
for { | ||
metrics <- getMetrics[F](loaded) | ||
formatted = metrics.map(statsDFormat(config)) | ||
ip <- blocker.delay(InetAddress.getByName(config.hostname)) | ||
_ <- formatted.traverse_(sendMetric[F](blocker, socket, ip, config.port)) | ||
} yield () | ||
} | ||
} | ||
case None => | ||
new StatsD[F] { | ||
def sendMetrics(loaded: LoaderMessage.ShreddingComplete): F[Unit] = Monad[F].unit | ||
} | ||
} | ||
private def mkSocket[F[_]: ContextShift: Sync](blocker: Blocker): Resource[F, DatagramSocket] = | ||
Resource.fromAutoCloseableBlocking(blocker)(Sync[F].delay(new DatagramSocket)) | ||
|
||
private def getMetrics[F[_]: Monad: Timer](loaded: LoaderMessage.ShreddingComplete): F[List[KVMetric]] = | ||
Timer[F].clock.instantNow.map { now => | ||
List( | ||
loaded.timestamps.min.map(min => Duration.between(min, now).toSeconds()).map(l => KVMetric(MaxLatencyName, l.toString)), | ||
loaded.timestamps.max.map(max => Duration.between(max, now).toSeconds()).map(l => KVMetric(MinLatencyName, l.toString)), | ||
loaded.count.map(c => KVMetric(CountGoodName, c.toString)) | ||
) | ||
.collect { case Some(m) => m } | ||
} | ||
|
||
private def sendMetric[F[_]: ContextShift: Sync]( | ||
blocker: Blocker, | ||
socket: DatagramSocket, | ||
addr: InetAddress, | ||
port: Int | ||
)( | ||
metric: String | ||
): F[Unit] = { | ||
val bytes = metric.getBytes(UTF_8) | ||
val packet = new DatagramPacket(bytes, bytes.length, addr, port) | ||
blocker.delay(socket.send(packet)) | ||
} | ||
|
||
private def statsDFormat(config: Config.StatsD)(metric: KVMetric): String = { | ||
val tagStr = config.tags.map { case (k, v) => s"$k:$v" }.mkString(",") | ||
val prefix = config.metricsPrefix | ||
s"${prefix}${metric.key}:${metric.value}|g|#$tagStr" | ||
} | ||
} |
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
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