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, #send_requestV3, #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.



40
41
42
43
44
45
46
47
48
# 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

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

#attemptsObject

Returns the value of attribute attempts.



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

def attempts
  @attempts
end

#configObject

Returns the value of attribute config.



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

def config
  @config
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

#idSecretObject

Returns the value of attribute idSecret.



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

def idSecret
  @idSecret
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



149
150
151
152
153
154
# 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

#action_traceV3(traces) ⇒ Object



189
190
191
192
193
194
195
# 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

#audit_mode?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/ting_yun/ting_yun_service.rb', line 133

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

#connect(settings = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# 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) ⇒ Object



132
133
134
135
136
137
138
# 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) ⇒ Object



140
141
142
143
144
145
146
# 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) ⇒ Object



180
181
182
183
184
185
186
# 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_restartObject



87
88
89
90
91
92
93
# 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

#get_redirect_hostObject



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

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

#get_redirect_hostV3Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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

#getCmdObject



167
168
169
170
171
172
173
174
175
176
177
178
# 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

#handle_serialization_error(method, e) ⇒ Object



137
138
139
140
141
142
# 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 = {}) ⇒ Object

private



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# 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) ⇒ 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