opentelemetry-instrumentation-mysql2 0.24.3 → 0.26.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: 5fead6578e21138cd574525dfe0fef226474d1553bb98a29206785aa811f3855
4
- data.tar.gz: 6f23786abd74ed57ccd80f2b01c1bf3610db05f9b02fec00016d54c5943b9b35
3
+ metadata.gz: 8dd5337ce5e46679d577e51b807122cb037299d844ab8405d46c2043d2484931
4
+ data.tar.gz: c5df05b92be367a4c4397cfcb9eb76c5b5ca0f4fd0b6ee21d61a7bfafa1f1611
5
5
  SHA512:
6
- metadata.gz: 7d8f085435dd3084ee9cd4a15961fec9183736c5f41f821e5fd6ea469b5b5400025efc3442beca44b2f4d60b850929faeaf3c20fb2c456cf9cde1046af883326
7
- data.tar.gz: a7a33b5797845e4640264376d973212c3db062f0ac3a9d5c78e08bce69b504215ad1546745c94b9e41d49779b43fcfd6f12f7297cb113e5c22c2e6c2ab4608ec
6
+ metadata.gz: 8f183aee9dd0938d0c59928cafa9dd146fafaddb10a246b8486e333139abef478decd4fa1cea0c493012ab300c508302446102b15653092ead1e91d6a739e275
7
+ data.tar.gz: 4fc278b6756d5f744bf532999da01d95328eca700594e572039c2e51d81df668fc7674b040dbc2d8f350232a3fc267566974d40f393aef35428f1e308dc364be
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # Release History: opentelemetry-instrumentation-mysql2
2
2
 
3
+ ### v0.26.0 / 2024-02-08
4
+
5
+ * BREAKING CHANGE: Move shared sql behavior to helper gems
6
+
7
+
8
+ ### v0.25.0 / 2023-10-16
9
+
10
+ * BREAKING CHANGE: Obfuscation for mysql2, dalli and postgresql as default option for db_statement
11
+
12
+ * ADDED: Obfuscation for mysql2, dalli and postgresql as default option for db_statement
13
+
3
14
  ### v0.24.3 / 2023-08-03
4
15
 
5
16
  * FIXED: Remove inline linter rules
data/README.md CHANGED
@@ -46,9 +46,9 @@ end
46
46
  ```ruby
47
47
  OpenTelemetry::SDK.configure do |c|
48
48
  c.use 'OpenTelemetry::Instrumentation::Mysql2', {
49
- # The obfuscation of SQL in the db.statement attribute is disabled by default.
50
- # To enable, set db_statement to :obfuscate.
51
- db_statement: :obfuscate,
49
+ # The obfuscation of SQL in the db.statement attribute is enabled by default.
50
+ # To disable, set db_statement to :include; to omit the query completely, set db_statement to :omit
51
+ db_statement: :include,
52
52
  }
53
53
  end
54
54
  ```
@@ -20,7 +20,7 @@ module OpenTelemetry
20
20
  end
21
21
 
22
22
  option :peer_service, default: nil, validate: :string
23
- option :db_statement, default: :include, validate: %I[omit include obfuscate]
23
+ option :db_statement, default: :obfuscate, validate: %I[omit include obfuscate]
24
24
  option :span_name, default: :statement_type, validate: %I[statement_type db_name db_operation_and_name]
25
25
  option :obfuscation_limit, default: 2000, validate: :integer
26
26
 
@@ -9,58 +9,27 @@ module OpenTelemetry
9
9
  module Mysql2
10
10
  module Patches
11
11
  # Module to prepend to Mysql2::Client for instrumentation
