Class: Uptrace::DSN

Inherits:
Object
  • Object
show all
Defined in:
lib/uptrace/dsn.rb

Overview

Uptrace DSN

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dsn) ⇒ DSN

Returns a new instance of DSN.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/uptrace/dsn.rb', line 8

def initialize(dsn)
  raise ArgumentError, "DSN can't be empty" if dsn.empty?

  begin
    uri = URI.parse(dsn)
  rescue URI::InvalidURIError => e
    raise ArgumentError, %(can't parse DSN=#{dsn.inspect}: #{e})
  end

  @dsn = dsn
  @scheme = uri.scheme
  @host = uri.host
  @port = uri.port
  @project_id = uri.path.delete_prefix('/')
  @token = uri.user

  %w[scheme host].each do |k|
    v = public_send(k)
    raise ArgumentError, %(DSN=#{dsn.inspect} does not have a #{k}) if v.nil? || v.empty?
  end

  @host = 'uptrace.dev' if @host == 'api.uptrace.dev'
  return if @host != 'uptrace.dev'

  %w[project_id token].each do |k|
    v = public_send(k)
    raise ArgumentError, %(DSN=#{dsn.inspect} does not have a #{k}) if v.nil? || v.empty?
  end
end

Instance Attribute Details

#dsnObject (readonly)

Returns the value of attribute dsn.



6
7
8
# File 'lib/uptrace/dsn.rb', line 6

def dsn
  @dsn
end

#hostObject (readonly)

Returns the value of attribute host.



6
7
8
# File 'lib/uptrace/dsn.rb', line 6

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



6
7
8
# File 'lib/uptrace/dsn.rb', line 6

def port
  @port
end

#project_idObject (readonly)

Returns the value of attribute project_id.



6
7
8
# File 'lib/uptrace/dsn.rb', line 6

def project_id
  @project_id
end

#schemeObject (readonly)

Returns the value of attribute scheme.



6
7
8
# File 'lib/uptrace/dsn.rb', line 6

def scheme
  @scheme
end

#tokenObject (readonly)

Returns the value of attribute token.



6
7
8
# File 'lib/uptrace/dsn.rb', line 6

def token
  @token
end

Instance Method Details

#app_addrObject



42
43
44
45
46
# File 'lib/uptrace/dsn.rb', line 42

def app_addr
  return 'https://app.uptrace.dev' if @host == 'uptrace.dev'

  "#{@scheme}://#{@host}:#{@port}"
end

#otlp_addrObject



48
49
50
51
52
# File 'lib/uptrace/dsn.rb', line 48

def otlp_addr
  return 'https://otlp.uptrace.dev' if @host == 'uptrace.dev'

  "#{@scheme}://#{@host}:#{@port}"
end

#to_sObject



38
39
40
# File 'lib/uptrace/dsn.rb', line 38

def to_s
  @dsn
end