Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(metrics): Always extract distinct_id [INGEST-1330] #1275

Merged
merged 8 commits into from
May 19, 2022
Merged
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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

**Compatibility:** This version of Relay requires Sentry server `22.6.0` or newer.

**Features**:
**Bug Fixes**:

- Session metrics extraction: Count distinct_ids from all session updates to prevent undercounting users. ([#1275](https://github.com/getsentry/relay/pull/1275))

- Session metrics extraction: Count crashed+abnormal towards errored_preaggr. ([#1274](https://github.com/getsentry/relay/pull/1274))

Expand Down
34 changes: 31 additions & 3 deletions relay-server/src/metrics_extraction/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,25 @@ pub fn extract_session_metrics<T: SessionLike>(
with_tag(&tags, "session.status", status),
));
}

// Some SDKs only provide user information on subsequent updates (not when init==true), so
// collect the user ID here as well.
// We could also add the user ID to the "init" tag, but collecting it under a separate "ok" tag
// might help us with validation / debugging.
if let Some(distinct_id) = nil_to_none(session.distinct_id()) {
if session.total_count() == 0 && session.all_errors().is_none() {
// Assuming that aggregate items always have a total_count > 0,
// this is a session update to a healthy, individual session (init == false).
target.push(Metric::new_mri(
METRIC_NAMESPACE,
"user",
MetricUnit::None,
MetricValue::set_from_str(distinct_id),
timestamp,
with_tag(&tags, "session.status", SessionStatus::Ok),
));
}
}
}

#[cfg(test)]
Expand Down Expand Up @@ -292,8 +311,12 @@ mod tests {

extract_session_metrics(&session.attributes, &session, None, &mut metrics);

// A none-initial update will not trigger any metric if it's not errored/crashed
assert_eq!(metrics.len(), 0);
// A none-initial update which is not errored/crashed/abnormal will only emit a user metric.
assert_eq!(metrics.len(), 1);
let user_metric = &metrics[0];
assert_eq!(user_metric.name, "s:sessions/user@none");
assert!(matches!(user_metric.value, MetricValue::Set(_)));
assert_eq!(user_metric.tags["session.status"], "ok");
}

#[test]
Expand Down Expand Up @@ -406,14 +429,19 @@ mod tests {

extract_session_metrics(&session.attributes, &session, None, &mut metrics);

assert_eq!(metrics.len(), 1);
assert_eq!(metrics.len(), 2); // duration and user ID

let duration_metric = &metrics[0];
assert_eq!(duration_metric.name, "d:sessions/duration@second");
assert!(matches!(
duration_metric.value,
MetricValue::Distribution(_)
));

let user_metric = &metrics[1];
assert_eq!(user_metric.name, "s:sessions/user@none");
assert!(matches!(user_metric.value, MetricValue::Set(_)));
assert_eq!(user_metric.tags["session.status"], "ok");
}

#[test]
Expand Down