activerecord-instrumentation 0.4.0.jlauer1 → 0.4.0.jlauer2

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: 48fc834ad963b8c31ea09c9974ca1d024a692a71592a20e75f0079c9a2c93203
4
- data.tar.gz: 6be927a6854e55963f0fa1ab1ca0f5aa05c44e2e75d4eb8bebe7b3a459a00c9a
3
+ metadata.gz: 64b6684423e203bf4a46392f6c912f934fe568ff7f92ac4341e353dd3374f001
4
+ data.tar.gz: 2becfd40110f3da47d1ab03bcb80036bd1d1751fda300639ca5614779c575c91
5
5
  SHA512:
6
- metadata.gz: f4980f5d2f6d3c2081c42b1c5a2761fa10470b820ac75db0d1eda27e2b85fc4d973ddf901b69acc0aa43d0ab09e688934f7b847c6ed2c354431fa73c4e76d2ab
7
- data.tar.gz: 2be68549771581ee9e470bcc2664036206fbaa973d6adecdfef4b7f03993c3b2b9b8b45e170ed11da4293453925c3bdebd38192c00937edaa2ac555b15c4de63
6
+ metadata.gz: f4d38250a120a52b6f68190448b5c3e9961a5cafed4620cea367b98f2ec6e4588c4c35fcd6f369e15b9933d8eed13fe8b018808fe1ce0234aec415966e29a9cb
7
+ data.tar.gz: 236bf2931c3b2c743698efeb4a1c9cb69b32a0609d12663e5213254680fdf8b68789eb0f42d3f4faabb92467f64641cd94807d29b71db4cf737ecd1d41869163
data/CHANGELOG.md CHANGED
@@ -1,7 +1,7 @@
1
1
  Changelog
2
2
  =========
3
3
 
4
- ## 0.4.0.jlauer1 04/24/2020
4
+ ## 0.4.0.jlauer2 04/27/2020
5
5
  * Add SQL sanitizers
6
6
 
7
7
  ## 0.3.0 04/22/2020
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- activerecord-instrumentation (0.4.0.jlauer1)
4
+ activerecord-instrumentation (0.4.0.jlauer2)
5
5
  activerecord (~> 6.0)
6
6
  opentracing (~> 0.5)
7
7
 
data/README.md CHANGED
@@ -7,7 +7,7 @@ Adds OpenTracing instrumentation to ActiveRecord
7
7
  Add this line to your application's Gemfile:
8
8
 
9
9
  ```ruby
10
- gem 'activerecord-opentracing'
10
+ gem 'activerecord-instrumentation'
11
11
  ```
12
12
 
13
13
  ## Usage
@@ -16,8 +16,10 @@ gem 'activerecord-opentracing'
16
16
  require 'opentracing'
17
17
  OpenTracing.global_tracer = TracerImplementation.new
18
18
 
