Class: LaunchDarkly::Impl::Migrations::OpTracker

Inherits:
Object
  • Object
show all
Includes:
LaunchDarkly::Interfaces::Migrations::OpTracker
Defined in:
lib/ldclient-rb/impl/migrations/tracker.rb

Overview

Since:

  • 5.5.0

Instance Method Summary collapse

Constructor Details

#initialize(logger, key, flag, context, detail, default_stage) ⇒ OpTracker

Returns a new instance of OpTracker.

Parameters:

Since:

  • 5.5.0



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ldclient-rb/impl/migrations/tracker.rb', line 19

def initialize(logger, key, flag, context, detail, default_stage)
  @logger = logger
  @key = key
  @flag = flag
  @context = context
  @detail = detail
  @default_stage = default_stage
  @sampler = LaunchDarkly::Impl::Sampler.new(Random.new)

  @mutex = Mutex.new

  # @type [Symbol, nil]
  @operation = nil

  # @type [Set<Symbol>]
  @invoked = Set.new
  # @type [Boolean, nil]
  @consistent = nil

  # @type [Int]
  @consistent_ratio = @flag&.migration_settings&.check_ratio
  @consistent_ratio = 1 if @consistent_ratio.nil?

  # @type [Set<Symbol>]
  @errors = Set.new
  # @type [Hash<Symbol, Float>]
  @latencies = Hash.new
end

Instance Method Details

#buildObject

Since:

  • 5.5.0



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ldclient-rb/impl/migrations/tracker.rb', line 94

def build
  @mutex.synchronize do
    return "operation cannot contain an empty key" if @key.empty?
    return "operation not provided" if @operation.nil?
    return "no origins were invoked" if @invoked.empty?
    return "provided context was invalid" unless @context.valid?

    result = check_invoked_consistency
    return result unless result == true

    LaunchDarkly::Impl::MigrationOpEvent.new(
      LaunchDarkly::Impl::Util.current_time_millis,
      @context,
      @key,
      @flag,
      @operation,
      @default_stage,
      @detail,
      @invoked,
      @consistent,
      @consistent_ratio,
      @errors,
      @latencies
    )
  end
end

#consistent(is_consistent) ⇒ Object

Since:

  • 5.5.0



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ldclient-rb/impl/migrations/tracker.rb', line 64

def consistent(is_consistent)
  @mutex.synchronize do
    if @sampler.sample(@consistent_ratio)
      begin
        @consistent = is_consistent.call
      rescue => e
        LaunchDarkly::Util.log_exception(@logger, "Exception raised during consistency check; failed to record measurement", e)
      end
    end
  end
end

#error(origin) ⇒ Object

Since:

  • 5.5.0



76
77
78
79
80
81
82
# File 'lib/ldclient-rb/impl/migrations/tracker.rb', line 76

def error(origin)
  return unless LaunchDarkly::Migrations::VALID_ORIGINS.include? origin

  @mutex.synchronize do
    @errors.add(origin)
  end
end

#invoked(origin) ⇒ Object

Since:

  • 5.5.0



56
57
58
59
60
61
62
# File 'lib/ldclient-rb/impl/migrations/tracker.rb', line 56

def invoked(origin)
  return unless LaunchDarkly::Migrations::VALID_ORIGINS.include? origin

  @mutex.synchronize do
    @invoked.add(origin)
  end
end

#latency(origin, duration) ⇒ Object

Since:

  • 5.5.0



84
85
86
87
88
89
90
91
92
# File 'lib/ldclient-rb/impl/migrations/tracker.rb', line 84

def latency(origin, duration)
  return unless LaunchDarkly::Migrations::VALID_ORIGINS.include? origin
  return unless duration.is_a? Numeric
  return if duration < 0

  @mutex.synchronize do
    @latencies[origin] = duration
  end
end

#operation(operation) ⇒ Object

Since:

  • 5.5.0



48
49
50
51
52
53
54
# File 'lib/ldclient-rb/impl/migrations/tracker.rb', line 48

def operation(operation)
  return unless LaunchDarkly::Migrations::VALID_OPERATIONS.include? operation

  @mutex.synchronize do
    @operation = operation
  end
end