Skip to content

Commit 34f6df7

Browse files
committed
Add ability to provide default datadog tags, that will be added to each span
1 parent 9bca66c commit 34f6df7

File tree

2 files changed

+22
-6
lines changed
  • natchez-extras-datadog/src

2 files changed

+22
-6
lines changed

natchez-extras-datadog/src/main/scala/com/ovoenergy/natchez/extras/datadog/Datadog.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import cats.syntax.functor._
99
import com.ovoenergy.natchez.extras.datadog.DatadogSpan.SpanNames
1010
import fs2.Stream
1111
import io.circe.{Encoder, Printer}
12-
import natchez.{EntryPoint, Kernel, Span}
12+
import natchez.{EntryPoint, Kernel, Span, TraceValue}
1313
import org.http4s.Method.PUT
1414
import org.http4s.Uri.Path.unsafeFromString
1515
import org.http4s.circe.CirceInstances.builder
@@ -119,7 +119,8 @@ object Datadog {
119119
client: Client[F],
120120
service: String,
121121
resource: String,
122-
agentHost: Uri = uri"http://localhost:8126"
122+
agentHost: Uri = uri"http://localhost:8126",
123+
meta: Map[String, TraceValue] = Map.empty
123124
): Resource[F, EntryPoint[F]] =
124125
for {
125126
queue <- spanQueue
@@ -130,7 +131,7 @@ object Datadog {
130131
def root(name: String, options: Span.Options): Resource[F, Span[F]] =
131132
Resource
132133
.eval(SpanIdentifiers.create.flatMap(Ref.of[F, SpanIdentifiers]))
133-
.flatMap(DatadogSpan.create(queue, names(name)))
134+
.flatMap(DatadogSpan.create(queue, names(name), meta))
134135
.widen
135136

136137
def continue(name: String, kernel: Kernel, options: Span.Options): Resource[F, Span[F]] =

natchez-extras-datadog/src/test/scala/com/ovoenergy/natchez/extras/datadog/DatadogTest.scala

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import com.ovoenergy.natchez.extras.datadog.Datadog.entryPoint
77
import com.ovoenergy.natchez.extras.datadog.DatadogTags.SpanType.{Cache, Db, Web}
88
import com.ovoenergy.natchez.extras.datadog.DatadogTags.spanType
99
import munit.CatsEffectSuite
10-
import natchez.EntryPoint
10+
import natchez.{EntryPoint, TraceValue}
1111
import org.http4s.Request
1212
import org.http4s.circe.CirceEntityDecoder._
1313
import org.http4s.syntax.literals._
@@ -21,8 +21,8 @@ import scala.concurrent.duration._
2121
*/
2222
class DatadogTest extends CatsEffectSuite {
2323

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

2727
test("Obtain the agent host from the parameter") {
2828
assertIO(
@@ -126,6 +126,21 @@ class DatadogTest extends CatsEffectSuite {
126126
}
127127
}
128128

129+
test("Allow you to provide default tags") {
130+
for {
131+
res <- run(
132+
_.root("bar").use(_.span("subspan").use(_ => IO.unit)),
133+
Map("defaultTag1" -> "some-value", "defaultTag2" -> "some-other-value")
134+
)
135+
spans <- res.flatTraverse(_.as[List[List[SubmittableSpan]]]).map(_.flatten)
136+
} yield {
137+
assertEquals(spans.head.meta.get("defaultTag1"), Some("some-value"))
138+
assertEquals(spans.head.meta.get("defaultTag2"), Some("some-other-value"))
139+
assertEquals(spans.tail.head.meta.get("defaultTag1"), Some("some-value"))
140+
assertEquals(spans.tail.head.meta.get("defaultTag2"), Some("some-other-value"))
141+
}
142+
}
143+
129144
test("Inherit metadata into subspans but only at the time of creation") {
130145
run(
131146
_.root("bar:res").use { root =>

0 commit comments

Comments
 (0)