Skip to content

Commit

Permalink
Merge branch 'antonpirker/python/custom-tracing-information' into ant…
Browse files Browse the repository at this point in the history
…onpirker/python/span_description_is_now_name
  • Loading branch information
antonpirker committed Sep 12, 2024
2 parents 9cded6e + 44a3f70 commit 6342c57
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ The Sentry SDK for Python does a very good job of auto instrumenting your applic

Adding transactions will allow you to instrument and capture certain regions of your code.

<Note>
<Alert level="info">

If you're using one of Sentry's SDK integrations, transactions will be created for you automatically.

</Note>
</Alert>

The following example creates a transaction for an expensive operation (in this case, `eat_pizza`), and then sends the result to Sentry:

Expand All @@ -35,8 +35,9 @@ The [API reference](https://getsentry.github.io/sentry-python/api.html#sentry_sd

If you want to have more fine-grained performance monitoring, you can add child spans to your transaction, which can be done by either:

- Using a context manager or
- Using a decorator, (this works on sync and `async` functions)
- Using a context manager
- Using a decorator (this works on sync and async functions)
- Manually

Calling a `sentry_sdk.start_span()` will find the current active transaction and attach the span to it.

Expand All @@ -53,7 +54,6 @@ def eat_pizza(pizza):
while pizza.slices > 0:
with sentry_sdk.start_span(name="Eat Slice"):
eat_slice(pizza.slices.pop())

```

### Using a Decorator
Expand All @@ -69,15 +69,32 @@ def eat_pizza(pizza):
with sentry_sdk.start_transaction(op="task", name="Eat Pizza"):
while pizza.slices > 0:
eat_slice(pizza.slices.pop())

```

<Alert title="Static & Class Methods" level="warning">
<Alert title="Static & Class Methods" level="info">

When tracing a static or class method, you **must** add the `@sentry_sdk.trace` decorator **after** the `@staticmethod` or `@classmethod` decorator (i.e., **closer** to the function definition). Otherwise, your function will break!

</Alert>

### Manually

```python
import sentry_sdk

def eat_slice(slice):
...

def eat_pizza(pizza):
with sentry_sdk.start_transaction(op="task", name="Eat Pizza"):
while pizza.slices > 0:
span = sentry_sdk.start_span(description="Eat Slice")
eat_slice(pizza.slices.pop())
span.finish()
```

When you create your span manually, make sure to call `span.finish()` after the block of code you want to wrap in a span to finish the span. If you do not finish the span it will not be sent to Sentry.

## Nested Spans

Spans can be nested to form a span tree. If you'd like to learn more, read our [distributed tracing](/product/sentry-basics/tracing/distributed-tracing/) documentation.
Expand All @@ -90,16 +107,10 @@ import sentry_sdk
def chew():
...

def swallow():
...

def eat_slice(slice):
with sentry_sdk.start_span(name="Eat Slice"):
with sentry_sdk.start_span(name="Chew"):
chew()
with sentry_sdk.start_span(name="Swallow"):
swallow()

```

### Using a Decorator
Expand All @@ -112,15 +123,32 @@ def chew():
...

@sentry_sdk.trace
def swallow():
def eat_slice(slice):
chew()
```

### Manually

```python
import sentry_sdk

def chew():
...

@sentry_sdk.trace
def eat_slice(slice):
parent_span = sentry_sdk.start_span(description="Eat Slice")

child_span = parent_span.start_child(description="Chew")
chew()
swallow()
child_span.finish()

parent_span.finish()
```

The parameters of `start_span()` and `start_child()` are the same. See the [API reference](https://getsentry.github.io/sentry-python/api.html#sentry_sdk.api.start_span) for more details.

When you create your span manually, make sure to call `span.finish()` after the block of code you want to wrap in a span to finish the span. If you do not finish the span it will not be sent to Sentry.

## Define Span Creation in a Central Place

To avoid having custom performance instrumentation code scattered all over your code base, pass a parameter <PlatformIdentifier name="functions-to-trace" /> to your `sentry_sdk.init()` call.
Expand All @@ -145,21 +173,21 @@ sentry_sdk.init(

Now, whenever a function specified in `functions_to_trace` will be executed, a span will be created and attached as a child to the currently running span.

<Alert level="warning" title="Important">
<Alert title="Important" level="info">

To enable performance monitoring for the functions specified in `functions_to_trace`, the SDK needs to load the function modules. Be aware, there may be code being executed in modules during module loading. To avoid this, use the method described above to trace your functions.

</Alert>

## Accessing the Current Transaction

To change data in an already ongoing transaction, use `Hub.current.scope.transaction`. This property will return a transaction if there's one running, otherwise it will return `None`.
To change data in an already ongoing transaction, use `sentry_sdk.get_current_scope().transaction`. This property will return a transaction if there's one running, otherwise it will return `None`.

```python
import sentry_sdk

def eat_pizza(pizza):
transaction = sentry_sdk.Hub.current.scope.transaction
transaction = sentry_sdk.get_current_scope().transaction

if transaction is not None:
transaction.set_tag("num_of_slices", len(pizza.slices))
Expand All @@ -170,7 +198,7 @@ def eat_pizza(pizza):

## Accessing the Current Span

To change data in the current span, use `sentry_sdk.Hub.current.scope.span`. This property will return a span if there's one running, otherwise it will return `None`.
To change data in the current span, use ` sentry_sdk.get_current_span()`. This function will return a span if there's one running, otherwise it will return `None`.

In this example, we'll set a tag in the span created by the `@sentry_sdk.trace` decorator.

Expand All @@ -179,10 +207,8 @@ import sentry_sdk

@sentry_sdk.trace
def eat_slice(slice):
span = sentry_sdk.Hub.current.scope.span
span = sentry_sdk.get_current_span()

if span is not None:
span.set_tag("slice_id", slice.id)

...
```
4 changes: 4 additions & 0 deletions platform-includes/set-release/dotnet.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ The SDK attempts to locate the release to attach to events sent to Sentry.
The SDK will first check if there's a version set on the environment via `SENTRY_RELEASE` and use it as-is.

If no version is found, the SDK will look at the [entry assembly's](<https://msdn.microsoft.com/en-us/library/system.reflection.assembly.getentryassembly(v=vs.110).aspx>) `AssemblyInformationalVersionAttribute`, which accepts a string value and is often used to set a GIT commit hash. If that returns null, then the SDK will look at the default `AssemblyVersionAttribute`, which accepts the numeric version number. The resulting release will be in the format `<assembly-name>@<version-number>`.

<Note>
An `AssemblyInformationalVersionAttribute` is included automatically in SDK-style projects with a default value of `"1.0.0.0"`. You can set the `Version` property in your project file to override this value or [disable it entirely](https://learn.microsoft.com/en-us/dotnet/standard/assembly/set-attributes-project-file#use-package-properties-as-assembly-attributes) by setting the `GenerateAssemblyInformationalVersionAttribute` property to `false`.
</Note>
2 changes: 1 addition & 1 deletion src/build/resolveOpenAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {DeRefedOpenAPI} from './open-api/types';

// SENTRY_API_SCHEMA_SHA is used in the sentry-docs GHA workflow in getsentry/sentry-api-schema.
// DO NOT change variable name unless you change it in the sentry-docs GHA workflow in getsentry/sentry-api-schema.
const SENTRY_API_SCHEMA_SHA = '600d5fbcfecb4085f6fd6f482cd6f1615ddc2b37';
const SENTRY_API_SCHEMA_SHA = '011581850181bb11b69df23151044f9e2b4567e4';

const activeEnv = process.env.GATSBY_ENV || process.env.NODE_ENV || 'development';

Expand Down

0 comments on commit 6342c57

Please sign in to comment.