Sha256: 43d608f849b653fcce6788a91046bbe9d76046bcdf1ccd3fb496a1adb6bfceb3

Contents?: true

Size: 1.46 KB

Versions: 37

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module Datadog
  module AppSec
    # Instrumentation for AppSec
    module Instrumentation
      # Instrumentation gateway implementation
      class Gateway
        # Instrumentation gateway middleware
        class Middleware
          attr_reader :key, :block

          def initialize(key, &block)
            @key = key
            @block = block
          end

          def call(stack, env)
            @block.call(stack, env)
          end
        end

        private_constant :Middleware

        def initialize
          @middlewares = Hash.new { |h, k| h[k] = [] }
        end

        def push(name, env, &block)
          block ||= -> {}

          middlewares_for_name = middlewares[name]

          return [block.call, nil] if middlewares_for_name.empty?

          wrapped = lambda do |_env|
            [block.call, nil]
          end

          # TODO: handle exceptions, except for wrapped
          stack = middlewares_for_name.reverse.reduce(wrapped) do |next_, middleware|
            lambda do |env_|
              middleware.call(next_, env_)
            end
          end

          stack.call(env)
        end

        def watch(name, key, &block)
          @middlewares[name] << Middleware.new(key, &block) unless middlewares[name].any? { |m| m.key == key }
        end

        private

        attr_reader :middlewares
      end

      def self.gateway
        @gateway ||= Gateway.new # TODO: not thread safe
      end
    end
  end
end

Version data entries

37 entries across 37 versions & 2 rubygems

Version Path
datadog-2.9.0 lib/datadog/appsec/instrumentation/gateway.rb
datadog-2.8.0 lib/datadog/appsec/instrumentation/gateway.rb
datadog-2.7.1 lib/datadog/appsec/instrumentation/gateway.rb
datadog-2.7.0 lib/datadog/appsec/instrumentation/gateway.rb
datadog-2.6.0 lib/datadog/appsec/instrumentation/gateway.rb
datadog-2.5.0 lib/datadog/appsec/instrumentation/gateway.rb
datadog-2.4.0 lib/datadog/appsec/instrumentation/gateway.rb
datadog-2.3.0 lib/datadog/appsec/instrumentation/gateway.rb
datadog-2.2.0 lib/datadog/appsec/instrumentation/gateway.rb
ddtrace-1.23.3 lib/datadog/appsec/instrumentation/gateway.rb
ddtrace-1.23.2 lib/datadog/appsec/instrumentation/gateway.rb
datadog-2.1.0 lib/datadog/appsec/instrumentation/gateway.rb
datadog-2.0.0 lib/datadog/appsec/instrumentation/gateway.rb
ddtrace-1.23.1 lib/datadog/appsec/instrumentation/gateway.rb
datadog-2.0.0.beta2 lib/datadog/appsec/instrumentation/gateway.rb
ddtrace-1.22.0 lib/datadog/appsec/instrumentation/gateway.rb
datadog-2.0.0.beta1 lib/datadog/appsec/instrumentation/gateway.rb
ddtrace-1.21.1 lib/datadog/appsec/instrumentation/gateway.rb
ddtrace-1.21.0 lib/datadog/appsec/instrumentation/gateway.rb
ddtrace-1.20.0 lib/datadog/appsec/instrumentation/gateway.rb