Skip to content

Commit acff681

Browse files
markushilizokm
andauthored
Add metrics API docs for Java/Android (#9374)
* Add docs for Java/Android * Apply suggestions from code review Co-authored-by: Liza Mock <[email protected]> * Update list of SDKs supporting metrics, order it a-z * Extend Java metrics docs --------- Co-authored-by: Liza Mock <[email protected]>
1 parent fe2aa4c commit acff681

File tree

3 files changed

+355
-7
lines changed

3 files changed

+355
-7
lines changed
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
---
2+
title: Set Up Metrics
3+
description: "Learn how to measure the data points you care about by configuring Metrics in your Android app."
4+
---
5+
6+
<Include name="feature-stage-alpha-metrics.mdx" />
7+
<Note>
8+
9+
Metrics are supported with Sentry Android SDK version `7.6.0` and above.
10+
11+
</Note>
12+
13+
Sentry metrics help you pinpoint and solve issues that impact user experience and app performance by measuring the data points that are important to you. You can track things like processing time, event size, user signups, and conversion rates, then correlate them back to tracing data in order to get deeper insights and solve issues faster.
14+
15+
16+
## Enabling the Metrics Feature
17+
18+
Here's how to add Metrics to your application:
19+
20+
```Kotlin {filename:MyApplication.kt}
21+
import io.sentry.android.core.SentryAndroid
22+
23+
SentryAndroid.init(this) { options ->
24+
options.enableMetrics = true
25+
}
26+
```
27+
28+
```Java {filename:MyApplication.java}
29+
import io.sentry.android.core.SentryAndroid;
30+
31+
SentryAndroid.init(this, options -> {
32+
options.setEnableMetrics(true);
33+
});
34+
```
35+
36+
```xml {filename:AndroidManifest.xml}
37+
<manifest>
38+
<application>
39+
<meta-data android:name="io.sentry.enable-metrics" android:value="true" />
40+
</application>
41+
</manifest>
42+
```
43+
44+
## Emit a Counter
45+
46+
Counters are one of the more basic types of metrics and can be used to count certain event occurrences.
47+
48+
To emit a counter, do the following:
49+
50+
```Kotlin
51+
Sentry.metrics()
52+
.increment(
53+
"button_login_click", // key
54+
1.0, // value
55+
null, // unit
56+
mapOf( // tags
57+
"provider" to "e-mail"
58+
)
59+
)
60+
```
61+
62+
```Java
63+
final Map<String, String> tags = new HashMap<>();
64+
tags.put("provider", "e-mail");
65+
66+
Sentry.metrics().increment(
67+
"button_login_click", // key
68+
1.0, // value
69+
null, // unit
70+
tags // tags
71+
);
72+
```
73+
74+
## Emit a Distribution
75+
76+
Distributions help you get the most insights from your data by allowing you to obtain aggregations such as `p90`, `min`, `max`, and `avg`.
77+
78+
To emit a distribution, do the following:
79+
80+
```Kotlin
81+
Sentry.metrics().distribution(
82+
"image_download_duration",
83+
150.0,
84+
MeasurementUnit.Duration.MILLISECOND,
85+
mapOf(
86+
"type" to "thumbnail"
87+
)
88+
)
89+
```
90+
91+
```java
92+
final Map<String, String> tags = new HashMap<>();
93+
tags.put("type", "thumbnail");
94+
95+
Sentry.metrics().distribution(
96+
"image_download_duration",
97+
150.0,
98+
MeasurementUnit.Duration.MILLISECOND,
99+
tags);
100+
```
101+
102+
## Emit a Set
103+
104+
Sets are useful for looking at unique occurrences and counting the unique elements you added.
105+
106+
To emit a set, do the following:
107+
108+
```Kotlin
109+
Sentry.metrics().set(
110+
"user_view",
111+
"jane",
112+
MeasurementUnit.Custom("username"),
113+
mapOf(
114+
"page" to "home"
115+
)
116+
)
117+
```
118+
119+
```Java
120+
// Add 'jane' to a set used for tracking the number of users that viewed a page.
121+
final Map<String, String> tags = new HashMap<>();
122+
tags.put("page", "home");
123+
124+
Sentry.metrics().set(
125+
"user_view",
126+
"jane",
127+
new MeasurementUnit.Custom("username"),
128+
tags);
129+
```
130+
131+
## Emit a Gauge
132+
133+
Gauges let you obtain aggregates like `min`, `max`, `avg`, `sum`, and `count`. They can be represented in a more space-efficient way than distributions, but they can't be used to get percentiles. If percentiles aren't important to you, we recommend using gauges.
134+
135+
To emit a gauge, do the following:
136+
137+
```Kotlin
138+
Sentry.metrics().gauge(
139+
"page_load",
140+
15.0,
141+
MeasurementUnit.Duration.MILLISECOND,
142+
mapOf(
143+
"page" to "/home"
144+
)
145+
)
146+
```
147+
148+
```Java
149+
final Map<String, String> tags = new HashMap<>();
150+
tags.put("page", "/home");
151+
152+
Sentry.metrics().gauge(
153+
"page_load",
154+
15.0,
155+
MeasurementUnit.Duration.MILLISECOND,
156+
tags);
157+
```
158+
159+
## Emit a Timer
160+
161+
Timers can be used to measure the execution time of a specific block of code. They're implemented like distributions, but measured in seconds.
162+
163+
To emit a timer, do the following:
164+
165+
```Kotlin
166+
Sentry.metrics().timing("load_user_profile") {
167+
// db.load() ...
168+
}
169+
```
170+
171+
```Java
172+
Sentry.metrics().timing("load_user_profile", () -> {
173+
// db.load() ...
174+
});
175+
```
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
---
2+
title: Set Up Metrics
3+
description: "Learn how to measure the data points you care about by configuring Metrics in your Java app."
4+
---
5+
6+
<Include name="feature-stage-alpha-metrics.mdx" />
7+
<Note>
8+
9+
Metrics are supported with Sentry Java SDK version `7.6.0` and above.
10+
11+
</Note>
12+
13+
Sentry metrics help you pinpoint and solve issues that impact user experience and app performance by measuring the data points that are important to you. You can track things like processing time, event size, user signups, and conversion rates, then correlate them back to tracing data in order to get deeper insights and solve issues faster.
14+
15+
## Enabling the Metrics Feature
16+
17+
Here's how to add Metrics to your application:
18+
19+
```Java {filename:MyApplication.java}
20+
import io.sentry.android.core.SentryAndroid;
21+
22+
Sentry.init(this, options -> {
23+
options.setEnableMetrics(true);
24+
});
25+
```
26+
27+
```Kotlin {filename:MyApplication.kt}
28+
import io.sentry.android.core.SentryAndroid
29+
30+
Sentry.init(this) { options ->
31+
options.enableMetrics = true
32+
}
33+
```
34+
35+
```text {tabTitle:Properties File} {filename:sentry.properties}
36+
sentry.enable-metrics=true
37+
```
38+
39+
## Emit a Counter
40+
41+
Counters are one of the more basic types of metrics and can be used to count certain event occurrences.
42+
43+
To emit a counter, do the following:
44+
45+
```Java
46+
final Map<String, String> tags = new HashMap<>();
47+
tags.put("provider", "e-mail");
48+
49+
Sentry.metrics().increment(
50+
"button_login_click", // key
51+
1.0, // value
52+
null, // unit
53+
tags // tags
54+
);
55+
```
56+
57+
```Kotlin
58+
Sentry.metrics()
59+
.increment(
60+
"button_login_click", // key
61+
1.0, // value
62+
null, // unit
63+
mapOf( // tags
64+
"provider" to "e-mail"
65+
)
66+
)
67+
```
68+
69+
## Emit a Distribution
70+
71+
Distributions help you get the most insights from your data by allowing you to obtain aggregations such as `p90`, `min`, `max`, and `avg`.
72+
73+
To emit a distribution, do the following:
74+
75+
```java
76+
final Map<String, String> tags = new HashMap<>();
77+
tags.put("type", "thumbnail");
78+
79+
Sentry.metrics().distribution(
80+
"image_download_duration",
81+
150.0,
82+
MeasurementUnit.Duration.MILLISECOND,
83+
tags);
84+
```
85+
86+
```Kotlin
87+
Sentry.metrics().distribution(
88+
"image_download_duration",
89+
150.0,
90+
MeasurementUnit.Duration.MILLISECOND,
91+
mapOf(
92+
"type" to "thumbnail"
93+
)
94+
)
95+
```
96+
97+
## Emit a Set
98+
99+
Sets are useful for looking at unique occurrences and counting the unique elements you added.
100+
101+
To emit a set, do the following:
102+
103+
```Java
104+
// Add 'jane' to a set used for tracking the number of users that viewed a page.
105+
final Map<String, String> tags = new HashMap<>();
106+
tags.put("page", "home");
107+
108+
Sentry.metrics().set(
109+
"user_view",
110+
"jane",
111+
new MeasurementUnit.Custom("username"),
112+
tags);
113+
```
114+
115+
```Kotlin
116+
Sentry.metrics().set(
117+
"user_view",
118+
"jane",
119+
MeasurementUnit.Custom("username"),
120+
mapOf(
121+
"page" to "home"
122+
)
123+
)
124+
```
125+
126+
## Emit a Gauge
127+
128+
Gauges let you obtain aggregates like `min`, `max`, `avg`, `sum`, and `count`. They can be represented in a more space-efficient way than distributions, but they can't be used to get percentiles. If percentiles aren't important to you, we recommend using gauges.
129+
130+
To emit a gauge, do the following:
131+
132+
```Java
133+
final Map<String, String> tags = new HashMap<>();
134+
tags.put("page", "/home");
135+
136+
Sentry.metrics().gauge(
137+
"page_load",
138+
15.0,
139+
MeasurementUnit.Duration.MILLISECOND,
140+
tags);
141+
```
142+
143+
```Kotlin
144+
Sentry.metrics().gauge(
145+
"page_load",
146+
15.0,
147+
MeasurementUnit.Duration.MILLISECOND,
148+
mapOf(
149+
"page" to "/home"
150+
)
151+
)
152+
```
153+
154+
## Emit a Timer
155+
156+
Timers can be used to measure the execution time of a specific block of code. They're implemented like distributions, but measured in seconds.
157+
158+
To emit a timer, do the following:
159+
160+
```Java
161+
Sentry.metrics().timing("load_user_profile", () -> {
162+
// db.load() ...
163+
});
164+
```
165+
166+
```Kotlin
167+
Sentry.metrics().timing("load_user_profile") {
168+
// db.load() ...
169+
}
170+
```

docs/product/metrics/metrics-set-up.mdx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ To set up and start sending metrics, click a link for one of the supported SDKs
1414

1515
Metrics are currently available on the following platforms:
1616

17-
- [Python](/platforms/python/metrics/)
18-
- [PHP](/platforms/php/metrics/)
17+
1918
- [.NET](/platforms/dotnet/metrics/)
20-
- [Vanilla JavaScript](/platforms/javascript/metrics/)
21-
- [Node](/platforms/node/metrics/)
22-
- [Next.js](/platforms/javascript/guides/nextjs/metrics/)
23-
- [SvelteKit](/platforms/javascript/guides/sveltekit/metrics/)
24-
- [Remix](/platforms/javascript/guides/remix/metrics/)
19+
- [Android](/platforms/android/metrics/)
2520
- [Astro](/platforms/javascript/guides/astro/metrics/)
2621
- [Electron](/platforms/javascript/guides/electron/metrics/)
22+
- [Java](/platforms/java/metrics/)
23+
- [Next.js](/platforms/javascript/guides/nextjs/metrics/)
24+
- [Node](/platforms/node/metrics/)
25+
- [PHP](/platforms/php/metrics/)
26+
- [Python](/platforms/python/metrics/)
2727
- [React Native](/platforms/react-native/metrics/)
28+
- [Remix](/platforms/javascript/guides/remix/metrics/)
29+
- [SvelteKit](/platforms/javascript/guides/sveltekit/metrics/)
30+
- [Vanilla JavaScript](/platforms/javascript/metrics/)

0 commit comments

Comments
 (0)