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(billing): Generate and add idempotency key to outcomes message #78795

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions src/sentry/utils/outcomes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import annotations

import hashlib
import time
from datetime import datetime
from enum import IntEnum
from uuid import uuid4

from sentry.conf.types.kafka_definition import Topic
from sentry.constants import DataCategory
Expand Down Expand Up @@ -102,6 +104,13 @@ def track_outcome(
billing_config["real_topic_name"] if use_billing else outcomes_config["real_topic_name"]
)

# Generate idempotency key using message fields
# This is to ensure that the message is processed at exactly once; even if the outcome is reprocessed again.
# For billing purposes, this lets us avoid duplicate usage counts.
random_str = uuid4().hex
idempotency_key_fields = f"{random_str}|{event_id}|{org_id}|{project_id}|{key_id}|{outcome.value}|{category}|{reason}|{quantity}|{timestamp}"
idempotency_key = hashlib.sha256(idempotency_key_fields.encode("utf-8")).hexdigest()

# Send a snuba metrics payload.
publisher.publish(
topic_name,
Expand All @@ -116,6 +125,7 @@ def track_outcome(
"event_id": event_id,
"category": category,
"quantity": quantity,
"idempotency_key": idempotency_key,
}
),
)
Expand Down
Loading
Loading