Skip to content

Commit

Permalink
Tracker should not be a case class (close #152)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Streeter authored and istreeter committed Dec 23, 2020
1 parent 299e747 commit 7d82204
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,16 @@ import scala.concurrent.duration._
* @param namespace Tracker namespace
* @param appId ID of the application
* @param encodeBase64 Whether to encode JSONs
* @param metadata optionally a json containing the metadata context for the running instance
* @param globalContexts Contexts to attach to every payload
*/
final case class Tracker[F[_]: Monad: Clock: UUIDProvider](emitters: NonEmptyList[Emitter[F]],
namespace: String,
appId: String,
defaultSubject: Subject = Subject(),
encodeBase64: Boolean = true,
metadata: Option[SelfDescribingJson] = None) {
class Tracker[F[_]: Monad: Clock: UUIDProvider](emitters: NonEmptyList[Emitter[F]],
namespace: String,
appId: String,
defaultSubject: Subject = Subject(),
encodeBase64: Boolean = true,
globalContexts: List[SelfDescribingJson] = Nil) {
import Tracker._

/**
* Send assembled payload to emitters or schedule it as callback of getting context
*
* @param payload constructed event map
*/
private def track(payload: Payload): F[Unit] = {
val payloadToSend = metadata
.map(context => addContexts(payload, List(context)))
.getOrElse(payload)

send(payloadToSend)
}

/**
* Pass the assembled payload to every emitter
*
Expand Down Expand Up @@ -96,7 +83,7 @@ final case class Tracker[F[_]: Monad: Clock: UUIDProvider](emitters: NonEmptyLis
newPayload
}

addContexts(payloadWithTimestamp, contexts)
addContexts(payloadWithTimestamp, globalContexts ++ contexts)
}

/**
Expand Down Expand Up @@ -136,7 +123,7 @@ final case class Tracker[F[_]: Monad: Clock: UUIDProvider](emitters: NonEmptyLis
.addJson(envelope.normalize, encodeBase64, "ue_px", "ue_pr")

completePayload(payload, contexts, timestamp, subject)
.flatMap(track)
.flatMap(send)
}

/**
Expand Down Expand Up @@ -170,7 +157,7 @@ final case class Tracker[F[_]: Monad: Clock: UUIDProvider](emitters: NonEmptyLis
.add("se_va", value.map(_.toString))

completePayload(payload, contexts, timestamp, subject)
.flatMap(track)
.flatMap(send)
}

/**
Expand Down Expand Up @@ -198,7 +185,7 @@ final case class Tracker[F[_]: Monad: Clock: UUIDProvider](emitters: NonEmptyLis
.add("refr", referrer)

completePayload(payload, contexts, timestamp, subject)
.flatMap(track)
.flatMap(send)
}

/**
Expand Down Expand Up @@ -244,7 +231,7 @@ final case class Tracker[F[_]: Monad: Clock: UUIDProvider](emitters: NonEmptyLis
.add("tr_cu", currency)

completePayload(payload, contexts, timestamp, subject)
.flatMap(track)
.flatMap(send)
}

/**
Expand Down Expand Up @@ -282,7 +269,7 @@ final case class Tracker[F[_]: Monad: Clock: UUIDProvider](emitters: NonEmptyLis
.add("ti_cu", currency)

completePayload(payload, contexts, timestamp, subject)
.flatMap(track)
.flatMap(send)
}

/**
Expand Down Expand Up @@ -402,13 +389,22 @@ final case class Tracker[F[_]: Monad: Clock: UUIDProvider](emitters: NonEmptyLis
* @return The tracker instance
*/
def setSubject(newSubject: Subject): Tracker[F] =
new Tracker[F](emitters, namespace, appId, newSubject, encodeBase64, metadata)
new Tracker[F](emitters, namespace, appId, newSubject, encodeBase64, globalContexts)

/**
* Add a global context.
*
* @param newContext The global context to attach to every event.
* @return The tracker instance
*/
def addContext(newContext: SelfDescribingJson): Tracker[F] =
new Tracker[F](emitters, namespace, appId, defaultSubject, encodeBase64, newContext :: globalContexts)
}

object Tracker {

def apply[F[_]: Monad: Clock: UUIDProvider](emitter: Emitter[F], namespace: String, appId: String): Tracker[F] =
Tracker(NonEmptyList.one(emitter), namespace, appId)
new Tracker(NonEmptyList.one(emitter), namespace, appId)

/** Tracker's version */
val Version = s"scala-${generated.ProjectSettings.version}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
*/
package com.snowplowanalytics.snowplow.scalatracker

import cats._
import cats.implicits._
import cats.effect.{Clock, Sync}
import cats.effect.Sync
import scalaj.http.{HttpRequest, HttpResponse}

package object metadata {
Expand All @@ -27,17 +26,17 @@ package object metadata {
* Adds EC2 context to each sent event
* Blocks event queue until either context resolved or timed out
*/
def enableEc2Context[G[_]: Sync](implicit F: Monad[F], clock: Clock[F], uuid: UUIDProvider[F]): G[Tracker[F]] =
def enableEc2Context[G[_]: Sync](): G[Tracker[F]] =
new Ec2Metadata[G]().getInstanceContext
.map(metadata => tracker.copy(metadata = Some(metadata)))
.map(metadata => tracker.addContext(metadata))

/**
* Adds GCP context to each sent event
* Blocks event queue until either context resolved or timed out
*/
def enableGceContext[G[_]: Sync](implicit F: Monad[F], clock: Clock[F], uuid: UUIDProvider[F]): G[Tracker[F]] =
def enableGceContext[G[_]: Sync](): G[Tracker[F]] =
new GceMetadata[G]().getInstanceContext
.map(metadata => tracker.copy(metadata = Some(metadata)))
.map(metadata => tracker.addContext(metadata))

}

Expand Down

0 comments on commit 7d82204

Please sign in to comment.