-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #165 from solarwinds/NH-94756
NH-94756: dbo for mysql2
- Loading branch information
Showing
14 changed files
with
195 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
# © 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. | ||
|
||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
# © 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 'tag_sql/sw_mysql2_patch' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright (c) 2023 SolarWinds, LLC. | ||
# All rights reserved. | ||
|
||
require 'minitest_helper' | ||
require './lib/solarwinds_apm/config' | ||
require './lib/solarwinds_apm/opentelemetry' | ||
require './lib/solarwinds_apm/support/txn_name_manager' | ||
require './lib/solarwinds_apm/otel_config' | ||
require './lib/solarwinds_apm/api' | ||
require './lib/solarwinds_apm/support' | ||
require './lib/solarwinds_apm/constants' | ||
require './lib/solarwinds_apm/oboe_init_options' | ||
|
||
# rubocop:disable Naming/MethodName | ||
module SolarWindsAPM | ||
module Span | ||
def self.createSpan(trans_name, domain, span_time, has_error); end | ||
end | ||
end | ||
# rubocop:enable Naming/MethodName | ||
|
||
describe 'mysql2 patch test' do | ||
puts "\n\033[1m=== TEST RUN: #{RUBY_VERSION} #{File.basename(__FILE__)} #{Time.now.strftime('%Y-%m-%d %H:%M')} ===\033[0m\n" | ||
|
||
let(:sdk) { OpenTelemetry::SDK } | ||
let(:exporter) { sdk::Trace::Export::InMemorySpanExporter.new } | ||
let(:span_processor) { sdk::Trace::Export::SimpleSpanProcessor.new(exporter) } | ||
let(:finished_spans) { exporter.finished_spans } | ||
|
||
it 'tag_sql_mysql2_integrate_test' do | ||
require './lib/solarwinds_apm/patch/tag_sql/sw_mysql2_patch' | ||
|
||
OpenTelemetry::SDK.configure(&:use_all) | ||
OpenTelemetry.tracer_provider.add_span_processor(span_processor) | ||
|
||
client_ancestors = Mysql2::Client.ancestors | ||
_(client_ancestors[0]).must_equal OpenTelemetry::Instrumentation::Mysql2::Patches::Client | ||
_(client_ancestors[1]).must_equal SolarWindsAPM::Patch::TagSql::SWOMysql2Patch | ||
_(client_ancestors[2]).must_equal Mysql2::Client | ||
|
||
mysql2_client = Mysql2::Client.new | ||
sql = mysql2_client.query('SELECT * FROM ABC;') | ||
|
||
pattern = %r{/\*traceparent='[\da-f-]+'*\*/$} | ||
assert_match pattern, finished_spans[0].attributes['sw.query_tag'], "Doesn't match sw.query_tag" | ||
|
||
pattern = %r{^SELECT \* FROM ABC;\s+/\*traceparent='[\da-f-]+'*\*/$} | ||
assert_match pattern, sql, "Sql doesn't contain traceparent" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright (c) 2023 SolarWinds, LLC. | ||
# All rights reserved. | ||
|
||
require 'minitest_helper' | ||
require './lib/solarwinds_apm/config' | ||
require './lib/solarwinds_apm/opentelemetry' | ||
require './lib/solarwinds_apm/support/txn_name_manager' | ||
require './lib/solarwinds_apm/otel_config' | ||
|
||
describe 'mysql2 patch test' do | ||
puts "\n\033[1m=== TEST RUN: #{RUBY_VERSION} #{File.basename(__FILE__)} #{Time.now.strftime('%Y-%m-%d %H:%M')} ===\033[0m\n" | ||
|
||
it 'mysql_patch_order_test_when_tag_sql_is_false' do | ||
SolarWindsAPM::Config[:tag_sql] = false | ||
SolarWindsAPM::OTelConfig.initialize | ||
|
||
client_ancestors = Mysql2::Client.ancestors | ||
_(client_ancestors[0]).must_equal OpenTelemetry::Instrumentation::Mysql2::Patches::Client | ||
_(client_ancestors[1]).must_equal Mysql2::Client | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters