class RSpec::Buildkite::Analytics::Uploader

Constants

REQUEST_EXCEPTIONS

Public Class Methods

configure() click to toggle source
# File lib/rspec/buildkite/analytics/uploader.rb, line 104
def self.configure
  RSpec::Buildkite::Analytics.uploader = self

  RSpec.configure do |config|
    config.before(:suite) do
      config.add_formatter RSpec::Buildkite::Analytics::Reporter

      if RSpec::Buildkite::Analytics.api_token
        contact_uri = URI.parse(RSpec::Buildkite::Analytics.url)

        http = Net::HTTP.new(contact_uri.host, contact_uri.port)
        http.use_ssl = contact_uri.scheme == "https"

        authorization_header = "Token token=\"#{RSpec::Buildkite::Analytics.api_token}\""

        contact = Net::HTTP::Post.new(contact_uri.path, {
          "Authorization" => authorization_header,
          "Content-Type" => "application/json",
        })
        contact.body = {
          run_env: CI.env
        }.to_json

        response = begin
          http.request(contact)
        rescue *REQUEST_EXCEPTIONS => e
          puts "Error communicating with the server: #{e.message}"
        end

        case response.code
        when "401"
          puts "Invalid Suite API key. Please double check your Suite API key."
        when "200"
          json = JSON.parse(response.body)

          if (socket_url = json["cable"]) && (channel = json["channel"])
            RSpec::Buildkite::Analytics.session = Session.new(socket_url, authorization_header, channel)
          end
        else
          request_id = response.to_hash["x-request-id"]
          puts "Unknown error. If this error persists, please contact support+analytics@buildkite.com with this request ID `#{request_id}`."
        end
      else
        puts "No Suite API key provided. You can get the API key from your Suite settings page."
      end
    end

    config.around(:each) do |example|
      tracer = RSpec::Buildkite::Analytics::Tracer.new

      # The _buildkite prefix here is added as a safeguard against name collisions
      # as we are in the main thread
      Thread.current[:_buildkite_tracer] = tracer
      example.run
      Thread.current[:_buildkite_tracer] = nil

      tracer.finalize

      trace = RSpec::Buildkite::Analytics::Uploader::Trace.new(example, tracer.history)
      RSpec::Buildkite::Analytics.uploader.traces << trace
    end

    config.after(:suite) do
      # This needs the lonely operater as the session will be nil
      # if auth against the API token fails
      RSpec::Buildkite::Analytics.session&.close
    end
  end

  RSpec::Buildkite::Analytics::Network.configure
  RSpec::Buildkite::Analytics::Object.configure

  ActiveSupport::Notifications.subscribe("sql.active_record") do |name, start, finish, id, payload|
    tracer&.backfill(:sql, finish - start, **{ query: payload[:sql] })
  end
end
tracer() click to toggle source
# File lib/rspec/buildkite/analytics/uploader.rb, line 181
def self.tracer
  Thread.current[:_buildkite_tracer]
end
traces() click to toggle source
# File lib/rspec/buildkite/analytics/uploader.rb, line 89
def self.traces
  @traces ||= []
end