class Trakerr::TrakerrClient

Attributes

apiKey[RW]

API Key

contextAppBrowser[RW]

contextAppBrowser is optional MVC and ASP.net applications the browser name the application is running on.

contextAppBrowserVersion[RW]

contextAppBrowserVersion is optional for MVC and ASP.net applications the browser version the application is running on.

contextAppOS[RW]

ContextAppOS is the OS the program is running on.

contextAppOSVersion[RW]

ContextAppOSVersion is the version of the OS the code is running on.

contextAppVersion[RW]

App Version of the client the API is tying into.

contextDataCenter[RW]

ContextDatacenter is the optional datacenter the code may be running on.

contextDataCenterRegion[RW]

ContextDatacenterRegion is the optional datacenter region the code may be running on.

contextDeploymentStage[RW]

Deployment stage of the codebade the API is tying into.

contextEnvHostname[RW]

ContextEnvHostname is hostname of the pc running the code.

contextEnvLanguage[RW]

String name of the language being used.

contextEnvName[RW]

The name of the interpreter

contextEnvVersion[RW]

ContextEnvVersion is the version of the interpreter the program is run on.

Public Class Methods

new(apiKey, contextAppVersion="1.0", contextDeploymentStage="development") click to toggle source

Initializes the TrakerrClient class. apiKey:String: Should be your API key string. contextAppVersion:String: Should be the version of your application. contextEnvName:String: Should be the deployment stage of your program.

# File trakerr/lib/trakerr.rb, line 72
def initialize(apiKey,
               contextAppVersion="1.0",
               contextDeploymentStage="development")

  default_config = Trakerr::Configuration.default
  default_config.base_path = default_config.base_path

  @apiKey = apiKey
  @contextAppVersion = contextAppVersion
  @contextDeploymentStage = contextDeploymentStage

  @contextEnvLanguage = "Ruby"
  
  if RUBY_PLATFORM == "java"
    @contextEnvName = "jruby"
    @contextEnvVersion = JRUBY_VERSION
  else
    @contextEnvName = "ruby"
    @contextEnvVersion = RUBY_VERSION
  end

  @contextEnvHostname = Socket.gethostname
    
  host_os = RbConfig::CONFIG['host_os']
  case host_os
    when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
      text = `systeminfo`

      @contextAppOS = GetTextFromLine(text, "OS Name:", "\n")
      @contextAppOS.chomp! if @contextAppOS != nil
      @contextAppOS.strip! if @contextAppOS != nil

                              version = GetTextFromLine(text, "OS Version:", "\n").split
      version[0].chomp! if version != nil
      version[0].strip! if version != nil
      @contextAppOSVersion = contextAppOSVersion || version[0]

        
    when /darwin|mac os/
      text = `system_profiler SPSoftwareDataType`

      @contextAppOS = GetTextFromLine(text, "System Version:", "(").chomp.strip
                              @contextAppOSVersion = contextAppOSVersion || GetTextFromLine(text, "Kernel Version:", "\n").chomp.strip
        
    when /linux/, /solaris|bsd/
      #uname -s and -r
      @contextAppOS = `uname -s`.chomp.strip
      @contextAppOSVersion = contextAppOSVersion || `uname -r`.chomp.strip
  end

  if @contextAppOS == nil 
    @contextAppOS = RbConfig::CONFIG["target_os"]
  end
  if @contextAppOSVersion == nil
    @contextAppOSVersion = RbConfig::CONFIG['host_os']
  end
  
  @contextAppBrowser = contextAppBrowser
  @contextAppBrowserVersion = contextAppBrowserVersion
  @contextDataCenter = contextDataCenter
  @contextDataCenterRegion = contextDataCenterRegion
  api_client = Trakerr::ApiClient.new(default_config)
  @events_api = Trakerr::EventsApi.new(api_client)
end

Public Instance Methods

CreateAppEvent(err = false, log_level="Error", classification="issue", eventType="unknown", eventMessage="unknown") click to toggle source

Creates a new AppEvent and returns it with a stacktrace if err is an exception object. If passed false, it returns an AppEvent without a stacktrace. RETURNS: An AppEvent instance with the default event information. err:Exception: The exception that is captured or rescued, or false if you don't need a stacktrace. log_level:String: Logging level, currently one of 'debug','info','warning','error', 'fatal', defaults to 'error'. See loglevel in AppEvent for an always current list of values. Will argument error if passed another value. classification:String: Optional extra descriptor string. Will default to issue if not passed a value. eventType:string: String representation of the type of error. Defaults to err.class.name if err is an exception, unknown if not. eventMessage:String: String representation of the message of the error. Defaults to err.message if err is an exception, unknown if not.

