Skip to content

Commit b970968

Browse files
committed
Instrument filtering, test
1 parent 696ba4d commit b970968

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

lib/html/pipeline.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,23 @@ def call(html, context = {}, result = nil)
8484
context = @default_context.merge(context)
8585
context = context.freeze
8686
result ||= @result_class.new
87-
result[:output] = @filters.inject(html) { |doc, filter| filter.call(doc, context, result) }
87+
result[:output] =
88+
@filters.inject(html) do |doc, filter|
89+
instrument "call_filter.html_pipeline", :filter => filter.name do
90+
filter.call(doc, context, result)
91+
end
92+
end
8893
result
8994
end
9095

96+
def instrument(event, payload = nil, &block)
97+
return yield unless instrumentation_service
98+
instrumentation_service.instrument event, payload do
99+
yield
100+
end
101+
end
102+
attr_accessor :instrumentation_service
103+
91104
# Like call but guarantee the value returned is a DocumentFragment.
92105
# Pipelines may return a DocumentFragment or a String. Callers that need a
93106
# DocumentFragment should use this method.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class MockedInstrumentationService
2+
attr_reader :events
3+
def initialize(events = [])
4+
@events = events
5+
end
6+
def instrument(event, payload = nil)
7+
res = yield
8+
events << [event, payload, res]
9+
res
10+
end
11+
end

test/html/pipeline_test.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require "test_helper"
2+
require "helpers/mocked_instrumentation_service"
3+
4+
class HTML::PipelineTest < Test::Unit::TestCase
5+
Pipeline = HTML::Pipeline
6+
class TestFilter
7+
def self.call(input, context, result)
8+
input
9+
end
10+
end
11+
12+
def setup
13+
@context = {}
14+
@result_class = Hash
15+
@pipeline = Pipeline.new [TestFilter], @context, @result_class
16+
end
17+
18+
def test_filter_instrumentation
19+
service = MockedInstrumentationService.new
20+
@pipeline.instrumentation_service = service
21+
filter("hello")
22+
event, payload, res = service.events.pop
23+
assert event, "event expected"
24+
assert_equal "call_filter.html_pipeline", event
25+
assert_equal TestFilter.name, payload[:filter]
26+
end
27+
28+
def filter(input)
29+
@pipeline.call(input)
30+
end
31+
end

0 commit comments

Comments
 (0)