Method: ZuoraConnect::MetricsMiddleware#call

Defined in:
lib/middleware/metrics_middleware.rb

#call(env) ⇒ Object



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
# File 'lib/middleware/metrics_middleware.rb', line 44

def call(env)
  @bad_headers = ["HTTP_X_FORWARDED_HOST", "HTTP_X_FORWARDED_PORT", "HTTP_X_FORWARDED_PROTO", "HTTP_X_FORWARDED_SCHEME", "HTTP_X_FORWARDED_SSL"]
  if !ActionDispatch::Request::HTTP_METHODS.include?(env["REQUEST_METHOD"].upcase)
    [405, {"Content-Type" => "text/plain"}, ["Method Not Allowed"]]
  else
    if Thread.current[:isHallway]
      # We need the forwarded host header to identify location of tenant
      whitelist = Regexp.new(".*[\.]zuora[\.]com$|^zuora[\.]com$")
      @bad_headers.delete('HTTP_X_FORWARDED_HOST') if whitelist.match(env['HTTP_X_FORWARDED_HOST']).present?
    end

    #Remove bad headers
    @bad_headers.each { |header| env.delete(header) }

    if defined?(Prometheus) && env['PATH_INFO'] == '/connect/internal/metrics'

      # Prometheus Stuff
      metrics = ZuoraObservability::Metrics.resque
      metrics = defined?(ZuoraConnect::AppInstance.get_metrics) ? ZuoraConnect::AppInstance.get_metrics(metrics) : metrics

      redis_up = metrics.present? && metrics.dig(:Resque, :Workers_Total).present? ? 1 : 0
      Prometheus::REDIS_CONNECTION.set(redis_up)

      process_prometheus_metric(metrics: metrics)

      if defined?(Unicorn) && Unicorn.respond_to?(:listener_names)
        ZuoraObservability::Metrics.unicorn_listener.each do |key, value|
          gauge = Prometheus.const_get("unicorn_#{key}".gsub(/[^a-zA-Z0-9_]/, '_').upcase)
          gauge.set(value) if gauge.present?
        end
      end
    end

    #Thread.current[:appinstance] = nil
    start_time = Time.now
    begin
      @status, @headers, @response = @app.call(env)
    ensure
      # Writing to telegraf: Handle 404
      if [404, 500].include?(@status)
        content_type = @headers['Content-Type'].split(';')[0] if @headers['Content-Type']
        content_type = content_type.gsub('text/javascript', 'application/javascript')
        tags = { status: @status, content_type: content_type }

        tags = tags.merge({ controller: 'ActionController' })
        tags = tags.merge({ action: 'RoutingError' }) if @status == 404

        values = { response_time: ((Time.now - start_time) * 1000).round(2) }

        ZuoraConnect::AppInstanceBase.write_to_telegraf(direction: :inbound, tags: tags, values: values)
      end
      Thread.current[:inbound_metric] = nil
    end
    [@status, @headers, @response]
  end
end