File: instrumentation.rb

package info (click to toggle)
ruby-gh 0.21.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,644 kB
  • sloc: ruby: 1,793; makefile: 4
file content (39 lines) | stat: -rw-r--r-- 989 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# frozen_string_literal: true

require 'gh'

module GH
  # Public: This class caches responses.
  class Instrumentation < Wrapper
    # Public: Get/set instrumenter to use. Compatible with ActiveSupport::Notification and Travis::EventLogger.
    attr_accessor :instrumenter

    def setup(backend, options)
      self.instrumenter ||= Travis::EventLogger.method(:notify) if defined? Travis::EventLogger
      self.instrumenter ||= ActiveSupport::Notifications.method(:instrument) if defined? ActiveSupport::Notifications
      super
    end

    def http(verb, url, *)
      instrument(:http, verb:, url:) { super }
    end

    def load(data)
      instrument(:load, data:) { super }
    end

    def [](key)
      instrument(:access, key:) { super }
    end

    private

    def instrument(type, payload = {})
      return yield unless instrumenter

      result = nil
      instrumenter.call("#{type}.gh", payload.merge(gh: frontend)) { result = yield }
      result
    end
  end
end