Class: HubStep::Transport::HTTPJSON

Inherits:
LightStep::Transport::Base
  • Object
show all
Defined in:
lib/hubstep/transport/http_json.rb

Overview

HTTPJSON is our customized transport which add some additional instrumentation and performance improvements.

Callback Notes: To provide some observability into this transport’s operation, we allow a callback ‘on_report_callback` to be provided. This callback will be called with the signature (report, result, duration_ms) where result can be either the http response or an exception. This callback will be delivered while maintaining this transports mutex which should provide some measure of thread-safety for the caller.

Constant Summary collapse

ENCRYPTION_TLS =
'tls'
ENCRYPTION_NONE =
'none'

Instance Method Summary collapse

Constructor Details

#initialize(host:, port:, encryption: ENCRYPTION_TLS, access_token:, on_report_callback: nil) ⇒ HTTPJSON

Initialize the transport

Parameters:

  • host (String)

    host of the domain to the endpoind to push data

  • port (Numeric)

    port on which to connect

  • encryption (ENCRYPTION_TLS, ENCRYPTION_NONE) (defaults to: ENCRYPTION_TLS)

    kind of encryption to use

  • access_token (String)

    access token for LightStep server

  • on_report_callback (method) (defaults to: nil)

    Called after reporting has completed

Raises:

  • (Tracer::ConfigurationError)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/hubstep/transport/http_json.rb', line 36

def initialize(host:, port:, encryption: ENCRYPTION_TLS, access_token:, on_report_callback: nil)
  @on_report_callback = on_report_callback

  raise Tracer::ConfigurationError, "host must be specified" if host.nil? || host.empty?
  raise Tracer::ConfigurationError, "port must be specified" if port.nil?
  raise Tracer::ConfigurationError, "access_token must be a string" unless String === access_token
  raise Tracer::ConfigurationError, "access_token cannot be blank"  if access_token.empty?

  @access_token = access_token

  # This mutex protects the use of our Net::HTTP instance which we
  # maintain as a long lived connection. While a Lightstep::Transport is
  # typically called only from within the reporting thread, there are
  # some situations where this can be bypassed (directly calling `flush`
  # for example)
  @mutex = Mutex.new

  @http = Net::HTTP.new(host, port)
  @http.use_ssl = encryption == ENCRYPTION_TLS
  @http.keep_alive_timeout = 5
end

Instance Method Details

#report(report) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/hubstep/transport/http_json.rb', line 58

def report(report)
  start = Time.now

  req = request report

  @mutex.synchronize do
    # Typically, keep-alive for Net:HTTP is handled inside a start block,
    # but that's awkward with our threading model. By starting it manually,
    # once, the TCP connection should remain open for multiple report calls.
    @http.start unless @http.started?

    begin
      res = @http.request(req)
    rescue => e
      res = e
    ensure
      @on_report_callback&.call(report, res, start)
    end
  end

  nil
end