Created
April 19, 2024 23:44
-
-
Save fallwith/c53aeee50b8d00f0adcd579d9d246196 to your computer and use it in GitHub Desktop.
Use the New Relic Ruby agent to report 10,000 spans within a minute
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# frozen_string_literal: true | |
# Instructions: | |
# 1. Copy a valid newrelic.yml file to the same directory as span_events.rb | |
# 2. Make sure at least the following parameters are set in newrelic.yml: | |
# - license_key: <your license key> | |
# - audit_log.enabled: true | |
# - span_events.max_samples_stored: 10000 | |
# 3. ruby span_events.rb | |
# 4. Check the stdout output to confirm that a capacity of 10,000 spans was | |
# successfully configured and that 10,000 spans were indeed reported. | |
# 5. Count the number of '"type":"Span"' occurrences in the | |
# log/newrelic_audit.log file to confirm it is 10,000: | |
# | |
# grep -o '"type":"Span"' log/newrelic_audit.log|wc -l | |
# | |
# NOTE: Be sure to delete the log directory before repeating the steps. | |
require 'bundler/inline' | |
puts 'Bundling the New Relic Ruby agent...' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'newrelic_rpm' | |
end | |
require 'new_relic/agent' | |
require 'tasks/newrelic' | |
module The | |
# The::Example - repro demo class | |
class Example | |
include ::NewRelic::Agent::Instrumentation::ControllerInstrumentation | |
include ::NewRelic::Agent::MethodTracer | |
def transaction_creating_method | |
12_000.times { span_creating_method } | |
end | |
add_transaction_tracer :transaction_creating_method, category: :task | |
def span_creating_method | |
11 + 38 | |
end | |
add_method_tracer :span_creating_method | |
end | |
end | |
NewRelic::Agent.manual_start | |
puts 'Testing...' | |
sec = NewRelic::Agent.instance.span_event_aggregator.instance_variable_get(:@buffer).instance_variable_get(:@capacity) | |
puts "The New Relic Ruby agent's span event capacity is internally showing as maxing out at #{sec} events" | |
start_time = Time.now | |
The::Example.new.transaction_creating_method | |
stop_time = Time.now | |
puts "Elapsed #{stop_time - start_time}" | |
s = NewRelic::Agent.instance.span_event_aggregator.instance_variable_get(:@buffer).instance_variable_get(:@seen) | |
i = NewRelic::Agent.instance.span_event_aggregator.instance_variable_get(:@buffer).instance_variable_get(:@items).size | |
puts "The New Relic Ruby agent's span event buffer believes it has seen #{s} spans and is prepared to report on " \ | |
"#{i} spans" | |
puts 'Shutting down the New Relic Ruby agent...' | |
NewRelic::Agent.shutdown |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment