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-94756: dbo for mysql2 #165

Merged
merged 8 commits into from
Nov 18, 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
2 changes: 1 addition & 1 deletion ext/oboe_metal/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
dir_config('oboe', 'src', 'lib')

# create Makefile
if have_library('oboe', 'oboe_config_get_revision', 'oboe.h')
if have_library('oboe')
$libs = append_library($libs, 'oboe')
$libs = append_library($libs, 'stdc++')

Expand Down
6 changes: 6 additions & 0 deletions lib/solarwinds_apm/oboe_init_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,14 @@ def appoptics_collector?
(allowed_uri.include? ENV.fetch('SW_APM_COLLECTOR', nil))
end

def java_collector?(uri)
java_collector_regex = /java-collector:\d+/
uri.match?(java_collector_regex)
end

def sanitize_collector_uri(uri)
return uri if uri.nil? || uri.empty?
return uri if java_collector?(uri)

begin
sanitized_uri = ::URI.parse("http://#{uri}").host
Expand Down
4 changes: 2 additions & 2 deletions lib/solarwinds_apm/opentelemetry/solarwinds_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def initialize(txn_manager)
# started span.
def on_start(span, parent_context)
SolarWindsAPM.logger.debug do
"[#{self.class}/#{__method__}] processor on_start span: #{span.inspect}, parent_context: #{parent_context.inspect}"
"[#{self.class}/#{__method__}] processor on_start span: #{span.to_span_data.inspect}, parent_context: #{parent_context.inspect}"
end

return if non_entry_span(parent_context: parent_context)
Expand All @@ -47,7 +47,7 @@ def on_start(span, parent_context)
#
# @param [Span] span the {Span} that just ended.
def on_finish(span)
SolarWindsAPM.logger.debug { "[#{self.class}/#{__method__}] processor on_finish span: #{span.inspect}" }
SolarWindsAPM.logger.debug { "[#{self.class}/#{__method__}] processor on_finish span: #{span.to_span_data.inspect}" }

return if non_entry_span(span: span)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ def extract(carrier, context: ::OpenTelemetry::Context.current, getter: ::OpenTe
def inject(carrier, context: ::OpenTelemetry::Context.current,
setter: ::OpenTelemetry::Context::Propagation.text_map_setter)
span_context = ::OpenTelemetry::Trace.current_span(context).context
SolarWindsAPM.logger.debug do
"[#{self.class}/#{__method__}] context: #{context.inspect}; span_context: #{span_context.inspect}"
end

SolarWindsAPM.logger.debug { "[#{self.class}/#{__method__}] context current_span: #{context.instance_variable_get(:@entries)&.values&.first.inspect}" }
SolarWindsAPM.logger.debug { "[#{self.class}/#{__method__}] span_context: #{span_context.inspect}" }

return unless span_context&.valid?

x_trace = Utils.traceparent_from_context(span_context)
Expand Down
10 changes: 10 additions & 0 deletions lib/solarwinds_apm/otel_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ def self.initialize
ENV['OTEL_LOG_LEVEL'] = SolarWindsAPM::Config::SW_LOG_LEVEL_MAPPING.dig(log_level, :otel)
end

# for dbo, traceparent injection as comments
require_relative 'patch/tag_sql_patch' if SolarWindsAPM::Config[:tag_sql]

::OpenTelemetry::SDK.configure { |c| c.use_all(@@config_map) }

validate_propagator(::OpenTelemetry.propagation.instance_variable_get(:@propagators))
Expand All @@ -126,6 +129,13 @@ def self.initialize

# configure sampler afterwards
::OpenTelemetry.tracer_provider.sampler = @@config[:sampler]

if ENV['SW_APM_AUTO_CONFIGURE'] == 'false'
SolarWindsAPM.logger.info '==================================================================='
SolarWindsAPM.logger.info "\e[1mSolarWindsAPM manual initialization was successful.\e[0m"
SolarWindsAPM.logger.info '==================================================================='
end

nil
end

Expand Down
34 changes: 34 additions & 0 deletions lib/solarwinds_apm/patch/tag_sql/sw_mysql2_patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0
xuan-cao-swi marked this conversation as resolved.
Show resolved Hide resolved

module SolarWindsAPM
module Patch
module TagSql
module SWOMysql2Patch
def query(sql, options = {})
current_span = ::OpenTelemetry::Trace.current_span

annotated_sql = ''
if current_span.context.trace_flags.sampled?
traceparent = SolarWindsAPM::Utils.traceparent_from_context(current_span.context)
annotated_traceparent = "/*traceparent='#{traceparent}'*/"
current_span.add_attributes({ 'sw.query_tag' => annotated_traceparent })
annotated_sql = "#{sql} #{annotated_traceparent}"
else
annotated_sql = sql
end

super(annotated_sql, options)
end
end
end
end
end

# need to prepend before mysql2 instrumentation prepend the original function
# after entire process, the call sequence will be:
# upstream instrumentation -> our patch -> original function
Mysql2::Client.prepend(SolarWindsAPM::Patch::TagSql::SWOMysql2Patch) if defined?(Mysql2::Client)
cheempz marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 7 additions & 0 deletions lib/solarwinds_apm/patch/tag_sql_patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0
xuan-cao-swi marked this conversation as resolved.
Show resolved Hide resolved

require_relative 'tag_sql/sw_mysql2_patch'
14 changes: 0 additions & 14 deletions lib/solarwinds_apm/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,3 @@
require_relative 'support/utils'
require_relative 'support/x_trace_options'
require_relative 'support/support_report'

if SolarWindsAPM::Config[:tag_sql]
if defined?(Rails)
if Rails.version < '7'
require_relative 'support/swomarginalia/railtie'
else
require_relative 'support/swomarginalia/comment'
require_relative 'support/swomarginalia/formatter' if Rails.version <= '7.1'
end
elsif defined?(ActiveRecord)
require_relative 'support/swomarginalia/load_swomarginalia'
SolarWindsAPM::SWOMarginalia::LoadSWOMarginalia.insert
end
end
2 changes: 1 addition & 1 deletion lib/solarwinds_apm/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module SolarWindsAPM
module Version
MAJOR = 6 # breaking,
MINOR = 1 # feature,
PATCH = 0 # fix => BFF
PATCH = 3 # fix => BFF
PRE = nil

STRING = [MAJOR, MINOR, PATCH, PRE].compact.join('.')
Expand Down
18 changes: 18 additions & 0 deletions test/solarwinds_apm/oboe_init_options_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,24 @@
_(sanitized_uri).must_equal 'google.ca.appoptics'
end

it 'return_original_uri_for_java_collector' do
uri = 'java-collector:12224'
sanitized_uri = SolarWindsAPM::OboeInitOptions.instance.send(:sanitize_collector_uri, uri)
_(sanitized_uri).must_equal 'java-collector:12224'

uri = 'java-collector:1'
sanitized_uri = SolarWindsAPM::OboeInitOptions.instance.send(:sanitize_collector_uri, uri)
_(sanitized_uri).must_equal 'java-collector:1'

uri = 'java-collector'
sanitized_uri = SolarWindsAPM::OboeInitOptions.instance.send(:sanitize_collector_uri, uri)
_(sanitized_uri).must_equal 'java-collector'

uri = 'java-collector:regexregex'
sanitized_uri = SolarWindsAPM::OboeInitOptions.instance.send(:sanitize_collector_uri, uri)
_(sanitized_uri).must_equal ''
end

it 'test_when_otel_service_name_exist' do
ENV['SW_APM_REPORTER'] = 'ssl'
ENV['OTEL_SERVICE_NAME'] = 'abcdef'
Expand Down