Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NH-71431: refactor log msg and remove unused code #115

Merged
merged 6 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 14 additions & 48 deletions lib/oboe_metal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@
# Disable docs and Camelcase warns since we're implementing
# an interface here. See OboeBase for details.
module SolarWindsAPM
extend SolarWindsAPMBase
include Oboe_metal

@reporter = nil
@loaded = nil

class << self
attr_accessor :reporter, :loaded
end

# Reporter that send span data to SWO
class Reporter
class << self
Expand Down Expand Up @@ -53,52 +59,6 @@ def send_status(evt, context=nil, with_system_timestamp: true)
SolarWindsAPM.reporter.sendStatus(evt, context, with_system_timestamp)
end

##
# clear_all_traces
#
# Truncates the trace output file to zero
#
def clear_all_traces
File.truncate(SolarWindsAPM::OboeInitOptions.instance.host, 0)
end

##
# obtain_all_traces
#
# Retrieves all traces written to the trace file
#
def obtain_all_traces
io = File.open(SolarWindsAPM::OboeInitOptions.instance.host, 'r')
contents = io.readlines(nil)
io.close

return contents if contents.empty?

traces = []

# We use Gem.loaded_spec because older versions of the bson
# gem didn't even have a version embedded in the gem. If the
# gem isn't in the bundle, it should rightfully error out
# anyways.
#
if Gem.loaded_specs['bson'] && Gem.loaded_specs['bson'].version.to_s < '4.0'
s = StringIO.new(contents[0])

until s.eof?
traces << if ::BSON.respond_to? :read_bson_document
BSON.read_bson_document(s)
else
BSON::Document.from_bson(s)
end
end
else
bbb = ::BSON::ByteBuffer.new(contents[0])
traces << Hash.from_bson(bbb) until bbb.length == 0
end

traces
end

private

# Internal: Report that instrumentation for the given layer has been
Expand Down Expand Up @@ -217,4 +177,10 @@ def sample_rate(rate)
end

SolarWindsAPM.loaded = true
# rubocop:enable Style/Documentation
# rubocop:enable Style/Documentation

# Setup an alias
SolarWindsApm = SolarWindsAPM
SolarwindsApm = SolarWindsAPM
SolarwindsAPM = SolarWindsAPM
Solarwindsapm = SolarWindsAPM
98 changes: 51 additions & 47 deletions lib/solarwinds_apm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,73 @@
# 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.

begin
require 'solarwinds_apm/version'
require 'solarwinds_apm/logger'
if ENV.fetch('SW_APM_ENABLED', 'true') == 'false'
SolarWindsAPM.logger.warn 'SW_APM_ENABLED environment variable detected and was set to false; SolarWindsAPM disabled'
return
end

require 'solarwinds_apm/version'
require 'solarwinds_apm/thread_local'
require 'solarwinds_apm/support_report'
require 'solarwinds_apm/constants'
require 'solarwinds_apm/api'
require 'solarwinds_apm/base'
require 'solarwinds_apm/logger'
require 'solarwinds_apm/config'

SolarWindsAPM::Config.load_config_file
SolarWindsAPM.loaded = false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems safer to keep this initialization of loaded to false, any reason to remove?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I changed the structure of SolarWindsAPM module (oboe_metal.rb) that initialize the loaded as false; so its default will be false as always. It will change to true once reporter initialization process finished without any issue.

begin
if /linux/.match?(RUBY_PLATFORM)
require_relative './libsolarwinds_apm.so'
require 'solarwinds_apm/oboe_init_options'
require_relative './oboe_metal' # initialize Reporter; sets SolarWindsAPM.loaded = true if successful
require 'solarwinds_apm/config'
SolarWindsAPM::Config.load_config_file

require_relative './libsolarwinds_apm.so' # load c-lib oboe
require 'solarwinds_apm/oboe_init_options' # setup oboe reporter options
require_relative './oboe_metal' # initialize reporter: SolarWindsAPM.loaded = true

SolarWindsAPM.logger.info '==================================================================='
SolarWindsAPM.logger.info "SolarWindsAPM info: Platform #{RUBY_PLATFORM}."
xuan-cao-swi marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add a log message about the Ruby version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

SolarWindsAPM.logger.info "Current solarwinds_apm version: #{SolarWindsAPM::Version::STRING}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to also detect and log out the OTel API/SDK versions (and possibly the instrumentation-all version) installed, seems useful for troubleshooting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added. also updated the test page.

