Skip to content

Commit

Permalink
add three basic test case for myql2 patch
Browse files Browse the repository at this point in the history
  • Loading branch information
xuan-cao-swi committed Nov 8, 2024
1 parent 2f3a0f1 commit 67494f2
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
15 changes: 15 additions & 0 deletions test/minitest_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,18 @@ def stub_for_mkmf_test
end
end
end

# For dbo patching test
module Mysql2
class Client
attr_reader :query_options

def initialize(_opts = {})
@query_options = {}
end

def query(sql, _options = {})
sql
end
end
end
52 changes: 52 additions & 0 deletions test/patch/sw_mysql2_patch_in_span_test.rb
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/utils'

describe 'mysql2 patch test' do
it 'mysql2_call_chain_include_patch' do
SolarWindsAPM::Config[:tag_sql] = true
SolarWindsAPM::OTelConfig.initialize

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
end

describe 'tag_sql_mysql2_test_in_span' do
let(:sdk) { OpenTelemetry::SDK }
let(:exporter) { sdk::Trace::Export::InMemorySpanExporter.new }
let(:span_processor) { sdk::Trace::Export::SimpleSpanProcessor.new(exporter) }
let(:provider) do
OpenTelemetry.tracer_provider = sdk::Trace::TracerProvider.new.tap do |provider|
provider.add_span_processor(span_processor)
end
end
let(:tracer) { provider.tracer(__FILE__, sdk::VERSION) }
let(:parent_context) { OpenTelemetry::Context.empty }
let(:finished_spans) { exporter.finished_spans }

it 'mysql_patch_order_test_with_in_span' do
SolarWindsAPM::Config[:tag_sql] = true
SolarWindsAPM::OTelConfig.initialize

OpenTelemetry::Context.with_current(parent_context) do
tracer.in_span('root') do
mysql2_client = Mysql2::Client.new
sql = mysql2_client.query('SELECT * FROM ABC;')
pattern = %r{^SELECT \* FROM ABC;\s+/\*traceparent='[\da-f-]+'*\*/$}
assert_match pattern, sql, "Sql dones't contain traceparent"
end
end
end
end
end
21 changes: 21 additions & 0 deletions test/patch/sw_mysql2_patch_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 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
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

0 comments on commit 67494f2

Please sign in to comment.