Skip to content

Commit 26e8684

Browse files
authored
Merge pull request #516 from ivantopo/issue#513/tagMetric-not-working-on-unsampled-spans
apply metric tags on spans, even if the span is not sampled, fixes #513
2 parents fa342db + 5f748c1 commit 26e8684

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

kamon-core-tests/src/test/scala/kamon/trace/SpanMetrics.scala renamed to kamon-core-tests/src/test/scala/kamon/trace/SpanMetricsSpec.scala

+15-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import org.scalatest.{Matchers, WordSpecLike}
2121

2222
import scala.util.control.NoStackTrace
2323

24-
class SpanMetrics extends WordSpecLike with Matchers with MetricInspection with Reconfigure {
24+
class SpanMetricsSpec extends WordSpecLike with Matchers with MetricInspection with Reconfigure {
2525

26-
sampleAlways()
26+
sampleNever()
2727

2828
"Span Metrics" should {
2929
"be recorded for successful execution" in {
@@ -57,6 +57,18 @@ class SpanMetrics extends WordSpecLike with Matchers with MetricInspection with
5757
Span.Metrics.ProcessingTime.valuesForTag("operation") shouldNot contain(operation)
5858
}
5959

60+
"allow specifying custom Span metric tags" in {
61+
val operation = "span-with-custom-metric-tags"
62+
buildSpan(operation)
63+
.withMetricTag("custom-metric-tag-on-builder", "value")
64+
.start()
65+
.tagMetric("custom-metric-tag-on-span", "value")
66+
.finish()
67+
68+
Span.Metrics.ProcessingTime.valuesForTag("custom-metric-tag-on-builder") should contain("value")
69+
Span.Metrics.ProcessingTime.valuesForTag("custom-metric-tag-on-span") should contain("value")
70+
}
71+
6072
"be recorded if metrics are enabled by calling enableMetrics() on the Span" in {
6173
val operation = "span-with-re-enabled-metrics"
6274
buildSpan(operation)
@@ -68,7 +80,7 @@ class SpanMetrics extends WordSpecLike with Matchers with MetricInspection with
6880
Span.Metrics.ProcessingTime.valuesForTag("operation") should contain(operation)
6981
}
7082

71-
"record correctly error latency and count" in {
83+
"record error latency and count" in {
7284
val operation = "span-failure"
7385
val operationTag = "operation" -> operation
7486

kamon-core/src/main/scala/kamon/trace/Span.scala

+20-13
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,12 @@ object Span {
123123
}
124124

125125
override def tagMetric(key: String, value: String): Span = synchronized {
126-
if(sampled && open) {
127-
spanTags = spanTags + (key -> TagValue.String(value))
128-
126+
if(open) {
129127
if(collectMetrics)
130128
customMetricTags = customMetricTags + (key -> value)
129+
130+
if(sampled)
131+
spanTags = spanTags + (key -> TagValue.String(value))
131132
}
132133
this
133134
}
@@ -142,23 +143,29 @@ object Span {
142143
}
143144

144145
override def addError(error: String): Span = synchronized {
145-
if(sampled && open) {
146+
if(open) {
146147
hasError = true
147-
spanTags = spanTags ++ Map(
148-
"error" -> TagValue.True,
149-
"error.object" -> TagValue.String(error)
150-
)
148+
149+
if(sampled) {
150+
spanTags = spanTags ++ Map(
151+
"error" -> TagValue.True,
152+
"error.object" -> TagValue.String(error)
153+
)
154+
}
151155
}
152156
this
153157
}
154158

155159
override def addError(error: String, throwable: Throwable): Span = synchronized {
156-
if(sampled && open) {
160+
if(open) {
157161
hasError = true
158-
spanTags = spanTags ++ Map(
159-
"error" -> TagValue.True,
160-
"error.object" -> TagValue.String(throwable.getMessage())
161-
)
162+
163+
if(sampled) {
164+
spanTags = spanTags ++ Map(
165+
"error" -> TagValue.True,
166+
"error.object" -> TagValue.String(throwable.getMessage())
167+
)
168+
}
162169
}
163170
this
164171
}

0 commit comments

Comments
 (0)