19
- require 'active_record/opentracing'
20
- ActiveRecord::OpenTracing.instrument
19
+ require 'active_record/open_tracing'
20
+ # If a sanitizer is specified, it will be used to scrub the sql statements. This is optional.
21
+ sanitizer = ActiveRecord::OpenTracing::SqlSanitizer.build_sanitizer(:mysql)
22
+ ActiveRecord::OpenTracing.instrument(sanitizer: sanitizer)
21
23
  ```
22
24
 
23
25
  # Development
@@ -9,15 +9,20 @@ require "active_record/open_tracing/sql_sanitizer"
9
9
 
10
10
  module ActiveRecord
11
11
  module OpenTracing
12
- def self.instrument(tracer: ::OpenTracing.global_tracer, sanitizer: nil)
12
+ # Instruments activerecord for use with OpenTracing
13
+ #
14
+ # @param tracer [OpenTracing::Tracer] The tracer to which to send traces
15
+ # @param sanitizer [Symbol, String, nil] The sanitizer to use. Options are :mysql,
16
+ # :postgres, :sql_server, :sqlite. If no sanitizer is specified, or a falsy value
17
+ # is passed, sql will not be sanitized.
18
+ # @param sql_logging [Boolean] Whether to log sql statements to the tracer
19
+ def self.instrument(tracer: ::OpenTracing.global_tracer, sanitizer: nil, sql_logging_enabled: true)
13
20
  sql_sanitizer = sanitizer && SqlSanitizer.build_sanitizer(sanitizer)
14
- processor = Processor.new(tracer, sanitizer: sql_sanitizer)
21
+ processor = Processor.new(tracer, sanitizer: sql_sanitizer, sql_logging_enabled: sql_logging_enabled)
15
22
 
16
23
  ActiveSupport::Notifications.subscribe("sql.active_record") do |*args|
17
24
  processor.call(*args)
18
25
  end
19
-
20
- self
21
26
  end
22
27
  end
23
28
  end
@@ -8,11 +8,12 @@ module ActiveRecord
8
8
  SPAN_KIND = "client"
9
9
  DB_TYPE = "sql"
10
10
 
11
- attr_reader :tracer, :sanitizer
11
+ attr_reader :tracer, :sanitizer, :sql_logging_enabled
12
12
 
13
- def initialize(tracer, sanitizer: nil)
13
+ def initialize(tracer, sanitizer: nil, sql_logging_enabled: true)
14
14
  @tracer = tracer
15
15
  @sanitizer = sanitizer
16
+ @sql_logging_enabled = sql_logging_enabled
16
17
  end
17
18
 
18
19
  def call(_event_name, start, finish, _id, payload)
@@ -48,51 +49,34 @@ module ActiveRecord
48
49
  "span.kind" => SPAN_KIND,
49
50
  "db.instance" => db_instance,
50
51
  "db.cached" => payload.fetch(:cached, false),
51
- "db.statement" => sanitize_sql(payload.fetch(:sql).squish),
52
52
  "db.type" => DB_TYPE,
53
- "peer.address" => db_address
54
- }
53
+ "peer.address" => peer_address_tag
54
+ }.merge(db_statement(payload))
55
55
  end
56
56
 
57
- def sanitize_sql(sql)
58
- sanitizer ? sanitizer.sanitize(sql) : sql
57
+ def db_statement(payload)
58
+ sql_logging_enabled ? { "db.statement" => sanitize_sql(payload.fetch(:sql).squish) } : {}
59
59
  end
60
60
 
61
- def db_instance
62
- @db_instance ||= db_config.fetch(:database)
61
+ def sanitize_sql(sql)
62
+ sanitizer ? sanitizer.sanitize(sql) : sql
63
63
  end
64
64
 
65
- def db_address
66
- @db_address ||= [
67
- adapter_str,
68
- username_str,
69
- host_str,
70
- database_str
65
+ def peer_address_tag
66
+ @peer_address_tag ||= [
67
+ "#{connection_config.fetch(:adapter)}://",
68
+ connection_config[:username],
69
+ connection_config[:host] && "@#{connection_config[:host]}",
70
+ "/#{db_instance}"
71
71
  ].join
72
72
  end
73
73
 
74
- def adapter_str
75
- "#{connection_config.fetch(:adapter)}://"
76
- end
77
-
78
- def username_str
79
- connection_config[:username]
80
- end
81
-
82
- def host_str
83
- "@#{connection_config[:host]}" if connection_config[:host]
84
- end
85
-
86
- def database_str
87
- "/#{connection_config.fetch(:database)}"
74
+ def db_instance
75
+ @db_instance ||= connection_config.fetch(:database)
88
76
  end
89
77
 
90
78
  def connection_config
91
- ActiveRecord::Base.connection_config
92
- end
93
-
94
- def db_config
95
- @db_config ||= ActiveRecord::Base.connection_config
79
+ @connection_config ||= ActiveRecord::Base.connection_config
96
80
  end
97
81
  end
98
82
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module OpenTracing
5
- VERSION = "0.4.0.jlauer1"
5
+ VERSION = "0.4.0.jlauer2"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-instrumentation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0.jlauer1
4
+ version: 0.4.0.jlauer2
5
5
  platform: ruby
6
6
  authors:
7
7
  - SaleMove TechMovers
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-04-24 00:00:00.000000000 Z
12
+ date: 2020-04-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler