module Salestation
  class Web < Module
    class StatsdMiddleware

      def initialize(app, statsd, metric:)
        @app = app
        @statsd = statsd
        @metric = metric
      end

      def call(env)
        start = Time.now

        status, header, body = @app.call env

        method, path = env['sinatra.route'].split

        @statsd.timing(@metric, (Time.now - start) * 1000, tags: [
          "path:#{ path }",
          "method:#{ method }",
          "status:#{ status }"
        ])

        [status, header, body]
      end
    end
  end
end

module Salestation
  class Web < Module
    class StatsdMiddleware

      def initialize(app, statsd, metric:)
        @app = app
        @statsd = statsd
        @metric = metric
      end

      def call(env)
        start = Time.now

        status, header, body = @app.call env

        method = env['REQUEST_METHOD']
        path =
          if route = env['sinatra.route']
            route.split(' ').last
          else
            'unknown-route'
          end

        @statsd.timing(@metric, (Time.now - start) * 1000, tags: [
          "path:#{ path }",
          "method:#{ method }",
          "status:#{ status }"
        ])

        [status, header, body]
      end
    end
  end
end

Fix non-found route error in StatsdMiddleware
Fix non-found route error in StatsdMiddleware

sinatra.route is only available when there is a route. In case an user
gave a wrong url then we do not have a route.
