Skip to content

Commit

Permalink
feat: handle conditional attributes in OpenTelemetryMetrics
Browse files Browse the repository at this point in the history
  • Loading branch information
varshith257 authored Nov 28, 2024
1 parent 50cb4f7 commit 4e13068
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ object OpenTelemetryMetrics {
case Left(_) => "500"
},
"error.type" -> {
case Left(ex) => ex.getClass.getName // Exception class name for pre-response errors
case Right(_) => None // No error.type for successful responses
case Left(ex) => ex.getClass.getName // Exception class name for errors
case Right(_) => ""
}
)
)
Expand Down Expand Up @@ -197,7 +197,11 @@ object OpenTelemetryMetrics {
l.forRequest.foldLeft(Attributes.builder())((b, label) => { b.put(label._1, label._2(ep, req)) }).build()

private def asOpenTelemetryAttributes(l: MetricLabels, res: Either[Throwable, ServerResponse[_]], phase: Option[String]): Attributes = {
val builder = l.forResponse.foldLeft(Attributes.builder())((b, label) => { b.put(label._1, label._2(res)) })
val builder = Attributes.builder()
l.forResponse.foreach { case (key, valueFn) =>
val value = valueFn(res)
if (value.nonEmpty) builder.put(key, value)
}
phase.foreach(v => builder.put(l.forResponsePhase.name, v))
builder.build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ class OpenTelemetryMetricsTest extends AnyFlatSpec with Matchers {
"http",
AttributeKey.stringKey("http.response.status_code"),
"200",
AttributeKey.stringKey("error.type"),
""
) && dp.getValue == 2 =>
true
case dp
Expand All @@ -123,8 +121,6 @@ class OpenTelemetryMetricsTest extends AnyFlatSpec with Matchers {
"http",
AttributeKey.stringKey("http.response.status_code"),
"400",
AttributeKey.stringKey("error.type"),
""
) && dp.getValue == 2 =>
true
case _ => false
Expand Down Expand Up @@ -172,8 +168,6 @@ class OpenTelemetryMetricsTest extends AnyFlatSpec with Matchers {
"body",
AttributeKey.stringKey("url.scheme"),
"http",
AttributeKey.stringKey("error.type"),
""
)
)
}
Expand Down Expand Up @@ -230,9 +224,7 @@ class OpenTelemetryMetricsTest extends AnyFlatSpec with Matchers {
AttributeKey.stringKey("url.scheme"),
"http",
AttributeKey.stringKey("http.response.status_code"),
"500",
AttributeKey.stringKey("error.type"),
""
"500"
)
point.getValue shouldBe 1
}
Expand Down

0 comments on commit 4e13068

Please sign in to comment.