-
Notifications
You must be signed in to change notification settings - Fork 32
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
17 changed files
with
318 additions
and
48 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
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
118 changes: 118 additions & 0 deletions
118
http4s/src/main/scala/com.snowplowanalytics.snowplow.collector.core/Telemetry.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,118 @@ | ||
package com.snowplowanalytics.snowplow.collector.core | ||
|
||
import org.typelevel.log4cats.Logger | ||
import org.typelevel.log4cats.slf4j.Slf4jLogger | ||
|
||
import cats.data.NonEmptyList | ||
import cats.implicits._ | ||
|
||
import cats.effect.{Async, Resource, Sync} | ||
import cats.effect.std.Random | ||
|
||
import fs2.Stream | ||
|
||
import org.http4s.client.{Client => HttpClient} | ||
|
||
import _root_.io.circe.Json | ||
import _root_.io.circe.syntax._ | ||
|
||
import com.snowplowanalytics.iglu.core.{SchemaKey, SchemaVer, SelfDescribingData} | ||
|
||
import com.snowplowanalytics.snowplow.scalatracker.{Tracker, Tracking} | ||
import com.snowplowanalytics.snowplow.scalatracker.Emitter._ | ||
import com.snowplowanalytics.snowplow.scalatracker.Emitter.{Result => TrackerResult} | ||
import com.snowplowanalytics.snowplow.scalatracker.emitters.http4s.Http4sEmitter | ||
|
||
object Telemetry { | ||
|
||
implicit private def unsafeLogger[F[_]: Sync]: Logger[F] = | ||
Slf4jLogger.getLogger[F] | ||
|
||
def run[F[_]: Async: Tracking]( | ||
telemetryConfig: Config.Telemetry, | ||
httpClient: HttpClient[F], | ||
appInfo: AppInfo, | ||
region: Option[String], | ||
cloud: Option[String] | ||
): Stream[F, Unit] = | ||
if (telemetryConfig.disable) | ||
Stream.empty.covary[F] | ||
else { | ||
val sdj = makeHeartbeatEvent( | ||
telemetryConfig, | ||
region, | ||
cloud, | ||
appInfo.moduleName, | ||
appInfo.version | ||
) | ||
Stream.resource(initTracker(telemetryConfig, appInfo.moduleName, httpClient)).flatMap { tracker => | ||
Stream.fixedDelay[F](telemetryConfig.interval).evalMap { _ => | ||
tracker.trackSelfDescribingEvent(unstructEvent = sdj) >> tracker.flushEmitters() | ||
} | ||
} | ||
} | ||
|
||
private def initTracker[F[_]: Async: Tracking]( | ||
config: Config.Telemetry, | ||
appName: String, | ||
client: HttpClient[F] | ||
): Resource[F, Tracker[F]] = | ||
for { | ||
random <- Resource.eval(Random.scalaUtilRandom[F]) | ||
emitter <- { | ||
implicit val r: Random[F] = random | ||
Http4sEmitter.build( | ||
EndpointParams(config.url, port = Some(config.port), https = config.secure), | ||
client, | ||
retryPolicy = RetryPolicy.MaxAttempts(10), | ||
callback = Some(emitterCallback[F] _) | ||
) | ||
} | ||
} yield new Tracker(NonEmptyList.of(emitter), "tracker-telemetry", appName) | ||
|
||
private def emitterCallback[F[_]: Sync]( | ||
params: EndpointParams, | ||
req: Request, | ||
res: TrackerResult | ||
): F[Unit] = | ||
res match { | ||
case TrackerResult.Success(_) => | ||
Logger[F].debug(s"Telemetry heartbeat successfully sent to ${params.getGetUri}") | ||
case TrackerResult.Failure(code) => | ||
Logger[F].warn(s"Sending telemetry hearbeat got unexpected HTTP code $code from ${params.getUri}") | ||
case TrackerResult.TrackerFailure(exception) => | ||
Logger[F].warn( | ||
s"Telemetry hearbeat failed to reach ${params.getUri} with following exception $exception after ${req.attempt} attempts" | ||
) | ||
case TrackerResult.RetriesExceeded(failure) => | ||
Logger[F].error(s"Stopped trying to send telemetry heartbeat after following failure: $failure") | ||
} | ||
|
||
private def makeHeartbeatEvent( | ||
teleCfg: Config.Telemetry, | ||
region: Option[String], | ||
cloud: Option[String], | ||
appName: String, | ||
appVersion: String | ||
): SelfDescribingData[Json] = | ||
SelfDescribingData( | ||
SchemaKey("com.snowplowanalytics.oss", "oss_context", "jsonschema", SchemaVer.Full(1, 0, 1)), | ||
Json.obj( | ||
"userProvidedId" -> teleCfg.userProvidedId.asJson, | ||
"autoGeneratedId" -> teleCfg.autoGeneratedId.asJson, | ||
"moduleName" -> teleCfg.moduleName.asJson, | ||
"moduleVersion" -> teleCfg.moduleVersion.asJson, | ||
"instanceId" -> teleCfg.instanceId.asJson, | ||
"appGeneratedId" -> java.util.UUID.randomUUID.toString.asJson, | ||
"cloud" -> cloud.asJson, | ||
"region" -> region.asJson, | ||
"applicationName" -> appName.asJson, | ||
"applicationVersion" -> appVersion.asJson | ||
) | ||
) | ||
|
||
case class TelemetryInfo( | ||
region: Option[String], | ||
cloud: Option[String] | ||
) | ||
} |
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
Oops, something went wrong.