Skip to content

Commit

Permalink
Populate span tags with metric tags
Browse files Browse the repository at this point in the history
  • Loading branch information
markushi committed Mar 7, 2024
1 parent 1d15db8 commit 772c391
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions sentry/src/main/java/io/sentry/metrics/MetricsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,11 @@ public void timing(
aggregator.getLocalMetricsAggregator();

final @Nullable ISpan span = aggregator.startSpanForMetric("metric.timing", key);
if (span != null && tags != null) {
for (final @NotNull Map.Entry<String, String> entry : tags.entrySet()) {
span.setTag(entry.getKey(), entry.getValue());
}
}
try {
aggregator
.getMetricsAggregator()
Expand Down
23 changes: 23 additions & 0 deletions sentry/src/test/java/io/sentry/metrics/MetricsApiTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package io.sentry.metrics

import io.sentry.IMetricsAggregator
import io.sentry.ISpan
import io.sentry.MeasurementUnit
import io.sentry.metrics.MetricsApi.IMetricsInterface
import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.verify
import kotlin.test.Test
import kotlin.test.assertEquals
Expand Down Expand Up @@ -319,6 +321,27 @@ class MetricsApiTest {
verify(fixture.lastSpan!!).finish()
}

@Test
fun `timing applies metric tags as span tags`() {
val span = mock<ISpan>()
val api = fixture.getSut(
spanProvider = {
span
},
defaultTags = mapOf(
"release" to "1.0"
)
)
// when timing is called
api.timing("key", {
// no-op
}, MeasurementUnit.Duration.NANOSECOND, mapOf("a" to "b"))

// the last span should have the metric tags, without the default ones
verify(fixture.lastSpan!!, never()).setTag("release", "1.0")
verify(fixture.lastSpan!!).setTag("a", "b")
}

@Test
fun `if timing throws an exception, span still finishes`() {
val api = fixture.getSut()
Expand Down

0 comments on commit 772c391

Please sign in to comment.