Module: TingYun::Agent::InstanceMethods::ContainerDataManager

Included in:
TingYun::Agent::InstanceMethods
Defined in:
lib/ting_yun/agent/instance_methods/container_data_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#error_collectorObject (readonly)

Returns the value of attribute error_collector.



17
18
19
# File 'lib/ting_yun/agent/instance_methods/container_data_manager.rb', line 17

def error_collector
  @error_collector
end

#sql_samplerObject (readonly)

Returns the value of attribute sql_sampler.



17
18
19
# File 'lib/ting_yun/agent/instance_methods/container_data_manager.rb', line 17

def sql_sampler
  @sql_sampler
end

#stats_engineObject (readonly)

Returns the value of attribute stats_engine.



17
18
19
# File 'lib/ting_yun/agent/instance_methods/container_data_manager.rb', line 17

def stats_engine
  @stats_engine
end

#transaction_samplerObject (readonly)

Returns the value of attribute transaction_sampler.



17
18
19
# File 'lib/ting_yun/agent/instance_methods/container_data_manager.rb', line 17

def transaction_sampler
  @transaction_sampler
end

Instance Method Details

#container_for_endpoint(endpoint) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/ting_yun/agent/instance_methods/container_data_manager.rb', line 39

def container_for_endpoint(endpoint)
  case endpoint
    when :metric_data then
      @stats_engine
    # type code here
  end
end

#drop_buffered_dataObject



20
21
22
23
24
25
# File 'lib/ting_yun/agent/instance_methods/container_data_manager.rb', line 20

def drop_buffered_data
  @stats_engine.reset!
  @transaction_sampler.reset!
  @sql_sampler.reset!
  @error_collector.reset!
end

#harvest_and_send_errorsObject



65
66
67
# File 'lib/ting_yun/agent/instance_methods/container_data_manager.rb', line 65

def harvest_and_send_errors
  harvest_and_send_from_container(@error_collector.error_trace_array, :error_data)
end

#harvest_and_send_exceptionsObject



69
70
71
# File 'lib/ting_yun/agent/instance_methods/container_data_manager.rb', line 69

def harvest_and_send_exceptions
  harvest_and_send_from_container(@error_collector.exception_error_array, :exception_data)
end

#harvest_and_send_external_errorsObject



74
75
76
# File 'lib/ting_yun/agent/instance_methods/container_data_manager.rb', line 74

def harvest_and_send_external_errors
  harvest_and_send_from_container(@error_collector.external_error_array, :external_error_data)
end

#harvest_and_send_from_container(container, endpoint) ⇒ Object

Harvests data from the given container, sends it to the named endpoint on the service, and automatically merges back in upon a recoverable failure.

The given container should respond to:

#harvest!
  returns an enumerable collection of data items to be sent to the
  collector.

#reset!
  drop any stored data and reset to a clean state.

#merge!(items)
  merge the given items back into the internal buffer of the
  container, so that they may be harvested again later.


106
107
108
109
# File 'lib/ting_yun/agent/instance_methods/container_data_manager.rb', line 106

def harvest_and_send_from_container(container, endpoint)
  items = harvest_from_container(container, endpoint)
  send_data_to_endpoint(endpoint, items, container)
end

#harvest_and_send_slowest_sqlObject



84
85
86
87
# File 'lib/ting_yun/agent/instance_methods/container_data_manager.rb', line 84

def harvest_and_send_slowest_sql
  harvest_and_send_from_container(@sql_sampler, :sql_trace)

end

#harvest_and_send_timeslice_dataObject



61
62
63
# File 'lib/ting_yun/agent/instance_methods/container_data_manager.rb', line 61

def harvest_and_send_timeslice_data
  harvest_and_send_from_container(@stats_engine, :metric_data)
end

#harvest_and_send_transaction_tracesObject



79
80
81
82
# File 'lib/ting_yun/agent/instance_methods/container_data_manager.rb', line 79

def harvest_and_send_transaction_traces
  harvest_and_send_from_container(@transaction_sampler, :action_trace_data)

end

#harvest_from_container(container, endpoint) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ting_yun/agent/instance_methods/container_data_manager.rb', line 111

def harvest_from_container(container, endpoint)
  items =[]
  begin
     if TingYun::Agent.config[:'enabled']
       items = container.harvest!
     else
       container.reset!
     end
  rescue => e
    TingYun::Agent.logger.error("Failed to harvest #{endpoint} data, resetting. Error: ", e)
    container.reset!
  end
  items
end

#init_containersObject



32
33
34
35
36
37
# File 'lib/ting_yun/agent/instance_methods/container_data_manager.rb', line 32

def init_containers
  @stats_engine = TingYun::Agent::Collector::StatsEngine.new
  @error_collector = TingYun::Agent::Collector::ErrorCollector.new
  @transaction_sampler = TingYun::Agent::Collector::TransactionSampler.new
  @sql_sampler = TingYun::Agent::Collector::SqlSampler.new
end

#reset_objects_with_locksObject



27
28
29
# File 'lib/ting_yun/agent/instance_methods/container_data_manager.rb', line 27

def reset_objects_with_locks
  init_containers
end

#send_data_to_endpoint(endpoint, items, container) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ting_yun/agent/instance_methods/container_data_manager.rb', line 126

def send_data_to_endpoint(endpoint, items, container)
  TingYun::Agent.logger.info("Sending #{items.size} items to #{endpoint}")
  begin
    if container.respond_to?(:harvest_base_quantile_hash!)
      @service.send(endpoint, items, container.harvest_base_quantile_hash!)
    else
      @service.send(endpoint, items)
    end
  rescue => e
    TingYun::Agent.logger.info("Unable to send #{endpoint} data, will try again later. Error: ", e)
    # container.merge!(items)
    raise
  ensure
    items = nil #  to GC
  end


end

#transmit_dataObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ting_yun/agent/instance_methods/container_data_manager.rb', line 47

def transmit_data
  ::TingYun::Agent.logger.debug('Sending data to Ting Yun Service')

  @events.notify(:middleware_harvest)
  @service.session do # use http keep-alive
    harvest_and_send_errors
    harvest_and_send_external_errors
    harvest_and_send_exceptions
    harvest_and_send_timeslice_data
    harvest_and_send_transaction_traces
    harvest_and_send_slowest_sql
  end
end