Sha256: cb5fb14ab48ecad3fb040baab303a4ae7d4062dc09ec056dcab8cdc8f9e90d2a

Contents?: true

Size: 1.18 KB

Versions: 6

Compression:

Stored size: 1.18 KB

Contents

require "delegate"
require "flipper/instrumenters/noop"

module Flipper
  module Cloud
    class Instrumenter < SimpleDelegator
      def initialize(options = {})
        @brow = options.fetch(:brow)
        @instrumenter = options.fetch(:instrumenter, Instrumenters::Noop)
        super @instrumenter
      end

      def instrument(name, payload = {}, &block)
        result = @instrumenter.instrument(name, payload, &block)
        push name, payload
        result
      end

      private

      def push(name, payload)
        return unless name == Flipper::Feature::InstrumentationName
        return unless :enabled? == payload[:operation]

        dimensions = {
          "feature" => payload[:feature_name].to_s,
          "result" => payload[:result].to_s,
        }

        if (thing = payload[:thing])
          dimensions["flipper_id"] = thing.value.to_s
        end

        if (actors = payload[:actors])
          dimensions["flipper_ids"] = actors.map { |actor| actor.value.to_s }
        end

        event = {
          type: "enabled",
          dimensions: dimensions,
          measures: {},
          ts: Time.now.utc,
        }
        @brow.push event
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
flipper-1.0.0 lib/flipper/cloud/instrumenter.rb
flipper-1.0.0.pre lib/flipper/cloud/instrumenter.rb
flipper-cloud-0.28.3 lib/flipper/cloud/instrumenter.rb
flipper-cloud-0.28.2 lib/flipper/cloud/instrumenter.rb
flipper-cloud-0.28.1 lib/flipper/cloud/instrumenter.rb
flipper-cloud-0.28.0 lib/flipper/cloud/instrumenter.rb