Skip to content

Commit 5bfb646

Browse files
committed
SentryMetricsParameters create shortcut for attributes map
1 parent 76e7b2c commit 5bfb646

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

sentry/api/sentry.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5283,6 +5283,7 @@ public final class io/sentry/metrics/SentryMetricsParameters {
52835283
public fun <init> ()V
52845284
public static fun create (Lio/sentry/SentryAttributes;)Lio/sentry/metrics/SentryMetricsParameters;
52855285
public static fun create (Lio/sentry/SentryDate;Lio/sentry/SentryAttributes;)Lio/sentry/metrics/SentryMetricsParameters;
5286+
public static fun create (Ljava/util/Map;)Lio/sentry/metrics/SentryMetricsParameters;
52865287
public fun getAttributes ()Lio/sentry/SentryAttributes;
52875288
public fun getHint ()Lio/sentry/Hint;
52885289
public fun getOrigin ()Ljava/lang/String;

sentry/src/main/java/io/sentry/metrics/SentryMetricsParameters.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.sentry.Hint;
44
import io.sentry.SentryAttributes;
55
import io.sentry.SentryDate;
6+
import java.util.Map;
67
import org.jetbrains.annotations.NotNull;
78
import org.jetbrains.annotations.Nullable;
89

@@ -60,4 +61,15 @@ public void setHint(final @Nullable Hint hint) {
6061
final @Nullable SentryAttributes attributes) {
6162
return create(null, attributes);
6263
}
64+
65+
/**
66+
* A shortcut for SentryMetricsParameters.create(SentryAttributes.fromMap())
67+
*
68+
* @param attributes a map of attributes
69+
* @return parameters
70+
*/
71+
public static @NotNull SentryMetricsParameters create(
72+
final @Nullable Map<String, Object> attributes) {
73+
return create(null, SentryAttributes.fromMap(attributes));
74+
}
6375
}

sentry/src/test/java/io/sentry/ScopesTest.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3324,6 +3324,36 @@ class ScopesTest {
33243324
)
33253325
}
33263326

3327+
@Test
3328+
fun `creating count metric with attributes from map and shortcut factory method works`() {
3329+
val (sut, mockClient) = getEnabledScopes()
3330+
3331+
sut
3332+
.metrics()
3333+
.count(
3334+
"metric name",
3335+
1.0,
3336+
"visit",
3337+
SentryMetricsParameters.create(mapOf("attrname1" to "attrval1")),
3338+
)
3339+
3340+
verify(mockClient)
3341+
.captureMetric(
3342+
check {
3343+
assertEquals("metric name", it.name)
3344+
assertEquals(1.0, it.value)
3345+
assertEquals("visit", it.unit)
3346+
assertEquals("counter", it.type)
3347+
3348+
val attr1 = it.attributes?.get("attrname1")!!
3349+
assertEquals("attrval1", attr1.value)
3350+
assertEquals("string", attr1.type)
3351+
},
3352+
anyOrNull(),
3353+
anyOrNull(),
3354+
)
3355+
}
3356+
33273357
@Test
33283358
fun `creating count metric with attributes works`() {
33293359
val (sut, mockClient) = getEnabledScopes()

0 commit comments

Comments
 (0)