Class: Hyperdx::Ruby

Inherits:
Logger
  • Object
show all
Defined in:
lib/hyperdx/ruby.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, opts = {}) ⇒ Ruby

Returns a new instance of Ruby.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/hyperdx/ruby.rb', line 21

def initialize(key, opts = {})
  super(nil, nil, nil)
  @app = opts[:app] || "default"
  @log_level = opts[:level] || "INFO"
  @env = opts[:env]
  @meta = opts[:meta]
  @internal_logger = Logger.new($stdout)
  @internal_logger.level = Logger::DEBUG
  endpoint = opts[:endpoint] || Resources::ENDPOINT
  @hostname = opts[:hostname] || Socket.gethostname

  if @hostname.size > Resources::MAX_INPUT_LENGTH || @app.size > Resources::MAX_INPUT_LENGTH
    @internal_logger.debug("Hostname or Appname is over #{Resources::MAX_INPUT_LENGTH} characters")
    return
  end

  @ip =  opts.key?(:ip) ? opts[:ip] : ""
  @mac = opts.key?(:mac) ? opts[:mac] : ""
  url = "#{endpoint}?hdx_platform=ruby"
  uri = URI(url)

  request = Net::HTTP::Post.new(uri.request_uri, "Content-Type" => "application/json")
  request['Authorization'] = "Bearer #{key}"
  request[:'user-agent'] = opts[:'user-agent'] || "ruby/#{Hyperdx::VERSION}"
  @client = Hyperdx::Client.new(request, uri, opts)
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



19
20
21
# File 'lib/hyperdx/ruby.rb', line 19

def app
  @app
end

#envObject

Returns the value of attribute env.



19
20
21
# File 'lib/hyperdx/ruby.rb', line 19

def env
  @env
end

#metaObject

Returns the value of attribute meta.



19
20
21
# File 'lib/hyperdx/ruby.rb', line 19

def meta
  @meta
end

Instance Method Details

#<<(msg = nil, opts = {}) ⇒ Object



110
111
112
113
114
# File 'lib/hyperdx/ruby.rb', line 110

def <<(msg = nil, opts = {})
  log(msg, opts.merge(
             level: ""
           ))
end

#add(*_arg) ⇒ Object



116
117
118
119
# File 'lib/hyperdx/ruby.rb', line 116

def add(*_arg)
  @internal_logger.debug("add not supported in HyperDX logger")
  false
end

#clearObject



103
104
105
106
107
108
# File 'lib/hyperdx/ruby.rb', line 103

def clear
  @app = "default"
  @log_level = "INFO"
  @env = nil
  @meta = nil
end

#closeObject



132
133
134
# File 'lib/hyperdx/ruby.rb', line 132

def close
  @client&.exitout
end

#datetime_format(*_arg) ⇒ Object



127
128
129
130
# File 'lib/hyperdx/ruby.rb', line 127

def datetime_format(*_arg)
  @internal_logger.debug("datetime_format not supported in HyperDX logger")
  false
end

#default_optsObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/hyperdx/ruby.rb', line 48

def default_opts
  {
    app: @app,
    env: @env,
    hostname: @hostname,
    ip: @ip,
    level: @log_level,
    mac: @mac,
    meta: @meta,
  }
end

#levelObject



60
61
62
# File 'lib/hyperdx/ruby.rb', line 60

def level
  @log_level
end

#level=(value) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/hyperdx/ruby.rb', line 64

def level=(value)
  if value.is_a? Numeric
    @log_level = Resources::LOG_LEVELS[value]
    return
  end

  @log_level = value
end

#log(message = nil, opts = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/hyperdx/ruby.rb', line 73

def log(message = nil, opts = {})
  if message.nil? && block_given?
    message = yield
  end
  if message.nil?
    @internal_logger.debug("provide either a message or block")
    return
  end
  message = message.to_s.encode("UTF-8")
  @client.write_to_buffer(message, default_opts.merge(opts).merge(
                                     timestamp: (Time.now.to_f * 1000).to_i
                                   ))
end

#unknown(msg = nil, opts = {}) ⇒ Object



121
122
123
124
125
# File 'lib/hyperdx/ruby.rb', line 121

def unknown(msg = nil, opts = {})
  log(msg, opts.merge(
             level: "UNKNOWN"
           ))
end