Sha256: 5b02e8f199099e5e8f655e6381f3529a5349a9b18d017b8072e34cacd66c444a

Contents?: true

Size: 2 KB

Versions: 2

Compression:

Stored size: 2 KB

Contents

module Test
  class Span < OpenTracing::Span
    class SpanAlreadyFinished < StandardError; end

    class LogEntry < Struct.new(:event, :timestamp, :fields); end

    include TypeCheck

    attr_reader :tracer, :operation_name, :start_time, :end_time, :tags, :logs

    def initialize(tracer:, context:, operation_name:, start_time: Time.now, tags: nil)
      Type! tracer, ::Test::Tracer
      Type! context, ::Test::SpanContext
      Type! operation_name, String
      Type! start_time, Time
      Type! tags, Hash, NilClass

      @tracer = tracer
      @context = context
      @operation_name = operation_name
      @tags = tags || {}
      @logs = []
      @start_time = start_time
      @end_time = nil
      @in_progress = true
    end

    def in_progress?
      @in_progress
    end

    def context
      @context
    end

    def operation_name=(name)
      Type! name, String
      ensure_in_progress!

      @operation_name = name
    end

    def set_tag(key, value)
      Type! key, String
      ensure_in_progress!

      @tags[key] = Type?(value, String, Fixnum, Bignum, Float, TrueClass, FalseClass) ? value : value.to_s
      self
    end

    def set_baggage_item(key, value)
      Type! key, String
      Type! value, String, NilClass
      ensure_in_progress!

      @context.baggage[key] = value.to_s
      self
    end

    def get_baggage_item(key)
      Type! key, String
      @context.baggage[key]
    end

    def log(event: nil, timestamp: Time.now, **fields)
      Type! event, String, NilClass
      Type! timestamp, Time
      ensure_in_progress!

      @logs << LogEntry.new(event, timestamp, fields)
    end

    def finish(end_time: Time.now)
      Type! end_time, Time
      ensure_in_progress!

      @end_time = end_time
      @in_progress = false
      @tracer.finished_spans << self
      self
    end

  private

    def ensure_in_progress!
      unless in_progress?
        raise SpanAlreadyFinished.new("No modification operations allowed. The span is already finished.")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
test-tracer-1.1.0 lib/test/span.rb
test-tracer-1.0.0 lib/test/span.rb