diff --git a/.rubocop.yml b/.rubocop.yml index b9e9455a..81641631 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -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 diff --git a/lib/solarwinds_apm.rb b/lib/solarwinds_apm.rb index 879bacf0..b055617f 100644 --- a/lib/solarwinds_apm.rb +++ b/lib/solarwinds_apm.rb @@ -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' @@ -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.' diff --git a/lib/solarwinds_apm/api/opentelemetry.rb b/lib/solarwinds_apm/api/opentelemetry.rb index b9938fe4..c5938086 100644 --- a/lib/solarwinds_apm/api/opentelemetry.rb +++ b/lib/solarwinds_apm/api/opentelemetry.rb @@ -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? diff --git a/lib/solarwinds_apm/noop.rb b/lib/solarwinds_apm/noop.rb index 184d8318..501a7983 100644 --- a/lib/solarwinds_apm/noop.rb +++ b/lib/solarwinds_apm/noop.rb @@ -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 \ No newline at end of file diff --git a/lib/solarwinds_apm/noop/api.rb b/lib/solarwinds_apm/noop/api.rb new file mode 100644 index 00000000..7ea39f93 --- /dev/null +++ b/lib/solarwinds_apm/noop/api.rb @@ -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) diff --git a/lib/solarwinds_apm/noop/context.rb b/lib/solarwinds_apm/noop/context.rb index 6b1ed8fe..08433c16 100644 --- a/lib/solarwinds_apm/noop/context.rb +++ b/lib/solarwinds_apm/noop/context.rb @@ -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 @@ -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 # diff --git a/lib/solarwinds_apm/noop/metadata.rb b/lib/solarwinds_apm/noop/metadata.rb index d8529def..85d2ce67 100644 --- a/lib/solarwinds_apm/noop/metadata.rb +++ b/lib/solarwinds_apm/noop/metadata.rb @@ -7,8 +7,7 @@ #### # noop version of SolarWindsAPM::Metadata # -# -module SolarWindsAPM +module Oboe_metal # rubocop:disable Naming/ClassAndModuleCamelCase # Metadata class Metadata ## @@ -20,6 +19,10 @@ def self.makeRandom Metadata.new end + def self.fromString(*) + Metadata.new + end + def isValid false end diff --git a/lib/solarwinds_apm/noop/reporter.rb b/lib/solarwinds_apm/noop/reporter.rb deleted file mode 100644 index 01768fc9..00000000 --- a/lib/solarwinds_apm/noop/reporter.rb +++ /dev/null @@ -1,28 +0,0 @@ -# © 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::Reporter -# -module SolarWindsAPM - # Reporter - 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 diff --git a/lib/solarwinds_apm/noop/span.rb b/lib/solarwinds_apm/noop/span.rb index 59aaea26..09477d2c 100644 --- a/lib/solarwinds_apm/noop/span.rb +++ b/lib/solarwinds_apm/noop/span.rb @@ -7,7 +7,7 @@ #### # noop version of SolarWindsAPM::Span # -module SolarWindsAPM +module Oboe_metal # rubocop:disable Naming/ClassAndModuleCamelCase # Span class Span ## diff --git a/test/initest_helper.rb b/test/initest_helper.rb index 6d044481..aa2925d2 100644 --- a/test/initest_helper.rb +++ b/test/initest_helper.rb @@ -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 diff --git a/test/solarwinds_apm/init_test/init_1_test.rb b/test/solarwinds_apm/init_test/init_1_test.rb index 414c90a3..c07a1b24 100644 --- a/test/solarwinds_apm/init_test/init_1_test.rb +++ b/test/solarwinds_apm/init_test/init_1_test.rb @@ -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 diff --git a/test/solarwinds_apm/init_test/init_2_test.rb b/test/solarwinds_apm/init_test/init_2_test.rb index 7299d62a..8f06ce32 100644 --- a/test/solarwinds_apm/init_test/init_2_test.rb +++ b/test/solarwinds_apm/init_test/init_2_test.rb @@ -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 diff --git a/test/solarwinds_apm/init_test/init_3_test.rb b/test/solarwinds_apm/init_test/init_3_test.rb index 59f045a1..ba622293 100644 --- a/test/solarwinds_apm/init_test/init_3_test.rb +++ b/test/solarwinds_apm/init_test/init_3_test.rb @@ -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 diff --git a/test/solarwinds_apm/init_test/init_4_test.rb b/test/solarwinds_apm/init_test/init_4_test.rb index 3cc6cb61..4eb30153 100644 --- a/test/solarwinds_apm/init_test/init_4_test.rb +++ b/test/solarwinds_apm/init_test/init_4_test.rb @@ -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 diff --git a/test/solarwinds_apm/init_test/init_5_test.rb b/test/solarwinds_apm/init_test/init_5_test.rb index 3653e7b6..9c35b3b6 100644 --- a/test/solarwinds_apm/init_test/init_5_test.rb +++ b/test/solarwinds_apm/init_test/init_5_test.rb @@ -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 diff --git a/test/solarwinds_apm/init_test/init_6_test.rb b/test/solarwinds_apm/init_test/init_6_test.rb index bef1482f..7ff707fc 100644 --- a/test/solarwinds_apm/init_test/init_6_test.rb +++ b/test/solarwinds_apm/init_test/init_6_test.rb @@ -15,5 +15,7 @@ require './lib/solarwinds_apm' assert_includes log_output.string, 'SW_APM_SERVICE_KEY not configured.' + + noop_shared_test end end diff --git a/test/solarwinds_apm/init_test/init_7_test.rb b/test/solarwinds_apm/init_test/init_7_test.rb index 8b31e70b..eb18d1df 100644 --- a/test/solarwinds_apm/init_test/init_7_test.rb +++ b/test/solarwinds_apm/init_test/init_7_test.rb @@ -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 diff --git a/test/solarwinds_apm/init_test/init_8_test.rb b/test/solarwinds_apm/init_test/init_8_test.rb index 67266ee6..22065e7b 100644 --- a/test/solarwinds_apm/init_test/init_8_test.rb +++ b/test/solarwinds_apm/init_test/init_8_test.rb @@ -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