trakerr_client 1.0.0r

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. data/.gitignore +5 -0
  2. data/README.md +148 -0
  3. data/generated/.gitignore +50 -0
  4. data/generated/.rspec +2 -0
  5. data/generated/.swagger-codegen-ignore +23 -0
  6. data/generated/LICENSE +201 -0
  7. data/generated/README.md +97 -0
  8. data/generated/docs/AppEvent.md +30 -0
  9. data/generated/docs/CustomData.md +9 -0
  10. data/generated/docs/CustomDoubleData.md +17 -0
  11. data/generated/docs/CustomStringData.md +17 -0
  12. data/generated/docs/Error.md +10 -0
  13. data/generated/docs/EventsApi.md +55 -0
  14. data/generated/docs/InnerStackTrace.md +10 -0
  15. data/generated/docs/StackTraceLine.md +10 -0
  16. data/generated/docs/StackTraceLines.md +7 -0
  17. data/generated/docs/Stacktrace.md +7 -0
  18. data/generated/git_push.sh +67 -0
  19. data/generated/lib/trakerr_client/api/events_api.rb +90 -0
  20. data/generated/lib/trakerr_client/api_client.rb +378 -0
  21. data/generated/lib/trakerr_client/api_error.rb +47 -0
  22. data/generated/lib/trakerr_client/configuration.rb +207 -0
  23. data/generated/lib/trakerr_client/models/app_event.rb +454 -0
  24. data/generated/lib/trakerr_client/models/custom_data.rb +208 -0
  25. data/generated/lib/trakerr_client/models/custom_double_data.rb +280 -0
  26. data/generated/lib/trakerr_client/models/custom_string_data.rb +280 -0
  27. data/generated/lib/trakerr_client/models/error.rb +217 -0
  28. data/generated/lib/trakerr_client/models/inner_stack_trace.rb +217 -0
  29. data/generated/lib/trakerr_client/models/stack_trace_line.rb +217 -0
  30. data/generated/lib/trakerr_client/models/stack_trace_lines.rb +190 -0
  31. data/generated/lib/trakerr_client/models/stacktrace.rb +190 -0
  32. data/generated/lib/trakerr_client/version.rb +26 -0
  33. data/generated/lib/trakerr_client.rb +60 -0
  34. data/generated/spec/api/events_api_spec.rb +58 -0
  35. data/generated/spec/api_client_spec.rb +237 -0
  36. data/generated/spec/configuration_spec.rb +53 -0
  37. data/generated/spec/models/app_event_spec.rb +167 -0
  38. data/generated/spec/models/custom_data_spec.rb +59 -0
  39. data/generated/spec/models/custom_double_data_spec.rb +107 -0
  40. data/generated/spec/models/custom_string_data_spec.rb +107 -0
  41. data/generated/spec/models/error_spec.rb +65 -0
  42. data/generated/spec/models/inner_stack_trace_spec.rb +65 -0
  43. data/generated/spec/models/stack_trace_line_spec.rb +65 -0
  44. data/generated/spec/models/stack_trace_lines_spec.rb +47 -0
  45. data/generated/spec/models/stacktrace_spec.rb +47 -0
  46. data/generated/spec/spec_helper.rb +122 -0
  47. data/generated/trakerr_client.gemspec +55 -0
  48. data/mkgem.sh +6 -0
  49. data/test_app.rb +40 -0
  50. data/trakerr/lib/event_trace_builder.rb +163 -0
  51. data/trakerr/lib/trakerr.rb +235 -0
  52. data/trakerr_client.gemspec +58 -0
  53. metadata +313 -0
