Class: Sentry::DSN
- Inherits:
-
Object
- Object
- Sentry::DSN
- Defined in:
- lib/sentry/dsn.rb
Constant Summary collapse
- PROTOCOL_VERSION =
"7"- PORT_MAP =
{ "http" => 80, "https" => 443 }.freeze
- REQUIRED_ATTRIBUTES =
%w[host path public_key project_id].freeze
- LOCALHOST_NAMES =
%w[localhost 127.0.0.1 ::1 [::1]].freeze
- LOCALHOST_PATTERN =
/\.local(host|domain)?$/i- ORG_ID_REGEX =
/\Ao(\d+)\./
Instance Method Summary collapse
- #csp_report_uri ⇒ Object
- #envelope_endpoint ⇒ Object
- #generate_auth_header(client: nil) ⇒ Object
-
#initialize(dsn_string) ⇒ DSN
constructor
A new instance of DSN.
- #local? ⇒ Boolean
- #localhost? ⇒ Boolean
- #otlp_traces_endpoint ⇒ Object
- #private_ip? ⇒ Boolean
- #resolved_ips_private? ⇒ Boolean
- #server ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(dsn_string) ⇒ DSN
Returns a new instance of DSN.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/sentry/dsn.rb', line 18 def initialize(dsn_string) @raw_value = dsn_string uri = URI.parse(dsn_string) uri_path = uri.path.split("/") if uri.user # DSN-style string @project_id = uri_path.pop @public_key = uri.user @secret_key = !(uri.password.nil? || uri.password.empty?) ? uri.password : nil end @scheme = uri.scheme @host = uri.host @port = uri.port if uri.port @path = uri_path.join("/") @org_id = extract_org_id_from_host end |
Instance Method Details
#csp_report_uri ⇒ Object
53 54 55 |
# File 'lib/sentry/dsn.rb', line 53 def csp_report_uri "#{server}/api/#{project_id}/security/?sentry_key=#{public_key}" end |
#envelope_endpoint ⇒ Object
57 58 59 |
# File 'lib/sentry/dsn.rb', line 57 def envelope_endpoint "#{path}/api/#{project_id}/envelope/" end |
#generate_auth_header(client: nil) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/sentry/dsn.rb', line 93 def generate_auth_header(client: nil) now = Sentry.utc_now.to_i fields = { "sentry_version" => PROTOCOL_VERSION, "sentry_timestamp" => now, "sentry_key" => @public_key } fields["sentry_client"] = client if client fields["sentry_secret"] = @secret_key if @secret_key "Sentry " + fields.map { |key, value| "#{key}=#{value}" }.join(", ") end |
#local? ⇒ Boolean
65 66 67 |
# File 'lib/sentry/dsn.rb', line 65 def local? @local ||= (localhost? || private_ip? || resolved_ips_private?) end |
#localhost? ⇒ Boolean
69 70 71 |
# File 'lib/sentry/dsn.rb', line 69 def localhost? LOCALHOST_NAMES.include?(host.downcase) || LOCALHOST_PATTERN.match?(host) end |
#otlp_traces_endpoint ⇒ Object
61 62 63 |
# File 'lib/sentry/dsn.rb', line 61 def otlp_traces_endpoint "#{path}/api/#{project_id}/integration/otlp/v1/traces/" end |
#private_ip? ⇒ Boolean
73 74 75 76 77 78 79 80 81 |
# File 'lib/sentry/dsn.rb', line 73 def private_ip? @private_ip ||= begin begin IPAddr.new(host).private? rescue IPAddr::InvalidAddressError false end end end |
#resolved_ips_private? ⇒ Boolean
83 84 85 86 87 88 89 90 91 |
# File 'lib/sentry/dsn.rb', line 83 def resolved_ips_private? @resolved_ips_private ||= begin begin Resolv.getaddresses(host).any? { |ip| IPAddr.new(ip).private? } rescue Resolv::ResolvError, IPAddr::InvalidAddressError false end end end |
#server ⇒ Object
47 48 49 50 51 |
# File 'lib/sentry/dsn.rb', line 47 def server server = "#{scheme}://#{host}" server += ":#{port}" unless port == PORT_MAP[scheme] server end |
#to_s ⇒ Object
43 44 45 |
# File 'lib/sentry/dsn.rb', line 43 def to_s @raw_value end |
#valid? ⇒ Boolean
39 40 41 |
# File 'lib/sentry/dsn.rb', line 39 def valid? REQUIRED_ATTRIBUTES.all? { |k| public_send(k) } end |