-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat(ingest-metrics): Ensure billing metrics consumer avoids outcome double-billing #117186
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -202,6 +202,7 @@ | |
| "has_transaction": PREFIX + 281, | ||
| "was_transaction": PREFIX + 282, | ||
| "is_segment": PREFIX + 283, | ||
| "billing_outcome_accepted": PREFIX + 284, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if you already bikeshedded on the name but |
||
| # GENERAL/MISC (don't have a category) | ||
| "": PREFIX + 1000, | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |||
| SPAN_METRICS_NAMES, | ||||
| TRANSACTION_METRICS_NAMES, | ||||
| ) | ||||
| from sentry.testutils.helpers.features import with_feature | ||||
| from sentry.utils.outcomes import Outcome | ||||
|
|
||||
|
|
||||
|
|
@@ -141,3 +142,103 @@ def generate_kafka_message(generic_metric: GenericMetric) -> Message[KafkaPayloa | |||
|
|
||||
| strategy.join() | ||||
| assert next_step.join.call_count == 1 | ||||
|
|
||||
|
|
||||
| @with_feature("organizations:relay-generate-billing-outcome") | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Mentioning the feature here should not be necessary because the consumer-side behavior is determined by the tag, not the feature flag, right? This might also be what confused sentry[bot] in their comment above. |
||||
| @mock.patch("sentry.ingest.billing_metrics_consumer.track_outcome") | ||||
| def test_no_double_billing_outcomes(track_outcome) -> None: | ||||
| topic = Topic("snuba-generic-metrics") | ||||
|
|
||||
| organization = 123 | ||||
| project_1 = 56789 | ||||
|
|
||||
| span_usage_mri = "c:spans/usage@none" | ||||
| span_usage_id = SPAN_METRICS_NAMES[span_usage_mri] | ||||
|
|
||||
| generic_metrics: list[GenericMetric] = [ | ||||
| { | ||||
| "mapping_meta": {"c": {str(span_usage_id): span_usage_mri}}, | ||||
| "metric_id": span_usage_id, | ||||
| "type": "d", | ||||
| "org_id": organization, | ||||
| "project_id": project_1, | ||||
| "timestamp": 123456, | ||||
| "value": 65.0, | ||||
| "tags": {str(SHARED_TAG_STRINGS["billing_outcome_accepted"]): "true"}, | ||||
| "use_case_id": "spans", | ||||
| "retention_days": 90, | ||||
| }, | ||||
| { | ||||
| "mapping_meta": {"c": {str(span_usage_id): span_usage_mri}}, | ||||
| "metric_id": span_usage_id, | ||||
| "type": "d", | ||||
| "org_id": organization, | ||||
| "project_id": project_1, | ||||
| "timestamp": 123456, | ||||
| "value": 12.0, | ||||
| "tags": {str(SHARED_TAG_STRINGS["is_segment"]): "true"}, | ||||
| "use_case_id": "spans", | ||||
| "retention_days": 90, | ||||
| }, | ||||
| ] | ||||
|
|
||||
| next_step = mock.MagicMock() | ||||
|
|
||||
| strategy = BillingTxCountMetricConsumerStrategy( | ||||
| next_step=next_step, | ||||
| ) | ||||
|
|
||||
| generate_kafka_message_counter = 0 | ||||
|
|
||||
| def generate_kafka_message(generic_metric: GenericMetric) -> Message[KafkaPayload]: | ||||
| nonlocal generate_kafka_message_counter | ||||
|
|
||||
| encoded = orjson.dumps(generic_metric) | ||||
| payload = KafkaPayload(key=None, value=encoded, headers=[]) | ||||
| message = Message( | ||||
| BrokerValue( | ||||
| payload, | ||||
| Partition(topic, index=0), | ||||
| generate_kafka_message_counter, | ||||
| datetime.now(timezone.utc), | ||||
| ) | ||||
| ) | ||||
| generate_kafka_message_counter += 1 | ||||
| return message | ||||
|
|
||||
| # Mimick the behavior of StreamProcessor._run_once: Call poll repeatedly, | ||||
| # then call submit when there is a message. | ||||
| strategy.poll() | ||||
| strategy.poll() | ||||
| assert track_outcome.call_count == 0 | ||||
| for generic_metric in generic_metrics: | ||||
| strategy.poll() | ||||
| strategy.submit(generate_kafka_message(generic_metric)) | ||||
|
|
||||
| assert track_outcome.mock_calls == [ | ||||
| mock.call( | ||||
| org_id=organization, | ||||
| project_id=project_1, | ||||
| key_id=None, | ||||
| outcome=Outcome.ACCEPTED, | ||||
| reason=None, | ||||
| timestamp=mock.ANY, | ||||
| event_id=None, | ||||
| category=DataCategory.SPAN, | ||||
| quantity=12, | ||||
| ), | ||||
| mock.call( | ||||
| org_id=organization, | ||||
| project_id=project_1, | ||||
| key_id=None, | ||||
| outcome=Outcome.ACCEPTED, | ||||
| reason=None, | ||||
| timestamp=mock.ANY, | ||||
| event_id=None, | ||||
| category=DataCategory.TRANSACTION, | ||||
| quantity=12, | ||||
| ), | ||||
| ] | ||||
|
|
||||
| strategy.join() | ||||
| assert next_step.join.call_count == 1 | ||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The feature flag also needs to be exposed to Relay, search for
EXPOSABLE_FEATURES