Skip to content

Commit ee2adcf

Browse files
committed
Instrument any contextually interesting data, cleanup, test
1 parent 607e059 commit ee2adcf

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

lib/html/pipeline.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,13 @@ def call(html, context = {}, result = nil)
9494
context = @default_context.merge(context)
9595
context = context.freeze
9696
result ||= @result_class.new
97-
instrument "call_pipeline.html_pipeline", :filters => @filters.map(&:name) do |payload|
97+
payload = default_payload :filters => @filters.map(&:name),
98+
:doc => html, :context => context, :result => result
99+
instrument "call_pipeline.html_pipeline", payload do
98100
result[:output] =
99101
@filters.inject(html) do |doc, filter|
100102
perform_filter(filter, doc, context, result)
101103
end
102-
payload[:result] = result
103104
end
104105
result
105106
end
@@ -110,8 +111,9 @@ def call(html, context = {}, result = nil)
110111
#
111112
# Returns the result of the filter.
112113
def perform_filter(filter, doc, context, result)
113-
instrument "call_filter.html_pipeline", :filter => filter.name do |payload|
114-
payload[:result] = result
114+
payload = default_payload :filter => filter.name,
115+
:doc => doc, :context => context, :result => result
116+
instrument "call_filter.html_pipeline", payload do |payload|
115117
payload[:output] = filter.call(doc, context, result)
116118
end
117119
end
@@ -121,13 +123,22 @@ def perform_filter(filter, doc, context, result)
121123
#
122124
# Returns the result of the provided block.
123125
def instrument(event, payload = nil)
126+
payload ||= default_payload
124127
return yield(payload) unless instrumentation_service
125-
payload ||= {}
126128
instrumentation_service.instrument event, payload do |payload|
127129
yield payload
128130
end
129131
end
130132

133+
# Internal: Default payload for instrumentation.
134+
#
135+
# Accepts a Hash of additional payload data to be merged.
136+
#
137+
# Returns a Hash.
138+
def default_payload(payload = {})
139+
{:pipeline => self.class.name}.merge(payload)
140+
end
141+
131142
# Like call but guarantee the value returned is a DocumentFragment.
132143
# Pipelines may return a DocumentFragment or a String. Callers that need a
133144
# DocumentFragment should use this method.

test/html/pipeline_test.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class HTML::PipelineTest < Test::Unit::TestCase
55
Pipeline = HTML::Pipeline
66
class TestFilter
77
def self.call(input, context, result)
8-
input
8+
input.reverse
99
end
1010
end
1111

@@ -19,22 +19,28 @@ def test_filter_instrumentation
1919
service = MockedInstrumentationService.new
2020
service.subscribe "call_filter.html_pipeline"
2121
@pipeline.instrumentation_service = service
22-
filter("hello")
22+
filter(body = "hello")
2323
event, payload, res = service.events.pop
2424
assert event, "event expected"
2525
assert_equal "call_filter.html_pipeline", event
2626
assert_equal TestFilter.name, payload[:filter]
27+
assert_equal @pipeline.class.name, payload[:pipeline]
28+
assert_equal body, payload[:doc]
29+
assert_equal body.reverse, payload[:output]
2730
end
2831

2932
def test_pipeline_instrumentation
3033
service = MockedInstrumentationService.new
3134
service.subscribe "call_pipeline.html_pipeline"
3235
@pipeline.instrumentation_service = service
33-
filter("hello")
36+
filter(body = "hello")
3437
event, payload, res = service.events.pop
3538
assert event, "event expected"
3639
assert_equal "call_pipeline.html_pipeline", event
3740
assert_equal @pipeline.filters.map(&:name), payload[:filters]
41+
assert_equal @pipeline.class.name, payload[:pipeline]
42+
assert_equal body, payload[:doc]
43+
assert_equal body.reverse, payload[:result][:output]
3844
end
3945

4046
def test_default_instrumentation_service

0 commit comments

Comments
 (0)