# encoding: UTF-8

# Stats controller for sending arbitrary data to statsd from the web.
#
# The initial use case is deploy tracking.
class StatsController < ApplicationController
  skip_before_action :verify_authenticity_token

  def create
    if authorized?
      StatsD.increment(params[:key])
      render(nothing: true)
    else
      render(nothing: true, status: :forbidden)
    end
  end

  private

  def authorized?
    !Rails.application.secrets.stats_secret.blank? &&
      Rails.application.secrets.stats_secret == params[:secret]
  end
end

# encoding: UTF-8

# Stats controller for sending arbitrary data to statsd from the web.
#
# The initial use case is deploy tracking.
class StatsController < ApplicationController
  skip_before_action :verify_authenticity_token

  def create
    if authorized?
      StatsD.increment(params[:key])
      head(:ok, content_length: 0)
    else
      head(:forbidden, content_length: 0)
    end
  end

  private

  def authorized?
    !Rails.application.secrets.stats_secret.blank? &&
      Rails.application.secrets.stats_secret == params[:secret]
  end
end

Set content length header to 0 on stats post responses.
Set content length header to 0 on stats post responses.

This is a workaround for what seems like a Varnish bug. Without
the content length header, rails generates an empty response with
transfer-encoding chunked. This is valid but Varnish removes the
transfer encoding header, creating a response without content length
or chunked encoding. This invalid response causes upstream ELBs to
return 502 bad gateway.
