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): Introduce continuous profiling mode #2830

Open
wants to merge 45 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
984695e
feat(profiling): Introduce continuous profiling mode
Zylphrex Mar 15, 2024
92e3263
fix up import and a few things
Zylphrex Mar 15, 2024
455778b
set timestamp accordingly
Zylphrex Mar 15, 2024
3452c7e
ensure thread running at start of transaction
Zylphrex Mar 15, 2024
12cfee6
fix import
Zylphrex Mar 15, 2024
b1be566
format json
Zylphrex Mar 21, 2024
60efea7
some basic tests + teardown
Zylphrex Mar 26, 2024
fd1fa37
Merge branch 'master' into txiao/feat/introduce-continuous-profiling-…
Zylphrex Mar 27, 2024
dea4987
send envelope
Zylphrex Mar 27, 2024
ca4594c
run black
Zylphrex Mar 27, 2024
94e77cd
add another test but it doesnt work yet
Zylphrex Mar 27, 2024
bf81ab2
test should work now
Zylphrex Mar 27, 2024
96b1807
run lint
Zylphrex Mar 27, 2024
420d5b8
should be chunk id
Zylphrex Mar 27, 2024
c59adb1
single timestamp per profiler id
Zylphrex Mar 27, 2024
ec1240d
lint
Zylphrex Mar 27, 2024
fa5e0d4
ensure chunks are flushed
Zylphrex Mar 27, 2024
988f609
add profiler id to span data
Zylphrex Apr 4, 2024
719dc1a
fix circular import
Zylphrex Apr 5, 2024
f7f488e
fix lint
Zylphrex Apr 11, 2024
aa177de
Merge branch 'master' of github.com:getsentry/sentry-python into txia…
Zylphrex Apr 11, 2024
f70a04a
Merge branch 'master' into txiao/feat/introduce-continuous-profiling-…
Zylphrex Apr 25, 2024
c1e2bb5
lint
Zylphrex Apr 25, 2024
02cbba2
expose start/stop apis
Zylphrex Apr 26, 2024
dff3b9c
autostart profiler once per process
Zylphrex Apr 26, 2024
e4289aa
Merge branch 'master' into txiao/feat/introduce-continuous-profiling-…
Zylphrex May 7, 2024
d0b7704
Merge branch 'master' into txiao/feat/introduce-continuous-profiling-…
Zylphrex May 17, 2024
af4e370
change to auto start and manual start api
Zylphrex May 17, 2024
7ec70c7
fix test
Zylphrex May 17, 2024
0937dd2
fix test
Zylphrex May 17, 2024
10f38bc
run black
Zylphrex May 17, 2024
4276b32
Merge branch 'master' into txiao/feat/introduce-continuous-profiling-…
Zylphrex May 28, 2024
62d8dde
address PR feedback
Zylphrex May 31, 2024
f33dfb8
Merge branch 'master' of github.com:getsentry/sentry-python into txia…
Zylphrex May 31, 2024
4f78dfc
move into profiler folder
Zylphrex Jun 6, 2024
22fb4d2
fix tests and format
Zylphrex Jun 6, 2024
235db8a
fix tests
Zylphrex Jun 6, 2024
578e1e9
fix tests
Zylphrex Jun 6, 2024
ed66f59
move thread id/name into trace context
Zylphrex Jun 6, 2024
404fa45
fix api docs
Zylphrex Jun 6, 2024
8020324
move common imports
Zylphrex Jun 6, 2024
e8df608
Revert "move thread id/name into trace context"
Zylphrex Jun 6, 2024
817c570
export start/stop profiler
Zylphrex Jun 6, 2024
fdb17e5
Merge branch 'master' into txiao/feat/introduce-continuous-profiling-…
antonpirker Jun 7, 2024
29cb67d
re-export profiler for back compat
Zylphrex Jun 7, 2024
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
2 changes: 1 addition & 1 deletion docs/apidocs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ API Docs
.. autoclass:: sentry_sdk.tracing.Span
:members:

.. autoclass:: sentry_sdk.profiler.Profile
.. autoclass:: sentry_sdk.profiler.transaction_profiler.Profile
:members:

.. autoclass:: sentry_sdk.session.Session
Expand Down
4 changes: 3 additions & 1 deletion sentry_sdk/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,14 @@
"session",
"internal",
"profile",
"profile_chunk",
"metric_bucket",
"monitor",
]
SessionStatus = Literal["ok", "exited", "crashed", "abnormal"]

