Skip to content

Commit d1eea99

Browse files
committed
[sdk] add log_info and log_exception to the SDK
[AO-11876]
1 parent ed95a64 commit d1eea99

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

lib/appoptics_apm/api.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module API
1313
require_relative './sdk/tracing'
1414
require_relative './sdk/custom_metrics'
1515
require_relative './sdk/current_trace'
16+
require_relative './sdk/logging' # to make sure it is loaded <- not very elegant
1617

1718
extend AppOpticsAPM::SDK::Tracing
1819
extend AppOpticsAPM::SDK::CustomMetrics

lib/appoptics_apm/sdk/logging.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright (c) 2019 SolarWinds, LLC.
2+
# All rights reserved.
3+
#
4+
5+
module AppOpticsAPM
6+
module SDK
7+
module Logging
8+
9+
# Log an information event in the current span
10+
#
11+
# a possible use-case is to collect extra information during the execution of a request
12+
#
13+
# === Arguments:
14+
# * +opts+ - (optional) hash containing key/value pairs that will be reported with this span.
15+
#
16+
def log_info(opts)
17+
AppOpticsAPM::API.log_info(AppOpticsAPM.layer, opts)
18+
end
19+
20+
# Log an exception/error event in the current span
21+
#
22+
# this may be helpful to track problems while rescuing an exception
23+
#
24+
# === Arguments:
25+
# * +exception+ - an exception, must respond to :message and :backtrace
26+
# * +opts+ - (optional) hash containing key/value pairs that will be reported with this span.
27+
#
28+
def log_exception(exception, opts)
29+
AppOpticsAPM::API.log_exception(AppOpticsAPM.layer, exception, opts)
30+
end
31+
32+
end
33+
34+
extend Logging
35+
36+
end
37+
end

test/unit/api_sdk/sdk_test.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,41 @@ def computation_with_appoptics(n)
409409
end
410410
end
411411

412+
describe 'log events' do
413+
before do
414+
clear_all_traces
415+
end
416+
417+
after do
418+
clear_all_traces
419+
AppOpticsAPM::Context.clear
420+
end
421+
422+
it 'SDK should log exceptions' do
423+
AppOpticsAPM::SDK.start_trace('test_01') do
424+
AppOpticsAPM::SDK.log_exception( StandardError.new, { the: 'exception' })
425+
end
426+
427+
traces = get_all_traces
428+
traces.size == 3
429+
430+
traces[1]['Label'].must_equal 'error'
431+
traces[1]['the'].must_equal 'exception'
432+
end
433+
434+
it 'SDK should log info' do
435+
AppOpticsAPM::SDK.start_trace('test_01') do
436+
AppOpticsAPM::SDK.log_info( { the: 'information' })
437+
end
438+
439+
traces = get_all_traces
440+
traces.size == 3
441+
442+
traces[1]['Label'].must_equal 'info'
443+
traces[1]['the'].must_equal 'information'
444+
end
445+
end
446+
412447
describe 'set_transaction_name' do
413448

414449
it 'should not set the transaction name if the arg is not a string or an empty string' do

0 commit comments

Comments
 (0)