Commit 6f3fc171 authored by Igor's avatar Igor
Browse files

swap specs for render_collection and sql instrumenters, add spec for non-sampled case

parent c5b0de30
Loading
Loading
Loading
Loading
+13 −20
Original line number Diff line number Diff line
require_relative "../../../../support/tracing/rails/shared_examples"

describe Labkit::Tracing::Rails::ActiveRecord::SqlInstrumenter do
describe Labkit::Tracing::Rails::ActionView::RenderCollectionInstrumenter do
  using RSpec::Parameterized::TableSyntax

  where(:name, :operation_name, :exception, :connection_id, :cached, :cached_response, :sql) do
    nil | "active_record:sqlquery" | nil | nil | nil | false | nil
    "" | "active_record:sqlquery" | nil | nil | nil | false | nil
    "User Load" | "active_record:User Load" | nil | nil | nil | false | nil
    "Repo Load" | "active_record:Repo Load" | StandardError.new | nil | nil | false | nil
    nil | "active_record:sqlquery" | nil | 123 | nil | false | nil
    nil | "active_record:sqlquery" | nil | nil | false | false | nil
    nil | "active_record:sqlquery" | nil | nil | true | true | nil
    nil | "active_record:sqlquery" | nil | nil | true | true | "SELECT * FROM users"
  where(:identifier, :count, :expected_count, :cache_hits, :expected_cache_hits, :exception) do
    nil | nil | 0 | nil | 0 | nil
    "" | nil | 0 | nil | 0 | nil
    "show.haml" | nil | 0 | nil | 0 | nil
    nil | 0 | 0 | nil | 0 | nil
    nil | 1 | 1 | nil | 0 | nil
    nil | nil | 0 | 0 | 0 | nil
    nil | nil | 0 | 1 | 1 | nil
    nil | nil | 0 | nil | 0 | StandardError.new
  end

  with_them do
    it_behaves_like "a tracing instrumenter" do
      let(:expected_span_name) { operation_name }
      let(:payload) { { name: name, exception: exception, connection_id: connection_id, cached: cached, sql: sql } }
      let(:expected_span_name) { "render_collection" }
      let(:payload) { { exception: exception, identifier: identifier, count: count, cache_hits: cache_hits } }
      let(:expected_tags) do
        {
          "component" => "ActiveRecord",
          "span.kind" => "client",
          "db.type" => "sql",
          "db.connection_id" => connection_id,
          "db.cached" => cached_response,
          "db.statement" => sql,
        }
        { "component" => "ActionView", "template.id" => identifier, "template.count" => expected_count, "template.cache.hits" => expected_cache_hits }
      end
    end
  end
+23 −13
Original line number Diff line number Diff line
require_relative "../../../../support/tracing/rails/shared_examples"

describe Labkit::Tracing::Rails::ActionView::RenderCollectionInstrumenter do
describe Labkit::Tracing::Rails::ActiveRecord::SqlInstrumenter do
  using RSpec::Parameterized::TableSyntax

  where(:identifier, :count, :expected_count, :cache_hits, :expected_cache_hits, :exception) do
    nil | nil | 0 | nil | 0 | nil
    "" | nil | 0 | nil | 0 | nil
    "show.haml" | nil | 0 | nil | 0 | nil
    nil | 0 | 0 | nil | 0 | nil
    nil | 1 | 1 | nil | 0 | nil
    nil | nil | 0 | 0 | 0 | nil
    nil | nil | 0 | 1 | 1 | nil
    nil | nil | 0 | nil | 0 | StandardError.new
  where(:name, :operation_name, :exception, :connection_id, :cached, :cached_response, :sql, :expected_sql, :tracing_sampled) do
    nil | "active_record:sqlquery" | nil | nil | nil | false | nil | nil | true
    "" | "active_record:sqlquery" | nil | nil | nil | false | nil | nil | true
    "User Load" | "active_record:User Load" | nil | nil | nil | false | nil | nil | true
    "Repo Load" | "active_record:Repo Load" | StandardError.new | nil | nil | false | nil | nil | true
    nil | "active_record:sqlquery" | nil | 123 | nil | false | nil | nil | true
    nil | "active_record:sqlquery" | nil | nil | false | false | nil | nil | true
    nil | "active_record:sqlquery" | nil | nil | true | true | nil | nil | true
    nil | "active_record:sqlquery" | nil | nil | true | true | "SELECT * FROM users" | "SELECT * FROM users" | true
    nil | "active_record:sqlquery" | nil | nil | true | true | "SELECT 42" | "SELECT $1" | true
    nil | "active_record:sqlquery" | nil | nil | true | true | "SELECT 42" | nil | false
  end

  with_them do
    it_behaves_like "a tracing instrumenter" do
      let(:expected_span_name) { "render_collection" }
      let(:payload) { { exception: exception, identifier: identifier, count: count, cache_hits: cache_hits } }
      let(:expected_span_name) { operation_name }
      let(:payload) { { name: name, exception: exception, connection_id: connection_id, cached: cached, sql: sql } }
      let(:expected_tags) do
        { "component" => "ActionView", "template.id" => identifier, "template.count" => expected_count, "template.cache.hits" => expected_cache_hits }
        {
          "component" => "ActiveRecord",
          "span.kind" => "client",
          "db.type" => "sql",
          "db.connection_id" => connection_id,
          "db.cached" => cached_response,
          "db.statement" => expected_sql,
        }
      end
      let(:sampled) { tracing_sampled }
    end
  end
end
+2 −1
Original line number Diff line number Diff line
@@ -5,11 +5,12 @@ RSpec.shared_examples "a tracing instrumenter" do

  let(:fake_span) { double("OpenTracing span") }
  let(:fake_scope) { double("OpenTracing scope", span: fake_span) }
  let(:sampled) { true }

  before do
    instrumenter.scope_stack.clear

    allow(Labkit::Tracing).to receive(:sampled?).and_return(true)
    allow(Labkit::Tracing).to receive(:sampled?).and_return(sampled)
  end

  describe "#start" do