ProfilerMode = Literal["sleep", "thread", "gevent", "unknown"]
ContinuousProfilerMode = Literal["thread", "gevent", "unknown"]
ProfilerMode = Union[ContinuousProfilerMode, Literal["sleep"]]

# Type of the metric.
MetricType = Literal["d", "s", "g", "c"]
Expand Down
15 changes: 14 additions & 1 deletion sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@
from sentry_sdk.utils import ContextVar
from sentry_sdk.sessions import SessionFlusher
from sentry_sdk.envelope import Envelope
from sentry_sdk.profiler import has_profiling_enabled, Profile, setup_profiler
from sentry_sdk.profiler.continuous_profiler import setup_continuous_profiler
from sentry_sdk.profiler.transaction_profiler import (
has_profiling_enabled,
Profile,
setup_profiler,
)
from sentry_sdk.scrubber import EventScrubber
from sentry_sdk.monitor import Monitor
from sentry_sdk.spotlight import setup_spotlight
Expand Down Expand Up @@ -378,6 +383,14 @@ def _capture_envelope(envelope):
setup_profiler(self.options)
except Exception as e:
logger.debug("Can not set up profiler. (%s)", e)
else:
try:
setup_continuous_profiler(
self.options,
capture_func=_capture_envelope,
)
except Exception as e:
logger.debug("Can not set up continuous profiler. (%s)", e)
sentrivana marked this conversation as resolved.
Show resolved Hide resolved

finally:
_client_init_debug.set(old_debug)
Expand Down
9 changes: 9 additions & 0 deletions sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class EndpointType(Enum):

from sentry_sdk._types import (
BreadcrumbProcessor,
ContinuousProfilerMode,
Event,
EventProcessor,
Hint,
Expand All @@ -55,6 +56,8 @@ class EndpointType(Enum):
"attach_explain_plans": dict[str, Any],
"max_spans": Optional[int],
"record_sql_params": Optional[bool],
"continuous_profiling_auto_start": Optional[bool],
"continuous_profiling_mode": Optional[ContinuousProfilerMode],
"otel_powered_performance": Optional[bool],
"transport_zlib_compression_level": Optional[int],
"transport_num_pools": Optional[int],
Expand Down Expand Up @@ -364,6 +367,12 @@ class SPANDATA:
Example: "MainThread"
"""

PROFILER_ID = "profiler.id"
"""
Label identifying the profiler id that the span occurred in. This should be a string.
Example: "5249fbada8d5416482c2f6e47e337372"
"""


class OP:
ANTHROPIC_MESSAGES_CREATE = "ai.messages.create.anthropic"
Expand Down
10 changes: 10 additions & 0 deletions sentry_sdk/envelope.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ def add_profile(
# type: (...) -> None
self.add_item(Item(payload=PayloadRef(json=profile), type="profile"))

def add_profile_chunk(
self, profile_chunk # type: Any
):
# type: (...) -> None
self.add_item(
Item(payload=PayloadRef(json=profile_chunk), type="profile_chunk")
)

def add_checkin(
self, checkin # type: Any
):
Expand Down Expand Up @@ -265,6 +273,8 @@ def data_category(self):
return "internal"
elif ty == "profile":
return "profile"
elif ty == "profile_chunk":
return "profile_chunk"
elif ty == "statsd":
return "metric_bucket"
elif ty == "check_in":
Expand Down
41 changes: 41 additions & 0 deletions sentry_sdk/profiler/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from sentry_sdk.profiler.continuous_profiler import start_profiler, stop_profiler
from sentry_sdk.profiler.transaction_profiler import (
MAX_PROFILE_DURATION_NS,
PROFILE_MINIMUM_SAMPLES,
Profile,
Scheduler,
ThreadScheduler,
GeventScheduler,
has_profiling_enabled,
setup_profiler,
teardown_profiler,
)
from sentry_sdk.profiler.utils import (
DEFAULT_SAMPLING_FREQUENCY,
MAX_STACK_DEPTH,
get_frame_name,
extract_frame,
extract_stack,
frame_id,
)

__all__ = [
"start_profiler",
"stop_profiler",
# Re-exported for backwards compatibility
"MAX_PROFILE_DURATION_NS",
"PROFILE_MINIMUM_SAMPLES",
"Profile",
"Scheduler",
"ThreadScheduler",
"GeventScheduler",
"has_profiling_enabled",
"setup_profiler",
"teardown_profiler",
"DEFAULT_SAMPLING_FREQUENCY",
"MAX_STACK_DEPTH",
"get_frame_name",
"extract_frame",
"extract_stack",
"frame_id",
]