Sha256: 966be0b4bc3ead59d313cf312490c5b649cb845d48e591a84c489b5f04496bf3

Contents?: true

Size: 1.25 KB

Versions: 6

Compression:

Stored size: 1.25 KB

Contents

module Fiveruns::Dash
  
  class Trace
    
    attr_reader :context, :data, :stack
    def initialize(context)
      @context = context
      @stack = []
      validate!
    end
    
    def step(&block)
      s = Step.new
      @stack.last.children << s if !@stack.empty?
      @stack << s
      result = yield
      last_step = @stack.pop
      @data = last_step if @stack.empty?
      result
    end
        
    def add_data(metric, contexts, value)
      unless @stack.empty?
        @stack.last.metrics.push(
          metric.key.merge({:value => value, :contexts => contexts})
        )
      end
    end
        
    def to_fjson
      { :context => context,
        :data => (@data || {})
      }.to_fjson
    end
    
    private
    
    def validate!
      unless @context.is_a?(Array) && @context.size % 2 == 0
        raise ArgumentError, 'Invalid context: #{@context.inspect} (must be an array with an even number of elements)'
      end
    end
    
    class Step
                  
      def metrics
        @metrics ||= []
      end
      
      def children
        @children ||= []
      end
            
      def to_fjson
        {
          :metrics => metrics,
          :children => children,
        }.to_fjson
      end
      
    end
    
  end
  
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
fiveruns-dash-ruby-0.8.10 lib/fiveruns/dash/trace.rb
fiveruns-dash-ruby-0.8.4 lib/fiveruns/dash/trace.rb
fiveruns-dash-ruby-0.8.5 lib/fiveruns/dash/trace.rb
fiveruns-dash-ruby-0.8.6 lib/fiveruns/dash/trace.rb
fiveruns-dash-ruby-0.8.8 lib/fiveruns/dash/trace.rb
fiveruns-dash-ruby-0.8.9 lib/fiveruns/dash/trace.rb