# encoding: utf-8 require 'zlib' require 'one_apm/logger/audit_logger' require 'one_apm/support/encoders' require 'one_apm/support/marshaller' require 'one_apm/support/json_marshaller' require 'one_apm/collector/collector/server_methods' require 'one_apm/collector/collector/http_connection' require 'one_apm/collector/collector/helper' module OneApm module Collector class CollectorService include OneApm::Collector::CollectorService::Helper include OneApm::Collector::CollectorService::ServerMethods include OneApm::Collector::CollectorService::HttpConnection PROTOCOL_VERSION = 1 attr_accessor :request_timeout, :agent_id attr_reader :collector,:marshaller, :metric_id_cache def initialize(license_key = nil, collector = nil) @license_key = license_key || OneApm::Manager.config[:license_key] @collector = collector || server @request_timeout = OneApm::Manager.config[:timeout] @metric_id_cache = {} @audit_logger = ::OneApm::Logger::AuditLogger.new OneApm::Manager.config.register_callback(:'audit_log.enabled') do |enabled| @audit_logger.enabled = enabled end OneApm::Manager.config.register_callback(:ssl) do |ssl| if !ssl OneApm::Manager.logger.warn("Agent is configured not to use SSL when communicating with OneApm's servers") else OneApm::Manager.logger.debug("Agent is configured to use SSL") end end OneApm::Manager.config.register_callback(:marshaller) do |marshaller| @marshaller = OneApm::Support::JsonMarshaller.new end end def get_redirect_host invoke_remote(:get_redirect_host) end def connect(settings={}) if host = get_redirect_host @collector = server_from_host(host) end response = invoke_remote(:connect, [settings]) @agent_id = response['agent_run_id'] if response response end def shutdown(time) invoke_remote(:shutdown, [@agent_id, time.to_i]) if @agent_id end def metric_data(stats_hash) timeslice_start = stats_hash.started_at timeslice_end = stats_hash.harvested_at || Time.now metric_data_array = build_metric_data_array(stats_hash) result = invoke_remote( :metric_data, [@agent_id, timeslice_start.to_f, timeslice_end.to_f, metric_data_array], :item_count => metric_data_array.size ) fill_metric_id_cache(result) result end def error_data(unsent_errors) invoke_remote(:error_data, [@agent_id, unsent_errors], :item_count => unsent_errors.size) end def transaction_sample_data(traces) invoke_remote(:transaction_sample_data, [@agent_id, traces], :item_count => traces.size) end def sql_trace_data(sql_traces) invoke_remote(:sql_trace_data, [sql_traces], :item_count => sql_traces.size) end def profile_data(profile) invoke_remote(:profile_data, [@agent_id, profile], :skip_normalization => true) || '' end def get_agent_commands invoke_remote(:get_agent_commands, [@agent_id]) end def agent_command_results(results) invoke_remote(:agent_command_results, [@agent_id, results]) end def get_xray_metadata(xray_ids) invoke_remote(:get_xray_metadata, [@agent_id, *xray_ids]) end def analytic_event_data(data) invoke_remote(:analytic_event_data, [@agent_id, data], :item_count => data.size) end def custom_event_data(data) invoke_remote(:custom_event_data, [@agent_id, data], :item_count => data.size) end def utilization_data(data) invoke_remote(:utilization_data, data) end end end end