opentelemetry-instrumentation-trilogy 0.51.1 → 0.52.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f6241d5ba63c7c5865f7cf548d39aa770f596aeb2c4ee2afecb7cdfa9c49c717
4
- data.tar.gz: 1bfd9359a9a17b6e964a5bb820ca97edecfe68c9881bd7ff22ebadb2c969ae9e
3
+ metadata.gz: 98394ad5f8a94bbc13c06521d80b89c761ec59de025cc0af41d855a13491be86
4
+ data.tar.gz: d35aa0062e3deefde1a080ae6b49f9d9d2f4fe243c25ac8058901f04a222f93f
5
5
  SHA512:
6
- metadata.gz: 620b27ac8992366fc522a232c679cc1065b31e2dd3919f922b1e55552f71e23390745242a18f31d25d84876211d216759e2275ff272f25ba8dbe660259287183
7
- data.tar.gz: b651e768889aa07c3871516fb12eae564457c5c896f49ed861218a09ed405fab8707b67e61b906ab249d2cdd5a4ab2d420565a6ae453fe9c1f6544300b915831
6
+ metadata.gz: c12b338b919be130784c76b0d26195d96717115b333c2acfac2b2463fb3ba4f2d33a56735e55d48b44e772c15fc3d6e2a684f4aebf03383d1df3e78b4fb3f061
7
+ data.tar.gz: 11a064a8f1215e110cf16920dea54dc068a0d72911f7815db21113e9cf423f677bfb3773cab379cedb49255ad2ec70ce03bdf4ac541cc704556608e9affb3dc6
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Release History: opentelemetry-instrumentation-trilogy
2
2
 
3
+ ### v0.52.0 / 2023-03-06
4
+
5
+ * ADDED: Add with_attributes context propagation to Trilogy instrumentation
6
+ * ADDED: Add option to configure span name for trilogy
7
+ * FIXED: Ensure encoding errors handled during SQL obfuscation for Trilogy
8
+
3
9
  ### v0.51.1 / 2023-01-14
4
10
 
5
11
  * DOCS: Fix gem homepage
data/README.md CHANGED
@@ -40,6 +40,17 @@ OpenTelemetry::SDK.configure do |c|
40
40
  end