===================================================================
I, [2024-02-05T07:20:09.144844 #507]  INFO -- : Ruby 3.1.0 on platform aarch64-linux.
I, [2024-02-05T07:20:09.144853 #507]  INFO -- : Current solarwinds_apm version: 6.0.0.prev7.
I, [2024-02-05T07:20:09.144859 #507]  INFO -- : OpenTelemetry version: 1.3.1.
I, [2024-02-05T07:20:09.144872 #507]  INFO -- : OpenTelemetry instrumentation version: 0.56.0.
===================================================================

SolarWindsAPM.logger.info '==================================================================='

SolarWindsAPM::Reporter.start # start the reporter, any issue will be logged

if SolarWindsAPM.loaded
require 'solarwinds_apm/constants'
require 'solarwinds_apm/api'
require 'solarwinds_apm/support'
require 'solarwinds_apm/opentelemetry'
require 'solarwinds_apm/otel_config'

if ENV['SW_APM_AUTO_CONFIGURE'] != 'false'
SolarWindsAPM::OTelConfig.initialize
elsif ENV['SW_APM_AUTO_CONFIGURE'] == 'false'
SolarWindsAPM.logger.warn '=============================================================='
SolarWindsAPM.logger.warn 'SW_APM_AUTO_CONFIGURE set to false.'
SolarWindsAPM.logger.warn 'You need to initialize Ruby library in application with'
SolarWindsAPM.logger.warn 'SolarWindsAPM::OTelConfig.initialize_with_config do |config|'
SolarWindsAPM.logger.warn ' config[key] = value'
SolarWindsAPM.logger.warn 'end'
SolarWindsAPM.logger.warn 'See: https://github.com/solarwinds/apm-ruby/blob/main/CONFIGURATION.md#in-code-configuration'
SolarWindsAPM.logger.warn "\e[1mPlease discard this message if application have already taken this action.\e[0m"
SolarWindsAPM.logger.warn '=============================================================='
end
else
require 'solarwinds_apm/noop'
SolarWindsAPM.logger.warn '=============================================================='
SolarWindsAPM.logger.warn 'SolarWindsAPM not loaded. Tracing disabled.'
SolarWindsAPM.logger.warn 'There may be a problem with the service key or other settings.'
SolarWindsAPM.logger.warn 'Please check previous log messages.'
SolarWindsAPM.logger.warn '=============================================================='
end
else
SolarWindsAPM.logger.warn '==================================================================='
SolarWindsAPM.logger.warn "SolarWindsAPM warning: Platform #{RUBY_PLATFORM} not yet supported."
SolarWindsAPM.logger.warn "SolarWindsAPM warning: Platform #{RUBY_PLATFORM} not yet supported on current solarwinds_apm #{SolarWindsAPM::Version::STRING}"
SolarWindsAPM.logger.warn 'see: https://documentation.solarwinds.com/en/success_center/observability/default.htm#cshid=config-ruby-agent'
SolarWindsAPM.logger.warn 'Tracing disabled.'
SolarWindsAPM.logger.warn 'Contact technicalsupport@solarwinds.com if this is unexpected.'
SolarWindsAPM.logger.warn '==================================================================='
end
rescue LoadError => e
unless ENV['RAILS_GROUP'] == 'assets' || ENV['SW_APM_NO_LIBRARIES_WARNING']
SolarWindsAPM.logger.error '=============================================================='
SolarWindsAPM.logger.error 'Missing SolarWindsAPM libraries. Tracing disabled.'
SolarWindsAPM.logger.error "Error: #{e.message}"
SolarWindsAPM.logger.error 'See: https://documentation.solarwinds.com/en/success_center/observability/default.htm#cshid=config-ruby-agent'
SolarWindsAPM.logger.error '=============================================================='
end
end

if SolarWindsAPM.loaded
require 'solarwinds_apm/support'
require 'solarwinds_apm/opentelemetry'
require 'solarwinds_apm/otel_config'
if ENV['SW_APM_AUTO_CONFIGURE'] == 'false'
SolarWindsAPM.logger.warn "SolarWindsAPM warning: Ruby library is not initilaized.
You may need to initialize Ruby library in application like the following:
SolarWindsAPM::OTelConfig.initialize_with_config do |config|
...
end"
else
SolarWindsAPM::OTelConfig.initialize
end

SolarWindsAPM::Reporter.start
else
SolarWindsAPM.logger.warn '=============================================================='
SolarWindsAPM.logger.warn 'SolarWindsAPM not loaded. Tracing disabled.'
SolarWindsAPM.logger.warn 'There may be a problem with the service key or other settings.'
SolarWindsAPM.logger.warn 'Please check previous log messages.'
SolarWindsAPM.logger.warn '=============================================================='
require 'solarwinds_apm/noop/context'
require 'solarwinds_apm/noop/metadata'
SolarWindsAPM.logger.error '=============================================================='
SolarWindsAPM.logger.error 'Missing SolarWindsAPM libraries or components. Tracing disabled.'
SolarWindsAPM.logger.error "Error: #{e.message}"
SolarWindsAPM.logger.error 'See: https://documentation.solarwinds.com/en/success_center/observability/default.htm#cshid=config-ruby-agent'
SolarWindsAPM.logger.error '=============================================================='
end

rescue StandardError => e
$stderr.puts "[solarwinds_apm/error] Problem loading: #{e.inspect}"
$stderr.puts e.backtrace
Expand Down
2 changes: 1 addition & 1 deletion lib/solarwinds_apm/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ module API
extend SolarWindsAPM::API::OpenTelemetry
extend SolarWindsAPM::API::CustomMetrics
end
end
end
2 changes: 1 addition & 1 deletion lib/solarwinds_apm/api/opentelemetry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ def in_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil,
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/solarwinds_apm/api/tracing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ def solarwinds_ready?(wait_milliseconds=3000, integer_response: false)
end
end
end
end
end
3 changes: 2 additions & 1 deletion lib/solarwinds_apm/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ def self.symbol?(obj)
def self.print_config
SolarWindsAPM.logger.warn {"[#{name}/#{__method__}] General configurations list blow:"}
@@config.each do |k,v|
SolarWindsAPM.logger.warn {"[#{name}/#{__method__}] Config Key/Value: #{k}, #{v.inspect}"}
SolarWindsAPM.logger.warn {"[#{name}/#{__method__}] Config Key/Value: #{k}, #{v.inspect}"} unless @@instrumentation.include?(k)
end
nil
end

##
Expand Down
12 changes: 5 additions & 7 deletions lib/solarwinds_apm/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
module SolarWindsAPM
# Constants
module Constants
HTTP_METHOD = "http.method".freeze
HTTP_ROUTE = "http.route".freeze
HTTP_STATUS_CODE = "http.status_code".freeze
HTTP_URL = "http.url".freeze

HTTP_METHOD = "http.method".freeze
HTTP_ROUTE = "http.route".freeze
HTTP_STATUS_CODE = "http.status_code".freeze
HTTP_URL = "http.url".freeze
INTL_SWO_AO_COLLECTOR = "collector.appoptics.com".freeze
INTL_SWO_AO_STG_COLLECTOR = "collector-stg.appoptics.com".freeze
INTL_SWO_COMMA = ",".freeze
Expand All @@ -30,7 +29,6 @@ module Constants
INTL_SWO_OTEL_SCOPE_VERSION = "otel.scope.version".freeze
INTL_SWO_OTEL_STATUS = "otel.status".freeze
INTL_SWO_OTEL_STATUS_DESCRIPTION = "otel.status_description".freeze

INTERNAL_TRIGGERED_TRACE = "TriggeredTrace".freeze
end
end
end
10 changes: 10 additions & 0 deletions lib/solarwinds_apm/noop.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# © 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.

require_relative './noop/context'
require_relative './noop/metadata'
require_relative './noop/reporter'
require_relative './noop/span'
2 changes: 1 addition & 1 deletion lib/solarwinds_apm/noop/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ we load these classes when in noop mode and they expose noop behavior.

so far only one class is needed:

- SolarWindsAPM::Context and its toString() method from oboe
- SolarWindsAPM::Context and its toString() method from oboe
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,25 @@
#
# 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
##
# Provides thread local storage for SolarWindsAPM.
#
# Example usage:
# module SolarWindsAPMBase
# extend SolarWindsAPM::ThreadLocal
# thread_local :layer_op
# end
module ThreadLocal
def thread_local(name)
key = "__#{self}_#{name}__".intern
# Reporter
class Reporter
##
# noop version of :send_report
#
def self.send_report(event, with_system_timestamp: false); end

define_method(name) do
Thread.current[key]
end
##
# noop version of :send_status
#
def self.send_status(event, context=nil, with_system_timestamp: false); end

define_method("#{name}=") do |value|
Thread.current[key] = value
end
end
##
# noop version of :start
#
def self.start; end
end
end
29 changes: 14 additions & 15 deletions lib/solarwinds_apm/base.rb → lib/solarwinds_apm/noop/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@
#
# 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.

##
# This module is the base module for SolarWindsAPM reporting.
####
# noop version of SolarWindsAPM::Span
#
module SolarWindsAPMBase
extend SolarWindsAPM::ThreadLocal

attr_accessor :reporter, :loaded
end

module SolarWindsAPM
extend SolarWindsAPMBase
end
# Span
class Span
##
# noop version of :createHttpSpan
#
def self.createHttpSpan(*); end

# Setup an alias
SolarWindsApm = SolarWindsAPM
SolarwindsApm = SolarWindsAPM
SolarwindsAPM = SolarWindsAPM
Solarwindsapm = SolarWindsAPM
##
# noop version of :createSpan
#
def self.createSpan(*); end
end
end
2 changes: 1 addition & 1 deletion lib/solarwinds_apm/opentelemetry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
require_relative './opentelemetry/solarwinds_processor'
require_relative './opentelemetry/solarwinds_sampler'
require_relative './opentelemetry/solarwinds_exporter'
require_relative './opentelemetry/solarwinds_response_propagator'
require_relative './opentelemetry/solarwinds_response_propagator'
2 changes: 1 addition & 1 deletion lib/solarwinds_apm/opentelemetry/solarwinds_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,4 @@ def calculate_span_time(start_time: nil, end_time: nil)
end
end
end
end
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why github auto-remove the newline...

2 changes: 1 addition & 1 deletion lib/solarwinds_apm/opentelemetry/solarwinds_sampler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,4 @@ def otel_sampled?(otel_decision)
end
end
end
end
end
Loading