Named span access

Use our named span plugin to track open spans and access them by name from different parts of your codebase.

When starting custom spans, a reference to the object representing that span is returned and can be used to set further attributes or end the measurement.

Span span = BugsnagPerformance.startSpan("login");
val span = BugsnagPerformance.startSpan("login")

However, sometimes you may wish to end the span in a separate component, requiring an awkward passing of the object between scopes. The NamedSpanControlsPlugin module keeps track of all open spans and allows you to obtain a reference to them using just their name from anywhere in your codebase.

Span span = BugsnagPerformance.getSpanControls(new NamedSpanQuery("login"));
if (span != null) {
  span.setAttribute("user.id", 12345);
  span.end();
}

val span = BugsnagPerformance.getSpanControls(NamedSpanQuery("login"))
span?.let {
  it.setAttribute("user.id", 12345)
  it.end()
}

Installation

Add bugsnag-plugin-android-performance-named-spans as a dependency to your project:

dependencies {
    implementation "com.bugsnag:bugsnag-android-performance:2.+"
    implementation "com.bugsnag:bugsnag-plugin-android-performance-named-spans:2.+"
    implementation "com.squareup.okhttp3:okhttp:4.+"
}
dependencies {
    implementation("com.bugsnag:bugsnag-android-performance:2.+")
    implementation("com.bugsnag:bugsnag-plugin-android-performance-named-spans:2.+")
    implementation("com.squareup.okhttp3:okhttp:4.+")
}

Configuration

To enable the plugin, include it in the configuration options when starting the SDK:

import com.bugsnag.android.performance.controls.NamedSpanControlsPlugin;

PerformanceConfiguration config = PerformanceConfiguration.load(this);
config.addPlugin(new NamedSpanControlsPlugin());
BugsnagPerformance.start(config);
import com.bugsnag.android.performance.controls.NamedSpanControlsPlugin

BugsnagPerformance.start(PerformanceConfiguration.load(this).apply {
  addPlugin(NamedSpanControlsPlugin())
})

Usage

The plugin maintains a map of open spans that can be queried via BugsnagPerformance.getSpanControls(). The NamedSpanQuery class allows you to specify the name of the span you want to retrieve.

import com.bugsnag.android.performance.controls.NamedSpanQuery;

Span span = BugsnagPerformance.getSpanControls(new NamedSpanQuery("login"));

if (span != null) {
  span.setAttribute("user.id", 12345);
  span.end();
}

import com.bugsnag.android.performance.controls.NamedSpanQuery

val span = BugsnagPerformance.getSpanControls(NamedSpanQuery("login"))

span?.let {
  it.setAttribute("user.id", 12345)
  it.end()
}

If the span does not exist; has already ended; or is no longer valid, it will return null. If multiple spans with the same name are open, only the most recent one will be returned.

To avoid memory leaks caused by spans not being ended, spans older than 10 minutes are removed from tracked memory to allow them to be deallocated.