Class: RSpec::Buildkite::Analytics::Uploader

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/buildkite/analytics/uploader.rb

Defined Under Namespace

Classes: Trace

Constant Summary collapse

REQUEST_EXCEPTIONS =
[
  URI::InvalidURIError,
  Net::HTTPBadResponse,
  Net::HTTPHeaderSyntaxError,
  Net::ReadTimeout,
  Net::OpenTimeout,
  OpenSSL::SSL::SSLError,
  OpenSSL::SSL::SSLErrorWaitReadable,
  EOFError
]

Class Method Summary collapse

Class Method Details

.configureObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/rspec/buildkite/analytics/uploader.rb', line 110

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,
          format: "websocket"
        }.to_json

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

        return unless response

        case response.code
        when "401"
          puts "Buildkite Test Analytics: 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 "rspec-buildkite-analytics could not establish an initial connection with Buildkite. You may be missing some data for this test suite, please contact support."
        end
      else
        if !!ENV["BUILDKITE_BUILD_ID"]
          puts "Buildkite Test Analytics: No Suite API key provided. You can get the API key from your Suite settings page."
        end
      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
  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

.tracerObject



186
187
188
# File 'lib/rspec/buildkite/analytics/uploader.rb', line 186

def self.tracer
  Thread.current[:_buildkite_tracer]
end

.tracesObject



95
96
97
# File 'lib/rspec/buildkite/analytics/uploader.rb', line 95

def self.traces
  @traces ||= []
end