-
Notifications
You must be signed in to change notification settings - Fork 41
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
16 changed files
with
360 additions
and
83 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
131 changes: 131 additions & 0 deletions
131
...ommon-fs2/src/main/scala/com/snowplowanalytics/snowplow/enrich/common/fs2/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,131 @@ | ||
/* | ||
* 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.enrich.common.fs2 | ||
|
||
import org.typelevel.log4cats.Logger | ||
import org.typelevel.log4cats.slf4j.Slf4jLogger | ||
|
||
import cats.data.NonEmptyList | ||
import cats.implicits._ | ||
|
||
import cats.effect.{ConcurrentEffect, Resource, Sync, Timer} | ||
|
||
import fs2.Stream | ||
|
||
import org.http4s.client.{Client => HttpClient} | ||
|
||
import _root_.io.circe.Json | ||
import _root_.io.circe.Encoder | ||
import _root_.io.circe.syntax._ | ||
|
||
import com.snowplowanalytics.iglu.core.{SchemaKey, SchemaVer, SelfDescribingData} | ||
|
||
import com.snowplowanalytics.snowplow.scalatracker.Tracker | ||
import com.snowplowanalytics.snowplow.scalatracker.Emitter._ | ||
import com.snowplowanalytics.snowplow.scalatracker.Emitter.{Result => TrackerResult} | ||
import com.snowplowanalytics.snowplow.scalatracker.emitters.http4s.Http4sEmitter | ||
|
||
import com.snowplowanalytics.snowplow.enrich.common.fs2.config.io.{Telemetry => TelemetryConfig} | ||
|
||
object Telemetry { | ||
|
||
private implicit def unsafeLogger[F[_]: Sync]: Logger[F] = | ||
Slf4jLogger.getLogger[F] | ||
|
||
def run[F[_]: ConcurrentEffect: Timer, A](env: Environment[F, A]): Stream[F, Unit] = | ||
env.telemetryConfig.disable match { | ||
case true => | ||
Stream.empty.covary[F] | ||
case _ => | ||
val sdj = makeHeartbeatEvent( | ||
env.telemetryConfig, | ||
env.region, | ||
env.cloud, | ||
env.processor.artifact, | ||
env.processor.version | ||
) | ||
val tracker = initTracker(env.telemetryConfig, env.processor.artifact, env.httpClient) | ||
Stream | ||
.fixedDelay[F](env.telemetryConfig.interval) | ||
.evalMap { _ => | ||
tracker.use { t => | ||
t.trackSelfDescribingEvent(unstructEvent = sdj) >> | ||
t.flushEmitters() | ||
} | ||
} | ||
} | ||
|
||
private def initTracker[F[_]: ConcurrentEffect: Timer]( | ||
config: TelemetryConfig, | ||
appName: String, | ||
client: HttpClient[F] | ||
): Resource[F, Tracker[F]] = | ||
for { | ||
emitter <- Http4sEmitter.build( | ||
EndpointParams(config.collectorUri, port = Some(config.collectorPort), 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: TelemetryConfig, | ||
region: Option[String], | ||
cloud: Option[Cloud], | ||
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 | ||
) | ||
) | ||
|
||
sealed trait Cloud | ||
|
||
object Cloud { | ||
case object Aws extends Cloud | ||
case object Gcp extends Cloud | ||
|
||
implicit val encoder: Encoder[Cloud] = Encoder.encodeString.contramap[Cloud](_.toString().toUpperCase()) | ||
} | ||
} |
Oops, something went wrong.