-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
docs(python): Update stream mode in instrumentation files (tracing part 2/3) #18532
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
Open
inventarSarah
wants to merge
2
commits into
master
Choose a base branch
from
smi/span-first/python-tracing-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+550
−75
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3,15 +3,22 @@ title: Instrument Caches | |||||
| sidebar_order: 1000 | ||||||
| description: "Learn how to manually instrument your code to use Sentry's Caches module. " | ||||||
| --- | ||||||
|
|
||||||
| A cache can be used to speed up data retrieval, thereby improving application performance. Because instead of getting data from a potentially slow data layer, your application will be getting data from memory (in a best case scenario). Caching can speed up read-heavy workloads for applications like Q&A portals, gaming, media sharing, and social networking. | ||||||
|
|
||||||
| <Alert> | ||||||
|
|
||||||
| This page covers both transaction mode (default) and stream mode. See <PlatformLink to="/tracing/new-spans/">New Spans</PlatformLink> to learn more. | ||||||
|
|
||||||
| </Alert> | ||||||
|
|
||||||
| Sentry offers a [cache-monitoring dashboard](https://sentry.io/orgredirect/organizations/:orgslug/dashboards/) that can be auto-instrumented for popular Python caching setups (like <PlatformLink to="/integrations/django/">Django</PlatformLink> and <PlatformLink to="/integrations/redis/">Redis</PlatformLink>). | ||||||
|
|
||||||
| If you're using a custom caching solution that doesn't have auto instrumentation, you can manually instrument it and use Sentry to get a look into how your caching solution is performing by following the setup instructions below. | ||||||
|
|
||||||
| To make it possible for Sentry to give you an overview of your cache performance, you'll need to create two spans - one indicating that something is being put into the cache, and a second one indicating that something is being fetched from the cache. | ||||||
|
|
||||||
| Make sure that there's a transaction running when you create the spans. If you're using a web framework those transactions will be created for you automatically. See <PlatformLink to="/tracing/">Tracing</PlatformLink> for more information. | ||||||
| In transaction mode, make sure that there's a transaction running when you create the spans. If you're using a web framework those transactions will be created for you automatically. See <PlatformLink to="/tracing/">Tracing</PlatformLink> for more information. | ||||||
|
|
||||||
| For detailed information about which data can be set, see the [Cache Module Developer Specification](https://develop.sentry.dev/sdk/performance/modules/caches/). | ||||||
|
|
||||||
|
|
@@ -21,16 +28,16 @@ If you're using anything other than <PlatformLink to="/integrations/django/">Dja | |||||
|
|
||||||
| ### Add Span When Putting Data Into the Cache | ||||||
|
|
||||||
| If the cache you’re using isn’t supported by auto instrumentation mentioned above, you can use the custom instrumentation instructions below to emit cache spans: | ||||||
| If the cache you're using isn't supported by auto instrumentation mentioned above, you can use the custom instrumentation instructions below to emit cache spans: | ||||||
|
|
||||||
| 1. Set the cache value with whatever cache library you happen to be using. | ||||||
| 2. Wrap the part of your application that uses the cached value with `with sentry_sdk.start_span(...)` | ||||||
| 2. Wrap the part of your application that uses the cached value with `with sentry_sdk.start_span(...)` (transaction mode) or `with sentry_sdk.traces.start_span(...)` (stream mode). | ||||||
| 3. Set `op` to `cache.put`. | ||||||
|
Contributor
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
|
||||||
| 4. Set `cache.item_size` to an integer representing the size of the cached item. | ||||||
|
|
||||||
| (The steps described above are documented in the snippet.) | ||||||
|
|
||||||
| ```python | ||||||
| ```python {tabTitle:Transaction Mode (Default)} | ||||||
| import my_caching | ||||||
| import sentry_sdk | ||||||
|
|
||||||
|
|
@@ -52,20 +59,43 @@ with sentry_sdk.start_span(op="cache.put") as span: | |||||
| span.set_data("cache.item_size", len(value)) # Warning: if value is very big this could use lots of memory | ||||||
| ``` | ||||||
|
|
||||||
| ```python {tabTitle:Stream Mode} | ||||||
| import my_caching | ||||||
| import sentry_sdk | ||||||
|
|
||||||
| key = "myCacheKey123" | ||||||
| value = "The value I want to cache." | ||||||
|
|
||||||
| with sentry_sdk.traces.start_span(attributes={"sentry.op": "cache.put"}) as span: | ||||||
| # Set a key in your caching using your custom caching solution | ||||||
| my_caching.set(key, value) | ||||||
|
|
||||||
| # Describe the cache server you are accessing | ||||||
| span.set_attributes({ | ||||||
| "network.peer.address": "cache.example.com/supercache", | ||||||
| "network.peer.port": 9000, | ||||||
|
|
||||||
| # Add the key(s) you want to set | ||||||
| "cache.key": [key], | ||||||
|
|
||||||
| # Add the size of the value you stored in the cache | ||||||
| "cache.item_size": len(value), # Warning: if value is very big this could use lots of memory | ||||||
| }) | ||||||
| ``` | ||||||
|
|
||||||
| ### Add Span When Retrieving Data From the Cache | ||||||
|
|
||||||
| If the cache you’re using isn’t supported by auto instrumentation mentioned above, you can use the custom instrumentation instructions below to emit cache spans: | ||||||
| If the cache you're using isn't supported by auto instrumentation mentioned above, you can use the custom instrumentation instructions below to emit cache spans: | ||||||
|
|
||||||
| 1. Fetch the cached value from whatever cache library you happen to be using. | ||||||
| 2. Wrap the part of your application that uses the cached value with `with sentry_sdk.start_span(...)` | ||||||
| 2. Wrap the part of your application that uses the cached value with `with sentry_sdk.start_span(...)` (transaction mode) or `with sentry_sdk.traces.start_span(...)` (stream mode). | ||||||
| 3. Set `op` to `cache.get`. | ||||||
| 4. Set `cache.hit` to a boolean value representing whether the value was successfully fetched from the cache or not. | ||||||
| 5. Set `cache.item_size` to an integer representing the size of the cached item. | ||||||
|
|
||||||
| (The steps described above are documented in the snippet.) | ||||||
|
|
||||||
| ```python | ||||||
| ```python {tabTitle:Transaction Mode (Default)} | ||||||
| import my_caching | ||||||
| import sentry_sdk | ||||||
|
|
||||||
|
|
@@ -94,4 +124,35 @@ with sentry_sdk.start_span(op="cache.get") as span: | |||||
| span.set_data("cache.hit", False) | ||||||
| ``` | ||||||
|
|
||||||
| ```python {tabTitle:Stream Mode} | ||||||
| import my_caching | ||||||
| import sentry_sdk | ||||||
|
|
||||||
| key = "myCacheKey123" | ||||||
| value = None | ||||||
|
|
||||||
| with sentry_sdk.traces.start_span(attributes={"sentry.op": "cache.get"}) as span: | ||||||
| # Get a key from your caching solution | ||||||
| value = my_caching.get(key) | ||||||
|
|
||||||
| # Describe the cache server you are accessing | ||||||
| span.set_attributes({ | ||||||
| "network.peer.address": "cache.example.com/supercache", | ||||||
| "network.peer.port": 9000, | ||||||
|
|
||||||
| # Add the key(s) you just retrieved from the cache | ||||||
| "cache.key": [key], | ||||||
| }) | ||||||
|
|
||||||
| if value is not None: | ||||||
| # If you retrieved a value, the cache was hit | ||||||
| span.set_attribute("cache.hit", True) | ||||||
|
|
||||||
| # Optionally also add the size of the value you retrieved | ||||||
| span.set_attribute("cache.item_size", len(value)) | ||||||
| else: | ||||||
| # If you could not retrieve a value, it was a miss | ||||||
| span.set_attribute("cache.hit", False) | ||||||
| ``` | ||||||
|
|
||||||
| You should now have the right spans in place. Head over to the [Cache dashboard](https://sentry.io/orgredirect/organizations/:orgslug/dashboards/) to see how your cache is performing. | ||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.