Class: Appsignal::CheckIn::Event Private

Inherits:
Object
  • Object
show all
Defined in:
lib/appsignal/check_in/event.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Class Method Summary collapse

Class Method Details

.cron(identifier:, digest:, kind:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



18
19
20
21
22
23
24
25
# File 'lib/appsignal/check_in/event.rb', line 18

def cron(identifier:, digest:, kind:)
  new(
    :check_in_type => "cron",
    :identifier => identifier,
    :digest => digest,
    :kind => kind
  )
end

.deduplicate_cron!(events) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
120
121
122
123
# File 'lib/appsignal/check_in/event.rb', line 70

def deduplicate_cron!(events) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity
  # Remove redundant cron check-in events from the given list of events.
  # This is done by removing redundant *pairs* of events -- that is,
  # for each identifier, only send one complete pair of start and
  # finish events. Remove all other complete pairs of start and finish
  # events for that identifier, but keep any other start or finish events
  # that don't have a matching pair.
  #
  # Note that this method assumes that the events in this list have already
  # been rejected based on `Event.redundant?`, so we don't check to remove
  # check-in events that are functionally identical.
  start_digests = Hash.new { |h, k| h[k] = Set.new }
  finish_digests = Hash.new { |h, k| h[k] = Set.new }
  complete_digests = Hash.new { |h, k| h[k] = Set.new }
  keep_digest = {}

  # Compute a list of complete digests for each identifier, that is, digests
  # for which both a start and finish cron check-in event exist. Store the
  # last seen digest for each identifier as the one to keep.
  events.each do |event|
    if event[:check_in_type] == "cron"
      if event[:kind] == "start"
        start_digests[event[:identifier]] << event[:digest]
        if finish_digests[event[:identifier]].include?(event[:digest])
          complete_digests[event[:identifier]] << event[:digest]
          keep_digest[event[:identifier]] = event[:digest]
        end
      elsif event[:kind] == "finish"
        finish_digests[event[:identifier]] << event[:digest]
        if start_digests[event[:identifier]].include?(event[:digest])
          complete_digests[event[:identifier]] << event[:digest]
          keep_digest[event[:identifier]] = event[:digest]
        end
      end
    end
  end

  start_digests = nil
  finish_digests = nil

  events.reject! do |event|
    # Do not remove events that are not cron check-in events or that
    # have an unknown kind.
    return false unless
      event[:check_in_type] == "cron" && (
        event[:kind] == "start" ||
        event[:kind] == "finish")

    # Remove any event that is part of a complete digest pair, except
    # for the one digest that should be kept.
    keep_digest[event[:identifier]] != event[:digest] &&
      complete_digests[event[:identifier]].include?(event[:digest])
  end
end

.describe(events) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/appsignal/check_in/event.rb', line 51

def describe(events)
  if events.empty?
    # This shouldn't happen.
    "no check-in events"
  elsif events.length > 1
    "#{events.length} check-in events"
  else
    event = events.first
    if event[:check_in_type] == "cron"
      "cron check-in `#{event[:identifier] || "unknown"}` " \
        "#{event[:kind] || "unknown"} event (digest #{event[:digest] || "unknown"})"
    elsif event[:check_in_type] == "heartbeat"
      "heartbeat check-in `#{event[:identifier] || "unknown"}` event"
    else
      "unknown check-in event"
    end
  end
end

.heartbeat(identifier:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



27
28
29
30
31
32
# File 'lib/appsignal/check_in/event.rb', line 27

def heartbeat(identifier:)
  new(
    :check_in_type => "heartbeat",
    :identifier => identifier
  )
end

.new(check_in_type:, identifier:, digest: nil, kind: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



8
9
10
11
12
13
14
15
16
# File 'lib/appsignal/check_in/event.rb', line 8

def new(check_in_type:, identifier:, digest: nil, kind: nil)
  {
    :identifier => identifier,
    :digest => digest,
    :kind => kind,
    :timestamp => Time.now.utc.to_i,
    :check_in_type => check_in_type
  }.compact
end

.redundant?(event, other) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/appsignal/check_in/event.rb', line 34

def redundant?(event, other)
  return false if
    other[:check_in_type] != event[:check_in_type] ||
      other[:identifier] != event[:identifier]

  return false if event[:check_in_type] == "cron" && (
    other[:digest] != event[:digest] ||
    other[:kind] != event[:kind]
  )

  return false if
    event[:check_in_type] != "cron" &&
      event[:check_in_type] != "heartbeat"

  true
end