Commit ee2adcfd authored by Matt Todd's avatar Matt Todd

Instrument any contextually interesting data, cleanup, test

parent 607e0593
......@@ -94,12 +94,13 @@ module HTML
context = @default_context.merge(context)
context = context.freeze
result ||= @result_class.new
instrument "call_pipeline.html_pipeline", :filters => @filters.map(&:name) do |payload|
payload = default_payload :filters => @filters.map(&:name),
:doc => html, :context => context, :result => result
instrument "call_pipeline.html_pipeline", payload do
result[:output] =
@filters.inject(html) do |doc, filter|
perform_filter(filter, doc, context, result)
end
payload[:result] = result
end
result
end
......@@ -110,8 +111,9 @@ module HTML
#
# Returns the result of the filter.
def perform_filter(filter, doc, context, result)
instrument "call_filter.html_pipeline", :filter => filter.name do |payload|
payload[:result] = result
payload = default_payload :filter => filter.name,
:doc => doc, :context => context, :result => result
instrument "call_filter.html_pipeline", payload do |payload|
payload[:output] = filter.call(doc, context, result)
end
end
......@@ -121,13 +123,22 @@ module HTML
#
# Returns the result of the provided block.
def instrument(event, payload = nil)
payload ||= default_payload
return yield(payload) unless instrumentation_service
payload ||= {}
instrumentation_service.instrument event, payload do |payload|
yield payload
end
end
# Internal: Default payload for instrumentation.
#
# Accepts a Hash of additional payload data to be merged.
#
# Returns a Hash.
def default_payload(payload = {})
{:pipeline => self.class.name}.merge(payload)
end
# Like call but guarantee the value returned is a DocumentFragment.
# Pipelines may return a DocumentFragment or a String. Callers that need a
# DocumentFragment should use this method.
......
......@@ -5,7 +5,7 @@ class HTML::PipelineTest < Test::Unit::TestCase
Pipeline = HTML::Pipeline
class TestFilter
def self.call(input, context, result)
input
input.reverse
end
end
......@@ -19,22 +19,28 @@ class HTML::PipelineTest < Test::Unit::TestCase
service = MockedInstrumentationService.new
service.subscribe "call_filter.html_pipeline"
@pipeline.instrumentation_service = service
filter("hello")
filter(body = "hello")
event, payload, res = service.events.pop
assert event, "event expected"
assert_equal "call_filter.html_pipeline", event
assert_equal TestFilter.name, payload[:filter]
assert_equal @pipeline.class.name, payload[:pipeline]
assert_equal body, payload[:doc]
assert_equal body.reverse, payload[:output]
end
def test_pipeline_instrumentation
service = MockedInstrumentationService.new
service.subscribe "call_pipeline.html_pipeline"
@pipeline.instrumentation_service = service
filter("hello")
filter(body = "hello")
event, payload, res = service.events.pop
assert event, "event expected"
assert_equal "call_pipeline.html_pipeline", event
assert_equal @pipeline.filters.map(&:name), payload[:filters]
assert_equal @pipeline.class.name, payload[:pipeline]
assert_equal body, payload[:doc]
assert_equal body.reverse, payload[:result][:output]
end
def test_default_instrumentation_service
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment