Skip to content

Commit 3d29a21

Browse files
committed
Simplify Spotlight
1 parent 3026b9b commit 3d29a21

File tree

8 files changed

+88
-148
lines changed

8 files changed

+88
-148
lines changed

sentry-ruby/lib/sentry/configuration.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
require "sentry/dsn"
88
require "sentry/release_detector"
99
require "sentry/transport/configuration"
10-
require "sentry/spotlight/configuration"
1110
require "sentry/linecache"
1211
require "sentry/interfaces/stacktrace_builder"
1312

@@ -142,6 +141,14 @@ class Configuration
142141
# Whether to capture local variables from the raised exception's frame. Default is false.
143142
# @return [Boolean]
144143
attr_accessor :include_local_variables
144+
145+
# Whether to capture events and traces into Spotlight. Default is false.
146+
# If you set this to true, Sentry will send events and traces to the local
147+
# Sidecar proxy at http://localhost:8969/stream.
148+
# If you want to use a different Sidecar proxy address, set this to String
149+
# with the proxy URL.
150+
# @return [Boolean, String]
151+
attr_accessor :spotlight
145152

146153
# @deprecated Use {#include_local_variables} instead.
147154
alias_method :capture_exception_frame_locals, :include_local_variables
@@ -360,11 +367,11 @@ def initialize
360367
self.traces_sampler = nil
361368
self.enable_tracing = nil
362369

370+
self.spotlight = false
371+
363372
@transport = Transport::Configuration.new
364373
@gem_specs = Hash[Gem::Specification.map { |spec| [spec.name, spec.version.to_s] }] if Gem::Specification.respond_to?(:map)
365374

366-
@spotlight = Spotlight::Configuration.new
367-
368375
run_post_initialization_callbacks
369376
end
370377

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,63 @@
1-
require "sentry/spotlight/configuration"
2-
require "sentry/spotlight/transport"
1+
# frozen_string_literal: true
2+
3+
require "net/http"
4+
require "zlib"
5+
6+
module Sentry
7+
8+
# Spotlight Transport class is like HTTPTransport,
9+
# but it's experimental, with limited featureset.
10+
# - It does not care about rate limits, assuming working with local Sidecar proxy
11+
# - Designed to just report events to Spotlight in development.
12+
#
13+
# TODO: This needs a cleanup, we could extract most of common code into a module.
14+
class Spotlight
15+
GZIP_ENCODING = "gzip"
16+
GZIP_THRESHOLD = 1024 * 30
17+
CONTENT_TYPE = 'application/x-sentry-envelope'
18+
USER_AGENT = "sentry-ruby/#{Sentry::VERSION}"
19+
20+
# Takes the sidecar URL in and initializes the new Spotlight transport.
21+
# HTTPTransport will call this if config.spotlight is truthy, and pass it here.
22+
# so sidecar_url arg can either be true, or a string with the sidecar URL.
23+
def initialize(sidecar_url)
24+
@sidecar_url = sidecar_url.is_a?(String) ? sidecar_url : "http://localhost:8769/stream"
25+
end
26+
27+
def send_data(data)
28+
headers = {
29+
'Content-Type' => CONTENT_TYPE,
30+
'Content-Encoding' => "",
31+
'User-Agent' => USER_AGENT
32+
}
33+
34+
response = conn.start do |http|
35+
request = ::Net::HTTP::Post.new(@sidecar_url, headers)
36+
request.body = data
37+
http.request(request)
38+
end
39+
40+
unless response.code.match?(/\A2\d{2}/)
41+
error_info = "the server responded with status #{response.code}"
42+
error_info += "\nbody: #{response.body}"
43+
error_info += " Error in headers is: #{response['x-sentry-error']}" if response['x-sentry-error']
44+
45+
raise Sentry::ExternalError, error_info
46+
end
47+
48+
# TODO: We might want to rescue the other HTTP_ERRORS like in HTTPTransport
49+
rescue SocketError, * Sentry::HTTPTransport::HTTP_ERRORS => e
50+
raise Sentry::ExternalError.new(e.message)
51+
end
52+
53+
private
54+
55+
# Similar to HTTPTransport connection, but does not support Proxy and SSL
56+
def conn
57+
sidecar = URI(@sidecar_url)
58+
connection = ::Net::HTTP.new(sidecar.hostname, sidecar.port, nil)
59+
connection.use_ssl = false
60+
connection
61+
end
62+
end
63+
end

