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.

BugsnagPerformanceSpan *span = [BugsnagPerformance startSpanWithName:@"login"];
let span = BugsnagPerformance.startSpan(name: "login")

However, sometimes you may wish to end the span in a separate component, requiring an awkward passing of the object between scopes. The BugsnagPerformanceNamedSpans 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.

BugsnagPerformanceNamedSpanQuery *query = [BugsnagPerformanceNamedSpanQuery queryWithName:@"login"];
BugsnagPerformanceSpan *span = [BugsnagPerformance getSpanControlsWithQuery:query];
if (span != nil) {
  [span setAttribute:@"user.id" withValue:@(12345)];
  [span end];
}
guard let span =
  BugsnagPerformance.getSpanControls(with: BugsnagPerformanceNamedSpanQuery(name:"login"))
  as? BugsnagPerformanceSpan else { return }
span.setAttribute("user.id", withValue: 12345)
span.end()

Installation

Using CocoaPods

Add the BugsnagPerformanceNamedSpans pod to your Podfile:

pod 'BugsnagPerformanceNamedSpans'

Run pod install after updating your Podfile.

Using Swift Package Manager

Open your Xcode project and select FileAdd Packages…

Search for https://github.com/bugsnag/bugsnag-cocoa-performance as the package URL

Set Dependency Rule exact version to v1.15.0, then click Add Package.

When prompted on the next screen, select the BugsnagPerformanceNamedSpans package product to add it to your project.

Manual Installation

Select your project in the Project Navigator, then add BugsnagPerformanceNamedSpans.framework to the Frameworks, Libraries and Embedded Content section of your app’s target.

Configuration

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

#import <BugsnagPerformanceNamedSpans/BugsnagPerformanceNamedSpans.h>

BugsnagPerformanceConfiguration *config = [BugsnagPerformanceConfiguration loadConfig];
[config addPlugin:[BugsnagPerformanceNamedSpansPlugin new]];
[BugsnagPerformance startWithConfiguration:config];
import BugsnagPerformanceNamedSpans

let config = BugsnagPerformanceConfiguration.loadConfig()
config.add(BugsnagPerformanceNamedSpansPlugin())
BugsnagPerformance.start(configuration: config)

Usage

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

#import <BugsnagPerformanceNamedSpans/BugsnagPerformanceNamedSpans.h>

BugsnagPerformanceNamedSpanQuery *query = [BugsnagPerformanceNamedSpanQuery queryWithName:@"login"];
BugsnagPerformanceSpan *span = [BugsnagPerformance getSpanControlsWithQuery:query];

if (span != nil) {
  [span setAttribute:@"user.id" withValue:@(12345)];
  [span end];
}
import BugsnagPerformanceNamedSpans

guard let span = BugsnagPerformance.getSpanControls(
  with: BugsnagPerformanceNamedSpanQuery(name:"login"))
  as? BugsnagPerformanceSpan else { return }

span.setAttribute("user.id", withValue: 12345)
span.end()

If the span does not exist; has already ended; or is no longer valid, it will return nil. 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.