Skip to content

Commit 5f452ab

Browse files
authored
Merge pull request #1 from Optum/develop
v0.2.0
2 parents f455f30 + 15d03fe commit 5f452ab

File tree

8 files changed

+135
-3
lines changed

8 files changed

+135
-3
lines changed

.github/workflows/rubocop.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@ jobs:
2121
bash -c "
2222
rubocop --require code_scanning --format CodeScanning::SarifFormatter -o rubocop.sarif
2323
[[ $? -ne 2 ]]
24-
"
24+
"
25+
- name: Upload Sarif output
26+
uses: github/codeql-action/upload-sarif@v1
27+
with:
28+
sarif_file: rubocop.sarif

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Telemetry::Logger
22

3+
## v0.2.0
4+
* Adding new module called `Telemetry::Logger::ExceptionHandler`
5+
* Adding methods for `exception` and magic for detecting `ElasticAPM` and `OpenTelemetry`
6+
* Adding `Upload Sarif output` step to `rubocop` GitHub action
7+
* Adding `SECURITY.md` file
8+
39
## v0.1.1
410
* Fixing issue with color option using the wrong method name
511
* Removing format logger from the setup command

README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,49 @@
11
# Telemetry::Logger
22
A generic gem to handle logging for all other telemetry gems
33

4-
Example
4+
#### Setting up the logger
5+
```ruby
6+
Telemetry::Logger.setup(level: 'warn', color: false, log_file: './telemetry.log')
7+
8+
opts = {
9+
include_pid: false,
10+
level: 'info',
11+
log_file: nil,
12+
color: true,
13+
application: 'telemetry',
14+
app_version: Telemetry::Logger::VERSION
15+
}
16+
```
17+
18+
#### Example Logging
519
```ruby
620
Telemetry::Logger.setup(level: 'info')
721
Telemetry::Logger.info 'test info'
22+
823
Telemetry::Logger.debug 'test debug'
924
Telemetry::Logger.warn 'test warn'
1025
Telemetry::Logger.error 'test error'
1126
Telemetry::Logger.fatal 'test fatal'
1227
Telemetry::Logger.unknown 'test unknown'
1328
```
29+
30+
#### Example Exception Tracking
31+
Instead of repeating the same error method all over the place for exceptions, you can use the `exception` method to
32+
save on complexity and automatically report exceptions to supported APMs
33+
34+
```ruby
35+
Telemetry::Logger.setup(level: 'info')
36+
Telemetry::Logger.exception(StandardError.new('test error'), level: 'warn')
37+
Telemetry::Logger.exception(StandardError.new('test error'), level: 'fatal')
38+
Telemetry::Logger.exception(StandardError.new('test error'), handled: true)
39+
Telemetry::Logger.exception(StandardError.new('test error'), backtrace: true)
40+
41+
# options for exception method
42+
opts = {
43+
level: 'error', # sets the log level
44+
handled: true, # tells the apms if we handled this exception
45+
backtrace: true, # should we log the backtrace?
46+
backtrace_limit: 20, # how many lines should we limit the backtrace to?
47+
raise: false, # should we reraise this exception instead of swallowing it?
48+
}
49+
```

SECURITY.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Security Policy
2+
3+
## Supported Versions
4+
5+
Use this section to tell people about which versions of your project are
6+
currently being supported with security updates.
7+
8+
| Version | Supported |
9+
| ------- | ------------------ |
10+
| 0.x.x | :white_check_mark: |
11+
12+
13+
## Reporting a Vulnerability
14+
15+
Use this section to tell people how to report a vulnerability.
16+
17+
Tell them where to go, how often they can expect to get an update on a
18+
reported vulnerability, what to expect if the vulnerability is accepted or
19+
declined, etc.

lib/telemetry/logger.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22
require 'telemetry/logger/defaults'
33
require 'telemetry/logger/builder'
44
require 'telemetry/logger/methods'
5+
require 'telemetry/logger/exception_handler'
56

67
module Telemetry
78
module Logger
89
class << self
910
include Telemetry::Logger::Defaults
1011
include Telemetry::Logger::Methods
1112
include Telemetry::Logger::Builder
13+
include Telemetry::Logger::ExceptionHandler
1214

1315
def setup(level: 'info', **opts)
16+
@opts = opts
1417
output(**opts)
1518
self.log_level = level
1619
self
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module Telemetry
2+
module Logger
3+
module ExceptionHandler
4+
def exception(exc, level: 'error', backtrace: true, backtrace_limit: 20, **opts)
5+
level = 'unknown' unless %w[debug info warn error fatal].include? level.to_s
6+
Telemetry::Logger.send(level, "#{exc.class}: #{exc.message}")
7+
Telemetry::Logger.send(level, exc.backtrace[0..backtrace_limit]) if backtrace && !exc.backtrace.nil?
8+
9+
send_to_apm(exc, level: level)
10+
11+
raise(exc) if opts[:raise]
12+
end
13+
14+
def send_to_apm(exc, **opts)
15+
handled = opts[:handled] || %w[info debug].include?(opts[:level])
16+
::ElasticAPM.report(exc, handled: handled) if elastic_apm?
17+
::OpenTelemetry.handle_error(exception: exc, message: exc.message) if open_telemetry?
18+
end
19+
20+
def elastic_apm?
21+
@elastic_apm unless @elastic_apm.nil?
22+
23+
@elastic_apm = Kernel.const_defined? 'ElasticAPM'
24+
end
25+
26+
def open_telemetry?
27+
@open_telemetry unless @open_telemetry.nil?
28+
29+
@open_telemetry = Kernel.const_defined? 'OpenTelemetry'
30+
end
31+
end
32+
end
33+
end

lib/telemetry/logger/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Telemetry
22
module Logger
3-
VERSION = '0.1.1'.freeze
3+
VERSION = '0.2.0'.freeze
44
end
55
end

spec/exception_handler_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require 'spec_helper'
2+
require 'telemetry/logger/exception_handler'
3+
4+
RSpec.describe Telemetry::Logger::ExceptionHandler do
5+
it { should be_a Module }
6+
before :all do
7+
@class = Class.new do
8+
# include Telemetry::Logger::ExceptionHandler
9+
def initialize
10+
extend Telemetry::Logger::ExceptionHandler
11+
end
12+
end
13+
14+
Telemetry::Logger.log_level = 'debug'
15+
16+
@exception = StandardError.new('this is my test exception')
17+
end
18+
19+
it 'should be able to log exceptions' do
20+
expect(@class.new.exception(@exception))
21+
expect { @class.new.exception(@exception, level: 'debug') }.to output(/DEBUG/).to_stdout_from_any_process
22+
expect { @class.new.exception(@exception, level: 'info') }.to output(/INFO/).to_stdout_from_any_process
23+
expect { @class.new.exception(@exception, level: 'warn') }.to output(/WARN/).to_stdout_from_any_process
24+
expect { @class.new.exception(@exception, level: 'error') }.to output(/ERROR/).to_stdout_from_any_process
25+
expect { @class.new.exception(@exception, level: 'fatal') }.to output(/FATAL/).to_stdout_from_any_process
26+
end
27+
28+
it 'should be able to re raise an exception' do
29+
expect { @class.new.exception(StandardError.new('test'), raise: true) }.to raise_exception StandardError
30+
end
31+
end

0 commit comments

Comments
 (0)