Class: OpenObserve::Destinations

Inherits:
Resource
  • Object
show all
Includes:
DirectorySource
Defined in:
lib/openobserve/destinations.rb

Overview

Alert destinations — where a firing alert is sent (webhook, e-mail, SNS).

Served by the v1 alerting API even though alert CRUD moved to v2; see Alerts for that split.

Every request goes through Resource#raw. That retires the generated ListDestinations200ResponseInner, which the generator pressed into service as both the response type of the listing and the write parameter of every mutation — accepting only the thirteen attributes the checked-in spec declares, and raising ArgumentError on any other.

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::Destination

Parameters:

  • attributes (Hash)

    destination definition, as plain keys

Returns:



39
40
41
# File 'lib/openobserve/destinations.rb', line 39

def create(**attributes)
  Destination.from(call { raw(:POST, destinations_path, body: attributes) })
end

#delete(name) ⇒ void

This method returns an undefined value.

Parameters:

  • name (String)

    destination name



52
53
54
55
# File 'lib/openobserve/destinations.rb', line 52

def delete(name)
  call { raw(:DELETE, "#{destinations_path}/#{encode(name)}") }
  nil
end

#get(name) ⇒ OpenObserve::Destination

Parameters:

  • name (String)

    destination name

Returns:

Raises:



31
32
33
34
35
# File 'lib/openobserve/destinations.rb', line 31

def get(name)
  Destination.from(
    one('destination') { raw(:GET, "#{destinations_path}/#{encode(name)}") }
  )
end

#list(module_name: nil) ⇒ Array<OpenObserve::Destination>

Parameters:

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

    restrict to a module ("alert" or "pipeline")

Returns:



22
23
24
25
26
# File 'lib/openobserve/destinations.rb', line 22

def list(module_name: nil)
  payload = collection { raw(:GET, destinations_path, query: { 'module' => module_name }) }

  Array(payload).map { |destination| Destination.from(destination) }
end

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

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

Creates what is missing and overwrites what is there; nothing is deleted, since an alert this run does not know about may still notify through it.

Examples:

client.destinations.sync("config/openobserve/destinations")
# => { created: [], updated: ["pagerduty"] }

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:



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/openobserve/destinations.rb', line 70

def sync(directory, pattern: DEFAULT_PATTERN)
  known  = list.filter_map { |destination| destination['name'] }.to_set
  result = { created: [], updated: [] }

  read_definitions(directory, pattern, 'name').each do |name, definition|
    exists = known.include?(name)
    result[exists ? :updated : :created] << name
    exists ? update(name, **definition) : create(**definition)
  end

  result
end

#update(name, **attributes) ⇒ OpenObserve::Destination

Parameters:

  • name (String)

    destination name

  • attributes (Hash)

    fields to change, as plain keys

Returns:



46
47
48
# File 'lib/openobserve/destinations.rb', line 46

def update(name, **attributes)
  Destination.from(call { raw(:PUT, "#{destinations_path}/#{encode(name)}", body: attributes) })
end