Sha256: 7972d3cf712c780d256968b33db219f1be5f222c888c672c0d83a8da2c61e2dc

Contents?: true

Size: 1.31 KB

Versions: 3

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module InstrumentAllTheThings
  module Instrumentors
    DEFAULT_GC_STATS_OPTIONS = {
      diffed_stats: %i[
        total_allocated_pages
        total_allocated_objects
        count
      ].freeze
    }.freeze

    # This is to make it easier to spec since other
    # gems may call this
    GC_STAT_GETTER = -> { GC.stat }

    GC_STATS_WRAPPER = lambda do |opts, context|
      opts = if opts == true
               DEFAULT_GC_STATS_OPTIONS
             else
               DEFAULT_GC_STATS_OPTIONS.merge(opts)
            end

      report_value = proc do |klass, stat_name, value|
        IATT.stat_reporter.histogram(
          context.stats_name(klass) + ".#{stat_name}_change",
          value
        )
      end

      lambda do |klass, next_blk, actual_code|
        starting_values = GC_STAT_GETTER.call.slice(*opts[:diffed_stats])
        next_blk.call(klass, actual_code).tap do
          new_values = GC_STAT_GETTER.call.slice(*opts[:diffed_stats])

          diff = new_values.merge(starting_values) do |_, new_value, starting_value|
            new_value - starting_value
          end

          if (span = IATT.tracer.active_span)
            span.set_tag('gc_stats', diff)
          end

          diff.each { |s, v| report_value.call(klass, s, v) }
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
instrument_all_the_things-1.0.1 lib/instrument_all_the_things/instrumentors/gc_stats.rb
instrument_all_the_things-0.9.1.alpha lib/instrument_all_the_things/instrumentors/gc_stats.rb
instrument_all_the_things-0.9.0.alpha lib/instrument_all_the_things/instrumentors/gc_stats.rb