Skip to content

Commit

Permalink
Merge pull request #123 from solarwinds/NH-76676
Browse files Browse the repository at this point in the history
NH-76676: using noop for all possible error from our agent
  • Loading branch information
xuan-cao-swi authored Apr 8, 2024
2 parents 5e236b7 + 4995718 commit 948f11e
Show file tree
Hide file tree
Showing 18 changed files with 174 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ AllCops:
# or gems.rb file, RuboCop reads the final value from the lock file.) If the
# Ruby version is still unresolved, RuboCop will use the oldest officially
# supported Ruby version (currently Ruby 2.6).
TargetRubyVersion: ~
TargetRubyVersion: 3.0
# Determines if a notification for extension libraries should be shown when
# rubocop is run. Keys are the name of the extension, and values are an array
# of gems in the Gemfile that the extension is suggested for, if not already
Expand Down
3 changes: 2 additions & 1 deletion lib/solarwinds_apm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
begin
require 'solarwinds_apm/logger'
require 'solarwinds_apm/version'
require 'solarwinds_apm/noop'
require 'opentelemetry-api'
if ENV.fetch('SW_APM_ENABLED', 'true') == 'false'
SolarWindsAPM.logger.info '==================================================================='
SolarWindsAPM.logger.info 'SW_APM_ENABLED environment variable detected and was set to false. SolarWindsAPM disabled'
Expand Down Expand Up @@ -65,7 +67,6 @@
SolarWindsAPM.logger.warn '=============================================================='
end
else
require 'solarwinds_apm/noop'
SolarWindsAPM.logger.warn '=============================================================='
SolarWindsAPM.logger.warn 'SolarWindsAPM not loaded. SolarWinds APM disabled'
SolarWindsAPM.logger.warn 'Please check previous log messages.'
Expand Down
2 changes: 1 addition & 1 deletion lib/solarwinds_apm/api/opentelemetry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module OpenTelemetry
# end
#
# === Returns:
# * Objective
# * value returned by block
#
def in_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil, &block)
if block.nil?
Expand Down
23 changes: 22 additions & 1 deletion lib/solarwinds_apm/noop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,26 @@

require_relative './noop/context'
require_relative './noop/metadata'
require_relative './noop/reporter'
require_relative './noop/span'
require_relative './noop/api'

module SolarWindsAPM
include Oboe_metal
# Reporter noop
class Reporter
##
# noop version of :send_report
#
def self.send_report(event, with_system_timestamp: false); end

##
# noop version of :send_status
#
def self.send_status(event, context=nil, with_system_timestamp: false); end

##
# noop version of :start
#
def self.start; end
end
end
83 changes: 83 additions & 0 deletions lib/solarwinds_apm/noop/api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# © 2023 SolarWinds Worldwide, LLC. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at:http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

####
# noop version of SolarWindsAPM::API
#
module SolarWindsAPM
# API
module API
end
end

module NoopAPI
# Tracing
module Tracing
# (wait_milliseconds=3000, integer_response: false)
def solarwinds_ready?(*_args, **options)
options && options[:integer_response] ? 0 : false
end
end

# CurrentTraceInfo
module CurrentTraceInfo
def current_trace_info
TraceInfo.new
end

class TraceInfo
attr_reader :tracestring, :trace_id, :span_id, :trace_flags, :do_log

def initialize
@trace_id = '00000000000000000000000000000000'
@span_id = '0000000000000000'
@trace_flags = '00'
@tracestring = '00-00000000000000000000000000000000-0000000000000000-00'
@service_name = ''
@do_log = :never
end

def for_log
''
end

def hash_for_log
{}
end
end
end

# CustomMetrics
module CustomMetrics
def increment_metric(*)
false
end

def summary_metric(*)
false
end
end

# OpenTelemetry
module OpenTelemetry
def in_span(*)
yield if block_given?
end
end

# TransactionName
module TransactionName
def set_transaction_name(*)
true
end
end
end

SolarWindsAPM::API.extend(NoopAPI::Tracing)
SolarWindsAPM::API.extend(NoopAPI::CurrentTraceInfo)
SolarWindsAPM::API.extend(NoopAPI::CustomMetrics)
SolarWindsAPM::API.extend(NoopAPI::OpenTelemetry)
SolarWindsAPM::API.extend(NoopAPI::TransactionName)
15 changes: 13 additions & 2 deletions lib/solarwinds_apm/noop/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
####
# noop version of SolarWindsAPM::Context
#
module SolarWindsAPM
# module SolarWindsAPM
# end

module Oboe_metal # rubocop:disable Naming/ClassAndModuleCamelCase
# Context for noop
module Context
class Context
##
# noop version of :toString
# toString would return the current trace context as string
Expand All @@ -18,6 +21,14 @@ def self.toString
'99-00000000000000000000000000000000-0000000000000000-00'
end

def self.isReady(*)
false
end

def self.getDecisions(*)
[-1, -1, -1, 0, 0.0, 0.0, -1, -1, '', '', 4]
end

##
# noop version of :clear
#
Expand Down
7 changes: 5 additions & 2 deletions lib/solarwinds_apm/noop/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
####
# noop version of SolarWindsAPM::Metadata
#
#
module SolarWindsAPM
module Oboe_metal # rubocop:disable Naming/ClassAndModuleCamelCase
# Metadata
class Metadata
##
Expand All @@ -20,6 +19,10 @@ def self.makeRandom
Metadata.new
end

def self.fromString(*)
Metadata.new
end

