Skip to content

feat: MySQL2 semantic convention stability migration #1621

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
12 changes: 10 additions & 2 deletions instrumentation/mysql2/Appraisals
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# frozen_string_literal: true

appraise 'mysql2-0.4.0' do
gem 'mysql2'
# To facilitate database semantic convention stability migration, we are using
# appraisal to test the different semantic convention modes along with different
# gem versions. For more information on the semantic convention modes, see:
# https://opentelemetry.io/docs/specs/semconv/non-normative/db-migration/

semconv_stability = %w[old stable dup]
semconv_stability.each do |stability|
appraise "mysql2-0.4.0-#{stability}" do
gem 'mysql2'
end
end
16 changes: 16 additions & 0 deletions instrumentation/mysql2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,19 @@ The `opentelemetry-instrumentation-mysql2` gem is distributed under the Apache 2
[community-meetings]: https://github.com/open-telemetry/community#community-meetings
[slack-channel]: https://cloud-native.slack.com/archives/C01NWKKMKMY
[discussions-url]: https://github.com/open-telemetry/opentelemetry-ruby/discussions

## Database semantic convention stability

In the OpenTelemetry ecosystem, database semantic conventions have now reached a stable state. However, the initial Mysql2 instrumentation was introduced before this stability was achieved, which resulted in database attributes being based on an older version of the semantic conventions.

To facilitate the migration to stable semantic conventions, you can use the `OTEL_SEMCONV_STABILITY_OPT_IN` environment variable. This variable allows you to opt-in to the new stable conventions, ensuring compatibility and future-proofing your instrumentation.

When setting the value for `OTEL_SEMCONV_STABILITY_OPT_IN`, you can specify which conventions you wish to adopt:

- `database` - Emits the stable database and networking conventions and ceases emitting the old conventions previously emitted by the instrumentation.
- `database/dup` - Emits both the old and stable database and networking conventions, enabling a phased rollout of the stable semantic conventions.
- Default behavior (in the absence of either value) is to continue emitting the old database and networking conventions the instrumentation previously emitted.

During the transition from old to stable conventions, Mysql2 instrumentation code comes in three patch versions: `dup`, `old`, and `stable`. These versions are identical except for the attributes they send. Any changes to Mysql2 instrumentation should consider all three patches.

For additional information on migration, please refer to our [documentation](https://opentelemetry.io/docs/specs/semconv/non-normative/db-migration/).
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ module Mysql2
# instrumentation
class Instrumentation < OpenTelemetry::Instrumentation::Base
install do |_config|
require_dependencies
patch_client
patch_type = determine_semconv
send(:"require_dependencies_#{patch_type}")
send(:"patch_client_#{patch_type}")
end

present do
Expand All @@ -26,12 +27,41 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base

private

def require_dependencies
require_relative 'patches/client'
def determine_semconv
stability_opt_in = ENV.fetch('OTEL_SEMCONV_STABILITY_OPT_IN', '')
values = stability_opt_in.split(',').map(&:strip)

if values.include?('database/dup')
'dup'
elsif values.include?('database')
'stable'
else
'old'
end
end

def require_dependencies_dup
require_relative 'patches/dup/client'
end

def require_dependencies_old
require_relative 'patches/old/client'
end

def require_dependencies_stable
require_relative 'patches/stable/client'
end

def patch_client_dup
::Mysql2::Client.prepend(Patches::Dup::Client)
end

def patch_client_old
::Mysql2::Client.prepend(Patches::Old::Client)
end

def patch_client
::Mysql2::Client.prepend(Patches::Client)
def patch_client_stable
::Mysql2::Client.prepend(Patches::Stable::Client)
end
end
end
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

require 'opentelemetry-helpers-mysql'
require 'opentelemetry-helpers-sql-obfuscation'

module OpenTelemetry
module Instrumentation
module Mysql2
module Patches
module Dup
# Module to prepend to Mysql2::Client for instrumentation
module Client
def query(sql, options = {})
tracer.in_span(
_otel_span_name(sql),
attributes: _otel_span_attributes(sql),
kind: :client
) do
super
end
end

def prepare(sql)
tracer.in_span(
_otel_span_name(sql),
attributes: _otel_span_attributes(sql),
kind: :client
) do
super
end
end

private

def _otel_span_name(sql)
OpenTelemetry::Helpers::MySQL.database_span_name(
sql,
OpenTelemetry::Instrumentation::Mysql2.attributes[
'db.operation.name'
],
_otel_database_name,
config
)
end

def _otel_span_attributes(sql)
attributes = _otel_client_attributes
case config[:db_statement]
when :include
attributes[SemanticConventions::Trace::DB_STATEMENT] = sql
attributes['db.query.text'] = sql
when :obfuscate
attributes[SemanticConventions::Trace::DB_STATEMENT] =
OpenTelemetry::Helpers::SqlObfuscation.obfuscate_sql(
sql, obfuscation_limit: config[:obfuscation_limit], adapter: :mysql
)
attributes['db.query.text'] =
OpenTelemetry::Helpers::SqlObfuscation.obfuscate_sql(
sql, obfuscation_limit: config[:obfuscation_limit], adapter: :mysql
)
end

attributes.merge!(OpenTelemetry::Instrumentation::Mysql2.attributes)
attributes.compact!
attributes
end

def _otel_database_name
# https://github.com/brianmario/mysql2/blob/ca08712c6c8ea672df658bb25b931fea22555f27/lib/mysql2/client.rb#L78
(query_options[:database] || query_options[:dbname] || query_options[:db])&.to_s
end

def _otel_client_attributes
# The client specific attributes can be found via the query_options instance variable
# exposed on the mysql2 Client
# https://github.com/brianmario/mysql2/blob/ca08712c6c8ea672df658bb25b931fea22555f27/lib/mysql2/client.rb#L25-L26
host = (query_options[:host] || query_options[:hostname]).to_s
port = query_options[:port].to_s

attributes = {
SemanticConventions::Trace::DB_SYSTEM => 'mysql',
SemanticConventions::Trace::NET_PEER_NAME => host,
SemanticConventions::Trace::NET_PEER_PORT => port,
'db.system.name' => 'mysql',
'server.address' => host,
'server.port' => port
}

attributes[SemanticConventions::Trace::DB_NAME] = _otel_database_name
attributes['db.namespace'] = _otel_database_name
attributes[SemanticConventions::Trace::PEER_SERVICE] = config[:peer_service]
attributes
end

def tracer
Mysql2::Instrumentation.instance.tracer
end

def config
Mysql2::Instrumentation.instance.config
end
end
end
end
end
end
end
Loading
Loading