Class: OpenCensus::Trace::Exporters::Datadog::Transport

Inherits:
Object
  • Object
show all
Defined in:
lib/opencensus/trace/exporters/datadog/transport.rb

Constant Summary collapse

DEFAULT_HOSTNAME =
'127.0.0.1'.freeze
DEFAULT_PORT =
'8126'.freeze
TRACE_ENDPOINT =
'/v0.4/traces'.freeze
TIMEOUT =
5
TRACE_COUNT_HEADER =
'X-Datadog-Trace-Count'.freeze
RUBY_INTERPRETER =
RUBY_VERSION > '1.9' ? RUBY_ENGINE + '-' + RUBY_PLATFORM : 'ruby-' + RUBY_PLATFORM
TRACER_VERSION =
'OC/' + OpenCensus::Datadog::VERSION

Instance Method Summary collapse

Constructor Details

#initialize(hostname, port) ⇒ Transport

Returns a new instance of Transport.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/opencensus/trace/exporters/datadog/transport.rb', line 18

def initialize(hostname, port)
  @hostname = hostname.nil? ? DEFAULT_HOSTNAME : hostname
  @port = port.nil? ? DEFAULT_PORT : port

  @headers = {}
  @headers['Datadog-Meta-Lang'] = 'ruby'
  @headers['Datadog-Meta-Lang-Version'] = RUBY_VERSION
  @headers['Datadog-Meta-Lang-Interpreter'] = RUBY_INTERPRETER
  @headers['Datadog-Meta-Tracer-Version'] = TRACER_VERSION
  @headers['Content-Type'] = 'application/msgpack'
end

Instance Method Details

#upload(data, count = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/opencensus/trace/exporters/datadog/transport.rb', line 30

def upload(data, count = nil)
  begin
    headers = count.nil? ? {} : { TRACE_COUNT_HEADER => count.to_s }
    headers = headers.merge(@headers)
    request = Net::HTTP::Post.new(TRACE_ENDPOINT, headers)
    request.body = data

    response = Net::HTTP.start(@hostname, @port, read_timeout: TIMEOUT) { |http| http.request(request) }

    status_code = response.code.to_i
    if status_code >= 400 then
      Datadog.log.error("[daatadog-exporter] #{response.message} (status: #{status_code})")
    end
    status_code
  rescue StandardError => e
    Datadog.log.error("[daatadog-exporter] failed HTTP request to agent: #{e.message}")
    500
  end
end