12
- module Client # rubocop:disable Metrics/ModuleLength
13
- QUERY_NAMES = [
14
- 'set names',
15
- 'select',
16
- 'insert',
17
- 'update',
18
- 'delete',
19
- 'begin',
20
- 'commit',
21
- 'rollback',
22
- 'savepoint',
23
- 'release savepoint',
24
- 'explain',
25
- 'drop database',
26
- 'drop table',
27
- 'create database',
28
- 'create table'
29
- ].freeze
30
-
31
- QUERY_NAME_RE = Regexp.new("^(#{QUERY_NAMES.join('|')})", Regexp::IGNORECASE)
32
-
33
- # From: https://github.com/newrelic/newrelic-ruby-agent/blob/0235b288d85b8bc795bdc1a24621dd9f84cfef45/lib/new_relic/agent/database/obfuscation_helpers.rb#L9-L34
34
- COMPONENTS_REGEX_MAP = {
35
- single_quotes: /'(?:[^']|'')*?(?:\\'.*|'(?!'))/,
36
- double_quotes: /"(?:[^"]|"")*?(?:\\".*|"(?!"))/,
37
- numeric_literals: /-?\b(?:[0-9]+\.)?[0-9]+([eE][+-]?[0-9]+)?\b/,
38
- boolean_literals: /\b(?:true|false|null)\b/i,
39
- hexadecimal_literals: /0x[0-9a-fA-F]+/,
40
- comments: /(?:#|--).*?(?=\r|\n|$)/i,
41
- multi_line_comments: %r{\/\*(?:[^\/]|\/[^*])*?(?:\*\/|\/\*.*)}
42
- }.freeze
43
-
44
- MYSQL_COMPONENTS = %i[
45
- single_quotes
46
- double_quotes
47
- numeric_literals
48
- boolean_literals
49
- hexadecimal_literals
50
- comments
51
- multi_line_comments
52
- ].freeze
53
-
12
+ module Client
54
13
  def query(sql, options = {})
55
14
  attributes = client_attributes
56
15
  case config[:db_statement]
57
16
  when :include
58
17
  attributes[SemanticConventions::Trace::DB_STATEMENT] = sql
59
18
  when :obfuscate
60
- attributes[SemanticConventions::Trace::DB_STATEMENT] = obfuscate_sql(sql)
19
+ attributes[SemanticConventions::Trace::DB_STATEMENT] =
20
+ OpenTelemetry::Helpers::SqlObfuscation.obfuscate_sql(
21
+ sql, obfuscation_limit: config[:obfuscation_limit], adapter: :mysql
22
+ )
61
23
  end
62
24
  tracer.in_span(
63
- database_span_name(sql),
25
+ OpenTelemetry::Helpers::MySQL.database_span_name(
26
+ sql,
27
+ OpenTelemetry::Instrumentation::Mysql2.attributes[
28
+ SemanticConventions::Trace::DB_OPERATION
29
+ ],
30
+ database_name,
31
+ config
32
+ ),
64
33
  attributes: attributes.merge!(OpenTelemetry::Instrumentation::Mysql2.attributes),
65
34
  kind: :client
66
35
  ) do
@@ -70,57 +39,6 @@ module OpenTelemetry
70
39
 
71
40
  private
72
41
 
73
- def obfuscate_sql(sql)
74
- if sql.size > config[:obfuscation_limit]
75
- first_match_index = sql.index(generated_mysql_regex)
76
- truncation_message = "SQL truncated (> #{config[:obfuscation_limit]} characters)"
77
- return truncation_message unless first_match_index
78
-
79
- truncated_sql = sql[..first_match_index - 1]
80
- "#{truncated_sql}...\n#{truncation_message}"
81
- else
82
- obfuscated = OpenTelemetry::Common::Utilities.utf8_encode(sql, binary: true)
83
- obfuscated = obfuscated.gsub(generated_mysql_regex, '?')
84
- obfuscated = 'Failed to obfuscate SQL query - quote characters remained after obfuscation' if detect_unmatched_pairs(obfuscated)
85
- obfuscated
86
- end
87
- rescue StandardError => e
88
- OpenTelemetry.handle_error(message: 'Failed to obfuscate SQL', exception: e)
89
- 'OpenTelemetry error: failed to obfuscate sql'
90
- end
91
-
92
- def generated_mysql_regex
93
- @generated_mysql_regex ||= Regexp.union(MYSQL_COMPONENTS.map { |component| COMPONENTS_REGEX_MAP[component] })
94
- end
95
-
96
- def detect_unmatched_pairs(obfuscated)
97
- # We use this to check whether the query contains any quote characters
98
- # after obfuscation. If so, that's a good indication that the original
99
- # query was malformed, and so our obfuscation can't reliably find
100
- # literals. In such a case, we'll replace the entire query with a
101
- # placeholder.
102
- %r{'|"|\/\*|\*\/}.match(obfuscated)
103
- end
104
-
105
- def database_span_name(sql)
106
- case config[:span_name]
107
- when :statement_type
108
- extract_statement_type(sql)
109
- when :db_name
110
- database_name
111
- when :db_operation_and_name
112
- op = OpenTelemetry::Instrumentation::Mysql2.attributes[SemanticConventions::Trace::DB_OPERATION]
113
- name = database_name
114
- if op && name
115
- "#{op} #{name}"
116
- elsif op
117
- op
118
- elsif name
119
- name
120
- end
121
- end || 'mysql'
122
- end
123
-
124
42
  def database_name
125
43
  # https://github.com/brianmario/mysql2/blob/ca08712c6c8ea672df658bb25b931fea22555f27/lib/mysql2/client.rb#L78
126
44
  (query_options[:database] || query_options[:dbname] || query_options[:db])&.to_s
@@ -150,13 +68,6 @@ module OpenTelemetry
150
68
  def config
151
69
  Mysql2::Instrumentation.instance.config
152
70
  end
153
-
154
- def extract_statement_type(sql)
155
- QUERY_NAME_RE.match(sql) { |match| match[1].downcase } unless sql.nil?
156
- rescue StandardError => e
157
- OpenTelemetry.logger.debug("Error extracting sql statement type: #{e.message}")
158
- nil
159
- end
160
71
  end
161
72
  end
162
73
  end
@@ -7,7 +7,7 @@
7
7
  module OpenTelemetry
8
8
  module Instrumentation
9
9
  module Mysql2
10
- VERSION = '0.24.3'
10
+ VERSION = '0.26.0'
11
11
  end
12
12
  end
13
13
  end
@@ -20,18 +20,18 @@ module OpenTelemetry
20
20
  # Returns the attributes hash representing the Mysql2 context found
21
21
  # in the optional context or the current context if none is provided.
22
22
  #
23
- # @param [optional Context] context The context to lookup the current
23
+ # @param context [optional Context] The context to lookup the current
24
24
  # attributes hash. Defaults to Context.current
25
25
  def attributes(context = nil)
26
26
  context ||= Context.current
27
27
  context.value(CURRENT_ATTRIBUTES_KEY) || {}
28
28
  end
29
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
30
+ # @param attributes_hash [Hash] The attributes to add to the context
31
+ # @param parent_context [optional Context] The context to use as the parent for
34
32
  # the returned context
33
+ # @return A context containing the merged attributes hash, derived from the
34
+ # optional parent context, or the current context if one was not provided.
35
35
  def context_with_attributes(attributes_hash, parent_context: Context.current)
36
36
  attributes_hash = attributes(parent_context).merge(attributes_hash)
37
37
  parent_context.set_value(CURRENT_ATTRIBUTES_KEY, attributes_hash)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opentelemetry-instrumentation-mysql2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.24.3
4
+ version: 0.26.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-08-03 00:00:00.000000000 Z
11
+ date: 2024-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: opentelemetry-api
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: opentelemetry-helpers-mysql
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: opentelemetry-helpers-sql-obfuscation
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: opentelemetry-instrumentation-base
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -44,14 +72,14 @@ dependencies:
44
72
  requirements:
45
73
  - - "~>"
46
74
  - !ruby/object:Gem::Version
47
- version: 2.2.0
75
+ version: '2.5'
48
76
  type: :development
49
77
  prerelease: false
50
78
  version_requirements: !ruby/object:Gem::Requirement
51
79
  requirements:
52
80
  - - "~>"
53
81
  - !ruby/object:Gem::Version
54
- version: 2.2.0
82
+ version: '2.5'
55
83
  - !ruby/object:Gem::Dependency
56
84
  name: bundler
57
85
  requirement: !ruby/object:Gem::Requirement
@@ -156,56 +184,56 @@ dependencies:
156
184
  requirements:
157
185
  - - "~>"
158
186
  - !ruby/object:Gem::Version
159
- version: 1.55.1
187
+ version: 1.60.1
160
188
  type: :development
161
189
  prerelease: false
162
190
  version_requirements: !ruby/object:Gem::Requirement
163
191
  requirements:
164
192
  - - "~>"
165
193
  - !ruby/object:Gem::Version
166
- version: 1.55.1
194
+ version: 1.60.1
167
195
  - !ruby/object:Gem::Dependency
168
- name: simplecov
196
+ name: rubocop-performance
169
197
  requirement: !ruby/object:Gem::Requirement
170
198
  requirements:
171
199
  - - "~>"
172
200
  - !ruby/object:Gem::Version
173
- version: 0.17.1
201
+ version: '1.20'
174
202
  type: :development
175
203
  prerelease: false
176
204
  version_requirements: !ruby/object:Gem::Requirement
177
205
  requirements:
178
206
  - - "~>"
179
207
  - !ruby/object:Gem::Version
180
- version: 0.17.1
208
+ version: '1.20'
181
209
  - !ruby/object:Gem::Dependency
182
- name: yard
210
+ name: simplecov
183
211
  requirement: !ruby/object:Gem::Requirement
184
212
  requirements:
185
213
  - - "~>"
186
214
  - !ruby/object:Gem::Version
187
- version: '0.9'
215
+ version: 0.17.1
188
216
  type: :development
189
217
  prerelease: false
190
218
  version_requirements: !ruby/object:Gem::Requirement
191
219
  requirements:
192
220
  - - "~>"
193
221
  - !ruby/object:Gem::Version
194
- version: '0.9'
222
+ version: 0.17.1
195
223
  - !ruby/object:Gem::Dependency
196
- name: yard-doctest
224
+ name: yard
197
225
  requirement: !ruby/object:Gem::Requirement
198
226
  requirements:
199
227
  - - "~>"
200
228
  - !ruby/object:Gem::Version
201
- version: 0.1.6
229
+ version: '0.9'
202
230
  type: :development
203
231
  prerelease: false
204
232
  version_requirements: !ruby/object:Gem::Requirement
205
233
  requirements:
206
234
  - - "~>"
207
235
  - !ruby/object:Gem::Version
208
- version: 0.1.6
236
+ version: '0.9'
209
237
  description: Mysql2 instrumentation for the OpenTelemetry framework
210
238
  email:
211
239
  - cncf-opentelemetry-contributors@lists.cncf.io
@@ -227,10 +255,10 @@ homepage: https://github.com/open-telemetry/opentelemetry-ruby-contrib
227
255
  licenses:
228
256
  - Apache-2.0
229
257
  metadata:
230
- changelog_uri: https://rubydoc.info/gems/opentelemetry-instrumentation-mysql2/0.24.3/file/CHANGELOG.md
258
+ changelog_uri: https://rubydoc.info/gems/opentelemetry-instrumentation-mysql2/0.26.0/file/CHANGELOG.md
231
259
  source_code_uri: https://github.com/open-telemetry/opentelemetry-ruby-contrib/tree/main/instrumentation/mysql2
232
260
  bug_tracker_uri: https://github.com/open-telemetry/opentelemetry-ruby-contrib/issues
233
- documentation_uri: https://rubydoc.info/gems/opentelemetry-instrumentation-mysql2/0.24.3
261
+ documentation_uri: https://rubydoc.info/gems/opentelemetry-instrumentation-mysql2/0.26.0
234
262
  post_install_message:
235
263
  rdoc_options: []
236
264
  require_paths: