PageRenderTime 25ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/peek/views/external_http.rb

https://gitlab.com/shaneutt/gitlab
Ruby | 101 lines | 81 code | 16 blank | 4 comment | 3 complexity | fd28d56fb07a571a18468bf065631ceb MD5 | raw file
  1. # frozen_string_literal: true
  2. module Peek
  3. module Views
  4. class ExternalHttp < DetailedView
  5. DEFAULT_THRESHOLDS = {
  6. calls: 10,
  7. duration: 1000,
  8. individual_call: 100
  9. }.freeze
  10. THRESHOLDS = {
  11. production: {
  12. calls: 10,
  13. duration: 1000,
  14. individual_call: 100
  15. }
  16. }.freeze
  17. def key
  18. 'external-http'
  19. end
  20. def results
  21. super.merge(calls: calls)
  22. end
  23. def self.thresholds
  24. @thresholds ||= THRESHOLDS.fetch(Rails.env.to_sym, DEFAULT_THRESHOLDS)
  25. end
  26. def format_call_details(call)
  27. full_path = generate_path(call)
  28. super.merge(
  29. label: "#{call[:method]} #{full_path}",
  30. code: code(call),
  31. proxy: proxy(call),
  32. error: error(call)
  33. )
  34. end
  35. private
  36. def duration
  37. ::Gitlab::Metrics::Subscribers::ExternalHttp.duration * 1000
  38. end
  39. def calls
  40. ::Gitlab::Metrics::Subscribers::ExternalHttp.request_count
  41. end
  42. def call_details
  43. ::Gitlab::Metrics::Subscribers::ExternalHttp.detail_store
  44. end
  45. def proxy(call)
  46. if call[:proxy_host].present?
  47. "Proxied via #{call[:proxy_host]}:#{call[:proxy_port]}"
  48. else
  49. nil
  50. end
  51. end
  52. def code(call)
  53. if call[:code].present?
  54. "Response status: #{call[:code]}"
  55. else
  56. nil
  57. end
  58. end
  59. def error(call)
  60. if call[:exception_object].present?
  61. "Exception: #{call[:exception_object]}"
  62. else
  63. nil
  64. end
  65. end
  66. def generate_path(call)
  67. uri = URI("")
  68. uri.scheme = call[:scheme]
  69. # The host can be a domain, IPv4 or IPv6.
  70. # Ruby handle IPv6 for us at
  71. # https://github.com/ruby/ruby/blob/v2_6_0/lib/uri/generic.rb#L662
  72. uri.hostname = call[:host]
  73. uri.port = call[:port]
  74. uri.path = call[:path]
  75. uri.query = generate_query(call[:query])
  76. uri.to_s
  77. rescue StandardError
  78. 'unknown'
  79. end
  80. def generate_query(query_string)
  81. query_string.is_a?(Hash) ? query_string.to_query : query_string.to_s
  82. end
  83. end
  84. end
  85. end