class TingYun::TingYunService

Constants

CONNECTION_ERRORS
PROTOCOL_VERSION

Attributes

appSessionKey[RW]
applicationId[RW]
attempts[RW]
config[RW]
data_version[RW]
idSecret[RW]
metric_id_cache[RW]
quantile_cache[RW]
request_timeout[RW]
shared_tcp_connection[RW]
ssl_cert_store[RW]

Public Class Methods

new(license_key=nil) click to toggle source
# File lib/ting_yun/ting_yun_service.rb, line 40
def initialize(license_key=nil)
  @license_key = license_key || TingYun::Agent.config[:'license_key']
  @request_timeout = TingYun::Agent.config[:timeout]
  @data_version =  ::TingYun::VERSION::DATA
  @marshaller =TingYun::Support::Serialize::JsonMarshaller.new
  @metric_id_cache = {}
  @quantile_cache = {}
  @hosts = TingYun::Agent.config[:collector_addresses].split(',')
end

Public Instance Methods

action_traceV3(traces) click to toggle source
# File lib/ting_yun/ting_yun_service/upload_service.rb, line 189
def action_traceV3(traces)
  upload_data = {
      :type => 'externalErrorTraceData',
      :errors => traces
  }
  invoke_remote(:trace, upload_data, :encoder=> json)
end
action_trace_data(traces) click to toggle source
# File lib/ting_yun/ting_yun_service/upload_service.rb, line 149
def action_trace_data(traces)
  upload_data = {
      :traces => traces
  }
  invoke_remote(:trace, upload_data, :encoder=> json)
end
audit_mode?() click to toggle source
# File lib/ting_yun/ting_yun_service.rb, line 133
def audit_mode?
  TingYun::Agent.config[:'nbs.audit_mode']
end
connect(settings={}) click to toggle source
# File lib/ting_yun/ting_yun_service.rb, line 50
def connect(settings={})
  if host = get_redirect_hostV3
    @collector = TingYun::Support.collector_from_host(host)
  end
  response = invoke_remote(:init, settings)
  @applicationId = response['appId']
  @appSessionKey = response['sessionKey'] unless response['sessionKey'].nil?
  @idSecret = response['idSecret']
  response
end
error_data(unsent_errors) click to toggle source
# File lib/ting_yun/ting_yun_service/upload_service.rb, line 132
def error_data(unsent_errors)
  upload_data = {
      :type => 'errorTraceData',
      :errors => unsent_errors
  }
  invoke_remote(:upload, [upload_data], :encoder=> json)
end
exception_data(unsent_exceptions) click to toggle source
# File lib/ting_yun/ting_yun_service/upload_service.rb, line 140
def exception_data(unsent_exceptions)
  upload_data = {
      :type => 'exceptionTraceData',
      :exceptions => unsent_exceptions
  }
  invoke_remote(:upload, [upload_data], :encoder=> json)
end
external_error_data(traces) click to toggle source
# File lib/ting_yun/ting_yun_service/upload_service.rb, line 180
def external_error_data(traces)
  upload_data = {
      :type => 'externalErrorTraceData',
      :errors => traces
  }
  invoke_remote(:upload, [upload_data], :encoder=> json)
end
force_restart() click to toggle source
# File lib/ting_yun/ting_yun_service.rb, line 87
def force_restart
  @applicationId = nil
  @appSessionKey = nil
  @metric_id_cache = {}
  @quantile_cache = {}
  close_shared_connection
end
getCmd() click to toggle source
# File lib/ting_yun/ting_yun_service/upload_service.rb, line 167
def getCmd

  upload_data = {
      :mTime => TingYun::Agent.config["mTime"],
  }
  result =  invoke_remote(:getCmd, upload_data, :encoder=> json)

  if !result.empty?
    server_config = TingYun::Configuration::ServerSource.new(result["args"])
    ::TingYun::Agent.config.replace_or_add_config(server_config)
  end
end
get_redirect_host() click to toggle source
# File lib/ting_yun/ting_yun_service.rb, line 61
def get_redirect_host
  @collector=TingYun::Support.collector
  invoke_remote(:getRedirectHost)
end
get_redirect_hostV3() click to toggle source
# File lib/ting_yun/ting_yun_service.rb, line 66
def get_redirect_hostV3
  @attempts = 0
  max_attempts = @hosts.size
  begin
    @collector=TingYun::Support.collectorV3(@hosts[@attempts])
    @attempts += 1
    invoke_remote(:redirect)
  rescue => e
    if @attempts < max_attempts
      TingYun::Agent.logger.error(e)
      TingYun::Agent.logger.debug("Retrying request to #{@hosts[@attempts]}")
      TingYun::Support.reset
      retry
    else
      @attempts = 0
      TingYun::Support.reset
      retry
    end
  end
end
handle_serialization_error(method, e) click to toggle source
# File lib/ting_yun/ting_yun_service.rb, line 137
def handle_serialization_error(method, e)
  msg = "Failed to serialize #{method} data using #{@marshaller.class.to_s}: #{e.inspect}"
  error = TingYun::Support::Exception::SerializationError.new(msg)
  error.set_backtrace(e.backtrace)
  raise error
end
invoke_remote(method, payload={}, options ={}) click to toggle source

private

# File lib/ting_yun/ting_yun_service.rb, line 103
def invoke_remote(method, payload={}, options ={})
  data = nil
  begin
    data = @marshaller.dump(payload, options)
  rescue StandardError, SystemStackError => e
    handle_serialization_error(method, e)
  end
  # serialize_finish_time = Time.now
  uri = remote_method_uri(method)
  full_uri = "http://#{@collector}#{uri}"

  if audit_mode?
    TingYun::Agent.logger.info("the prepare data: #{data} to url: #{full_uri}")
  else
    TingYun::Agent.logger.info("prepare to send data")
  end

  # data, encoding = compress_request_if_needed(data)
  response = send_requestV3(:data  => data,
                          :full_uri => full_uri,
                          :collector => @collector)

  if audit_mode?
    TingYun::Agent.logger.info("the return data: #{response.body}")
  else
    TingYun::Agent.logger.info("the send-process end")
  end
  @marshaller.load(decompress_response(response))
end
sql_trace(sql_trace) click to toggle source
# File lib/ting_yun/ting_yun_service/upload_service.rb, line 157
def sql_trace(sql_trace)
  upload_data = {
      :type => 'sqlTraceData',
      :sqlTraces => sql_trace
  }

  invoke_remote(:upload, [upload_data], :encoder=> json)

end