Class: OpenObserve::Alerts

Inherits:
Resource show all
Includes:
DirectorySource
Defined in:
lib/openobserve/alerts.rb

Overview

Alert definitions.

OpenObserve splits alerting across two API versions: alert CRUD lives on v2 (Api::Api::V2) while templates and destinations stayed on v1 (Api::Api::Alerts). A caller of the raw transport has to know which of the two classes holds what. Here the split is invisible: Alerts, Templates and Destinations are one domain each, whatever version serves them.

The folder query parameter trails on nearly every v2 operation; it is taken from the client, so callers set it once (or never, letting the server pick).

Every request goes through Resource#raw. Reads must, because Api::Api::V2#alerts and #alerts_get declare response types this domain cannot use. Writes do too, because the generated CreateAlertRequest/UpdateAlertRequest accept only the attributes the checked-in spec declares and raise ArgumentError on any other — so a field a newer OpenObserve understands would be rejected by the client rather than by the server.

Constant Summary collapse

DEFAULT_PATTERN =

Glob applied inside a #sync directory.

'*.json'

Constants inherited from Resource

Resource::AUTH

Instance Method Summary collapse

Methods included from DirectorySource

#read_definitions

Methods inherited from Resource

#initialize

Constructor Details

This class inherits a constructor from OpenObserve::Resource

Instance Method Details

#create(**attributes) ⇒ OpenObserve::Alert

Parameters:

  • attributes (Hash)

    the alert definition, as plain keys

Returns:



54
55
56
57
58
# File 'lib/openobserve/alerts.rb', line 54

def create(**attributes)
  Alert.from(
    call { raw(:POST, v2_path, query: { 'folder' => folder }, body: attributes) }
  )
end

#delete(id) ⇒ void

This method returns an undefined value.

Parameters:

  • id (String)

    alert id



71
72
73
74
# File 'lib/openobserve/alerts.rb', line 71

def delete(id)
  call { raw(:DELETE, "#{v2_path}/#{encode(id)}", query: { 'folder' => folder }) }
  nil
end

#disable(id) ⇒ void

This method returns an undefined value.

Parameters:

  • id (String)

    alert id



84
85
86
# File 'lib/openobserve/alerts.rb', line 84

def disable(id)
  toggle(id, false)
end

#enable(id) ⇒ void

This method returns an undefined value.

Parameters:

  • id (String)

    alert id



78
79
80
# File 'lib/openobserve/alerts.rb', line 78

def enable(id)
  toggle(id, true)
end

#get(id) ⇒ OpenObserve::Alert

Parameters:

  • id (String)

    alert id

Returns:

Raises:



46
47
48
49
50
# File 'lib/openobserve/alerts.rb', line 46

def get(id)
  Alert.from(
    one('alert') { raw(:GET, "#{v2_path}/#{encode(id)}", query: { 'folder' => folder }) }
  )
end

#list(stream: nil, stream_type: nil, name_contains: nil, owner: nil, enabled: nil, page_size: nil, page_idx: nil, alert_type: nil) ⇒ Array<OpenObserve::Alert>

Parameters:

  • stream (String, nil) (defaults to: nil)

    restrict to one stream

  • stream_type (String, nil) (defaults to: nil)

    "logs", "metrics" or "traces"

  • name_contains (String, nil) (defaults to: nil)

    substring match on the alert name

  • enabled (Boolean, nil) (defaults to: nil)

    restrict to enabled or disabled alerts

Returns:



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/openobserve/alerts.rb', line 31

def list(stream: nil, stream_type: nil, name_contains: nil, owner: nil, enabled: nil,
         page_size: nil, page_idx: nil, alert_type: nil)
  envelope = collection do
    raw(:GET, v2_path,
        query: { 'folder' => folder, 'stream_type' => stream_type, 'stream_name' => stream,
                 'alert_name_substring' => name_contains, 'owner' => owner, 'enabled' => enabled,
                 'page_size' => page_size, 'page_idx' => page_idx, 'alert_type' => alert_type, })
  end

  Array(envelope && envelope['list']).map { |alert| Alert.from(alert) }
end

#sync(directory, pattern: DEFAULT_PATTERN) ⇒ Hash{Symbol=>Array<String>}

Bring the alerts of a directory into OpenObserve, matching on name.

Creates what is missing and overwrites what is there. Nothing is deleted: an alert someone added in the interface is not this run's to remove.

Unlike templates and destinations, the API keys alerts by id, so the name is resolved against the listing first. Two alerts answering to one name stop the run rather than be guessed between — overwriting the wrong one would be silent.

Examples:

client.alerts.sync("config/openobserve/alerts")
# => { created: ["ingestion-silence"], updated: ["application-errors"] }

Parameters:

  • directory (String)

    directory holding the documents

  • pattern (String) (defaults to: DEFAULT_PATTERN)

    glob applied inside directory

Returns:

  • (Hash{Symbol=>Array<String>})

    names, under :created and :updated

Raises:



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/openobserve/alerts.rb', line 114

def sync(directory, pattern: DEFAULT_PATTERN)
  index  = index_by_name(list)
  result = { created: [], updated: [] }

  read_definitions(directory, pattern, 'name').each do |name, definition|
    current = unique(index[name] || [], name)
    result[current ? :updated : :created] << name
    current ? update(current.id, **definition) : create(**definition)
  end

  result
end

#trigger(id) ⇒ void

This method returns an undefined value.

Fire the alert now, without waiting for its schedule.

Parameters:

  • id (String)

    alert id



91
92
93
94
# File 'lib/openobserve/alerts.rb', line 91

def trigger(id)
  call { raw(:PATCH, "#{v2_path}/#{encode(id)}/trigger", query: { 'folder' => folder }) }
  nil
end

#update(id, **attributes) ⇒ OpenObserve::Alert

Parameters:

  • id (String)

    alert id

  • attributes (Hash)

    fields to change, as plain keys

Returns:



63
64
65
66
67
# File 'lib/openobserve/alerts.rb', line 63

def update(id, **attributes)
  Alert.from(
    call { raw(:PUT, "#{v2_path}/#{encode(id)}", query: { 'folder' => folder }, body: attributes) }
  )
end