-
-
Notifications
You must be signed in to change notification settings - Fork 494
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c8110c
commit 3026b9b
Showing
9 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require "sentry/spotlight/configuration" | ||
require "sentry/spotlight/transport" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
module Sentry | ||
module Spotlight | ||
# Sentry Spotlight configuration. | ||
class Configuration | ||
|
||
# When enabled, Sentry will send all events and traces to the provided | ||
# Spotlight Sidecar URL. | ||
# Defaults to false. | ||
# @return [Boolean] | ||
attr_reader :enabled | ||
|
||
# Spotlight Sidecar URL as a String. | ||
# Defaults to "http://localhost:8969/stream" | ||
# @return [String] | ||
attr_accessor :sidecar_url | ||
|
||
def initialize | ||
@enabled = false | ||
@sidecar_url = "http://localhost:8969/stream" | ||
end | ||
|
||
def enabled? | ||
enabled | ||
end | ||
|
||
# Enables or disables Spotlight. | ||
def enabled=(value) | ||
unless [true, false].include?(value) | ||
raise ArgumentError, "Spotlight config.enabled must be a boolean" | ||
end | ||
|
||
if value == true | ||
unless ['development', 'test'].include?(environment_from_env) | ||
# Using the default logger here for a one-off warning. | ||
::Sentry::Logger.new(STDOUT).warn("[Spotlight] Spotlight is enabled in a non-development environment!") | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
# TODO: Sentry::Configuration already reads the env the same way as below, but it also has a way to _set_ environment | ||
# in it's config. So this introduces a bug where env could be different, depending on whether the user set the environment | ||
# manually. | ||
def environment_from_env | ||
ENV['SENTRY_CURRENT_ENV'] || ENV['SENTRY_ENVIRONMENT'] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development' | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# frozen_string_literal: true | ||
|
||
require "net/http" | ||
require "zlib" | ||
|
||
module Sentry | ||
module Spotlight | ||
|
||
|
||
# Spotlight Transport class is like HTTPTransport, | ||
# but it's experimental, with limited featureset. | ||
# - It does not care about rate limits, assuming working with local Sidecar proxy | ||
# - Designed to just report events to Spotlight in development. | ||
# | ||
# TODO: This needs a cleanup, we could extract most of common code into a module. | ||
class Transport | ||
|
||
GZIP_ENCODING = "gzip" | ||
GZIP_THRESHOLD = 1024 * 30 | ||
CONTENT_TYPE = 'application/x-sentry-envelope' | ||
USER_AGENT = "sentry-ruby/#{Sentry::VERSION}" | ||
|
||
# Initialize a new Spotlight transport | ||
# with the provided Spotlight configuration. | ||
def initialize(spotlight_configuration) | ||
@configuration = spotlight_configuration | ||
end | ||
|
||
def send_data(data) | ||
encoding = "" | ||
|
||
if should_compress?(data) | ||
data = Zlib.gzip(data) | ||
encoding = GZIP_ENCODING | ||
end | ||
|
||
headers = { | ||
'Content-Type' => CONTENT_TYPE, | ||
'Content-Encoding' => encoding, | ||
'X-Sentry-Auth' => generate_auth_header, | ||
'User-Agent' => USER_AGENT | ||
} | ||
|
||
response = conn.start do |http| | ||
request = ::Net::HTTP::Post.new(@configuration.sidecar_url, headers) | ||
request.body = data | ||
http.request(request) | ||
end | ||
|
||
unless response.code.match?(/\A2\d{2}/) | ||
error_info = "the server responded with status #{response.code}" | ||
error_info += "\nbody: #{response.body}" | ||
error_info += " Error in headers is: #{response['x-sentry-error']}" if response['x-sentry-error'] | ||
|
||
raise Sentry::ExternalError, error_info | ||
end | ||
rescue SocketError => e | ||
raise Sentry::ExternalError.new(e.message) | ||
end | ||
|
||
private | ||
|
||
def should_compress?(data) | ||
@transport_configuration.encoding == GZIP_ENCODING && data.bytesize >= GZIP_THRESHOLD | ||
end | ||
|
||
# Similar to HTTPTransport connection, but does not support Proxy and SSL | ||
def conn | ||
sidecar = URL(@configuration.sidecar_url) | ||
connection = ::Net::HTTP.new(sidecar.hostname, sidecar.port, nil) | ||
connection.use_ssl = false | ||
connection | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters