Class: LightStep::Transport::HTTPJSON

Inherits:
Base
  • Object
show all
Defined in:
lib/lightstep/transport/http_json.rb

Overview

HTTPJSON is a transport that sends reports via HTTP in JSON format. It is thread-safe, however it is not fork-safe. When forking, all items in the queue will be copied and sent in duplicate.

When forking, you should first ‘disable` the tracer, then `enable` it from within the fork (and in the parent post-fork). See `examples/fork_children/main.rb` for an example.

Constant Summary collapse

LIGHTSTEP_HOST =
"collector.lightstep.com"
LIGHTSTEP_PORT =
443
ENCRYPTION_TLS =
'tls'
ENCRYPTION_NONE =
'none'

Instance Method Summary collapse

Constructor Details

#initialize(host: LIGHTSTEP_HOST, port: LIGHTSTEP_PORT, verbose: 0, encryption: ENCRYPTION_TLS, access_token:) ⇒ HTTPJSON

Initialize the transport

Parameters:

  • host (String) (defaults to: LIGHTSTEP_HOST)

    host of the domain to the endpoind to push data

  • port (Numeric) (defaults to: LIGHTSTEP_PORT)

    port on which to connect

  • verbose (Numeric) (defaults to: 0)

    verbosity level. Right now 0-3 are supported

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

    kind of encryption to use

  • access_token (String)

    access token for LightStep server

Raises:

  • (ConfigurationError)


27
28
29
30
31
32
33
34
35
36
# File 'lib/lightstep/transport/http_json.rb', line 27

def initialize(host: LIGHTSTEP_HOST, port: LIGHTSTEP_PORT, verbose: 0, encryption: ENCRYPTION_TLS, access_token:)
  @host = host
  @port = port
  @verbose = verbose
  @encryption = encryption

  raise ConfigurationError, "access_token must be a string" unless String === access_token
  raise ConfigurationError, "access_token cannot be blank"  if access_token.empty?
  @access_token = access_token
end

Instance Method Details

#report(report) ⇒ Object

Queue a report for sending



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/lightstep/transport/http_json.rb', line 39

def report(report)
  p report if @verbose >= 3

  https = Net::HTTP.new(@host, @port)
  https.use_ssl = @encryption == ENCRYPTION_TLS
  req = Net::HTTP::Post.new('/api/v0/reports')
  req['LightStep-Access-Token'] = @access_token
  req['Content-Type'] = 'application/json'
  req['Connection'] = 'keep-alive'
  req.body = report.to_json
  res = https.request(req)

  puts res.to_s if @verbose >= 3

  nil
end