Sha256: 86b3ef01e71af36e9c547421fe6543bb15d271cbf6e58c09b6b811cfdf2bb004

Contents?: true

Size: 1.46 KB

Versions: 8

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module Sentry
  class SessionFlusher < ThreadedPeriodicWorker
    FLUSH_INTERVAL = 60

    def initialize(configuration, client)
      super(configuration.logger, FLUSH_INTERVAL)
      @client = client
      @pending_aggregates = {}
      @release = configuration.release
      @environment = configuration.environment

      log_debug("[Sessions] Sessions won't be captured without a valid release") unless @release
    end

    def flush
      return if @pending_aggregates.empty?

      @client.capture_envelope(pending_envelope)
      @pending_aggregates = {}
    end

    alias_method :run, :flush

    def add_session(session)
      return unless @release

      return unless ensure_thread

      return unless Session::AGGREGATE_STATUSES.include?(session.status)
      @pending_aggregates[session.aggregation_key] ||= init_aggregates(session.aggregation_key)
      @pending_aggregates[session.aggregation_key][session.status] += 1
    end

    private

    def init_aggregates(aggregation_key)
      aggregates = { started: aggregation_key.iso8601 }
      Session::AGGREGATE_STATUSES.each { |k| aggregates[k] = 0 }
      aggregates
    end

    def pending_envelope
      envelope = Envelope.new

      header = { type: 'sessions' }
      payload = { attrs: attrs, aggregates: @pending_aggregates.values }

      envelope.add_item(header, payload)
      envelope
    end

    def attrs
      { release: @release, environment: @environment }
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
sentry-ruby-core-5.19.0 lib/sentry/session_flusher.rb
sentry-ruby-5.19.0 lib/sentry/session_flusher.rb
sentry-ruby-core-5.18.2 lib/sentry/session_flusher.rb
sentry-ruby-5.18.2 lib/sentry/session_flusher.rb
sentry-ruby-core-5.18.1 lib/sentry/session_flusher.rb
sentry-ruby-5.18.1 lib/sentry/session_flusher.rb
sentry-ruby-5.18.0 lib/sentry/session_flusher.rb
sentry-ruby-core-5.18.0 lib/sentry/session_flusher.rb