Skip to content

Commit

Permalink
Merge branch 'main' into sql_obfuscation_rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
kaylareopelle authored Feb 20, 2024
2 parents 230a5a0 + 347a286 commit 3ab179e
Show file tree
Hide file tree
Showing 23 changed files with 272 additions and 82 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci-instrumentation-canary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: "Test Ruby 3.3"
# BLOCKED BY: https://github.com/bigcommerce/gruf/pull/197
if: "${{ matrix.gem != 'gruf' }}"
uses: ./.github/actions/test_gem
with:
gem: "opentelemetry-instrumentation-${{ matrix.gem }}"
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/ci-instrumentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: "Test Ruby 3.3"
# BLOCKED BY: https://github.com/bigcommerce/gruf/pull/197
if: "${{ matrix.gem != 'gruf' }}"
uses: ./.github/actions/test_gem
with:
gem: "opentelemetry-instrumentation-${{ matrix.gem }}"
Expand Down
8 changes: 8 additions & 0 deletions instrumentation/all/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Release History: opentelemetry-instrumentation-all

### v0.59.0 / 2024-02-16

* BREAKING CHANGE: GraphQL Legacy Tracer perf improvements [#867](https://github.com/open-telemetry/opentelemetry-ruby-contrib/pull/867).

### v0.58.0 / 2024-02-15

* CHANGED: upgrade mysql2 instrumentation

### v0.57.0 / 2024-02-08

* BREAKING CHANGE: Move shared sql behavior to helper gems
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
module OpenTelemetry
module Instrumentation
module All
VERSION = '0.57.0'
VERSION = '0.59.0'
end
end
end
4 changes: 2 additions & 2 deletions instrumentation/all/opentelemetry-instrumentation-all.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ Gem::Specification.new do |spec|
spec.add_dependency 'opentelemetry-instrumentation-excon', '~> 0.22.0'
spec.add_dependency 'opentelemetry-instrumentation-faraday', '~> 0.23.1'
spec.add_dependency 'opentelemetry-instrumentation-grape', '~> 0.1.3'
spec.add_dependency 'opentelemetry-instrumentation-graphql', '~> 0.27.0'
spec.add_dependency 'opentelemetry-instrumentation-graphql', '~> 0.28.0'
spec.add_dependency 'opentelemetry-instrumentation-gruf', '~> 0.1.0'
spec.add_dependency 'opentelemetry-instrumentation-http', '~> 0.23.1'
spec.add_dependency 'opentelemetry-instrumentation-http_client', '~> 0.22.1'
spec.add_dependency 'opentelemetry-instrumentation-koala', '~> 0.20.1'
spec.add_dependency 'opentelemetry-instrumentation-lmdb', '~> 0.22.1'
spec.add_dependency 'opentelemetry-instrumentation-mongo', '~> 0.22.1'
spec.add_dependency 'opentelemetry-instrumentation-mysql2', '~> 0.26.0'
spec.add_dependency 'opentelemetry-instrumentation-mysql2', '~> 0.27.0'
spec.add_dependency 'opentelemetry-instrumentation-net_http', '~> 0.22.1'
spec.add_dependency 'opentelemetry-instrumentation-pg', '~> 0.27.0'
spec.add_dependency 'opentelemetry-instrumentation-que', '~> 0.8.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
defined?(::Faraday)
end

option :span_kind, default: :client, validate: %i[client internal]
option :peer_service, default: nil, validate: :string

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ class TracerMiddleware < ::Faraday::Middleware

def call(env)
http_method = HTTP_METHODS_SYMBOL_TO_STRING[env.method]
config = Faraday::Instrumentation.instance.config

attributes = span_creation_attributes(
http_method: http_method, url: env.url
http_method: http_method, url: env.url, config: config
)
tracer.in_span(
"HTTP #{http_method}", attributes: attributes, kind: :client
"HTTP #{http_method}", attributes: attributes, kind: config.fetch(:span_kind)
) do |span|
OpenTelemetry.propagation.inject(env.request_headers)

Expand All @@ -39,21 +41,23 @@ def call(env)

private

attr_reader :app

def span_creation_attributes(http_method:, url:)
def span_creation_attributes(http_method:, url:, config:)
instrumentation_attrs = {
'http.method' => http_method,
'http.url' => OpenTelemetry::Common::Utilities.cleanse_url(url.to_s)
'http.url' => OpenTelemetry::Common::Utilities.cleanse_url(url.to_s),
'faraday.adapter.name' => app.class.name
}
instrumentation_attrs['net.peer.name'] = url.host if url.host
config = Faraday::Instrumentation.instance.config
instrumentation_attrs['peer.service'] = config[:peer_service] if config[:peer_service]

instrumentation_attrs.merge!(
OpenTelemetry::Common::HTTP::ClientContext.attributes
)
end

# Versions prior to 1.0 do not define an accessor for app
attr_reader :app if Gem::Version.new(Faraday::VERSION) < Gem::Version.new('1.0.0')

def tracer
Faraday::Instrumentation.instance.tracer
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,33 @@
_(span.attributes['peer.service']).must_equal 'example:faraday'
end

it 'defaults to span kind client' do
instrumentation.instance_variable_set(:@installed, false)
instrumentation.install

client.get('/success')

_(span.kind).must_equal :client
end

it 'allows overriding the span kind to internal' do
instrumentation.instance_variable_set(:@installed, false)
instrumentation.install(span_kind: :internal)

client.get('/success')

_(span.kind).must_equal :internal
end

it 'reports the name of the configured adapter' do
instrumentation.instance_variable_set(:@installed, false)
instrumentation.install

client.get('/success')

_(span.attributes.fetch('faraday.adapter.name')).must_equal Faraday::Adapter::Test.name
end

it 'prioritizes context attributes over config for peer service name' do
instrumentation.instance_variable_set(:@installed, false)
instrumentation.install(peer_service: 'example:faraday')
Expand Down
6 changes: 6 additions & 0 deletions instrumentation/graphql/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release History: opentelemetry-instrumentation-graphql

### v0.28.0 / 2024-02-16

* BREAKING CHANGE: GraphQL Legacy Tracer perf improvements [#867](https://github.com/open-telemetry/opentelemetry-ruby-contrib/pull/867).

* ADDED: GraphQL Legacy Tracer perf improvements [#867](https://github.com/open-telemetry/opentelemetry-ruby-contrib/pull/867).

### v0.27.0 / 2023-11-28

* CHANGED: Performance optimization cache attribute hashes [#723](https://github.com/open-telemetry/opentelemetry-ruby-contrib/pull/723)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module Tracers
# GraphQLTracer contains the OpenTelemetry tracer implementation compatible with
# the GraphQL tracer API
class GraphQLTracer < ::GraphQL::Tracing::PlatformTracing
DEFAULT_HASH = {}.freeze

self.platform_keys = {
'lex' => 'graphql.lex',
'parse' => 'graphql.parse',
Expand Down Expand Up @@ -86,24 +88,60 @@ def config
end

def attributes_for(key, data)
attributes = {}
case key
when 'execute_field', 'execute_field_lazy'
attributes['graphql.field.parent'] = data[:owner]&.graphql_name # owner is the concrete type, not interface
attributes['graphql.field.name'] = data[:field]&.graphql_name
attributes['graphql.lazy'] = key == 'execute_field_lazy'
when 'authorized', 'authorized_lazy'
attributes['graphql.type.name'] = data[:type]&.graphql_name
attributes['graphql.lazy'] = key == 'authorized_lazy'
when 'resolve_type', 'resolve_type_lazy'
attributes['graphql.type.name'] = data[:type]&.graphql_name
attributes['graphql.lazy'] = key == 'resolve_type_lazy'
when 'execute_field'
field_attr_cache = data[:query].context.namespace(:otel_attrs)[:execute_field_attrs] ||= attr_cache do |field|
attrs = {}
attrs['graphql.field.parent'] = field.owner.graphql_name if field.owner.graphql_name
attrs['graphql.field.name'] = field.graphql_name if field.graphql_name
attrs['graphql.lazy'] = false
attrs.freeze
end
field_attr_cache[data[:field]]
when 'execute_field_lazy'
lazy_field_attr_cache = data[:query].context.namespace(:otel_attrs)[:execute_field_lazy_attrs] ||= attr_cache do |field|
attrs = {}
attrs['graphql.field.parent'] = field.owner.graphql_name if field.owner.graphql_name
attrs['graphql.field.name'] = field.graphql_name if field.graphql_name
attrs['graphql.lazy'] = true
attrs.freeze
end
lazy_field_attr_cache[data[:field]]
when 'authorized', 'resolve_type'
type_attrs_cache = data[:context].namespace(:otel_attrs)[:type_attrs] ||= attr_cache do |type|
attrs = {}
attrs['graphql.type.name'] = type.graphql_name if type.graphql_name
attrs['graphql.lazy'] = false
attrs.freeze
end
type_attrs_cache[data[:type]]
when 'authorized_lazy', 'resolve_type_lazy'
type_lazy_attrs_cache = data[:context].namespace(:otel_attrs)[:type_lazy_attrs] ||= attr_cache do |type|
attrs = {}
attrs['graphql.type.name'] = type.graphql_name if type.graphql_name
attrs['graphql.lazy'] = true
attrs.freeze
end
type_lazy_attrs_cache[data[:type]]
when 'execute_query'
attributes['graphql.operation.name'] = data[:query].selected_operation_name if data[:query].selected_operation_name
attributes['graphql.operation.type'] = data[:query].selected_operation.operation_type
attributes['graphql.document'] = data[:query].query_string
attrs = {}
attrs['graphql.document'] = data[:query].query_string if data[:query].query_string
# rubocop:disable Style/SafeNavigation - using safe navigation creates more objects, we want to avoid this
attrs['graphql.operation.type'] = data[:query].selected_operation.operation_type if data[:query].selected_operation && data[:query].selected_operation.operation_type
# rubocop:enable Style/SafeNavigation
attrs['graphql.operation.name'] = data[:query].selected_operation_name || 'anonymous'
attrs.freeze
else
DEFAULT_HASH
end
end

def attr_cache
cache_h = Hash.new do |h, k|
h[k] = yield(k)
end
attributes
cache_h.compare_by_identity
cache_h
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
module OpenTelemetry
module Instrumentation
module GraphQL
VERSION = '0.27.0'
VERSION = '0.28.0'
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@
it 'omits nil attributes for execute_query' do
expected_attributes = {
'graphql.operation.type' => 'query',
'graphql.document' => '{ simpleField }'
'graphql.document' => '{ simpleField }',
'graphql.operation.name' => 'anonymous'
}

SomeGraphQLAppSchema.execute('{ simpleField }')
Expand Down Expand Up @@ -141,7 +142,7 @@
it 'includes attributes using platform types' do
skip if uses_platform_interfaces?
expected_attributes = {
'graphql.field.parent' => 'Car', # type name, not interface
'graphql.field.parent' => 'Vehicle', # interface name, not type
'graphql.field.name' => 'model',
'graphql.lazy' => false
}
Expand Down
12 changes: 8 additions & 4 deletions instrumentation/gruf/Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
#
# SPDX-License-Identifier: Apache-2.0

appraise 'gruf-2.15.1' do
gem 'gruf', '~> 2.15.1'
appraise 'gruf-2.17' do
gem 'gruf', '~> 2.17.0'
end

appraise 'gruf-2.16.1' do
gem 'gruf', '~> 2.16.1'
appraise 'gruf-2.18' do
gem 'gruf', '~> 2.18.0'
end

appraise 'gruf-2.19' do
gem 'gruf', '~> 2.19.0'
end
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'appraisal', '~> 2.5'
spec.add_development_dependency 'bundler', '>= 1.17'
spec.add_development_dependency 'grpc_mock'
spec.add_development_dependency 'gruf', '>= 2.15.1'
spec.add_development_dependency 'gruf', '>= 2.19.0'
spec.add_development_dependency 'minitest', '~> 5.0'
spec.add_development_dependency 'opentelemetry-sdk', '~> 1.0'
spec.add_development_dependency 'opentelemetry-test-helpers'
Expand Down
4 changes: 4 additions & 0 deletions instrumentation/mysql2/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release History: opentelemetry-instrumentation-mysql2

### v0.27.0 / 2024-02-15

* ADDED: Instrument mysql2 prepare statement

### v0.26.1 / 2024-02-08

* FIXED: Add missing requires for sql-helpers to mysql, pg, and trilogy instrumentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,40 @@ module Patches
# Module to prepend to Mysql2::Client for instrumentation
module Client
def query(sql, options = {})
attributes = client_attributes
tracer.in_span(
_otel_span_name(sql),
attributes: _otel_span_attributes(sql),
kind: :client
) do
super(sql, options)
end
end

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

private

def _otel_span_name(sql)
OpenTelemetry::Helpers::MySQL.database_span_name(
sql,
OpenTelemetry::Instrumentation::Mysql2.attributes[
SemanticConventions::Trace::DB_OPERATION
],
_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
Expand All @@ -24,30 +57,18 @@ def query(sql, options = {})
sql, obfuscation_limit: config[:obfuscation_limit], adapter: :mysql
)
end
tracer.in_span(
OpenTelemetry::Helpers::MySQL.database_span_name(
sql,
OpenTelemetry::Instrumentation::Mysql2.attributes[
SemanticConventions::Trace::DB_OPERATION
],
database_name,
config
),
attributes: attributes.merge!(OpenTelemetry::Instrumentation::Mysql2.attributes),
kind: :client
) do
super(sql, options)
end
end

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

def database_name
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 client_attributes
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
Expand All @@ -59,8 +80,9 @@ def client_attributes
SemanticConventions::Trace::NET_PEER_NAME => host,
SemanticConventions::Trace::NET_PEER_PORT => port
}
attributes[SemanticConventions::Trace::DB_NAME] = database_name if database_name
attributes[SemanticConventions::Trace::PEER_SERVICE] = config[:peer_service] if config[:peer_service]

attributes[SemanticConventions::Trace::DB_NAME] = _otel_database_name
attributes[SemanticConventions::Trace::PEER_SERVICE] = config[:peer_service]
attributes
end

Expand Down
Loading

0 comments on commit 3ab179e

Please sign in to comment.