Module: GrafanaAnnotations::Wrap

Included in:
GrafanaAnnotations
Defined in:
lib/grafana_annotations/wrap.rb

Instance Method Summary collapse

Instance Method Details

#wrap(api_client: nil, text:, tags:, ok_tag: :ok, error_tag: :error) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/grafana_annotations/wrap.rb', line 5

def wrap(api_client: nil, text:, tags:, ok_tag: :ok, error_tag: :error) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  return yield if GrafanaAnnotations.config.nil? && api_client.nil?

  api_client ||= GrafanaAnnotations.default_api_client
  res = api_client.create(time: Utils::Time.now_ms, tags: tags, text: text)
  maybe_log_result(res, 'sending grafana annotation')

  begin
    block_result = yield
    maybe_patch_annotation(api_client, res,
      text: "#{text} ok",
      tags: tags.push(ok_tag))

    block_result
  rescue StandardError => e
    maybe_patch_annotation(api_client, res,
      text: "#{text} #{e.class.name} #{e.message}",
      tags: tags.push(error_tag))

    raise
  end
end