Skip to content

Commit

Permalink
Aggregate metrics for spans (#3238)
Browse files Browse the repository at this point in the history
* Aggregate metrics for spans

* Add manifest property

* Add metrics sample Activity

* Update Changelog

* Minor cleanup

* Update sentry-android-core/src/test/java/io/sentry/android/core/ManifestMetadataReaderTest.kt

Co-authored-by: Stefano <stefano.siano@sentry.io>

* Address PR feedback

* Address PR feedback

* Address PR feedback

---------

Co-authored-by: Stefano <stefano.siano@sentry.io>
  • Loading branch information
markushi and stefanosiano committed Mar 6, 2024
1 parent 02729df commit e37359d
Show file tree
Hide file tree
Showing 49 changed files with 1,607 additions and 149 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Features

- Experimental: Add span summaries for metrics ([#3238](https://github.com/getsentry/sentry-java/pull/3238))

## 7.5.0

### Features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ final class ManifestMetadataReader {

static final String ENABLE_SCOPE_PERSISTENCE = "io.sentry.enable-scope-persistence";

static final String ENABLE_METRICS = "io.sentry.enable-metrics";

/** ManifestMetadataReader ctor */
private ManifestMetadataReader() {}

Expand Down Expand Up @@ -377,6 +379,9 @@ static void applyMetadata(
options.setEnableScopePersistence(
readBool(
metadata, logger, ENABLE_SCOPE_PERSISTENCE, options.isEnableScopePersistence()));

options.setEnableMetrics(
readBool(metadata, logger, ENABLE_METRICS, options.isEnableMetrics()));
}

options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ private static SentrySpan timeSpanToSentrySpan(
APP_METRICS_ORIGIN,
new ConcurrentHashMap<>(),
new ConcurrentHashMap<>(),
null,
defaultSpanData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1395,4 +1395,29 @@ class ManifestMetadataReaderTest {
// Assert
assertTrue(fixture.options.isEnableScopePersistence)
}

@Test
fun `applyMetadata reads enableMetrics flag to options`() {
// Arrange
val bundle = bundleOf(ManifestMetadataReader.ENABLE_METRICS to true)
val context = fixture.getContext(metaData = bundle)

// Act
ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider)

// Assert
assertTrue(fixture.options.isEnableMetrics)
}

@Test
fun `applyMetadata reads enableMetrics flag to options and keeps default if not found`() {
// Arrange
val context = fixture.getContext()

// Act
ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider)

// Assert
assertFalse(fixture.options.isEnableMetrics)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ class PerformanceAndroidEventProcessorTest {
null,
emptyMap(),
emptyMap(),
null,
null
)
tr.spans.add(appStartSpan)
Expand Down Expand Up @@ -339,6 +340,7 @@ class PerformanceAndroidEventProcessorTest {
null,
emptyMap(),
emptyMap(),
null,
null
)
tr.spans.add(appStartSpan)
Expand Down Expand Up @@ -389,6 +391,7 @@ class PerformanceAndroidEventProcessorTest {
null,
emptyMap(),
emptyMap(),
null,
null
)
tr.spans.add(appStartSpan)
Expand Down Expand Up @@ -435,6 +438,7 @@ class PerformanceAndroidEventProcessorTest {
null,
emptyMap(),
emptyMap(),
null,
null
)
tr.spans.add(appStartSpan)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
<activity android:name=".FrameDataForSpansActivity"
android:exported="false"/>

<activity android:name=".MetricsActivity"
android:exported="false"/>

<!-- NOTE: Replace the test DSN below with YOUR OWN DSN to see the events from this app in your Sentry project/dashboard-->
<meta-data android:name="io.sentry.dsn" android:value="https://1053864c67cc410aa1ffc9701bd6f93d@o447951.ingest.sentry.io/5428559" />

Expand Down Expand Up @@ -160,5 +163,7 @@

<meta-data android:name="io.sentry.performance-v2.enable" android:value="true" />

<meta-data android:name="io.sentry.enable-metrics" android:value="true" />

</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ public void run() {
binding.openFrameDataForSpans.setOnClickListener(
view -> startActivity(new Intent(this, FrameDataForSpansActivity.class)));

binding.openMetrics.setOnClickListener(
view -> startActivity(new Intent(this, MetricsActivity.class)));

setContentView(binding.getRoot());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.sentry.samples.android

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import io.sentry.Sentry
import kotlin.random.Random

class MetricsActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MaterialTheme {
Surface {
Column(
modifier = Modifier.padding(20.dp)
) {
Button(onClick = {
Sentry.metrics().increment("example.increment")
}) {
Text(text = "Increment")
}
Button(onClick = {
Sentry.metrics().distribution("example.distribution", Random.nextDouble())
}) {
Text(text = "Distribution")
}
Button(onClick = {
Sentry.metrics().gauge("example.gauge", Random.nextDouble())
}) {
Text(text = "Gauge")
}
Button(onClick = {
Sentry.metrics().set("example.set", Random.nextInt())
}) {
Text(text = "Set")
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/open_frame_data_for_spans"/>

<Button
android:id="@+id/open_metrics"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/open_metrics"/>
</LinearLayout>

</ScrollView>
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<string name="open_compose_activity">Open Compose Activity</string>
<string name="open_profiling_activity">Open Profiling Activity</string>
<string name="open_frame_data_for_spans">Open Frame Data for Spans Activity</string>
<string name="open_metrics">Delightful Developer Metrics</string>
<string name="test_timber_integration">Test Timber</string>
<string name="back_main">Back to Main Activity</string>
<string name="tap_me">text</string>
Expand Down

0 comments on commit e37359d

Please sign in to comment.