Skip to content

Commit

Permalink
Add aws log group name in Trace details if key is present in annotati…
Browse files Browse the repository at this point in the history
…on object (#5)
  • Loading branch information
ChristyGKrishnan authored May 24, 2024
1 parent 04fe68c commit d52d0b6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ Natchez is distributed tracing library for Scala.

Please proceed to the [microsite](https://typelevel.org/natchez/) for more information.

## AWS Log Group Name Configuration

In our Scala application, we use the `aws_group_name` key from the annotations to set the AWS Log group name.
31 changes: 25 additions & 6 deletions modules/xray/src/main/scala/natchez/xray/XRaySpan.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,25 @@ private[xray] final case class XRaySpan[F[_]: Concurrent: Clock: Random](
(goodKeys + ("malformed_keys" -> badKeys.keys
.mkString(",")
.asJson)) ++ fixedAnnotations

/*
* `aws_group_name` key in the annotations, when present, sets the AWS CloudWatch log group name.
* It is used to add log details to each trace.
* This is required when the log receiver container uses the X-Ray daemon image, which doesn't provide an option to set the group name.
*/
val logGroupValue = allAnnotations.get("aws_group_name")
val awsObject = logGroupValue
.map(lgValue =>
JsonObject(
"aws" -> JsonObject(
"cloudwatch_logs" -> Json.arr(
JsonObject(
"log_group" -> lgValue
).asJson
)
).asJson
)
)
.getOrElse(JsonObject.empty)
JsonObject(
"name" -> name.asJson,
"id" -> segmentId.asJson,
Expand All @@ -131,11 +149,12 @@ private[xray] final case class XRaySpan[F[_]: Concurrent: Clock: Random](
"links" -> options.links.asJson,
"span.kind" -> options.spanKind.asJson
).asJson
).deepMerge(exitCase match {
case Canceled => JsonObject.singleton("fault", true.asJson)
case Errored(e) => XRayException(id, e).asJsonObject
case Succeeded => JsonObject.empty
})
).deepMerge(awsObject)
.deepMerge(exitCase match {
case Canceled => JsonObject.singleton("fault", true.asJson)
case Errored(e) => XRayException(id, e).asJsonObject
case Succeeded => JsonObject.empty
})
}

private def header: String =
Expand Down

0 comments on commit d52d0b6

Please sign in to comment.