# File trakerr/lib/trakerr.rb, line 150
def CreateAppEvent(err = false, log_level="Error", classification="issue", eventType="unknown", eventMessage="unknown")
  raise ArgumentError, "All non err arguments are expected strings." unless (log_level.is_a? String) && (classification.is_a? String) && (eventType.is_a? String) && (eventMessage.is_a? String)
  if err != false
    raise ArgumentError, "err is expected instance of exception." unless err.is_a? Exception
 
    eventType = err.class.name if eventType == "unknown"

    eventMessage = err.message if eventMessage == "unknown"

  end

  log_level = log_level.downcase

  app_event_new = AppEvent.new({classification: classification, eventType: eventType, eventMessage: eventMessage})

  begin
    app_event_new.log_level = log_level
  rescue ArgumentError
    app_event_new.log_level = "error"
  end
  
  app_event_new.event_stacktrace = EventTraceBuilder.get_stacktrace(err) if err != false

  return app_event_new
end
FillDefaults(appEvent) click to toggle source

Populates the given AppEvent with the client level default values RETURNS: The AppEvent with Defaults filled. appEvent:AppEvent: The AppEvent to fill.

# File trakerr/lib/trakerr.rb, line 223
def FillDefaults(appEvent)
  appEvent.api_key = appEvent.api_key || @apiKey

  appEvent.context_app_version = appEvent.context_app_version || @contextAppVersion
  appEvent.deployment_stage = appEvent.deployment_stage || @contextDeploymentStage

  appEvent.context_env_language = appEvent.context_env_language || @contextEnvLanguage
  appEvent.context_env_name = appEvent.context_env_name || @contextEnvName
  appEvent.context_env_version = appEvent.context_env_version || @contextEnvVersion
  appEvent.context_env_hostname = appEvent.context_env_hostname || @contextEnvHostname

  appEvent.context_app_os = appEvent.context_app_os || @contextAppOS
  appEvent.context_app_os_version = appEvent.context_app_os_version || @contextAppOSVersion

  appEvent.context_app_browser = appEvent.context_app_browser || @contextAppBrowser
  appEvent.context_app_browser_version = appEvent.context_app_browser_version || @contextAppBrowserVersion

  appEvent.context_data_center = appEvent.context_data_center || @contextDataCenter
  appEvent.context_data_center_region = appEvent.context_data_center_region || @contextDataCenterRegion

  appEvent.event_time = DateTime.now.strftime("%Q").to_i
  return appEvent
end
SendEvent(appEvent) click to toggle source

Sends the given AppEvent to Trakerr appEvent:AppEvent: The AppEvent to send.

# File trakerr/lib/trakerr.rb, line 214
def SendEvent(appEvent)
  @events_api.events_post(FillDefaults(appEvent))
end
log(arg_hash, error, log_level = "error", classification = "issue") click to toggle source

A single line method to send an event to trakerr. Use may it in a begin-rescue and pass in an error, or set error to false if you don't need a stacktrace. arg_hash takes in a few common values that you may want to populate your app event with in a hash. arg_hash:Hash: A hash with a key value pair for each of the following elements {“user”: “…”, “session”: “…”, “evntname”: “…”, “evntmessage”: “…”}. Omit any element you don't need to fill in the event. If you are NOT sending an error it is recommended that you pass in an evntname and evntmessage. Remember that all keys are expected strings, and so it may be safer to you use the arrow operator (=>) so you don't forget to add the space. error:Exception: The exception you may be sending. Set this to false if you are sending a non-error. This throws an Argument error if error is not an Exception and it's child classes or false. log_level:String: The string representation of the level of the error. classification:String: The string representation on the classification of the issue.

# File trakerr/lib/trakerr.rb, line 193
def log(arg_hash, error, log_level = "error", classification = "issue")
  raise ArgumentError, "arg_hash is expected to be a hash" unless arg_hash.is_a? Hash
  raise ArgumentError, "log_level and classification is expected strings." unless (log_level.is_a? String) && (classification.is_a? String)

  app_event = nil
  if error != false
    raise ArgumentError, "err is expected instance of exception." unless error.is_a? Exception
    app_event = CreateAppEvent(error, log_level, classification, arg_hash.fetch("evntname", "unknown"), arg_hash.fetch("evntmessage", "unknown"))
    
  end
  app_event = CreateAppEvent(false,log_level, classification, arg_hash.fetch("evntname", "unknown"), arg_hash.fetch("evntmessage", "unknown")) if app_event.nil?
  app_event.event_user = arg_hash["user"] if arg_hash.has_key? "user"
  app_event.event_session = arg_hash["session"] if arg_hash.has_key? "session"

  SendEvent(app_event)
end

Private Instance Methods

GetTextFromLine(text, prefix, suffix) click to toggle source

Used for parsing large strings. Gets the text in between a prefix string and a suffix string. Currently used to parse responses from shell commands on OS. RETURNS: The String from text between prefix and suffix or nil if not found or errors occur. text:String: The text to search in. prefix:String: The prefix string to start getting the text after suffix:String: The suffix string to find the ending index for.

# File trakerr/lib/trakerr.rb, line 256
def GetTextFromLine(text, prefix, suffix)
  raise ArgumentError, "All arguments are expected strings." unless (text.is_a? String) && (prefix.is_a? String) && (suffix.is_a? String)

  prefixindex = text.index(prefix)
  return nil if prefixindex == nil
  prefixindex = prefixindex + prefix.length

  suffixindex = text.index(suffix, prefixindex)
  return nil if suffixindex == nil

  text[prefixindex...suffixindex]
end