Class: TingYun::TingYunService

Inherits:
Object
  • Object
show all
Includes:
Http, UploadService
Defined in:
lib/ting_yun/ting_yun_service.rb,
lib/ting_yun/ting_yun_service/ssl.rb,
lib/ting_yun/ting_yun_service/http.rb,
lib/ting_yun/ting_yun_service/request.rb,
lib/ting_yun/ting_yun_service/connection.rb,
lib/ting_yun/ting_yun_service/upload_service.rb

Defined Under Namespace

Modules: Connection, Http, Request, Ssl, UploadService

Constant Summary collapse

CONNECTION_ERRORS =
[Timeout::Error, EOFError, SystemCallError, SocketError].freeze
PROTOCOL_VERSION =
1

Constants included from UploadService

UploadService::EMPTY_PARENT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from UploadService

#base64_compressed_json, #build_metric_data_array, #compressed_json, #fill_metric_id_cache, #json, #metric_data

Methods included from Http

#decompress_response, #remote_method_uri

Methods included from Connection

#close_shared_connection, #create_and_start_http_connection, #create_http_connection, #establish_shared_connection, #http_connection, #session, #session_with_keepalive, #session_without_keepalive, #setup_connection_timeouts, #start_connection

Methods included from Request

#check_post_size, #compress_request_if_needed, #send_request, #user_agent, #valid_to_marshal?

Methods included from Ssl

#cert_file_path, #setup_connection_for_ssl

Constructor Details

#initialize(license_key = nil, collector = TingYun::Support.collector) ⇒ TingYunService



34
35
36
37
38
39
40
41
42
# File 'lib/ting_yun/ting_yun_service.rb', line 34

def initialize(license_key=nil,collector=TingYun::Support.collector)

  @license_key = license_key || TingYun::Agent.config[:'license_key']
  @request_timeout = TingYun::Agent.config[:timeout]
  @collector = collector
  @data_version = TingYun::VERSION::STRING
  @marshaller =TingYun::Support::Serialize::JsonMarshaller.new
  @metric_id_cache = {}
end

Instance Attribute Details

#applicationIdObject

Returns the value of attribute applicationId.



26
27
28
# File 'lib/ting_yun/ting_yun_service.rb', line 26

def applicationId
  @applicationId
end

#appSessionKeyObject

Returns the value of attribute appSessionKey.



26
27
28
# File 'lib/ting_yun/ting_yun_service.rb', line 26

def appSessionKey
  @appSessionKey
end

#data_versionObject

Returns the value of attribute data_version.



26
27
28
# File 'lib/ting_yun/ting_yun_service.rb', line 26

def data_version
  @data_version
end

#metric_id_cacheObject

Returns the value of attribute metric_id_cache.



26
27
28
# File 'lib/ting_yun/ting_yun_service.rb', line 26

def metric_id_cache
  @metric_id_cache
end

#request_timeoutObject

Returns the value of attribute request_timeout.



26
27
28
# File 'lib/ting_yun/ting_yun_service.rb', line 26

def request_timeout
  @request_timeout
end

#shared_tcp_connectionObject

Returns the value of attribute shared_tcp_connection.



26
27
28
# File 'lib/ting_yun/ting_yun_service.rb', line 26

def shared_tcp_connection
  @shared_tcp_connection
end

#ssl_cert_storeObject

Returns the value of attribute ssl_cert_store.



26
27
28
# File 'lib/ting_yun/ting_yun_service.rb', line 26

def ssl_cert_store
  @ssl_cert_store
end

Instance Method Details

#action_trace_data(traces) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/ting_yun/ting_yun_service/upload_service.rb', line 123

def action_trace_data(traces)
  upload_data = {
      :type => 'actionTraceData',
      :actionTraces => traces
  }
  invoke_remote(:upload, [upload_data], :encoder=> json)
end

#connect(settings = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/ting_yun/ting_yun_service.rb', line 44

def connect(settings={})
  if host = get_redirect_host
    @collector = TingYun::Support.collector_from_host(host)
  end
  response = invoke_remote(:initAgentApp, [settings])
  TingYun::Agent.logger.info("initAgentApp response: #{response}") if TingYun::Agent.config[:'nbs.audit_mode']
  @applicationId = response['applicationId']
  @appSessionKey = response['appSessionKey']
  response
end

#error_data(unsent_errors) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/ting_yun/ting_yun_service/upload_service.rb', line 114

def error_data(unsent_errors)
  upload_data = {
      :type => 'errorTraceData',
      :errors => unsent_errors
  }
  invoke_remote(:upload, [upload_data], :encoder=> json)
end

#external_error_data(traces) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/ting_yun/ting_yun_service/upload_service.rb', line 142

def external_error_data(traces)
  upload_data = {
      :type => 'externalErrorTraceData',
      :errors => traces
  }
  invoke_remote(:upload, [upload_data], :encoder=> json)
end

#force_restartObject



59
60
61
62
63
64
# File 'lib/ting_yun/ting_yun_service.rb', line 59

def force_restart
  @applicationId = nil
  @appSessionKey = nil
  @metric_id_cache = {}
  close_shared_connection
end

#get_redirect_hostObject



55
56
57
# File 'lib/ting_yun/ting_yun_service.rb', line 55

def get_redirect_host
  invoke_remote(:getRedirectHost)
end

#handle_serialization_error(method, e) ⇒ Object



114
115
116
117
118
119
# File 'lib/ting_yun/ting_yun_service.rb', line 114

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 = {}) ⇒ Object

private



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ting_yun/ting_yun_service.rb', line 74

def invoke_remote(method, payload=[], options = {})

  data = nil
  payload = payload[0]  if method == :initAgentApp
  begin
    data = @marshaller.dump(payload, options)
  rescue StandardError, SystemStackError => e
    handle_serialization_error(method, e)
  end
  # serialize_finish_time = Time.now

  if TingYun::Agent.config[:'nbs.audit_mode']
    TingYun::Agent.logger.info("the prepare data: #{data}")
  else
    TingYun::Agent.logger.info("prepare to send data")
  end

  data, encoding = compress_request_if_needed(data)
  # size = data.size

  uri = remote_method_uri(method)
  full_uri = "#{@collector}#{uri}"
  TingYun::Agent.logger.info("url: #{full_uri}") if TingYun::Agent.config[:'nbs.audit_mode']
  response = send_request(:data      => data,
                          :uri       => uri,
                          :encoding  => encoding,
                          :collector => @collector)

  if TingYun::Agent.config[:'nbs.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))
ensure
  # take the initiative to GC
  payload = nil
  data = nil
end

#sql_trace(sql_trace) ⇒ Object



132
133
134
135
136
137
138
139
140
# File 'lib/ting_yun/ting_yun_service/upload_service.rb', line 132

def sql_trace(sql_trace)
  upload_data = {
      :type => 'sqlTraceData',
      :sqlTraces => sql_trace
  }

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

end