Skip to content

Commit

Permalink
feat(profiling): add namespace for profile metrics (#3229)
Browse files Browse the repository at this point in the history
Sentry PR that added the `profles` use case:
https://github.com/getsentry/sentry/pull/65152/files
  • Loading branch information
viglia authored Mar 7, 2024
1 parent e831ffe commit 912a697
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- Add SDK information to spans. ([#3178](https://github.com/getsentry/relay/pull/3178))
- Filter null values from metrics summary tags. ([#3204](https://github.com/getsentry/relay/pull/3204))
- Emit a usage metric for every span seen. ([#3209](https://github.com/getsentry/relay/pull/3209))
- Add namespace for profile metrics. ([#3229](https://github.com/getsentry/relay/pull/3229))

## 24.2.0

Expand Down
4 changes: 4 additions & 0 deletions relay-base-schema/src/metrics/mri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ pub enum MetricNamespace {
Transactions,
/// Metrics extracted from spans.
Spans,
/// Metrics extracted from profile functions.
Profiles,
/// User-defined metrics directly sent by SDKs and applications.
Custom,
/// An unknown and unsupported metric.
Expand All @@ -133,6 +135,7 @@ impl MetricNamespace {
MetricNamespace::Sessions => "sessions",
MetricNamespace::Transactions => "transactions",
MetricNamespace::Spans => "spans",
MetricNamespace::Profiles => "profiles",
MetricNamespace::Custom => "custom",
MetricNamespace::Unsupported => "unsupported",
}
Expand All @@ -147,6 +150,7 @@ impl std::str::FromStr for MetricNamespace {
"sessions" => Ok(Self::Sessions),
"transactions" => Ok(Self::Transactions),
"spans" => Ok(Self::Spans),
"profiles" => Ok(Self::Profiles),
"custom" => Ok(Self::Custom),
_ => Ok(Self::Unsupported),
}
Expand Down
3 changes: 3 additions & 0 deletions relay-cogs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ pub enum AppFeature {
MetricsTransactions,
/// Metrics in the spans namespace.
MetricsSpans,
/// Metrics in the profiles namespace.
MetricsProfiles,
/// Metrics in the sessions namespace.
MetricsSessions,
/// Metrics in the custom namespace.
Expand Down Expand Up @@ -163,6 +165,7 @@ impl AppFeature {
Self::MetricMeta => "metric_meta",
Self::MetricsTransactions => "metrics_transactions",
Self::MetricsSpans => "metrics_spans",
Self::MetricsProfiles => "metrics_profiles",
Self::MetricsSessions => "metrics_sessions",
Self::MetricsCustom => "metrics_custom",
Self::MetricsUnsupported => "metrics_unsupported",
Expand Down
13 changes: 12 additions & 1 deletion relay-dynamic-config/src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,23 @@ pub struct Options {
)]
pub profile_metrics_sample_rate: f32,

/// Kill switch for shutting down profile metrics
/// Kill switch for shutting down unsampled_profile metrics
#[serde(
rename = "profiling.profile_metrics.unsampled_profiles.enabled",
deserialize_with = "default_on_error",
skip_serializing_if = "is_default"
)]
pub unsampled_profiles_enabled: bool,

/// Kill switch for shutting down profile function metrics
/// ingestion in the generic-metrics platform
#[serde(
rename = "profiling.generic_metrics.functions_ingestion.enabled",
deserialize_with = "default_on_error",
skip_serializing_if = "is_default"
)]
pub profiles_function_generic_metrics_enabled: bool,

/// Kill switch for controlling the cardinality limiter.
#[serde(
rename = "relay.cardinality-limiter.mode",
Expand Down Expand Up @@ -169,6 +178,7 @@ pub struct MetricBucketEncodings {
sessions: MetricEncoding,
transactions: MetricEncoding,
spans: MetricEncoding,
profiles: MetricEncoding,
custom: MetricEncoding,
unsupported: MetricEncoding,
}
Expand All @@ -180,6 +190,7 @@ impl MetricBucketEncodings {
MetricNamespace::Sessions => self.sessions,
MetricNamespace::Transactions => self.transactions,
MetricNamespace::Spans => self.spans,
MetricNamespace::Profiles => self.profiles,
MetricNamespace::Custom => self.custom,
MetricNamespace::Unsupported => self.unsupported,
}
Expand Down
1 change: 1 addition & 0 deletions relay-metrics/src/cogs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ fn to_app_feature(ns: MetricNamespace) -> AppFeature {
MetricNamespace::Sessions => AppFeature::MetricsSessions,
MetricNamespace::Transactions => AppFeature::MetricsTransactions,
MetricNamespace::Spans => AppFeature::MetricsSpans,
MetricNamespace::Profiles => AppFeature::MetricsProfiles,
MetricNamespace::Custom => AppFeature::MetricsCustom,
MetricNamespace::Unsupported => AppFeature::MetricsUnsupported,
}
Expand Down
1 change: 1 addition & 0 deletions relay-server/src/services/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,7 @@ fn is_metric_namespace_valid(state: &ProjectState, namespace: &MetricNamespace)
state.has_feature(Feature::ExtractSpansAndSpanMetricsFromEvent)
|| state.has_feature(Feature::StandaloneSpanIngestion)
}
MetricNamespace::Profiles => true,
MetricNamespace::Custom => state.has_feature(Feature::CustomMetrics),
MetricNamespace::Unsupported => false,
}
Expand Down
11 changes: 11 additions & 0 deletions relay-server/src/services/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,17 @@ impl StoreService {
);
return Ok(());
}
MetricNamespace::Profiles => {
if !self
.global_config
.current()
.options
.profiles_function_generic_metrics_enabled
{
return Ok(());
}
KafkaTopic::MetricsGeneric
}
_ => KafkaTopic::MetricsGeneric,
};
let headers = BTreeMap::from([("namespace".to_string(), namespace.to_string())]);
Expand Down
2 changes: 2 additions & 0 deletions relay-server/src/utils/metric_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct MetricStats {
custom: Stat,
sessions: Stat,
spans: Stat,
profiles: Stat,
transactions: Stat,
unsupported: Stat,
}
Expand Down Expand Up @@ -60,6 +61,7 @@ impl MetricStats {
MetricNamespace::Sessions => &mut self.sessions,
MetricNamespace::Transactions => &mut self.transactions,
MetricNamespace::Spans => &mut self.spans,
MetricNamespace::Profiles => &mut self.profiles,
MetricNamespace::Custom => &mut self.custom,
MetricNamespace::Unsupported => &mut self.unsupported,
}
Expand Down
37 changes: 37 additions & 0 deletions tests/integration/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2053,3 +2053,40 @@ def test_missing_global_filters_enables_metric_extraction(
tx, _ = tx_consumer.get_event()
assert tx is not None
assert metrics_consumer.get_metrics()


def test_profiles_metrics(mini_sentry, relay):
relay = relay(mini_sentry, options=TEST_CONFIG)

project_id = 42
mini_sentry.add_basic_project_config(project_id)

timestamp = int(datetime.now(tz=timezone.utc).timestamp())
metrics_payload = f"profiles/foo:42|c|T{timestamp}\nprofiles/bar:17|c|T{timestamp}"

relay.send_metrics(project_id, metrics_payload)

envelope = mini_sentry.captured_events.get(timeout=3)
assert len(envelope.items) == 1

metrics_item = envelope.items[0]
assert metrics_item.type == "metric_buckets"

received_metrics = json.loads(metrics_item.get_bytes().decode())
received_metrics = sorted(received_metrics, key=lambda x: x["name"])
assert received_metrics == [
{
"timestamp": timestamp,
"width": 1,
"name": "c:profiles/bar@none",
"value": 17.0,
"type": "c",
},
{
"timestamp": timestamp,
"width": 1,
"name": "c:profiles/foo@none",
"value": 42.0,
"type": "c",
},
]

0 comments on commit 912a697

Please sign in to comment.