class DasProfiler::Middleware::Submitter

Internal use class to submit traces to the DasProfiler service.

Constants

SUBMISSION_INTERVAL

Attributes

next_submission_time[R]

Public Class Methods

new(options) click to toggle source

@option options [String] :access_key_id

(ENV['DAS_PROFILER_ACCESS_KEY_ID']) The Das Profiler access key for
your account

@option options [String] :secret_access_key

(ENV['DAS_PROFILER_SECRET_ACCESS_KEY']) The Das Profiler secret access
key for your account

@option options [String] :app_name (ENV)

The name of the current application. Used to segment traces

@option options [String] :version The version of the current

application. Using your git shortsha or similar is highly recommended
# File lib/das_profiler/middleware/submitter.rb, line 58
def initialize(options)
  @configuration = Configuration.new(options)
  schedule_next_submission
  at_exit { submit }
end

Public Instance Methods

submit_if_needed() click to toggle source

Attempt to submit the latest set of results from StackProf

# File lib/das_profiler/middleware/submitter.rb, line 65
def submit_if_needed
  # If StackProf is currently running we hold back on the assumption that
  # this will shortly be called again after stopping StackProf. We want to
  # avoid rescheduling in that case, to minimize latency.
  return unless submission_needed? && !StackProf.running?

  schedule_next_submission if submit
end

Private Instance Methods

data_for(results) click to toggle source
# File lib/das_profiler/middleware/submitter.rb, line 102
def data_for(results)
  JSON.generate(
    metadata: @configuration.metadata.merge(time: Time.now.to_f),
    trace: Base64.encode64(Zlib::Deflate.deflate(JSON.generate(results)))
  )
end
schedule_next_submission() click to toggle source
# File lib/das_profiler/middleware/submitter.rb, line 78
def schedule_next_submission
  @next_submission_time =
    Process.clock_gettime(Process::CLOCK_MONOTONIC) + SUBMISSION_INTERVAL
end
submission_needed?() click to toggle source
# File lib/das_profiler/middleware/submitter.rb, line 83
def submission_needed?
  Process.clock_gettime(Process::CLOCK_MONOTONIC) >= next_submission_time
end
submit() click to toggle source

Submits a trace to the DasProfiler service @return [nil,truthy] nil if results were not submitted, otherwise a

truthy value
# File lib/das_profiler/middleware/submitter.rb, line 90
def submit
  results = StackProf.results
  return unless results && !results.empty?

  @configuration.client.put_record(
    delivery_stream_name: 'dasprofiler-dev',
    record: {
      data: data_for(results)
    }
  )
end