activerecord-honeycomb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 19aa8e7d89c4d2ccd9bfebb896b4d36723f9d438
4
+ data.tar.gz: 587b8e665d2a259cb9f563a489470d5f2e6b4aa0
5
+ SHA512:
6
+ metadata.gz: e765c841f53eaff2dc5c9e64c422f171a00c1528d191b62067c2e9de3a0ccb014576d916e70f4578a5a2a7683a73202714682a66d27e387e355cde7f95bda0ed
7
+ data.tar.gz: 320245b93cf1fb685182113a33a71f8ac65f8168dc61378dc093de1298d18d66af9944ab3d8e803e7a109f785ee7d1b54859660c857dcdcd40227f303cceae47
@@ -0,0 +1,107 @@
1
+ require 'active_record'
2
+ require 'securerandom'
3
+
4
+ module ActiveRecord
5
+ module ConnectionHandling
6
+ def honeycomb_connection(config)
7
+ real_config = config.merge(adapter: config.fetch(:real_adapter))
8
+
9
+ client = config[:honeycomb_client]
10
+
11
+ resolver = ConnectionAdapters::ConnectionSpecification::Resolver.new(Base.configurations)
12
+ spec = resolver.spec(real_config)
13
+
14
+ real_connection = ::ActiveRecord::Base.send(spec.adapter_method, spec.config)
15
+
16
+ unless real_connection.class.ancestors.include? ConnectionAdapters::HoneycombAdapter
17
+ real_connection.class.extend(Module.new do
18
+ private
19
+ define_method(:honeycomb_client) { client }
20
+ end) if client
21
+ real_connection.class.include ConnectionAdapters::HoneycombAdapter
22
+ end
23
+
24
+ real_connection
25
+ end
26
+ end
27
+
28
+ module ConnectionAdapters
29
+ module HoneycombAdapter
30
+ def self.included(klazz)
31
+ # TODO ugh clean this up
32
+ @_honeycomb ||= begin
33
+ if klazz.respond_to? :honeycomb_client, true # include private
34
+ klazz.send(:honeycomb_client)
35
+ elsif defined?(::Honeycomb.client)
36
+ ::Honeycomb.client
37
+ else
38
+ raise "Can't work without magic global Honeycomb.client at the moment"
39
+ end
40
+ end
41
+ klazz.class_exec(@_honeycomb) do |honeycomb_|
42
+ define_method(:honeycomb) { honeycomb_ }
43
+ end
44
+ super
45
+ end
46
+
47
+ def execute(sql, *args)
48
+ sending_honeycomb_event do |event|
49
+ event.add_field :sql, sql
50
+
51
+ adding_span_metadata_if_available(event, :statement) do
52
+ super
53
+ end
54
+ end
55
+ end
56
+
57
+ def exec_query(sql, *args)
58
+ sending_honeycomb_event do |event|
59
+ event.add_field :sql, sql
60
+
61
+ adding_span_metadata_if_available(event, :query) do
62
+ super
63
+ end
64
+ end
65
+ end
66
+
67
+ private
68
+ def sending_honeycomb_event
69
+ raise 'something went horribly wrong' unless honeycomb # TODO
70
+ event = honeycomb.event
71
+
72
+ start = Time.now
73
+ yield event
74
+ rescue Exception => e
75
+ if event
76
+ event.add_field :exception_class, e.class
77
+ event.add_field :exception_message, e.message
78
+ end
79
+ raise
80
+ ensure
81
+ if start && event
82
+ finish = Time.now
83
+ duration = finish - start
84
+ event.add_field :durationMs, duration * 1000
85
+ event.send
86
+ end
87
+ end
88
+
89
+ def adding_span_metadata_if_available(event, name)
90
+ return yield unless defined?(::Honeycomb.trace_id)
91
+
92
+ trace_id = ::Honeycomb.trace_id
93
+
94
+ event.add_field :traceId, trace_id if trace_id
95
+ span_id = SecureRandom.uuid
96
+ event.add_field :id, span_id
97
+ event.add_field :serviceName, 'active_record'
98
+ event.add_field :name, name
99
+
100
+ ::Honeycomb.with_span_id(span_id) do |parent_span_id|
101
+ event.add_field :parentId, parent_span_id
102
+ yield
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,15 @@
1
+ require 'active_record'
2
+
3
+ module ActiveRecord
4
+ module Honeycomb
5
+ def self.munge_config(config, client: nil, logger: nil)
6
+ munged = config.merge(
7
+ 'adapter' => 'honeycomb',
8
+ 'real_adapter' => config.fetch('adapter'),
9
+ )
10
+ munged['honeycomb_client'] = client if client
11
+ munged['honeycomb_logger'] = logger if logger
12
+ munged
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,6 @@
1
+ module ActiveRecord
2
+ module Honeycomb
3
+ GEM_NAME = 'activerecord-honeycomb'
4
+ VERSION = '0.0.1'
5
+ end
6
+ end
@@ -0,0 +1,7 @@
1
+ begin
2
+ gem 'activerecord'
3
+
4
+ require 'active_record/honeycomb'
5
+ rescue LoadError
6
+ warn 'ActiveRecord not detected, not enabling activerecord-honeycomb'
7
+ end
@@ -0,0 +1,25 @@
1
+ module ActiveRecord
2
+ module Honeycomb
3
+ module AutoInstall
4
+ class << self
5
+ def available?(**_)
6
+ gem 'activerecord'
7
+ rescue Gem::LoadError
8
+ false
9
+ end
10
+
11
+ def auto_install!(honeycomb_client:, logger: nil)
12
+ require 'active_record'
13
+ require 'activerecord-honeycomb'
14
+
15
+ ActiveRecord::Base.extend(Module.new do
16
+ define_method :establish_connection do |config, *args|
17
+ munged_config = ActiveRecord::Honeycomb.munge_config(config, client: honeycomb_client, logger: logger)
18
+ super(munged_config, *args)
19
+ end
20
+ end)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1 @@
1
+ require 'active_record/honeycomb'
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-honeycomb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sam Stokes
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: libhoney
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: bump
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: yard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: " TO DO *is* a description\n"
98
+ email:
99
+ - sam@honeycomb.io
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - lib/active_record/connection_adapters/honeycomb_adapter.rb
105
+ - lib/active_record/honeycomb.rb
106
+ - lib/active_record/honeycomb/version.rb
107
+ - lib/activerecord-honeycomb.rb
108
+ - lib/activerecord-honeycomb/auto_install.rb
109
+ - lib/activerecord/honeycomb.rb
110
+ homepage: https://github.com/honeycombio/activerecord-honeycomb
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.6.11
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Instrument your ActiveRecord queries with Honeycomb
134
+ test_files: []