Set Up Metrics for DartNEW

Metrics allow you to send, view and query counters, gauges and measurements from your Sentry-configured apps to track application health and drill down into related traces, logs, and errors.

With Sentry's Application Metrics, you can send counters, gauges, and distributions from your applications to Sentry. Once in Sentry, these metrics can be viewed alongside relevant errors, and searched using their individual attributes.

Metrics are supported in Sentry Dart SDK version 9.11.0 and above.

TypeUse For
countEvents (orders, clicks, API calls)
gaugeCurrent values (queue depth, connections)
distributionValue ranges (response times, payload sizes)

No setup required beyond SDK initialization. Metrics are captured whenever you call Sentry.metrics.*.

Copied
import 'package:sentry/sentry.dart';

// Count occurrences
Sentry.metrics.count('orders_created', 1);

// Track current values
Sentry.metrics.gauge('active_connections', 42);

// Track distributions
Sentry.metrics.distribution(
  'api_latency',
  187,
  unit: 'millisecond',
);

Attributes let you filter and group metrics in Sentry. Use them for:

  • Environment segmentation
  • Feature flag tracking
  • User tier analysis

Each metric has a 2KB size limit. If you exceed this, the metric will be dropped.

Copied
Sentry.metrics.count(
  'api_calls',
  1,
  attributes: {
    'endpoint': SentryAttribute.string('/api/orders'),
    'user_tier': SentryAttribute.string('pro'),
    'region': SentryAttribute.string('us-west'),
  },
);

Use scope APIs to set attributes that apply to all logs, metrics, and spans (only in stream mode) while the scope is active.

See Attributes for more information.

Copied
// Add attributes to the global scope
Sentry.setAttributes({'is_admin': SentryAttribute.bool(true)});

// All scope attributes are added to metrics
Sentry.metrics.count('clicks', 1);
Sentry.metrics.gauge(
  'time_since_refresh',
  4,
  unit: 'second',
);

For gauge and distribution metrics, specify a unit to help Sentry display values in a human-readable format.

Common units: millisecond, second, byte, kilobyte, megabyte. Use SentryMetricUnit to conveniently access supported units in Dart. See supported units for the full list.

Copied
Sentry.metrics.distribution(
  'response_time',
  187.5,
  unit: SentryMetricUnit.millisecond,
);

Sentry.metrics.gauge(
  'memory_usage',
  1024,
  unit: SentryMetricUnit.byte,
);

Filter or modify metrics before sending. The callback receives a metric and a Hint; return null to drop the metric. On SDK v9, the callback takes only the metric parameter.

Copied
import 'package:sentry/sentry.dart';

await Sentry.init((options) {
  options.dsn = '___PUBLIC_DSN___';
  options.beforeSendMetric = (metric, hint) {
    // Drop specific metrics
    if (metric.attributes.containsKey('debug')) {
      return null;
    }

    // Return the metric to send it
    return metric;
  };
});

Metrics are captured when you call Sentry.metrics.*; there is no option to turn them on or off. To stop sending metrics without removing those calls, return null from beforeSendMetric.

Copied
import 'package:sentry/sentry.dart';

// Disable metrics
await Sentry.init((options) {
  options.dsn = '___PUBLIC_DSN___';
  options.beforeSendMetric = (metric, hint) => null;
});

Sentry automatically attaches these attributes to every metric:

AttributeDescriptionContext
sentry.environmentEnvironment from SDK configAlways
sentry.releaseRelease version from SDK configAlways
sentry.sdk.nameSDK nameAlways
sentry.sdk.versionSDK versionAlways
os.nameOperating system nameIf available
os.versionOperating system versionIf available
user.id, user.name, user.emailUser identifiersIf available
sentry.replay_idSession replay IDIf replay available
Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").