41
41
  ```
42
42
 
43
+ The `trilogy` instrumentation allows the user to supply additional attributes via the `with_attributes` method. This makes it possible to supply additional attributes on trilogy spans. Attributes supplied in `with_attributes` supersede those automatically generated within `trilogy`'s automatic instrumentation. If you supply a `db.statement` attribute in `with_attributes`, this library's `:db_statement` configuration will not be applied.
44
+
45
+ ```ruby
46
+ require 'opentelemetry-instrumentation-trilogy'
47
+
48
+ client = Trilogy.new(:host => 'localhost', :username => 'root')
49
+ OpenTelemetry::Instrumentation::Trilogy.with_attributes('pizzatoppings' => 'mushrooms') do
50
+ client.query('SELECT 1')
51
+ end
52
+ ```
53
+
43
54
  ## How can I get involved?
44
55
 
45
56
  The `opentelemetry-instrumentation-trilogy` gem source is [on github][repo-github], along with related gems including `opentelemetry-api` and `opentelemetry-sdk`.
@@ -24,6 +24,7 @@ module OpenTelemetry
24
24
 
25
25
  option :peer_service, default: nil, validate: :string
26
26
  option :db_statement, default: :obfuscate, validate: %I[omit include obfuscate]
27
+ option :span_name, default: :statement_type, validate: %I[statement_type db_name db_operation_and_name]
27
28
 
28
29
  private
29
30
 
@@ -56,7 +56,7 @@ module OpenTelemetry
56
56
  def query(sql)
57
57
  tracer.in_span(
58
58
  database_span_name(sql),
59
- attributes: client_attributes(sql),
59
+ attributes: client_attributes(sql).merge!(OpenTelemetry::Instrumentation::Trilogy.attributes),
60
60
  kind: :client
61
61
  ) do
62
62
  super(sql)
@@ -87,10 +87,14 @@ module OpenTelemetry
87
87
  if sql.size > 2000
88
88
  'SQL query too large to remove sensitive data ...'
89
89
  else
90
- obfuscated = sql.gsub(FULL_SQL_REGEXP, '?')
90
+ obfuscated = OpenTelemetry::Common::Utilities.utf8_encode(sql, binary: true)
91
+ obfuscated = obfuscated.gsub(FULL_SQL_REGEXP, '?')
91
92
  obfuscated = 'Failed to obfuscate SQL query - quote characters remained after obfuscation' if detect_unmatched_pairs(obfuscated)
92
93
  obfuscated
93
94
  end
95
+ rescue StandardError => e
96
+ OpenTelemetry.handle_error(message: 'Failed to obfuscate SQL', exception: e)
97
+ 'OpenTelemetry error: failed to obfuscate sql'
94
98
  end
95
99
 
96
100
  def detect_unmatched_pairs(obfuscated)
@@ -102,20 +106,27 @@ module OpenTelemetry
102
106
  %r{'|"|\/\*|\*\/}.match(obfuscated)
103
107
  end
104
108
 
105
- def database_span_name(sql)
106
- # Setting span name to the SQL query without obfuscation would
107
- # result in PII + cardinality issues.
108
- # First attempt to infer the statement type then fallback to
109
- # current Otel approach {database.component_name}.{database_instance_name}
110
- # https://github.com/open-telemetry/opentelemetry-python/blob/39fa078312e6f41c403aa8cad1868264011f7546/ext/opentelemetry-ext-dbapi/tests/test_dbapi_integration.py#L53
111
- # This creates span names like mysql.default, mysql.replica, postgresql.staging etc.
112
-
113
- statement_type = extract_statement_type(sql)
114
-
115
- return statement_type unless statement_type.nil?
109
+ def database_span_name(sql) # rubocop:disable Metrics/CyclomaticComplexity
110
+ case config[:span_name]
111
+ when :statement_type
112
+ extract_statement_type(sql)
113
+ when :db_name
114
+ database_name
115
+ when :db_operation_and_name
116
+ op = OpenTelemetry::Instrumentation::Trilogy.attributes['db.operation']
117
+ name = database_name
118
+ if op && name
119
+ "#{op} #{name}"
120
+ elsif op
121
+ op
122
+ elsif name
123
+ name
124
+ end
125
+ end || 'mysql'
126
+ end
116
127
 
117
- # fallback
118
- 'mysql'
128
+ def database_name
129
+ connection_options[:database]
119
130
  end
120
131
 
121
132
  def net_peer_name
@@ -140,6 +151,7 @@ module OpenTelemetry
140
151
  QUERY_NAME_RE.match(sql) { |match| match[1].downcase } unless sql.nil?
141
152
  rescue StandardError => e
142
153
  OpenTelemetry.logger.error("Error extracting sql statement type: #{e.message}")
154
+ nil
143
155
  end
144
156
  end
145
157
  end
@@ -7,7 +7,7 @@
7
7
  module OpenTelemetry
8
8
  module Instrumentation
9
9
  module Trilogy
10
- VERSION = '0.51.1'
10
+ VERSION = '0.52.0'
11
11
  end
12
12
  end
13
13
  end
@@ -11,6 +11,45 @@ module OpenTelemetry
11
11
  module Instrumentation
12
12
  # Contains the OpenTelemetry instrumentation for the Trilogy gem
13
13
  module Trilogy
14
+ extend self
15
+
16
+ CURRENT_ATTRIBUTES_KEY = Context.create_key('trilogy-attributes-hash')
17
+
18
+ private_constant :CURRENT_ATTRIBUTES_KEY
19
+
20
+ # Returns the attributes hash representing the Trilogy context found
21
+ # in the optional context or the current context if none is provided.
22
+ #
23
+ # @param [optional Context] context The context to lookup the current
24
+ # attributes hash. Defaults to Context.current
25
+ def attributes(context = nil)
26
+ context ||= Context.current
27
+ context.value(CURRENT_ATTRIBUTES_KEY) || {}
28
+ end
29
+
30
+ # Returns a context containing the merged attributes hash, derived from the
31
+ # optional parent context, or the current context if one was not provided.
32
+ #
33
+ # @param [optional Context] context The context to use as the parent for
34
+ # the returned context
35
+ def context_with_attributes(attributes_hash, parent_context: Context.current)
36
+ attributes_hash = attributes(parent_context).merge(attributes_hash)
37
+ parent_context.set_value(CURRENT_ATTRIBUTES_KEY, attributes_hash)
38
+ end
39
+
40
+ # Activates/deactivates the merged attributes hash within the current Context,
41
+ # which makes the "current attributes hash" available implicitly.
42
+ #
43
+ # On exit, the attributes hash that was active before calling this method
44
+ # will be reactivated.
45
+ #
46
+ # @param [Span] span the span to activate
47
+ # @yield [Hash, Context] yields attributes hash and a context containing the
48
+ # attributes hash to the block.
49
+ def with_attributes(attributes_hash)
50
+ attributes_hash = attributes.merge(attributes_hash)
51
+ Context.with_value(CURRENT_ATTRIBUTES_KEY, attributes_hash) { |c, h| yield h, c }
52
+ end
14
53
  end
15
54
  end
16
55
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opentelemetry-instrumentation-trilogy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.51.1
4
+ version: 0.52.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - OpenTelemetry Authors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-14 00:00:00.000000000 Z
11
+ date: 2023-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: opentelemetry-api
@@ -275,10 +275,10 @@ homepage: https://github.com/open-telemetry/opentelemetry-ruby-contrib
275
275
  licenses:
276
276
  - Apache-2.0
277
277
  metadata:
278
- changelog_uri: https://rubydoc.info/gems/opentelemetry-instrumentation-trilogy/0.51.1/file/CHANGELOG.md
278
+ changelog_uri: https://rubydoc.info/gems/opentelemetry-instrumentation-trilogy/0.52.0/file/CHANGELOG.md
279
279
  source_code_uri: https://github.com/open-telemetry/opentelemetry-ruby-contrib/tree/main/instrumentation/trilogy
280
280
  bug_tracker_uri: https://github.com/open-telemetry/opentelemetry-ruby-contrib/issues
281
- documentation_uri: https://rubydoc.info/gems/opentelemetry-instrumentation-trilogy/0.51.1
281
+ documentation_uri: https://rubydoc.info/gems/opentelemetry-instrumentation-trilogy/0.52.0
282
282
  post_install_message:
283
283
  rdoc_options: []
284
284
  require_paths: