Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
5 changes: 5 additions & 0 deletions instrumentation/trilogy/.rubocop.yml
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
inherit_from: ../../.rubocop.yml

Metrics/ModuleLength:
Exclude:
- "lib/opentelemetry/instrumentation/trilogy/patches/stable/client.rb"
- "lib/opentelemetry/instrumentation/trilogy/patches/dup/client.rb"
19 changes: 14 additions & 5 deletions instrumentation/trilogy/Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@
#
# SPDX-License-Identifier: Apache-2.0

appraise 'trilogy-2.9' do
gem 'trilogy', '~> 2.9.0'
end
# 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 |mode|
appraise "trilogy-2-#{mode}" do
gem 'trilogy', '~> 2.9'
end

appraise 'trilogy-latest' do
gem 'trilogy'
appraise "trilogy-latest-#{mode}" do
gem 'trilogy'
end
end
16 changes: 16 additions & 0 deletions instrumentation/trilogy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ The `opentelemetry-instrumentation-trilogy` gem source is [on github][repo-githu

The OpenTelemetry Ruby gems are maintained by the OpenTelemetry Ruby special interest group (SIG). You can get involved by joining us on our [GitHub Discussions][discussions-url], [Slack Channel][slack-channel] or attending our weekly meeting. See the [meeting calendar][community-meetings] for dates and times. For more information on this and other language SIGs, see the OpenTelemetry [community page][ruby-sig].

## Database semantic convention stability

In the OpenTelemetry ecosystem, database semantic conventions have now reached a stable state. However, the initial Trilogy 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, Trilogy instrumentation code comes in three patch versions: `dup`, `old`, and `stable`. These versions are identical except for the attributes they send. Any changes to Trilogy 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/).

## License

The `opentelemetry-instrumentation-trilogy` gem is distributed under the Apache 2.0 license. See [LICENSE][license-github] for more information.
Expand Down
8 changes: 8 additions & 0 deletions instrumentation/trilogy/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ require 'rubocop/rake_task'

RuboCop::RakeTask.new

# Set OTEL_SEMCONV_STABILITY_OPT_IN based on appraisal name
gemfile = ENV.fetch('BUNDLE_GEMFILE', '')
if gemfile.include?('stable')
ENV['OTEL_SEMCONV_STABILITY_OPT_IN'] = 'database'
elsif gemfile.include?('dup')
ENV['OTEL_SEMCONV_STABILITY_OPT_IN'] = 'database/dup'
end

Rake::TestTask.new :test do |t|
t.libs << 'test'
t.libs << 'lib'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,47 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
option :propagator, default: 'none', validate: %w[none tracecontext vitess]
option :record_exception, default: true, validate: :boolean

attr_reader :propagator
attr_reader :propagator, :semconv

private

def require_dependencies
require_relative 'patches/client'
@semconv = determine_semconv

case @semconv
when :old
require_relative 'patches/old/client'
when :stable
require_relative 'patches/stable/client'
when :dup
require_relative 'patches/dup/client'
end
end

def patch_client
::Trilogy.prepend(Patches::Client)
case @semconv
when :old
::Trilogy.prepend(Patches::Old::Client)
when :stable
::Trilogy.prepend(Patches::Stable::Client)
when :dup
::Trilogy.prepend(Patches::Dup::Client)
end
end

def determine_semconv
opt_in = ENV.fetch('OTEL_SEMCONV_STABILITY_OPT_IN', nil)
return :old if opt_in.nil?

opt_in_values = opt_in.split(',').map(&:strip)

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

def configure_propagator(config)
Expand Down

This file was deleted.

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

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

require 'opentelemetry-helpers-mysql'
require 'opentelemetry-helpers-sql-processor'

