Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -5283,6 +5283,7 @@ public final class io/sentry/metrics/SentryMetricsParameters {
public fun <init> ()V
public static fun create (Lio/sentry/SentryAttributes;)Lio/sentry/metrics/SentryMetricsParameters;
public static fun create (Lio/sentry/SentryDate;Lio/sentry/SentryAttributes;)Lio/sentry/metrics/SentryMetricsParameters;
public static fun create (Ljava/util/Map;)Lio/sentry/metrics/SentryMetricsParameters;
public fun getAttributes ()Lio/sentry/SentryAttributes;
public fun getHint ()Lio/sentry/Hint;
public fun getOrigin ()Ljava/lang/String;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.sentry.Hint;
import io.sentry.SentryAttributes;
import io.sentry.SentryDate;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -60,4 +61,15 @@ public void setHint(final @Nullable Hint hint) {
final @Nullable SentryAttributes attributes) {
return create(null, attributes);
}

/**
* A shortcut for SentryMetricsParameters.create(SentryAttributes.fromMap())
*
* @param attributes a map of attributes
* @return parameters
*/
public static @NotNull SentryMetricsParameters create(
final @Nullable Map<String, Object> attributes) {
return create(null, SentryAttributes.fromMap(attributes));
}
}
30 changes: 30 additions & 0 deletions sentry/src/test/java/io/sentry/ScopesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3324,6 +3324,36 @@ class ScopesTest {
)
}

@Test
fun `creating count metric with attributes from map and shortcut factory method works`() {
val (sut, mockClient) = getEnabledScopes()

sut
.metrics()
.count(
"metric name",
1.0,
"visit",
SentryMetricsParameters.create(mapOf("attrname1" to "attrval1")),
)

verify(mockClient)
.captureMetric(
check {
assertEquals("metric name", it.name)
assertEquals(1.0, it.value)
assertEquals("visit", it.unit)
assertEquals("counter", it.type)

val attr1 = it.attributes?.get("attrname1")!!
assertEquals("attrval1", attr1.value)
assertEquals("string", attr1.type)
},
anyOrNull(),
anyOrNull(),
)
}

@Test
fun `creating count metric with attributes works`() {
val (sut, mockClient) = getEnabledScopes()
Expand Down
Loading