OpenTelemetry Instrumentation Helpers: SQL Obfuscation

This Ruby gem contains logic to obfuscate SQL. It's intended for use by by gem authors instrumenting SQL adapter libraries, such as mysql2, pg, and trilogy.

The logic is largely drawn from the New Relic Ruby agent's SQL Obfuscation Helpers module.

Usage

Add the gem to your instrumentation's gemspec file:

# opentelemetry-instrumentation-your-gem.gemspec
  spec.add_dependency 'opentelemetry-helpers-sql-obfuscation'

Add the gem to your instrumentation's Gemfile:

# Gemfile

group :test, :development do
  gem 'opentelemetry-helpers-sql-obfuscation', path: '../../helpers/sql-obfuscation'
end

Make sure the Instrumentation class for your gem contains configuration options for:

  • :obfuscation_limit: the length at which the obfuscated SQL string will be truncated. Example: option :obfuscation_limit, default: 2000, validate: :integer

If you want to add support for a new adapter, update the following constants to include keys for your adapter:

  • DIALECT_COMPONENTS
  • CLEANUP_REGEX

You must also add a new constant that calls the generate_regex method with your adapter's DIALECT_COMPONENTS that is named like <ADAPTER>_COMPONENTS_REGEX, such as: MYSQL_COMPONENTS_REGEX.

Check New Relic's SQL Obfuscation Helpers module to see if regular expressions for your adapter already exist.

Examples

To obfuscate sql in your library:

OpenTelemetry::Helpers::SqlObfuscation.obfuscate_sql(sql, obfuscation_limit: config[:obfuscation_limit], adapter: :postgres)

How can I get involved?

The opentelemetry-helpers-sql-obfuscation gem source is on github, along with related gems including opentelemetry-instrumentation-pg and opentelemetry-instrumentation-trilogy.

The OpenTelemetry Ruby gems are maintained by the OpenTelemetry-Ruby special interest group (SIG). You can get involved by joining us on our gitter channel or attending our weekly meeting. See the meeting calendar for dates and times. For more information on this and other language SIGs, see the OpenTelemetry community page.

License

The opentelemetry-helpers-sql-obfuscation gem is distributed under the Apache 2.0 license. See LICENSE for more information.