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, #calculate_quantile, #compressed_json, #fill_metric_id_cache, #generate_action, #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) ⇒ TingYunService

Returns a new instance of TingYunService.



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

def initialize(license_key=nil)

  @license_key = license_key || TingYun::Agent.config[:'license_key']
  @request_timeout = TingYun::Agent.config[:timeout]
  @data_version = "1.4"
  @marshaller =TingYun::Support::Serialize::JsonMarshaller.new
  @metric_id_cache = {}
  @quantile_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

#quantile_cacheObject

Returns the value of attribute quantile_cache.



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

def quantile_cache
  @quantile_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



148
149
150
151
152
153
154
# File 'lib/ting_yun/ting_yun_service/upload_service.rb', line 148

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

#audit_mode?Boolean

Returns:

  • (Boolean)


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

def audit_mode?
  TingYun::Agent.config[:'nbs.audit_mode']
end

#connect(settings = {}) ⇒ Object



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

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



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

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

#exception_data(unsent_exceptions) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/ting_yun/ting_yun_service/upload_service.rb', line 139

def exception_data(unsent_exceptions)
  upload_data = {
      :type => 'exceptionTraceData',
      :exceptions => unsent_exceptions
  }
  invoke_remote(:upload, [upload_data], :encoder=> json)
end

#external_error_data(traces) ⇒ Object



167
168
169
170
171
172
173
# File 'lib/ting_yun/ting_yun_service/upload_service.rb', line 167

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

#force_restartObject



62
63
64
65
66
67
68
# File 'lib/ting_yun/ting_yun_service.rb', line 62

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

#get_redirect_hostObject



57
58
59
60
# File 'lib/ting_yun/ting_yun_service.rb', line 57

def get_redirect_host
  @collector=TingYun::Support.collector
  invoke_remote(:getRedirectHost)
end

#handle_serialization_error(method, e) ⇒ Object



120
121
122
123
124
125
# File 'lib/ting_yun/ting_yun_service.rb', line 120

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



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
113
114
# File 'lib/ting_yun/ting_yun_service.rb', line 78

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
  uri = remote_method_uri(method)
  full_uri = "#{@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_request(:data      => data,
                          :uri       => uri,
                          :encoding  => encoding,
                          :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))
ensure
  # take the initiative to GC
  payload = nil
  data = nil
end

#sql_trace(sql_trace) ⇒ Object



157
158
159
160
161
162
163
164
165
# 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