Skip to content

Commit

Permalink
Add Datadog specific error tags (#1228)
Browse files Browse the repository at this point in the history
  • Loading branch information
TAKAyukiatkwsk authored Oct 10, 2023
1 parent 12983f9 commit d2db875
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import kamon.{ ClassLoading, Kamon }
import kamon.datadog.DatadogSpanReporter.Configuration
import kamon.module.{ ModuleFactory, SpanReporter }
import kamon.tag.{ Lookups, Tag, TagSet }
import kamon.trace.Span.TagKeys
import kamon.util.{ EnvironmentTags, Filter }
import org.slf4j.LoggerFactory

Expand Down Expand Up @@ -52,7 +53,14 @@ object KamonDataDogTranslatorDefault extends KamonDataDogTranslator {
val start = from.getEpochNano
val duration = Duration.between(from, span.to)
val marks = span.marks.map { m => m.key -> m.instant.getEpochNano.toString }.toMap
val tags = (span.tags.all() ++ span.metricTags.all() ++ additionalTags.all()).map { t =>
val errorTags = if (span.hasError) {
val builder = TagSet.builder()
span.tags.get(Lookups.option(TagKeys.ErrorMessage)).foreach(msg => builder.add("error.msg", msg))
span.tags.get(Lookups.option(TagKeys.ErrorStacktrace)).foreach(st => builder.add("error.stack", st))
builder.build()
} else TagSet.Empty

val tags = (span.tags.all() ++ span.metricTags.all() ++ errorTags.all() ++ additionalTags.all()).map { t =>
t.key -> Tag.unwrapValue(t).toString
}
val meta = (marks ++ tags).filterKeys(tagFilter.accept(_)).toMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,26 @@ trait TestData {
"error" -> 1
)

val spanWithErrorTags = span.copy(tags = TagSet.from(Map(
"error" -> true,
"error.type" -> "RuntimeException",
"error.message" -> "Error message",
"error.stacktrace" -> "Error stacktrace"
)), hasError = true)

val jsonWithErrorTags = json ++ Json.obj(
"meta" -> Json.obj(
"error" -> "true",
"env" -> "staging",
"error.type" -> "RuntimeException",
"error.message" -> "Error message",
"error.msg" -> "Error message",
"error.stacktrace" -> "Error stacktrace",
"error.stack" -> "Error stacktrace"
),
"error" -> 1
)

val spanWithTags = span.copy(metricTags =
TagSet.from(
Map(
Expand Down Expand Up @@ -152,6 +172,7 @@ trait TestData {
"span with marks" -> (Seq(spanWithMarks), Json.arr(Json.arr(jsonWithMarks))),
"span with meta and marks" -> (Seq(spanWithTagsAndMarks), Json.arr(Json.arr(jsonWithTagsAndMarks))),
"span with error" -> (Seq(spanWithError), Json.arr(Json.arr(jsonWithError))),
"span with error tags" -> (Seq(spanWithErrorTags), Json.arr(Json.arr(jsonWithErrorTags))),

"multiple spans with same trace" -> (Seq(span, spanWithTags), Json.arr(Json.arr(json, jsonWithTags)))
// "multiple spans with two traces" -> (Seq(span, spanWithTags, otherTraceSpan, span), Json.arr(Json.arr(json, jsonWithTags, json), Json.arr(otherTraceJson)))
Expand Down

0 comments on commit d2db875

Please sign in to comment.