class Atatus::BaseTransport

Constants

ERROR_ENDPOINT
ERROR_METRIC_ENDPOINT
HOSTINFO_ENDPOINT
METRIC_ENDPOINT
TRACE_ENDPOINT
TXN_ENDPOINT
TXN_HIST_ENDPOINT

Public Class Methods

new(config) click to toggle source
# File lib/atatus/collector/transport.rb, line 18
def initialize(config)
  @config = config

  @notify_host = @config.notify_host
  uri = URI(@notify_host)
  if not uri.kind_of?(URI::HTTPS) and not uri.kind_of?(URI::HTTP)
    @notify_host = "https://apm-rx.atatus.com"
  end

  @builder = Atatus::Builder.new(config)
  @headers = {}
  @headers['Content-Type'] = "application/json"

  @blocked = false
  @capture_percentiles = false
end

Public Instance Methods

error_metrics(start_time, end_time, metrics_data, requests_data) click to toggle source
# File lib/atatus/collector/transport.rb, line 57
def error_metrics(start_time, end_time, metrics_data, requests_data)
  payload = @builder.error_metrics(start_time, end_time, metrics_data, requests_data)
  post(ERROR_METRIC_ENDPOINT, payload)
end
errors(start_time, end_time, error_data) click to toggle source
# File lib/atatus/collector/transport.rb, line 62
def errors(start_time, end_time, error_data)
  payload = @builder.errors(start_time, end_time, error_data)
  post(ERROR_ENDPOINT, payload)
end
hostinfo(start_time) click to toggle source
# File lib/atatus/collector/transport.rb, line 35
def hostinfo(start_time)
  payload = @builder.hostinfo(start_time)
  post(HOSTINFO_ENDPOINT, payload)
end
metrics(start_time, end_time, metric_data) click to toggle source
# File lib/atatus/collector/transport.rb, line 67
def metrics(start_time, end_time, metric_data)
  payload = @builder.metrics(start_time, end_time, metric_data)
  post(METRIC_ENDPOINT, payload)
end
traces(start_time, end_time, data) click to toggle source
# File lib/atatus/collector/transport.rb, line 52
def traces(start_time, end_time, data)
  payload = @builder.traces(start_time, end_time, data)
  post(TRACE_ENDPOINT, payload)
end
txn_hist(start_time, end_time, data) click to toggle source
# File lib/atatus/collector/transport.rb, line 45
def txn_hist(start_time, end_time, data)
  if @capture_percentiles == true
    payload = @builder.txn_hist(start_time, end_time, data)
    post(TXN_HIST_ENDPOINT, payload)
  end
end
txns(start_time, end_time, data) click to toggle source
# File lib/atatus/collector/transport.rb, line 40
def txns(start_time, end_time, data)
  payload = @builder.txns(start_time, end_time, data)
  post(TXN_ENDPOINT, payload)
end

Private Instance Methods

post(endpoint, data) click to toggle source
# File lib/atatus/collector/transport.rb, line 74
def post(endpoint, data)
  # puts ::JSON.pretty_generate(data, :max_nesting => false)
  if @blocked == true and endpoint != HOSTINFO_ENDPOINT
    return
  end

  begin
    uri = URI(@notify_host + endpoint)
    uri.query = URI.encode_www_form({"license_key": @config.license_key, "agent_name": AGENT_NAME, "agent_version": VERSION})

    request = Net::HTTP::Post.new(uri.request_uri, @headers)
    request.body = ::JSON.dump(data)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER
    response = http.start { |http| http.request(request) }
  rescue SystemCallError, Timeout::Error, EOFError, SocketError => e
    error format('Atatus transport [%s%s] failed with exception: %s', @notify_host, endpoint, e.message)
    return
  end

  if @blocked == true
    @blocked = false
  end

  case response
  when Net::HTTPSuccess
    if endpoint == HOSTINFO_ENDPOINT
      @capture_percentiles = false

      if not response.body
        return
      end

      resp = JSON.parse response.body
      if resp
        if resp.key?("capturePercentiles")
          @capture_percentiles = resp["capturePercentiles"]
        end
      end
    else
      true
    end
  when Net::HTTPBadRequest
    if not response.body
      error format('Transport status 400, failed without content')
      return
    end

    resp = JSON.parse response.body
    if resp
      if resp.key?(:blocked)
        @blocked = resp[:blocked]
        if @blocked == true
          if resp.key?(:errorMessage)
            error format('Blocked from sending data as: %s', resp[:errorMessage])
            return
          end
        end
      end
    end

    if !resp
      error format('Transport status 400, failed with parsed content: %s', resp)
    else
      error format('Transport status 400, failed with content: %s', response.body)
    end
  else
    error format('Transport unexpected failure: [%s] [%s]', response.code, endpoint)
  end
end