Commit bfdc53b3 authored by Igor's avatar Igor
Browse files

rework specs, one per instrumenter

parent 635ff5c0
Loading
Loading
Loading
Loading
Loading
+33 −0
Original line number Diff line number Diff line
require_relative "../../../../support/tracing/rails/shared_examples"

describe Labkit::Tracing::Rails::ActiveRecord::SqlInstrumenter 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"
  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_tags) do
        {
          "component" => "ActiveRecord",
          "span.kind" => "client",
          "db.type" => "sql",
          "db.connection_id" => connection_id,
          "db.cached" => cached_response,
          "db.statement" => sql,
        }
      end
    end
  end
end
+22 −0
Original line number Diff line number Diff line
require_relative "../../../../support/tracing/rails/shared_examples"

describe Labkit::Tracing::Rails::ActionView::RenderPartialInstrumenter do
  using RSpec::Parameterized::TableSyntax

  where(:identifier, :exception) do
    nil | nil
    "" | nil
    "show.haml" | nil
    nil | StandardError.new
  end

  with_them do
    it_behaves_like "a tracing instrumenter" do
      let(:expected_span_name) { "render_partial" }
      let(:payload) { { exception: exception, identifier: identifier } }
      let(:expected_tags) do
        { "component" => "ActionView", "template.id" => identifier }
      end
    end
  end
end
+11 −0
Original line number Diff line number Diff line
require_relative "../../../../support/tracing/rails/shared_examples"

describe Labkit::Tracing::Rails::ActionView::RenderTemplateInstrumenter do
  it_behaves_like "a tracing instrumenter" do
    let(:expected_span_name) { "render_template" }
    let(:payload) { { identifier: "identifier", layout: "app/views/thing.html.haml" } }
    let(:expected_tags) do
      { "component" => "ActionView", "template.id" => payload[:identifier], "template.layout" => payload[:layout] }
    end
  end
end
+12 −0
Original line number Diff line number Diff line
# frozen_string_literal: true

describe Labkit::Tracing::Rails::ActionView::Subscriber do
  describe ".instrument" do
    it "is unsubscribeable" do
      unsubscribe = described_class.instrument

      expect(unsubscribe).not_to be_nil
      expect { unsubscribe.call }.not_to raise_error
    end
  end
end
+0 −130
Original line number Diff line number Diff line
# frozen_string_literal: true

describe Labkit::Tracing::Rails::ActionView::Subscriber do
  using RSpec::Parameterized::TableSyntax

  shared_examples "an actionview notification" do
    it "should notify the tracer when the hash contains null values" do
      expect(OpenTracing).to receive(:start_active_span).with(notification_name).and_return(scope)
      expect(scope).to receive(:span).and_return(span)

      expected_tags.each do |k, v|
        expect(span).to receive(:set_tag).with(k, v)
      end
      expect(span).to receive(:set_tag).with("error", true) if exception

      subscriber.start(notification_name, id, payload)
      subscriber.finish(notification_name, id, payload)
    end

    it "should notify the tracer when the payload is missing values" do
      expect(OpenTracing).to receive(:start_active_span).with(notification_name).and_return(scope)
      expect(scope).to receive(:span).and_return(span)

      expected_tags.each do |k, v|
        expect(span).to receive(:set_tag).with(k, v)
      end
      expect(span).to receive(:set_tag).with("error", true) if exception

      subscriber.start(notification_name, id, payload)
      subscriber.finish(notification_name, id, payload)
    end

    it "should not throw exceptions when with the default tracer" do
      expect {
        subscriber.start(notification_name, id, payload)
        subscriber.finish(notification_name, id, payload)
      }.not_to raise_error
    end
  end

  describe ".instrument" do
    it "is unsubscribeable" do
      unsubscribe = described_class.instrument

      expect(unsubscribe).not_to be_nil
      expect { unsubscribe.call }.not_to raise_error
    end
  end

  describe "#notify_render_template" do
    subject { described_class.new }

    let(:id) { SecureRandom.hex }
    let(:notification_name) { "render_template" }
    let(:subscriber) { Labkit::Tracing::Rails::ActionView::RenderTemplateInstrumenter.new }

    let(:scope) { OpenTracing::Scope.new }
    let(:span) { OpenTracing::Span.new }

    where(:identifier, :layout, :exception) do
      [[nil, nil, nil], ["", nil, nil], ["show.haml", nil, nil], [nil, "", nil], [nil, "layout.haml", nil], [nil, nil, StandardError.new]]
    end

    with_them do
      let(:payload) { { exception: exception, identifier: identifier, layout: layout } }

      let(:expected_tags) { { "component" => "ActionView", "template.id" => identifier, "template.layout" => layout } }

      it_behaves_like "an actionview notification"
    end
  end

  describe "#notify_render_collection" do
    subject { described_class.new }

    let(:id) { SecureRandom.hex }
    let(:notification_name) { "render_collection" }
    let(:subscriber) { Labkit::Tracing::Rails::ActionView::RenderCollectionInstrumenter.new }

    let(:scope) { OpenTracing::Scope.new }
    let(:span) { OpenTracing::Span.new }

    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
      let(:payload) { { exception: exception, identifier: identifier, count: count, cache_hits: cache_hits } }

      let(:expected_tags) do
        { "component" => "ActionView", "template.id" => identifier, "template.count" => expected_count, "template.cache.hits" => expected_cache_hits }
      end

      it_behaves_like "an actionview notification"
    end
  end

  describe "#notify_render_partial" do
    subject { described_class.new }

    let(:id) { SecureRandom.hex }
    let(:notification_name) { "render_partial" }
    let(:subscriber) { Labkit::Tracing::Rails::ActionView::RenderPartialInstrumenter.new }

    let(:scope) { OpenTracing::Scope.new }
    let(:span) { OpenTracing::Span.new }

    where(:identifier, :exception) do
      nil | nil
      "" | nil
      "show.haml" | nil
      nil | StandardError.new
    end

    with_them do
      let(:payload) { { exception: exception, identifier: identifier } }

      let(:expected_tags) { { "component" => "ActionView", "template.id" => identifier } }

      it_behaves_like "an actionview notification"
    end
  end
end
Loading