Class: OpenObserve::Templates
- Includes:
- DirectorySource
- Defined in:
- lib/openobserve/templates.rb
Overview
Alert templates — how a firing alert is formatted before it reaches a destination.
Served by the v1 alerting API, like Destinations.
Every request goes through Resource#raw. That also retires the generated
ListTemplates200ResponseInner — a response schema the generator pressed into service
as a write parameter, which accepted only the six attributes the checked-in spec
declares and raised ArgumentError on any other.
Constant Summary collapse
- DEFAULT_PATTERN =
Glob applied inside a #sync directory.
'*.json'- CAMEL_KEYS =
The model did one thing worth keeping: OpenObserve spells these two flags in camelCase while every other template field is snake_case. Callers keep writing
is_default:, and the translation happens here rather than in a schema. { is_default: 'isDefault', is_prebuilt: 'isPrebuilt' }.freeze
Constants inherited from Resource
Instance Method Summary collapse
- #create(**attributes) ⇒ OpenObserve::Template
- #delete(name) ⇒ void
- #get(name) ⇒ OpenObserve::Template
- #list ⇒ Array<OpenObserve::Template>
-
#prebuilt ⇒ Array<OpenObserve::Template>
The templates OpenObserve ships with (Slack, PagerDuty, …), usable as a starting point.
-
#sync(directory, pattern: DEFAULT_PATTERN) ⇒ Hash{Symbol=>Array<String>}
Bring the templates of a directory into OpenObserve, matching on
name. - #update(name, **attributes) ⇒ OpenObserve::Template
Methods included from DirectorySource
Methods inherited from Resource
Constructor Details
This class inherits a constructor from OpenObserve::Resource
Instance Method Details
#create(**attributes) ⇒ OpenObserve::Template
46 47 48 |
# File 'lib/openobserve/templates.rb', line 46 def create(**attributes) Template.from(call { raw(:POST, templates_path, body: payload(attributes)) }) end |
#delete(name) ⇒ void
This method returns an undefined value.
59 60 61 62 |
# File 'lib/openobserve/templates.rb', line 59 def delete(name) call { raw(:DELETE, "#{templates_path}/#{encode(name)}") } nil end |
#get(name) ⇒ OpenObserve::Template
40 41 42 |
# File 'lib/openobserve/templates.rb', line 40 def get(name) Template.from(one('template') { raw(:GET, "#{templates_path}/#{encode(name)}") }) end |
#list ⇒ Array<OpenObserve::Template>
23 24 25 26 27 |
# File 'lib/openobserve/templates.rb', line 23 def list payload = collection { raw(:GET, templates_path) } Array(payload).map { |template| Template.from(template) } end |
#prebuilt ⇒ Array<OpenObserve::Template>
The templates OpenObserve ships with (Slack, PagerDuty, …), usable as a starting point.
31 32 33 34 35 |
# File 'lib/openobserve/templates.rb', line 31 def prebuilt payload = collection { raw(:GET, "#{templates_path}/system/prebuilt") } Array(payload).map { |template| Template.from(template) } end |
#sync(directory, pattern: DEFAULT_PATTERN) ⇒ Hash{Symbol=>Array<String>}
Bring the templates of a directory into OpenObserve, matching on name.
Creates what is missing and overwrites what is there. Nothing is ever deleted — a template the directory no longer describes stays put, because another organisation's alerts may still point at it.
Safe to replay: a second run over an unchanged directory rewrites the same bodies.
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/openobserve/templates.rb', line 80 def sync(directory, pattern: DEFAULT_PATTERN) known = list.filter_map { |template| template['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::Template
53 54 55 |
# File 'lib/openobserve/templates.rb', line 53 def update(name, **attributes) Template.from(call { raw(:PUT, "#{templates_path}/#{encode(name)}", body: payload(attributes)) }) end |