lib/zipkin/json_client.rb in zipkin-0.2.0 vs lib/zipkin/json_client.rb in zipkin-0.3.0
- old
+ new
@@ -1,34 +1,44 @@
-require 'faraday'
-require 'faraday_middleware'
-require 'sucker_punch'
+require 'net/http'
+require 'uri'
+require 'json'
module Zipkin
class JsonClient
- def initialize(url)
- @faraday = Faraday.new(url: url) do |faraday|
- faraday.request :json
- faraday.request :retry, max: 3, interval: 10, backoff_factor: 2
- faraday.adapter Faraday.default_adapter
+ def initialize(url:, collector:, flush_interval:)
+ @collector = collector
+ @flush_interval = flush_interval
+ @spans_uri = URI.parse("#{url}/api/v1/spans")
+ end
+
+ def start
+ @thread = Thread.new do
+ loop do
+ emit_batch(@collector.retrieve)
+ sleep @flush_interval
+ end
end
end
- def send_span(payload)
- SpanSender.perform_async(payload: payload, faraday: @faraday)
+ def stop
+ @thread.terminate if @thread
+ emit_batch(@collector.retrieve)
end
- class SpanSender
- include SuckerPunch::Job
- workers 4
+ private
- def perform(payload:, faraday:)
- response = faraday.post '/api/v1/spans' do |req|
- req.body = [payload]
- end
+ def emit_batch(spans)
+ return if spans.empty?
- if response.status != 202
- STDERR.puts(response.body)
- end
+ http = Net::HTTP.new(@spans_uri.host, @spans_uri.port)
+ request = Net::HTTP::Post.new(@spans_uri.request_uri, {
+ 'Content-Type' => 'application/json'
+ })
+ request.body = JSON.dump(spans)
+ response = http.request(request)
+
+ if response.code != 202
+ STDERR.puts(response.body)
end
end
end
end