Skip to content

Commit

Permalink
Fix SQL explains in developer mode
Browse files Browse the repository at this point in the history
After the database metrics were added to the Newrelic web service,
we've been seeing errors when trying to view explains and backtraces
in developer mode. This change adjusts the array manipulation that
developer mode's explain_sql action uses to match the new format
returned by @segment.explain_sql.
  • Loading branch information
justinweiss authored and Sam Goldstein committed Jan 11, 2012
1 parent 8db407b commit 99704a8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/new_relic/rack/developer_mode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ def explain_sql
@obfuscated_sql = @segment.obfuscated_sql
end

explanations = @segment.explain_sql
headers, explanations = @segment.explain_sql
if explanations
@explanation = explanations.first
@explanation = explanations
if !@explanation.blank?
first_row = @explanation.first
# Show the standard headers if it looks like a mysql explain plan
Expand Down
31 changes: 31 additions & 0 deletions test/new_relic/rack/developer_mode_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,35 @@ def test_show_sample_summary_displays_sample_details
assert last_response.body.include?('SandwichesController')
assert last_response.body.include?('index')
end

def test_explain_sql_displays_query_plan
sample = @sampler.samples[0]
sql_segment = sample.sql_segments[0]
explain_results = NewRelic::Agent::Database.process_resultset(example_explain_as_hashes)

NewRelic::TransactionSample::Segment.any_instance.expects(:explain_sql).returns(explain_results)
get "/newrelic/explain_sql?id=#{sample.sample_id}&segment=#{sql_segment.segment_id}"

assert last_response.ok?
assert last_response.body.include?('PRIMARY')
assert last_response.body.include?('Key Length')
assert last_response.body.include?('Using index')
end

private

def example_explain_as_hashes
[{
'Id' => '1',
'Select Type' => 'SIMPLE',
'Table' => 'sandwiches',
'Type' => 'range',
'Possible Keys' => 'PRIMARY',
'Key' => 'PRIMARY',
'Key Length' => '4',
'Ref' => '',
'Rows' => '1',
'Extra' => 'Using index'
}]
end
end

0 comments on commit 99704a8

Please sign in to comment.