Skip to content

Commit

Permalink
Add ability to provide default datadog tags, that will be added to ea…
Browse files Browse the repository at this point in the history
…ch span
  • Loading branch information
voidcontext committed Apr 11, 2024
1 parent 9bca66c commit 34f6df7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import cats.syntax.functor._
import com.ovoenergy.natchez.extras.datadog.DatadogSpan.SpanNames
import fs2.Stream
import io.circe.{Encoder, Printer}
import natchez.{EntryPoint, Kernel, Span}
import natchez.{EntryPoint, Kernel, Span, TraceValue}
import org.http4s.Method.PUT
import org.http4s.Uri.Path.unsafeFromString
import org.http4s.circe.CirceInstances.builder
Expand Down Expand Up @@ -119,7 +119,8 @@ object Datadog {
client: Client[F],
service: String,
resource: String,
agentHost: Uri = uri"http://localhost:8126"
agentHost: Uri = uri"http://localhost:8126",
meta: Map[String, TraceValue] = Map.empty
): Resource[F, EntryPoint[F]] =
for {
queue <- spanQueue
Expand All @@ -130,7 +131,7 @@ object Datadog {
def root(name: String, options: Span.Options): Resource[F, Span[F]] =
Resource
.eval(SpanIdentifiers.create.flatMap(Ref.of[F, SpanIdentifiers]))
.flatMap(DatadogSpan.create(queue, names(name)))
.flatMap(DatadogSpan.create(queue, names(name), meta))
.widen

def continue(name: String, kernel: Kernel, options: Span.Options): Resource[F, Span[F]] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.ovoenergy.natchez.extras.datadog.Datadog.entryPoint
import com.ovoenergy.natchez.extras.datadog.DatadogTags.SpanType.{Cache, Db, Web}
import com.ovoenergy.natchez.extras.datadog.DatadogTags.spanType
import munit.CatsEffectSuite
import natchez.EntryPoint
import natchez.{EntryPoint, TraceValue}
import org.http4s.Request
import org.http4s.circe.CirceEntityDecoder._
import org.http4s.syntax.literals._
Expand All @@ -21,8 +21,8 @@ import scala.concurrent.duration._
*/
class DatadogTest extends CatsEffectSuite {

def run(f: EntryPoint[IO] => IO[Unit]): IO[List[Request[IO]]] =
TestClient[IO].flatMap(c => entryPoint(c.client, "test", "blah").use(f) >> c.requests)
def run(f: EntryPoint[IO] => IO[Unit], meta: Map[String, TraceValue] = Map.empty): IO[List[Request[IO]]] =
TestClient[IO].flatMap(c => entryPoint(c.client, "test", "blah", meta = meta).use(f) >> c.requests)

test("Obtain the agent host from the parameter") {
assertIO(
Expand Down Expand Up @@ -126,6 +126,21 @@ class DatadogTest extends CatsEffectSuite {
}
}

test("Allow you to provide default tags") {
for {
res <- run(
_.root("bar").use(_.span("subspan").use(_ => IO.unit)),
Map("defaultTag1" -> "some-value", "defaultTag2" -> "some-other-value")
)
spans <- res.flatTraverse(_.as[List[List[SubmittableSpan]]]).map(_.flatten)
} yield {
assertEquals(spans.head.meta.get("defaultTag1"), Some("some-value"))
assertEquals(spans.head.meta.get("defaultTag2"), Some("some-other-value"))
assertEquals(spans.tail.head.meta.get("defaultTag1"), Some("some-value"))
assertEquals(spans.tail.head.meta.get("defaultTag2"), Some("some-other-value"))
}
}

test("Inherit metadata into subspans but only at the time of creation") {
run(
_.root("bar:res").use { root =>
Expand Down

0 comments on commit 34f6df7

Please sign in to comment.