sentry-ruby/lib/sentry/spotlight/configuration.rb

Lines changed: 0 additions & 50 deletions
This file was deleted.

sentry-ruby/lib/sentry/spotlight/transport.rb

Lines changed: 0 additions & 77 deletions
This file was deleted.

sentry-ruby/lib/sentry/transport.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Transport
3232
def initialize(configuration)
3333
@logger = configuration.logger
3434
@transport_configuration = configuration.transport
35-
@spotlight_configuration = configuration.spotlight
35+
@spotlight = configuration.spotlight
3636
@dsn = configuration.dsn
3737
@rate_limits = {}
3838
@send_client_reports = configuration.send_client_reports

sentry-ruby/lib/sentry/transport/http_transport.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,21 @@ class HTTPTransport < Transport
2323
Zlib::BufError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED
2424
].freeze
2525

26-
2726
def initialize(*args)
2827
super
2928
@endpoint = @dsn.envelope_endpoint
3029

3130
log_debug("Sentry HTTP Transport will connect to #{@dsn.server}")
3231

33-
if @spotlight_configuration.enabled?
34-
@spotlight_transport = Sentry::Spotlight::Transport.new(@transport_configuration)
32+
if @spotlight
33+
@spotlight_transport = Sentry::Spotlight.new(@spotlight)
3534
end
3635
end
3736

3837
def send_data(data)
39-
@spotlight_transport.send_data(data) unless @spotlight_transport.nil?
38+
if should_send_to_spotlight?
39+
@spotlight_transport.send_data(data)
40+
end
4041

4142
encoding = ""
4243

@@ -142,6 +143,10 @@ def should_compress?(data)
142143
@transport_configuration.encoding == GZIP_ENCODING && data.bytesize >= GZIP_THRESHOLD
143144
end
144145

146+
def should_send_to_spotlight?
147+
!@spotlight_transport.nil?
148+
end
149+
145150
def conn
146151
server = URI(@dsn.server)
147152

sentry-ruby/spec/sentry/configuration_spec.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,7 @@
258258

259259
describe "#spotlight" do
260260
it "returns initialized Spotlight config by default" do
261-
spotlight_config = subject.spotlight
262-
expect(spotlight_config.enabled).to eq(false)
263-
expect(spotlight_config.sidecar_url).to eq("http://localhost:8969/stream")
261+
expect(subject.spotlight).to eq(false)
264262
end
265263
end
266264

sentry-ruby/spec/sentry/transport/http_transport_spec.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -324,28 +324,24 @@
324324

325325
describe "Spotlight integration" do
326326
let(:fake_response) { build_fake_response("200") }
327-
context "when spotlight is enabled" do
328-
let(:spotlight_transport) { Sentry::Spotlight::Transport.new(configuration.spotlight) }
329327

328+
context "when spotlight is enabled" do
330329
it "calls @spotlight_transport.send_data(data)" do
331-
configuration.spotlight.enabled = true
330+
configuration.spotlight = true
332331

333332
stub_request(fake_response)
334333

335-
subject.instance_variable_set(:@spotlight_transport, spotlight_transport)
336-
expect( spotlight_transport ).to receive(:send_data).with(data)
334+
expect( subject.instance_variable_get(:@spotlight_transport) ).to receive(:send_data).with(data)
337335
subject.send_data(data)
338336
end
339337
end
340338

341339
context "when spotlight integration is disabled" do
342340
let(:spotlight_transport) { nil }
343341
it "does not call @spotlight_transport.send_data(data)" do
344-
configuration.spotlight.enabled = false
345-
346342
stub_request(fake_response)
347343

348-
expect( spotlight_transport ).not_to receive(:send_data).with(data)
344+
expect( subject.instance_variable_get(:@spotlight_transport) ).not_to receive(:send_data).with(data)
349345
subject.send_data(data)
350346
end
351347
end

0 commit comments

Comments
 (0)