@@ -0,0 +1,163 @@
1
+ require "trakerr_client"
2
+
3
+
4
+ module Trakerr
5
+ class EventTraceBuilder
6
+
7
+ ##
8
+ #Gets the stactrace from the exception instance passed in.
9
+ #RETURNS: A Stacktrace object that contains the trace of the exception passed in.
10
+ #exc:Exception: The exception caught or rescued.
11
+ ##
12
+ def self.get_stacktrace(exc)
13
+ raise ArgumentError, "get_stacktrace expects an exception instance." unless exc.is_a? Exception
14
+
15
+ strace = Trakerr::Stacktrace.new
16
+ add_stack_trace(strace, exc)
17
+ return strace
18
+ end
19
+
20
+ private
21
+
22
+ ##
23
+ #Adds a InnerStackTrace to the Stacktrace object (which is a collection)
24
+ #strace:Stacktrace: The Stacktrace object to append the latest InnerStackTrace to.
25
+ #exc:Exception: The exception caught or rescued.
26
+ ##
27
+ def self.add_stack_trace(strace, exc)
28
+ raise ArgumentError, "add_stack_trace did not get passed in the correct arguments" unless exc.is_a? Exception and strace.instance_of? Stacktrace
29
+
30
+ newtrace = Trakerr::InnerStackTrace.new
31
+
32
+ newtrace.type = exc.class.name
33
+ newtrace.message = exc.message
34
+ newtrace.trace_lines = get_event_tracelines(best_regexp_for(exc), exc.backtrace)
35
+ strace.push(newtrace)
36
+ end
37
+
38
+ ##
39
+ #Formats and returns a StackTraceLines object that holds the current stacktrace from the error.
40
+ #RETURNS: A StackTraceLines object that contains the parsed traceback.
41
+ #regex:RegularExpression: The regular expression to parse the stacktrace text with.
42
+ #errarray:String[]: An array of strings which each of which is a StackTrace string line.
43
+ ##
44
+ def self.get_event_tracelines(regex, errarray)
45
+ raise ArgumentError, "errarray should be an iterable object." unless errarray.respond_to?('each')
46
+
47
+ stlines = Trakerr::StackTraceLines.new
48
+
49
+ errarray.each {|line|
50
+ stline = Trakerr::StackTraceLine.new
51
+ match = parse_stacktrace(regex, line)
52
+ stline.file, stline.line, stline.function = match[:file], match[:line], match[:function]
53
+
54
+ stlines.push(stline)
55
+ }
56
+ return stlines
57
+ end
58
+
59
+ ##
60
+ #Parses each given line by the regex
61
+ #RETURNS: A match object with the capture groups file function and line set.
62
+ #regex:RegularExpression: The regular expression to parse the stacktrace text with.
63
+ #line:String: A string with the traceline to parce
64
+ ##
65
+ def self.parse_stacktrace(regex, line)
66
+ raise ArgumentError, "line should be a string." unless line.is_a? String
67
+
68
+ match = regex.match(line)
69
+ return match if match
70
+
71
+ raise RegexpError, "line does not fit any of the supported stacktraces." #TODO: Error handle this?
72
+ end
73
+
74
+ def self.best_regexp_for(exc)
75
+ #add error check
76
+ if defined?(Java::JavaLang::Throwable) && exc.is_a?(Java::JavaLang::Throwable)
77
+ @@JAVA
78
+ elsif defined?(OCIError) && exc.is_a?(OCIError)
79
+ @@OCI
80
+ #elsif execjs_exception?(exception)
81
+ # Patterns::EXECJS disabled pending more complex test
82
+ else
83
+ @@RUBY
84
+ end
85
+ end
86
+
87
+ ##
88
+ # @return [Regexp] the pattern that matches standard Ruby stack frames,
89
+ # such as ./spec/notice_spec.rb:43:in `block (3 levels) in <top (required)>'
90
+ @@RUBY = %r{\A
91
+ (?<file>.+) # Matches './spec/notice_spec.rb'
92
+ :
93
+ (?<line>\d+) # Matches '43'
94
+ :in\s
95
+ `(?<function>.*)' # Matches "`block (3 levels) in <top (required)>'"
96
+ \z}x
97
+
98
+ ##
99
+ # @return [Regexp] the pattern that matches JRuby Java stack frames, such
100
+ # as org.jruby.ast.NewlineNode.interpret(NewlineNode.java:105)
101
+ @@JAVA = %r{\A
102
+ (?<function>.+) # Matches 'org.jruby.ast.NewlineNode.interpret'
103
+ \(
104
+ (?<file>
105
+ (?:uri:classloader:/.+(?=:)) # Matches '/META-INF/jruby.home/protocol.rb'
106
+ |
107
+ (?:uri_3a_classloader_3a_.+(?=:)) # Matches 'uri_3a_classloader_3a_/gems/...'
108
+ |
109
+ [^:]+ # Matches 'NewlineNode.java'
110
+ )
111
+ :?
112
+ (?<line>\d+)? # Matches '105'
113
+ \)
114
+ \z}x
115
+
116
+ ##
117
+ # @return [Regexp] the pattern that tries to assume what a generic stack
118
+ # frame might look like, when exception's backtrace is set manually.
119
+ @@GENERIC = %r{\A
120
+ (?:from\s)?
121
+ (?<file>.+) # Matches '/foo/bar/baz.ext'
122
+ :
123
+ (?<line>\d+)? # Matches '43' or nothing
124
+ (?:
125
+ in\s`(?<function>.+)' # Matches "in `func'"
126
+ |
127
+ :in\s(?<function>.+) # Matches ":in func"
128
+ )? # ... or nothing
129
+ \z}x
130
+
131
+ ##
132
+ # @return [Regexp] the pattern that matches exceptions from PL/SQL such as
133
+ # ORA-06512: at "STORE.LI_LICENSES_PACK", line 1945
134
+ # @note This is raised by https://github.com/kubo/ruby-oci8
135
+ @@OCI = /\A
136
+ (?:
137
+ ORA-\d{5}
138
+ :\sat\s
139
+ (?:"(?<function>.+)",\s)?
140
+ line\s(?<line>\d+)
141
+ |
142
+ #{@@GENERIC}
143
+ )
144
+ \z/x
145
+
146
+ ##
147
+ # @return [Regexp] the pattern that matches CoffeeScript backtraces
148
+ # usually coming from Rails & ExecJS
149
+ @@EXECJS = /\A
150
+ (?:
151
+ # Matches 'compile ((execjs):6692:19)'
152
+ (?<function>.+)\s\((?<file>.+):(?<line>\d+):\d+\)
153
+ |
154
+ # Matches 'bootstrap_node.js:467:3'
155
+ (?<file>.+):(?<line>\d+):\d+(?<function>)
156
+ |
157
+ # Matches the Ruby part of the backtrace
158
+ #{@@RUBY}
159
+ )
160
+ \z/x
161
+
162
+ end
163
+ end
@@ -0,0 +1,235 @@
1
+ require "event_trace_builder"
2
+ require "trakerr_client"
3
+ require "socket"
4
+ require "date"
5
+
6
+ module Trakerr
7
+ class TrakerrClient
8
+
9
+ ##API Key
10
+ attr_accessor :apiKey
11
+
12
+ ##App Version of the client the API is tying into.
13
+ attr_accessor :contextAppVersion
14
+
15
+ ##Deployment stage of the codebade the API is tying into.
16
+ attr_accessor :contextDeploymentStage
17
+
18
+ ##String name of the language being used.
19
+ attr_accessor :contextEnvLanguage
20
+
21
+ ##The name of the interpreter
22
+ attr_accessor :contextEnvName
23
+
24
+ ## ContextEnvVersion is the version of the interpreter the program is run on.
25
+ attr_accessor :contextEnvVersion
26
+
27
+ ## ContextEnvHostname is hostname of the pc running the code.
28
+ attr_accessor :contextEnvHostname
29
+
30
+ ## ContextAppOS is the OS the program is running on.
31
+ attr_accessor :contextAppOS
32
+
33
+ ## ContextAppOSVersion is the version of the OS the code is running on.
34
+ attr_accessor :contextAppOSVersion
35
+
36
+ ## contextAppBrowser is optional MVC and ASP.net applications the browser name the application is running on.
37
+ attr_accessor :contextAppBrowser
38
+
39
+ ## contextAppBrowserVersion is optional for MVC and ASP.net applications the browser version the application is running on.
40
+ attr_accessor :contextAppBrowserVersion
41
+
42
+ ## ContextDatacenter is the optional datacenter the code may be running on.
43
+ attr_accessor :contextDataCenter
44
+
45
+ ## ContextDatacenterRegion is the optional datacenter region the code may be running on.
46
+ attr_accessor :contextDataCenterRegion
47
+
48
+ ##
49
+ #Initializes the TrakerrClient class.
50
+ #apiKey:String: Should be your API key string.
51
+ #contextAppVersion:String: Should be the version of your application.
52
+ #contextEnvName:String: Should be the deployment stage of your program.
53
+ ##
54
+ def initialize(apiKey,
55
+ contextAppVersion = "1.0",
56
+ contextDeploymentStage = "development")
57
+
58
+ default_config = Trakerr::Configuration.default
59
+ default_config.base_path = default_config.base_path
60
+
61
+ @apiKey = apiKey
62
+ @contextAppVersion = contextAppVersion
63
+ @contextDeploymentStage = contextDeploymentStage
64
+
65
+ @contextEnvLanguage = "Ruby"
66
+
67
+ if RUBY_PLATFORM == "java"
68
+ @contextEnvName = "jruby"
69
+ @contextEnvVersion = JRUBY_VERSION
70
+ else
71
+ @contextEnvName = "ruby"
72
+ @contextEnvVersion = RUBY_VERSION
73
+ end
74
+
75
+ @contextEnvHostname = Socket.gethostname
76
+
77
+ host_os = RbConfig::CONFIG['host_os']
78
+ case host_os
79
+ when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
80
+ text = `systeminfo`
81
+
82
+ @contextAppOS = GetTextFromLine(text, "OS Name:", "\n")
83
+ @contextAppOS.chomp! if @contextAppOS != nil
84
+ @contextAppOS.strip! if @contextAppOS != nil
85
+
86
+ version = GetTextFromLine(text, "OS Version:", "\n").split
87
+ version[0].chomp! if version != nil
88
+ version[0].strip! if version != nil
89
+ @contextAppOSVersion = contextAppOSVersion || version[0]
90
+
91
+
92
+ when /darwin|mac os/
93
+ text = `system_profiler SPSoftwareDataType`
94
+
95
+ @contextAppOS = GetTextFromLine(text, "System Version:", "(").chomp.strip
96
+ @contextAppOSVersion = contextAppOSVersion || GetTextFromLine(text, "Kernel Version:", "\n").chomp.strip
97
+
98
+ when /linux/, /solaris|bsd/
99
+ #uname -s and -r
100
+ @contextAppOS = `uname -s`.chomp.strip
101
+ @contextAppOSVersion = contextAppOSVersion || `uname -r`.chomp.strip
102
+ end
103
+
104
+ if @contextAppOS == nil
105
+ @contextAppOS = RbConfig::CONFIG["target_os"]
106
+ end
107
+ if @contextAppOSVersion == nil
108
+ @contextAppOSVersion = RbConfig::CONFIG['host_os']
109
+ end
110
+
111
+ @contextAppBrowser = contextAppBrowser
112
+ @contextAppBrowserVersion = contextAppBrowserVersion
113
+ @contextDataCenter = contextDataCenter
114
+ @contextDataCenterRegion = contextDataCenterRegion
115
+ api_client = Trakerr::ApiClient.new(default_config)
116
+ @events_api = Trakerr::EventsApi.new(api_client)
117
+ end
118
+
119
+ ##
120
+ #Creates a new AppEvent and returns it with a stacktrace if err is an exception object.
121
+ #If passed false, it returns an AppEvent without a stacktrace.
122
+ #RETURNS: An AppEvent instance with the default event information.
123
+ #err:Exception: The exception that is captured or rescued, or false if you don't need a stacktrace.
124
+ #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.
125
+ #Will argument error if passed another value.
126
+ #classification:String: Optional extra descriptor string. Will default to issue if not passed a value.
127
+ #eventType:string: String representation of the type of error.
128
+ #Defaults to err.class.name if err is an exception, unknown if not.
129
+ #eventMessage:String: String representation of the message of the error.
130
+ #Defaults to err.message if err is an exception, unknown if not.
131
+ ##
132
+ def CreateAppEvent(err, log_level="Error", classification = "issue", eventType = "unknown", eventMessage = "unknown")
133
+ raise ArgumentError, "All non err arguments are expected strings." unless log_level.is_a? String and classification.is_a? String and eventType is_a? String and eventMessage.is_a? String
134
+ if err != false
135
+ raise ArgumentError, "err is expected instance of exception." unless err.is_a? Exception
136
+
137
+ if eventType == nil || eventType == "unknown"
138
+ eventType = err.class.name
139
+ end
140
+
141
+ if eventMessage == nil || eventMessage == "unknown"
142
+ eventMessage = err.message
143
+ end
144
+
145
+ end
146
+
147
+ log_level = log_level.downcase
148
+
149
+ app_event_new = AppEvent.new({classification: classification, eventType: eventType, eventMessage: eventMessage})
150
+
151
+ begin
152
+ app_event_new.log_level = log_level
153
+ rescue
154
+ app_event_new.log_level = "error"
155
+ end
156
+
157
+ app_event_new.event_stacktrace = EventTraceBuilder.get_stacktrace(err) if err != false
158
+
159
+ return app_event_new
160
+ end
161
+
162
+ ##
163
+ #Sends the given AppEvent to Trakerr
164
+ #appEvent:AppEvent: The AppEvent to send.
165
+ ##
166
+ def SendEvent(appEvent)
167
+ @events_api.events_post(FillDefaults(appEvent))
168
+ end
169
+
170
+ ##
171
+ #Sends the given error to Trakerr. Simplest use case for Trakerr in a catch, uses the default values when sending.
172
+ #You can provide an optional log_level or classification.
173
+ #error:Exception: The exception that is captured or rescued.
174
+ #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.
175
+ #classification:String: Optional extra descriptor string. Will default to issue if not passed a value.
176
+ ##
177
+ def SendException(error, log_level = "error", classification = "issue")
178
+ raise ArgumentError, "Error is expected type exception." unless error.is_a? Exception
179
+ raise ArgumentError, "log_level and classification are expected strings" unless log_level.is_a? String and classification.is_a? String
180
+
181
+ SendEvent(CreateAppEvent(Error, log_level, classification))
182
+ end
183
+
184
+ ##
185
+ #Populates the given AppEvent with the client level default values
186
+ #RETURNS: The AppEvent with Defaults filled.
187
+ #appEvent:AppEvent: The AppEvent to fill.
188
+ ##
189
+ def FillDefaults(appEvent)
190
+ appEvent.api_key = appEvent.api_key || @apiKey
191
+
192
+ appEvent.context_app_version = appEvent.context_app_version || @contextAppVersion
193
+ appEvent.deployment_stage = appEvent.deployment_stage || @contextDeploymentStage
194
+
195
+ appEvent.context_env_language = appEvent.context_env_language || @contextEnvLanguage
196
+ appEvent.context_env_name = appEvent.context_env_name || @contextEnvName
197
+ appEvent.context_env_version = appEvent.context_env_version || @contextEnvVersion
198
+ appEvent.context_env_hostname = appEvent.context_env_hostname || @contextEnvHostname
199
+
200
+ appEvent.context_app_os = appEvent.context_app_os || @contextAppOS
201
+ appEvent.context_app_os_version = appEvent.context_app_os_version || @contextAppOSVersion
202
+
203
+ appEvent.context_app_browser = appEvent.context_app_browser || @contextAppBrowser
204
+ appEvent.context_app_browser_version = appEvent.context_app_browser_version || @contextAppBrowserVersion
205
+
206
+ appEvent.context_data_center = appEvent.context_data_center || @contextDataCenter
207
+ appEvent.context_data_center_region = appEvent.context_data_center_region || @contextDataCenterRegion
208
+
209
+ appEvent.event_time = DateTime.now.strftime("%Q").to_i
210
+ return appEvent
211
+ end
212
+
213
+ private
214
+ ##
215
+ #Used for parsing large strings. Gets the text in between a prefix string and a suffix string.
216
+ #Currently used to parse responses from shell commands on OS.
217
+ #RETURNS: The String from text between prefix and suffix or nil if not found or errors occur.
218
+ #text:String: The text to search in.
219
+ #prefix:String: The prefix string to start getting the text after
220
+ #suffix:String: The suffix string to find the ending index for.
221
+ ##
222
+ def GetTextFromLine(text, prefix, suffix)
223
+ raise ArgumentError, "All arguments are expected strings." unless text.is_a? String and prefix.is_a? String and suffix.is_a? String
224
+
225
+ prefixindex = text.index(prefix)
226
+ return nil if prefixindex == nil
227
+ prefixindex = prefixindex + prefix.length
228
+
229
+ suffixindex = text.index(suffix, prefixindex)
230
+ return nil if suffixindex == nil
231
+
232
+ text[prefixindex...suffixindex]
233
+ end
234
+ end
235
+ end
@@ -0,0 +1,58 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ =begin
4
+ #Trakerr API
5
+
6
+ #Get your application events and errors to Trakerr via the *Trakerr API*.
7
+
8
+ OpenAPI spec version: 1.0.0
9
+
10
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
11
+
12
+ Licensed under the Apache License, Version 2.0 (the "License");
13
+ you may not use this file except in compliance with the License.
14
+ You may obtain a copy of the License at
15
+
16
+ http://www.apache.org/licenses/LICENSE-2.0
17
+
18
+ Unless required by applicable law or agreed to in writing, software
19
+ distributed under the License is distributed on an "AS IS" BASIS,
20
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
+ See the License for the specific language governing permissions and
22
+ limitations under the License.
23
+
24
+ =end
25
+
26
+ $:.push File.expand_path("../lib", __FILE__)
27
+ $:.push File.expand_path("../generated/lib", __FILE__)
28
+ $:.push File.expand_path("../trakerr/lib", __FILE__)
29
+
30
+ Gem::Specification.new do |s|
31
+ s.name = "trakerr_client"
32
+ s.version = "1.0.0r"
33
+ s.platform = Gem::Platform::RUBY
34
+ s.authors = ["Swagger-Codegen"]
35
+ s.email = [""]
36
+ s.homepage = "https://github.com/swagger-api/swagger-codegen"
37
+ s.summary = "Trakerr API Ruby Gem"
38
+ s.description = "Get your application events and errors to Trakerr via the *Trakerr API*."
39
+ s.license = "Apache-2.0"
40
+ s.required_ruby_version = '>= 1.9.3'
41
+
42
+ s.add_runtime_dependency 'typhoeus', '~> 1.0', '>= 1.0.1'
43
+ s.add_runtime_dependency 'json', '~> 1.8', '>= 1.8.3'
44
+
45
+ s.add_development_dependency 'rspec', '~> 3.4', '>= 3.4.0'
46
+ s.add_development_dependency 'vcr', '~> 3.0', '>= 3.0.1'
47
+ s.add_development_dependency 'webmock', '~> 1.24', '>= 1.24.3'
48
+ s.add_development_dependency 'autotest', '~> 4.4', '>= 4.4.6'
49
+ s.add_development_dependency 'autotest-rails-pure', '~> 4.1', '>= 4.1.2'
50
+ s.add_development_dependency 'autotest-growl', '~> 0.2', '>= 0.2.16'
51
+ s.add_development_dependency 'autotest-fsevent', '~> 0.2', '>= 0.2.11'
52
+
53
+ #s.files = `find *`.split("\n").uniq.sort.select{|f| !f.empty? }
54
+ s.files = `git ls-files`.split("\n").delete_if {|file| file.include? "spec"}
55
+ s.test_files = `git ls-files`.split("\n").delete_if {|file| not file.include? "spec"}
56
+ s.executables = []
57
+ s.require_paths = ["generated/lib", "trakerr/lib"]
58
+ end