Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mock to root aggregate and integrate latest API changes #712

Merged
merged 6 commits into from
Feb 8, 2023
Merged
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
4 changes: 3 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ lazy val root = tlCrossRootProject.aggregate(
datadog,
log,
newrelic,
mock,
mtl,
noop,
xray,
Expand Down Expand Up @@ -331,7 +332,8 @@ lazy val mock = project
description := "Mock Open Tracing implementation",
libraryDependencies ++= Seq(
"io.opentracing" % "opentracing-mock" % "0.33.0"
)
),
tlVersionIntroduced := List("2.12", "2.13", "3").map(_ -> "0.3.1").toMap
)

lazy val examples = project
Expand Down
2 changes: 1 addition & 1 deletion modules/docs/src/main/scala/Junk.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Typelevel
* Copyright (c) 2023 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
Expand Down
82 changes: 68 additions & 14 deletions modules/mock/src/main/scala/MockEntryPoint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,98 @@
package natchez
package mock

import scala.jdk.CollectionConverters._

import cats.effect.{Resource, Sync}
import cats.syntax.all._
import io.{opentracing => ot}
import io.opentracing.mock.MockTracer
import io.opentracing.propagation.{Format, TextMapAdapter}
import natchez.Span.SpanKind

/** Implementation is inspired from other entrypoints such as
* - JaegerEntryPoint
* - LightstepEntryPoint
*/
final case class MockEntrypoint[F[_]: Sync]() extends EntryPoint[F] {

val mockTracer = new MockTracer()

override def root(name: String): Resource[F, Span[F]] =
override def root(name: String, options: Span.Options): Resource[F, Span[F]] =
Resource
.make(Sync[F].delay(mockTracer.buildSpan(name).start()))(span => Sync[F].delay(span.finish()))
.make(
Sync[F]
.delay(mockTracer.buildSpan(name))
.flatMap(setOptionsAndStart(options))
)(span => Sync[F].delay(span.finish()))
.map(MockSpan(mockTracer, _))

override def continue(
name: String,
kernel: Kernel
kernel: Kernel,
options: Span.Options
): Resource[F, Span[F]] =
Resource
.make(
Sync[F].delay {
val spanCtxt = mockTracer.extract(
Format.Builtin.HTTP_HEADERS,
new TextMapAdapter(kernel.toHeaders.asJava)
)
mockTracer.buildSpan(name).asChildOf(spanCtxt).start()
}
Sync[F]
.delay {
val spanCtxt = mockTracer.extract(
Format.Builtin.HTTP_HEADERS,
new TextMapAdapter(kernel.toJava)
)
mockTracer.buildSpan(name).asChildOf(spanCtxt)
}
.flatMap(setOptionsAndStart(options))
)(span => Sync[F].delay(span.finish()))
.map(MockSpan(mockTracer, _))

override def continueOrElseRoot(
name: String,
kernel: Kernel
kernel: Kernel,
options: Span.Options
): Resource[F, Span[F]] =
continue(name, kernel).flatMap {
continue(name, kernel, options).flatMap {
case null =>
root(name)
case span => Resource.pure[F, Span[F]](span)
}

private def spanContextFromKernel(kernel: Kernel): F[ot.SpanContext] =
Sync[F].delay {
mockTracer.extract(
Format.Builtin.HTTP_HEADERS,
new TextMapAdapter(kernel.toJava)
)
}

private def setOptionsAndStart(
options: Span.Options
)(spanBuilder: MockTracer#SpanBuilder): F[ot.mock.MockSpan] =
options.links
.foldM(spanBuilder)(addLink(_)(_))
.flatMap(setSpanKind(options.spanKind))
.flatMap(sb => Sync[F].delay(sb.start()))

// https://opentelemetry.io/docs/reference/specification/compatibility/opentracing/#tracer-shim
private def addLink(
spanBuilder: MockTracer#SpanBuilder
)(kernel: Kernel): F[MockTracer#SpanBuilder] =
spanContextFromKernel(kernel).flatMap { spanContext =>
Sync[F].delay {
spanBuilder.addReference("follows_from", spanContext)
}
}

private def setSpanKind(
spanKind: SpanKind
)(spanBuilder: MockTracer#SpanBuilder): F[MockTracer#SpanBuilder] =
spanKindTag
.lift(spanKind)
.foldM(spanBuilder)((sb, k) => Sync[F].delay(sb.withTag("span.kind", k)))

// https://opentelemetry.io/docs/reference/specification/trace/api/#spankind
private val spanKindTag: PartialFunction[SpanKind, String] = {
case SpanKind.Client => "client"
case SpanKind.Server => "server"
case SpanKind.Consumer => "consumer"
case SpanKind.Producer => "producer"
}
}
51 changes: 27 additions & 24 deletions modules/mock/src/main/scala/MockSpan.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import natchez.TraceValue.{BooleanValue, NumberValue, StringValue}
import java.net.URI

final case class MockSpan[F[_]: Sync](tracer: ot.mock.MockTracer, span: ot.mock.MockSpan)
extends Span[F] {
extends Span.Default[F] {

def kernel: F[Kernel] =
Sync[F].delay {
Expand All @@ -27,7 +27,7 @@ final case class MockSpan[F[_]: Sync](tracer: ot.mock.MockTracer, span: ot.mock.
Format.Builtin.HTTP_HEADERS,
new TextMapAdapter(m)
)
Kernel(m.asScala.toMap)
Kernel.fromJava(m)
}

def put(fields: (String, TraceValue)*): F[Unit] =
Expand All @@ -37,20 +37,22 @@ final case class MockSpan[F[_]: Sync](tracer: ot.mock.MockTracer, span: ot.mock.
case (k, BooleanValue(v)) => Sync[F].delay(span.setTag(k, v))
}

def attachError(err: Throwable): F[Unit] =
def attachError(err: Throwable, fields: (String, TraceValue)*): F[Unit] =
put(
Tags.ERROR.getKey -> true
) >>
Sync[F].delay {
span.log(
Map(
span.log {
val otherFields = fields.toList.nested.map(_.value).value.toMap
val errorFields = Map(
Fields.EVENT -> "error",
Fields.ERROR_OBJECT -> err,
Fields.ERROR_KIND -> err.getClass.getSimpleName,
Fields.MESSAGE -> err.getMessage,
Fields.STACK -> err.getStackTrace.mkString
).asJava
)
)
(otherFields ++ errorFields).asJava
}
}.void

override def log(fields: (String, TraceValue)*): F[Unit] = {
Expand All @@ -61,24 +63,25 @@ final case class MockSpan[F[_]: Sync](tracer: ot.mock.MockTracer, span: ot.mock.
override def log(event: String): F[Unit] =
Sync[F].delay(span.log(event)).void

def span(name: String): Resource[F, Span[F]] =
Resource
.make {
Sync[F].delay(tracer.buildSpan(name).asChildOf(span).start)
} { s =>
Sync[F].delay(s.finish())
}
.map(MockSpan(tracer, _))
override protected val spanCreationPolicyOverride: Span.Options.SpanCreationPolicy =
Span.Options.SpanCreationPolicy.Default

def span(name: String, kernel: Kernel): Resource[F, Span[F]] = {
val parent =
tracer.extract(Format.Builtin.HTTP_HEADERS, new TextMapAdapter(kernel.toHeaders.asJava))
Resource
.make(Sync[F].delay(tracer.buildSpan(name).asChildOf(span).asChildOf(parent).start)) { s =>
Sync[F].delay(s.finish())
}
.map(MockSpan(tracer, _))
}
override def makeSpan(name: String, options: Span.Options): Resource[F, Span[F]] =
Span.putErrorFields {
Resource
.make {
val p = options.parentKernel.map(k =>
tracer.extract(
Format.Builtin.HTTP_HEADERS,
new TextMapAdapter(k.toJava)
)
)
Sync[F].delay(tracer.buildSpan(name).asChildOf(p.orNull).asChildOf(span).start)
} { s =>
Sync[F].delay(s.finish())
}
.map(MockSpan(tracer, _))
}

def traceId: F[Option[String]] =
span.context.toTraceId.some.pure[F]
Expand Down