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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
require 'delegate'
module Flipper
module Adapters
# Internal: Adapter that wraps another adapter and instruments all adapter
# operations.
class Instrumented < SimpleDelegator
include ::Flipper::Adapter
# Private: The name of instrumentation events.
InstrumentationName = "adapter_operation.#{InstrumentationNamespace}".freeze
# Private: What is used to instrument all the things.
attr_reader :instrumenter
# Public: The name of the adapter.
attr_reader :name
# Internal: Initializes a new adapter instance.
#
# adapter - Vanilla adapter instance to wrap.
#
# options - The Hash of options.
# :instrumenter - What to use to instrument all the things.
#
def initialize(adapter, options = {})
super(adapter)
@adapter = adapter
@name = :instrumented
@instrumenter = options.fetch(:instrumenter, Instrumenters::Noop)
end
# Public
def features
default_payload = {
operation: :features,
adapter_name: @adapter.name,
}
@instrumenter.instrument(InstrumentationName, default_payload) do |payload|
payload[:result] = @adapter.features
end
end
# Public
def add(feature)
default_payload = {
operation: :add,
adapter_name: @adapter.name,
feature_name: feature.name,
}
@instrumenter.instrument(InstrumentationName, default_payload) do |payload|
payload[:result] = @adapter.add(feature)
end
end
# Public
def remove(feature)
default_payload = {
operation: :remove,
adapter_name: @adapter.name,
feature_name: feature.name,
}
@instrumenter.instrument(InstrumentationName, default_payload) do |payload|
payload[:result] = @adapter.remove(feature)
end
end
# Public
def clear(feature)
default_payload = {
operation: :clear,
adapter_name: @adapter.name,
feature_name: feature.name,
}
@instrumenter.instrument(InstrumentationName, default_payload) do |payload|
payload[:result] = @adapter.clear(feature)
end
end
# Public
def get(feature)
default_payload = {
operation: :get,
adapter_name: @adapter.name,
feature_name: feature.name,
}
@instrumenter.instrument(InstrumentationName, default_payload) do |payload|
payload[:result] = @adapter.get(feature)
end
end
def get_multi(features)
default_payload = {
operation: :get_multi,
adapter_name: @adapter.name,
feature_names: features.map(&:name),
}
@instrumenter.instrument(InstrumentationName, default_payload) do |payload|
payload[:result] = @adapter.get_multi(features)
end
end
def get_all
default_payload = {
operation: :get_all,
adapter_name: @adapter.name,
}
@instrumenter.instrument(InstrumentationName, default_payload) do |payload|
payload[:result] = @adapter.get_all
end
end
# Public
def enable(feature, gate, thing)
default_payload = {
operation: :enable,
adapter_name: @adapter.name,
feature_name: feature.name,
gate_name: gate.name,
thing_value: thing.value,
}
@instrumenter.instrument(InstrumentationName, default_payload) do |payload|
payload[:result] = @adapter.enable(feature, gate, thing)
end
end
# Public
def disable(feature, gate, thing)
default_payload = {
operation: :disable,
adapter_name: @adapter.name,
feature_name: feature.name,
gate_name: gate.name,
thing_value: thing.value,
}
@instrumenter.instrument(InstrumentationName, default_payload) do |payload|
payload[:result] = @adapter.disable(feature, gate, thing)
end
end
end
end
end
|