module OpenTelemetry
module Instrumentation
module Trilogy
module Patches
module Dup
# Module to prepend to Trilogy for instrumentation (emits both old and stable semantic conventions)
module Client
def initialize(options = {})
@connection_options = options # This is normally done by Trilogy#initialize
@_otel_database_name = connection_options&.dig(:database)
@_otel_base_attributes = _build_otel_base_attributes.freeze

tracer.in_span(
'connect',
attributes: client_attributes.merge!(OpenTelemetry::Instrumentation::Trilogy.attributes),
kind: :client,
record_exception: config[:record_exception]
) do |span|
super
rescue StandardError => e
set_error_attributes(span, e)
raise
end
end

def ping(...)
tracer.in_span(
'ping',
attributes: client_attributes.merge!(OpenTelemetry::Instrumentation::Trilogy.attributes),
kind: :client,
record_exception: config[:record_exception]
) do |span|
super
rescue StandardError => e
set_error_attributes(span, e)
raise
end
end

def query(sql)
context_attributes = OpenTelemetry::Instrumentation::Trilogy.attributes

tracer.in_span(
OpenTelemetry::Helpers::MySQL.database_span_name(
sql,
context_attributes[OpenTelemetry::SemanticConventions::Trace::DB_OPERATION] || context_attributes['db.operation.name'],
@_otel_database_name,
config
),
attributes: client_attributes(sql).merge!(context_attributes),
kind: :client,
record_exception: config[:record_exception]
) do |span, context|
if propagator && sql.frozen?
sql = +sql
propagator.inject(sql, context: context)
sql.freeze
elsif propagator
propagator.inject(sql, context: context)
end

super
rescue StandardError => e
set_error_attributes(span, e)
raise
end
end

private

def _build_otel_base_attributes
database_user = connection_options&.dig(:username)
mysql_host = connection_options&.fetch(:host, nil) || 'unknown sock'
mysql_port = connection_options&.dig(:port)

# Include both old and stable attributes
attributes = {
# Old conventions
::OpenTelemetry::SemanticConventions::Trace::DB_SYSTEM => 'mysql',
::OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME => mysql_host,
# Stable conventions
'db.system.name' => 'mysql',
'server.address' => mysql_host
}

attributes['server.port'] = mysql_port if mysql_port

# Database name (old: db.name, stable: db.namespace)
if @_otel_database_name
attributes[::OpenTelemetry::SemanticConventions::Trace::DB_NAME] = @_otel_database_name
attributes['db.namespace'] = @_otel_database_name
end

# db.user (old only - removed in stable)
attributes[::OpenTelemetry::SemanticConventions::Trace::DB_USER] = database_user if database_user

# peer.service (same in both)
attributes[::OpenTelemetry::SemanticConventions::Trace::PEER_SERVICE] = config[:peer_service] unless config[:peer_service].nil?
attributes
end

def client_attributes(sql = nil)
attributes = @_otel_base_attributes.dup

attributes['db.instance.id'] = @connected_host unless @connected_host.nil?

if sql
case config[:db_statement]
when :obfuscate
obfuscated = OpenTelemetry::Helpers::SqlProcessor.obfuscate_sql(sql, obfuscation_limit: config[:obfuscation_limit], adapter: :mysql)
# Old convention
attributes[::OpenTelemetry::SemanticConventions::Trace::DB_STATEMENT] = obfuscated
# Stable convention
attributes['db.query.text'] = obfuscated
when :include
# Old convention
attributes[::OpenTelemetry::SemanticConventions::Trace::DB_STATEMENT] = sql
# Stable convention
attributes['db.query.text'] = sql
end
end

attributes
end

def set_error_attributes(span, error)
span.set_attribute('error.type', error.class.name)
span.set_attribute('db.response.status_code', error.error_code.to_s) if error.error_code
end

def tracer
Trilogy::Instrumentation.instance.tracer
end

def config
Trilogy::Instrumentation.instance.config
end

def propagator
Trilogy::Instrumentation.instance.propagator
end
end
end
end
end
end
end
Loading
Loading