OpenTelemetry FactoryBot Instrumentation

The FactoryBot instrumentation is a community-maintained instrumentation for the FactoryBot gem.

This instrumentation creates spans for FactoryBot operations (create, build, build_stubbed, attributes_for), providing visibility into test data creation patterns. This is particularly useful when combined with ActiveRecord instrumentation, as it makes it explicit which database operations are triggered by FactoryBot vs direct ActiveRecord calls.

How do I get started?

Install the gem using:

gem install opentelemetry-instrumentation-factory_bot

Or, if you use bundler, include opentelemetry-instrumentation-factory_bot in your Gemfile.

Usage

To use the instrumentation, call use with the name of the instrumentation:

OpenTelemetry::SDK.configure do |c|
  c.use 'OpenTelemetry::Instrumentation::FactoryBot'
end

Alternatively, you can also call use_all to install all the available instrumentation.

OpenTelemetry::SDK.configure do |c|
  c.use_all
end

Examples

Example usage can be seen in the example/ directory

A simple example:

require 'opentelemetry/sdk'
require 'opentelemetry/instrumentation/factory_bot'
require 'factory_bot'

OpenTelemetry::SDK.configure do |c|
  c.use 'OpenTelemetry::Instrumentation::FactoryBot'
end

# Define a factory
FactoryBot.define do
  factory :user do
    name { 'Test User' }
    email { 'test@example.com' }
  end
end

# Create a user - this will generate a span named "FactoryBot.build(user)"
user = FactoryBot.build(:user)

Span Attributes

The instrumentation adds the following attributes to spans:

  • factory_bot.strategy - The internal strategy name (create, build, stub, attributes_for)
  • factory_bot.factory_name - The name of the factory being used
  • factory_bot.traits - Comma-separated list of traits applied (if any)

How can I get involved?

The opentelemetry-instrumentation-factory_bot gem source is on github, along with related gems including opentelemetry-api and opentelemetry-sdk.

The OpenTelemetry Ruby gems are maintained by the OpenTelemetry Ruby special interest group (SIG). You can get involved by joining us on our GitHub Discussions, Slack Channel or attending our weekly meeting. See the meeting calendar for dates and times. For more information on this and other language SIGs, see the OpenTelemetry community page.

License

The opentelemetry-instrumentation-factory_bot gem is distributed under the Apache 2.0 license. See LICENSE for more information.