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(profiling): Add profile context to transaction #1860

Merged
merged 2 commits into from Jan 20, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion sentry_sdk/profiler.py
Expand Up @@ -103,6 +103,11 @@
},
)

ProfileContext = TypedDict(
"ProfileContext",
{"profile_id": str},
)

try:
from gevent.monkey import is_module_patched # type: ignore
except ImportError:
Expand Down Expand Up @@ -343,6 +348,7 @@ def __init__(
self.start_ns = 0 # type: int
self.stop_ns = 0 # type: int
self.active = False # type: bool
self.event_id = uuid.uuid4().hex # type: str

self.indexed_frames = {} # type: Dict[RawFrame, int]
self.indexed_stacks = {} # type: Dict[RawStackId, int]
Expand All @@ -352,6 +358,10 @@ def __init__(

transaction._profile = self

def get_profile_context(self):
# type: () -> ProfileContext
return {"profile_id": self.event_id}

def __enter__(self):
# type: () -> None
hub = self.hub or sentry_sdk.Hub.current
Expand Down Expand Up @@ -444,7 +454,7 @@ def to_json(self, event_opt, options):

return {
"environment": event_opt.get("environment"),
"event_id": uuid.uuid4().hex,
"event_id": self.event_id,
"platform": "python",
"profile": profile,
"release": event_opt.get("release", ""),
Expand Down
1 change: 1 addition & 0 deletions sentry_sdk/tracing.py
Expand Up @@ -709,6 +709,7 @@ def finish(self, hub=None, end_timestamp=None):

if hub.client is not None and self._profile is not None:
event["profile"] = self._profile
contexts.update({"profile": self._profile.get_profile_context()})

if has_custom_measurements_enabled():
event["measurements"] = self._measurements
Expand Down
33 changes: 33 additions & 0 deletions tests/integrations/wsgi/test_wsgi.py
Expand Up @@ -323,3 +323,36 @@ def test_app(environ, start_response):
for item in envelope.items:
count_item_types[item.type] += 1
assert count_item_types["profile"] == profile_count


def test_profile_context_sent(sentry_init, capture_envelopes, teardown_profiling):
def test_app(environ, start_response):
start_response("200 OK", [])
return ["Go get the ball! Good dog!"]

sentry_init(
traces_sample_rate=1.0,
_experiments={"profiles_sample_rate": 1.0},
)
app = SentryWsgiMiddleware(test_app)
envelopes = capture_envelopes()

client = Client(app)
client.get("/")

transaction = None
profile = None
for envelope in envelopes:
for item in envelope.items:
if item.type == "profile":
assert profile is None # should only have 1 profile
profile = item
elif item.type == "transaction":
assert transaction is None # should only have 1 transaction
transaction = item

assert transaction is not None
assert profile is not None
assert transaction.payload.json["contexts"]["profile"] == {
"profile_id": profile.payload.json["event_id"],
}