def isValid
false
end
Expand Down
28 changes: 0 additions & 28 deletions lib/solarwinds_apm/noop/reporter.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/solarwinds_apm/noop/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
####
# noop version of SolarWindsAPM::Span
#
module SolarWindsAPM
module Oboe_metal # rubocop:disable Naming/ClassAndModuleCamelCase
# Span
class Span
##
Expand Down
31 changes: 31 additions & 0 deletions test/initest_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,34 @@ def $stdout.write(string)
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

$LOAD_PATH.unshift("#{Dir.pwd}/lib/")

def noop_shared_test
_(SolarWindsAPM::Reporter.respond_to?(:start)).must_equal true
_(SolarWindsAPM::Reporter.respond_to?(:send_status)).must_equal true
_(SolarWindsAPM::Reporter.respond_to?(:send_report)).must_equal true
_(SolarWindsAPM::Metadata.respond_to?(:makeRandom)).must_equal true
_(SolarWindsAPM::Span.respond_to?(:createHttpSpan)).must_equal true
_(SolarWindsAPM::Span.respond_to?(:createSpan)).must_equal true
_(SolarWindsAPM::Context.toString).must_equal '99-00000000000000000000000000000000-0000000000000000-00'

_(defined?(SolarWindsAPM::API)).must_equal 'constant'
_(SolarWindsAPM::API.solarwinds_ready?(300)).must_equal false
_(SolarWindsAPM::API.increment_metric).must_equal false
_(SolarWindsAPM::API.summary_metric).must_equal false
_(SolarWindsAPM::API.in_span).must_equal nil
_(SolarWindsAPM::API.set_transaction_name).must_equal true
_(SolarWindsAPM::API.current_trace_info.hash_for_log.to_s).must_equal '{}'
_(SolarWindsAPM::API.current_trace_info.for_log).must_equal ''
_(SolarWindsAPM::API.current_trace_info.tracestring).must_equal '00-00000000000000000000000000000000-0000000000000000-00'
_(SolarWindsAPM::API.current_trace_info.trace_flags).must_equal '00'
_(SolarWindsAPM::API.current_trace_info.span_id).must_equal '0000000000000000'
_(SolarWindsAPM::API.current_trace_info.trace_id).must_equal '00000000000000000000000000000000'
_(SolarWindsAPM::API.current_trace_info.do_log).must_equal :never

in_span_result = SolarWindsAPM::API.in_span('params') do |_span|
value = 1 + 1
value
end

_(in_span_result).must_equal 2
end
2 changes: 2 additions & 0 deletions test/solarwinds_apm/init_test/init_1_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@
ENV['SW_APM_ENABLED'] = 'false'
require './lib/solarwinds_apm'
assert_includes log_output.string, 'SW_APM_ENABLED environment variable detected and was set to false. SolarWindsAPM disabled'

noop_shared_test
end
end
2 changes: 2 additions & 0 deletions test/solarwinds_apm/init_test/init_2_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@

require './lib/solarwinds_apm'
assert_includes log_output.string, 'SW_APM_SERVICE_KEY problem. API Token in wrong format'

noop_shared_test
end
end
2 changes: 2 additions & 0 deletions test/solarwinds_apm/init_test/init_3_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@

require './lib/solarwinds_apm'
assert_includes log_output.string, 'SW_APM_SERVICE_KEY format problem. Service Name is missing.'

noop_shared_test
end
end
2 changes: 2 additions & 0 deletions test/solarwinds_apm/init_test/init_4_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@

require './lib/solarwinds_apm'
assert_includes log_output.string, 'SolarWindsAPM warning: Platform macos not yet supported on current solarwinds_apm'

noop_shared_test
end
end
2 changes: 2 additions & 0 deletions test/solarwinds_apm/init_test/init_5_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@

require './lib/solarwinds_apm'
assert_includes log_output.string, 'SW_APM_SERVICE_KEY format problem. Service Name is missing.'

noop_shared_test
end
end
2 changes: 2 additions & 0 deletions test/solarwinds_apm/init_test/init_6_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@

require './lib/solarwinds_apm'
assert_includes log_output.string, 'SW_APM_SERVICE_KEY not configured.'

noop_shared_test
end
end
6 changes: 1 addition & 5 deletions test/solarwinds_apm/init_test/init_7_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@

assert_nil(defined?(SolarWindsAPM.loaded))

# we don't load noop in case that the oboe c lib is missing
assert_nil(defined?(SolarWindsAPM::Reporter))
assert_nil(defined?(SolarWindsAPM::Metadata))
assert_nil(defined?(SolarWindsAPM::Span))
assert_nil(defined?(SolarWindsAPM::Context))
noop_shared_test
end
end
10 changes: 2 additions & 8 deletions test/solarwinds_apm/init_test/init_8_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,8 @@

_(SolarWindsAPM.loaded).must_equal false

_(SolarWindsAPM::Reporter.respond_to?(:start)).must_equal true
_(SolarWindsAPM::Reporter.respond_to?(:send_status)).must_equal true
_(SolarWindsAPM::Reporter.respond_to?(:send_report)).must_equal true
_(SolarWindsAPM::Metadata.respond_to?(:makeRandom)).must_equal true
_(SolarWindsAPM::Span.respond_to?(:createHttpSpan)).must_equal true
_(SolarWindsAPM::Span.respond_to?(:createSpan)).must_equal true
_(SolarWindsAPM::Context.toString).must_equal '99-00000000000000000000000000000000-0000000000000000-00'

FileUtils.rm("#{Dir.pwd}/lib/libsolarwinds_apm.so")

noop_shared_test
end
end

0 comments on commit 948f11e

Please sign in to comment.