Sha256: 3283ede842eeefff7bb0622ce4a46793bd9fcb15b64aa95acebbb0fee399e84e

Contents?: true

Size: 1.42 KB

Versions: 9

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module RSpec::Buildkite::Analytics
  class Tracer
    class Span
      attr_accessor :section, :start_at, :end_at, :detail, :children

      def initialize(section, start_at, end_at, detail)
        @section = section
        @start_at = start_at
        @end_at = end_at
        @detail = detail
        @children = []
      end

      def as_json
        {
          section: section,
          start_at: start_at,
          end_at: end_at,
          duration: end_at - start_at,
          detail: detail,
          children: children.map(&:as_json),
        }
      end
    end

    def initialize
      @top = Span.new(:top, Concurrent.monotonic_time, nil, {})
      @stack = [@top]
    end

    def enter(section, **detail)
      new_entry = Span.new(section, Concurrent.monotonic_time, nil, detail)
      current_span.children << new_entry
      @stack << new_entry
    end

    def leave
      current_span.end_at = Concurrent.monotonic_time
      @stack.pop
    end

    def backfill(section, duration, **detail)
      new_entry = Span.new(section, Concurrent.monotonic_time - duration, Concurrent.monotonic_time, detail)
      current_span.children << new_entry
    end

    def current_span
      @stack.last
    end

    def finalize
      raise "Stack not empty" unless @stack.size == 1
      @top.end_at = Concurrent.monotonic_time
      self
    end

    def history
      @top.as_json
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rspec-buildkite-analytics-0.4.0 lib/rspec/buildkite/analytics/tracer.rb
rspec-buildkite-analytics-0.3.6 lib/rspec/buildkite/analytics/tracer.rb
rspec-buildkite-analytics-0.3.5 lib/rspec/buildkite/analytics/tracer.rb
rspec-buildkite-analytics-0.3.4 lib/rspec/buildkite/analytics/tracer.rb
rspec-buildkite-analytics-0.3.3 lib/rspec/buildkite/analytics/tracer.rb
rspec-buildkite-analytics-0.3.2 lib/rspec/buildkite/analytics/tracer.rb
rspec-buildkite-analytics-0.3.1 lib/rspec/buildkite/analytics/tracer.rb
rspec-buildkite-analytics-0.3.0 lib/rspec/buildkite/analytics/tracer.rb
rspec-buildkite-analytics-0.2.0 lib/rspec/buildkite/analytics/tracer.rb