Sending Metrics (SDK)

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:

Copied
sentry-sdk @ 1.34.0

Once the SDK is installed you have to add experimental flag into your SDK init:

Copied
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:

Copied
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,
)

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):

Copied
"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"

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.

You can edit this page on GitHub.