Sending Metrics (SDK)
Note
This is the recommended way to send metrics. We want to dogfood SDK methods to feel the ergonomics and the experience our customers will get.
Using Python SDK
In case you are looking to use the new metrics feature directly from the Python SDK you can do it as follows.
First, you have to use at least the 1.34.0
version of the sentry-python
SDK and add that as your dependency. For example:
sentry-sdk @ 1.34.0
Once the SDK is installed you have to add experimental flag into your SDK init:
import sentry_sdk
sentry_sdk.init(
# ...
_experiments={
"enable_metrics": True,
},
# ...
)
Ensure you have feature flags for both ingestion and UI enabled.
Then you can use the metrics method as follows:
import sentry_sdk
# Emit a counter
sentry_sdk.metrics.incr(
key="my_counter",
value=2,
tags=tags,
)
# Emit a distribution
sentry_sdk.metrics.distribution(
key="my_distribution",
value=15.0,
tags=tags,
# You can also specify a unit if you want
unit="second",
)
# Emit a set
sentry_sdk.metrics.set(
key="my_set",
value="john",
tags=tags,
)
# Emit a gauge
sentry_sdk.metrics.gauge(
key="my_gauge",
value=10,
tags=tags,
)
Note
Units Support
The following units are supported out of the box (but the system also allows you to pass your own unit if you need):
"nanosecond",
"microsecond",
"millisecond",
"second",
"minute",
"hour",
"day",
"week",
"bit",
"byte",
"kilobyte",
"kibibyte",
"megabyte",
"mebibyte",
"gigabyte",
"gibibyte",
"terabyte",
"tebibyte",
"petabyte",
"pebibyte",
"exabyte",
"exbibyte",
"ratio",
"percent",
"none"
Note
Please note that if you change the unit of a metric, it will be extracted as a new metric.
Automatic Tags Extraction
Since DDM has the goal of being tightly integrated into Sentry, the sentry-python
implementation already automatically adds the following tags to your metric:
transaction
release
environment
As we will see later on, these special tags are recognized by the DDM UI and will prompt you with helpful actions.