Skip to content
This repository has been archived by the owner on Oct 19, 2021. It is now read-only.

WIP Fixes #9 Upgrade to cats effect 3 #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ package com.colisweb.tracing.context

import cats.data.OptionT
import cats.effect._
import cats.effect.concurrent.Ref
import cats.effect.kernel.Ref
import cats.syntax.all._
import com.colisweb.tracing.core._
import com.typesafe.scalalogging.StrictLogging
import org.slf4j.Logger

import scala.concurrent.duration.MILLISECONDS

/** A tracing context that will log the beginning and the end of all traces along with
* their tags.
* The traces will be emitted with a TRACE level, so make sure to configure your logging backend
* to ennable the TRACE level for com.colisweb.tracing
*/
class LoggingTracingContext[F[_]: Sync: Timer](
class LoggingTracingContext[F[_]: Sync](
traceIdP: String,
spanIdP: String,
tagsRef: Ref[F, Tags],
Expand All @@ -41,7 +39,7 @@ object LoggingTracingContext extends StrictLogging {
/** Returns a Resource[F, TracingContext[F]]. The first log will be emitted
* as the resource is acquired, the second log when it is released.
*/
def apply[F[_]: Sync: Timer](
def apply[F[_]: Sync](
parentContext: Option[LoggingTracingContext[F]] = None,
idGenerator: Option[F[String]] = None,
slf4jLogger: org.slf4j.Logger = logger.underlying,
Expand All @@ -52,7 +50,7 @@ object LoggingTracingContext extends StrictLogging {
): TracingContextResource[F] =
resource(parentContext, idGenerator, slf4jLogger, operationName, correlationId).evalMap(ctx => ctx.addTags(tags).map(_ => ctx))

private def resource[F[_]: Sync: Timer](
private def resource[F[_]: Sync](
parentContext: Option[LoggingTracingContext[F]],
idGenerator: Option[F[String]],
slf4jLogger: org.slf4j.Logger,
Expand All @@ -68,7 +66,7 @@ object LoggingTracingContext extends StrictLogging {
tagsRef <- Ref[F].of[Tags](Map.empty)
spanId <- idGeneratorValue
traceId <- traceIdF
start <- Clock[F].monotonic(MILLISECONDS)
start <- Clock[F].monotonic.map(_.toMillis)
ctx = new LoggingTracingContext[F](traceId, spanId, tagsRef, correlationId)
details = SpanDetails(start, traceId, spanId, ctx, tagsRef)
_ <- logger.trace("Trace {} Starting Span {} ({})", traceId, spanId, operationName)
Expand All @@ -78,7 +76,7 @@ object LoggingTracingContext extends StrictLogging {
case SpanDetails(start, traceId, spanId, _, tagsRef) =>
for {
tags <- tagsRef.get
end <- Clock[F].monotonic(MILLISECONDS)
end <- Clock[F].monotonic.map(_.toMillis)
duration = end - start
_ <- logger.trace(
"Trace {} Finished Span {} ({}) in {}ms. Tags: {}",
Expand Down Expand Up @@ -109,7 +107,7 @@ object LoggingTracingContext extends StrictLogging {
* This is provided for convenience and consistency with regards to the other
* tracing contexts types.
*/
def builder[F[_]: Sync: Timer]: F[TracingContextBuilder[F]] =
def builder[F[_]: Sync]: F[TracingContextBuilder[F]] =
Sync[F].delay((operationName: String, tags: Tags, correlationId: String) =>
LoggingTracingContext.apply(correlationId = correlationId)(operationName, tags)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ trait RequestWithCorrelationIdHelper {

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ trait TracedRoutes {

implicit class TracedEndpoint[In, Err, Out](e: Endpoint[In, Err, Out, Any]) {

def toTracedRoute[F[_]: Sync: Concurrent: Timer](logic: (In, TracingContext[F]) => F[Either[Err, Out]])(implicit
def toTracedRoute[F[_]: Sync](logic: (In, TracingContext[F]) => F[Either[Err, Out]])(implicit
builder: TracingContextBuilder[F],
cs: ContextShift[F],
serverOptions: Http4sServerOptions[F]
): HttpRoutes[F] = {

Expand All @@ -37,10 +36,9 @@ trait TracedRoutes {
implicit class TracedEndpointRecoverErrors[In, Err <: Throwable, Out](
e: Endpoint[In, Err, Out, Any]
) {
def toTracedRouteRecoverErrors[F[_]: Sync: Concurrent: Timer](logic: (In, TracingContext[F]) => F[Out])(implicit
def toTracedRouteRecoverErrors[F[_]: Sync](logic: (In, TracingContext[F]) => F[Out])(implicit
builder: TracingContextBuilder[F],
eClassTag: ClassTag[Err],
cs: ContextShift[F],
serverOptions: Http4sServerOptions[F]
): HttpRoutes[F] =
TracedHttpRoutes.wrapHttpRoutes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import sttp.tapir.generic.auto._
import scala.concurrent.ExecutionContext

class TapirSpec extends AsyncFunSpec with Matchers {
implicit val timer : Timer[IO] = IO.timer(ExecutionContext.global)
implicit val timer: Timer[IO] = IO.timer(ExecutionContext.global)
import TapirSpec._

describe("Tapir Integration") {
Expand Down
2 changes: 1 addition & 1 deletion project/CompileTimeDependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import sbt._
object Versions {

final val cats = "2.4.2"
final val catsEffect = "2.3.3"
final val catsEffect = "3.0.0-RC2"
final val circe = "0.13.0"
final val datadog = "0.68.0"
final val fs2 = "3.0.1"
Expand Down