atatus 1.3.0 → 1.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +21 -0
- data/Gemfile +72 -22
- data/LICENSE +1 -1
- data/atatus.gemspec +2 -2
- data/lib/atatus/agent.rb +78 -29
- data/lib/atatus/central_config/cache_control.rb +18 -1
- data/lib/atatus/central_config.rb +72 -27
- data/lib/atatus/child_durations.rb +64 -0
- data/lib/atatus/collector/base.rb +102 -43
- data/lib/atatus/collector/builder.rb +134 -4
- data/lib/atatus/collector/hist.rb +54 -0
- data/lib/atatus/collector/layer.rb +1 -0
- data/lib/atatus/collector/transport.rb +41 -11
- data/lib/atatus/config/bytes.rb +17 -0
- data/lib/atatus/config/duration.rb +17 -0
- data/lib/atatus/config/options.rb +29 -9
- data/lib/atatus/config/regexp_list.rb +17 -0
- data/lib/atatus/config/wildcard_pattern_list.rb +64 -0
- data/lib/atatus/config.rb +129 -28
- data/lib/atatus/context/request/socket.rb +18 -1
- data/lib/atatus/context/request/url.rb +17 -0
- data/lib/atatus/context/request.rb +17 -0
- data/lib/atatus/context/response.rb +27 -2
- data/lib/atatus/context/user.rb +17 -0
- data/lib/atatus/context.rb +32 -1
- data/lib/atatus/context_builder.rb +19 -4
- data/lib/atatus/deprecations.rb +17 -0
- data/lib/atatus/error/exception.rb +24 -0
- data/lib/atatus/error/log.rb +17 -0
- data/lib/atatus/error.rb +27 -0
- data/lib/atatus/error_builder.rb +21 -2
- data/lib/atatus/grape.rb +62 -0
- data/lib/atatus/graphql.rb +91 -0
- data/lib/atatus/grpc.rb +99 -0
- data/lib/atatus/instrumenter.rb +135 -30
- data/lib/atatus/internal_error.rb +17 -0
- data/lib/atatus/logging.rb +17 -2
- data/lib/atatus/metadata/process_info.rb +17 -0
- data/lib/atatus/metadata/service_info.rb +21 -6
- data/lib/atatus/metadata/system_info/container_info.rb +49 -10
- data/lib/atatus/metadata/system_info/hw_info.rb +1 -1
- data/lib/atatus/metadata/system_info.rb +22 -3
- data/lib/atatus/metadata.rb +17 -0
- data/lib/atatus/metrics/breakdown_set.rb +31 -0
- data/lib/atatus/metrics/{cpu_mem.rb → cpu_mem_set.rb} +110 -63
- data/lib/atatus/metrics/metric.rb +140 -0
- data/lib/atatus/metrics/set.rb +123 -0
- data/lib/atatus/metrics/span_scoped_set.rb +56 -0
- data/lib/atatus/metrics/transaction_set.rb +26 -0
- data/lib/atatus/metrics/vm_set.rb +58 -0
- data/lib/atatus/metrics.rb +69 -27
- data/lib/atatus/metricset.rb +48 -4
- data/lib/atatus/middleware.rb +28 -8
- data/lib/atatus/naively_hashable.rb +17 -0
- data/lib/atatus/normalizers/grape/endpoint_run.rb +65 -0
- data/lib/atatus/normalizers/grape.rb +22 -0
- data/lib/atatus/normalizers/rails/action_controller.rb +44 -0
- data/lib/atatus/normalizers/rails/action_mailer.rb +43 -0
- data/lib/atatus/normalizers/{action_view.rb → rails/action_view.rb} +17 -0
- data/lib/atatus/normalizers/rails/active_record.rb +80 -0
- data/lib/atatus/normalizers/rails.rb +27 -0
- data/lib/atatus/normalizers.rb +23 -9
- data/lib/atatus/opentracing.rb +75 -42
- data/lib/atatus/rails.rb +29 -13
- data/lib/atatus/railtie.rb +19 -6
- data/lib/atatus/resque.rb +29 -0
- data/lib/atatus/sinatra.rb +53 -0
- data/lib/atatus/span/context/db.rb +43 -0
- data/lib/atatus/span/context/destination.rb +77 -0
- data/lib/atatus/span/context/http.rb +43 -0
- data/lib/atatus/span/context.rb +43 -28
- data/lib/atatus/span.rb +44 -15
- data/lib/atatus/span_helpers.rb +18 -1
- data/lib/atatus/spies/action_dispatch.rb +27 -6
- data/lib/atatus/spies/delayed_job.rb +26 -5
- data/lib/atatus/spies/dynamo_db.rb +62 -0
- data/lib/atatus/spies/elasticsearch.rb +53 -7
- data/lib/atatus/spies/faraday.rb +54 -20
- data/lib/atatus/spies/http.rb +36 -6
- data/lib/atatus/spies/json.rb +18 -0
- data/lib/atatus/spies/mongo.rb +41 -10
- data/lib/atatus/spies/net_http.rb +52 -11
- data/lib/atatus/spies/rake.rb +42 -23
- data/lib/atatus/spies/redis.rb +17 -0
- data/lib/atatus/spies/resque.rb +57 -0
- data/lib/atatus/spies/sequel.rb +54 -17
- data/lib/atatus/spies/shoryuken.rb +69 -0
- data/lib/atatus/spies/sidekiq.rb +46 -25
- data/lib/atatus/spies/sinatra.rb +20 -4
- data/lib/atatus/spies/sneakers.rb +74 -0
- data/lib/atatus/spies/sucker_punch.rb +58 -0
- data/lib/atatus/spies/tilt.rb +20 -1
- data/lib/atatus/spies.rb +33 -15
- data/lib/atatus/sql/signature.rb +169 -0
- data/lib/atatus/sql/tokenizer.rb +264 -0
- data/lib/atatus/sql/tokens.rb +63 -0
- data/lib/atatus/sql.rb +36 -0
- data/lib/atatus/sql_summarizer.rb +24 -6
- data/lib/atatus/stacktrace/frame.rb +17 -3
- data/lib/atatus/stacktrace.rb +17 -0
- data/lib/atatus/stacktrace_builder.rb +23 -3
- data/lib/atatus/subscriber.rb +23 -4
- data/lib/atatus/trace_context/traceparent.rb +111 -0
- data/lib/atatus/trace_context/tracestate.rb +148 -0
- data/lib/atatus/trace_context.rb +84 -51
- data/lib/atatus/transaction.rb +74 -18
- data/lib/atatus/transport/base.rb +44 -27
- data/lib/atatus/transport/connection/http.rb +58 -35
- data/lib/atatus/transport/connection/proxy_pipe.rb +24 -5
- data/lib/atatus/transport/connection.rb +28 -72
- data/lib/atatus/transport/filters/hash_sanitizer.rb +77 -0
- data/lib/atatus/transport/filters/secrets_filter.rb +30 -55
- data/lib/atatus/transport/filters.rb +18 -1
- data/lib/atatus/transport/headers.rb +83 -0
- data/lib/atatus/transport/serializers/context_serializer.rb +30 -3
- data/lib/atatus/transport/serializers/error_serializer.rb +17 -2
- data/lib/atatus/transport/serializers/metadata_serializer.rb +44 -22
- data/lib/atatus/transport/serializers/metricset_serializer.rb +34 -6
- data/lib/atatus/transport/serializers/span_serializer.rb +47 -12
- data/lib/atatus/transport/serializers/transaction_serializer.rb +18 -2
- data/lib/atatus/transport/serializers.rb +17 -5
- data/lib/atatus/transport/user_agent.rb +48 -0
- data/lib/atatus/transport/worker.rb +31 -7
- data/lib/atatus/util/inflector.rb +17 -0
- data/lib/atatus/util/lru_cache.rb +17 -0
- data/lib/atatus/util/throttle.rb +17 -0
- data/lib/atatus/util.rb +18 -1
- data/lib/atatus/version.rb +19 -1
- data/lib/atatus.rb +84 -16
- metadata +46 -26
- data/Rakefile +0 -19
- data/bench/.gitignore +0 -2
- data/bench/app.rb +0 -53
- data/bench/benchmark.rb +0 -36
- data/bench/report.rb +0 -55
- data/bench/rubyprof.rb +0 -39
- data/bench/stackprof.rb +0 -23
- data/bin/build_docs +0 -5
- data/bin/console +0 -15
- data/bin/setup +0 -8
- data/bin/with_framework +0 -7
- data/lib/atatus/metrics/vm.rb +0 -60
- data/lib/atatus/normalizers/action_controller.rb +0 -27
- data/lib/atatus/normalizers/action_mailer.rb +0 -26
- data/lib/atatus/normalizers/active_record.rb +0 -45
- data/lib/atatus/util/prefixed_logger.rb +0 -18
- data/vendor/.gitkeep +0 -0
data/lib/atatus/spies/sinatra.rb
CHANGED
@@ -1,3 +1,20 @@
|
|
1
|
+
# Licensed to Elasticsearch B.V. under one or more contributor
|
2
|
+
# license agreements. See the NOTICE file distributed with
|
3
|
+
# this work for additional information regarding copyright
|
4
|
+
# ownership. Elasticsearch B.V. licenses this file to you under
|
5
|
+
# the Apache License, Version 2.0 (the "License"); you may
|
6
|
+
# not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing,
|
12
|
+
# software distributed under the License is distributed on an
|
13
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14
|
+
# KIND, either express or implied. See the License for the
|
15
|
+
# specific language governing permissions and limitations
|
16
|
+
# under the License.
|
17
|
+
|
1
18
|
# frozen_string_literal: true
|
2
19
|
|
3
20
|
module Atatus
|
@@ -5,11 +22,10 @@ module Atatus
|
|
5
22
|
module Spies
|
6
23
|
# @api private
|
7
24
|
class SinatraSpy
|
8
|
-
# rubocop:disable Metrics/MethodLength
|
9
25
|
def install
|
10
|
-
if defined?(::Sinatra) &&
|
26
|
+
if defined?(::Sinatra) &&
|
11
27
|
defined?(::Sinatra::Base) &&
|
12
|
-
::Sinatra::Base.private_method_defined?(:dispatch!) &&
|
28
|
+
::Sinatra::Base.private_method_defined?(:dispatch!) &&
|
13
29
|
::Sinatra::Base.private_method_defined?(:compile_template)
|
14
30
|
|
15
31
|
::Sinatra::Base.class_eval do
|
@@ -35,9 +51,9 @@ module Atatus
|
|
35
51
|
compile_template_without_apm(engine, data, opts, *args, &block)
|
36
52
|
end
|
37
53
|
end
|
54
|
+
|
38
55
|
end
|
39
56
|
end
|
40
|
-
# rubocop:enable Metrics/MethodLength
|
41
57
|
end
|
42
58
|
|
43
59
|
register 'Sinatra::Base', 'sinatra/base', SinatraSpy.new
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# Licensed to Elasticsearch B.V. under one or more contributor
|
2
|
+
# license agreements. See the NOTICE file distributed with
|
3
|
+
# this work for additional information regarding copyright
|
4
|
+
# ownership. Elasticsearch B.V. licenses this file to you under
|
5
|
+
# the Apache License, Version 2.0 (the "License"); you may
|
6
|
+
# not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing,
|
12
|
+
# software distributed under the License is distributed on an
|
13
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14
|
+
# KIND, either express or implied. See the License for the
|
15
|
+
# specific language governing permissions and limitations
|
16
|
+
# under the License.
|
17
|
+
|
18
|
+
# frozen_string_literal: true
|
19
|
+
|
20
|
+
module Atatus
|
21
|
+
# @api private
|
22
|
+
module Spies
|
23
|
+
# @api private
|
24
|
+
class SneakersSpy
|
25
|
+
include Logging
|
26
|
+
|
27
|
+
def self.supported_version?
|
28
|
+
Gem.loaded_specs['sneakers'].version >= Gem::Version.create('2.12.0')
|
29
|
+
end
|
30
|
+
|
31
|
+
def install
|
32
|
+
unless SneakersSpy.supported_version?
|
33
|
+
warn(
|
34
|
+
'Sneakers version is below 2.12.0. Sneakers spy installation failed'
|
35
|
+
)
|
36
|
+
return
|
37
|
+
end
|
38
|
+
|
39
|
+
Sneakers.middleware.use(Middleware, nil)
|
40
|
+
end
|
41
|
+
|
42
|
+
# @api private
|
43
|
+
class Middleware
|
44
|
+
def initialize(app, *args)
|
45
|
+
@app = app
|
46
|
+
@args = args
|
47
|
+
end
|
48
|
+
|
49
|
+
def call(deserialized_msg, delivery_info, metadata, handler)
|
50
|
+
transaction =
|
51
|
+
Atatus.start_transaction(
|
52
|
+
delivery_info.consumer.queue.name,
|
53
|
+
'Sneakers'
|
54
|
+
)
|
55
|
+
|
56
|
+
Atatus.set_label(:routing_key, delivery_info.routing_key)
|
57
|
+
|
58
|
+
res = @app.call(deserialized_msg, delivery_info, metadata, handler)
|
59
|
+
transaction&.done(:success)
|
60
|
+
|
61
|
+
res
|
62
|
+
rescue ::Exception => e
|
63
|
+
Atatus.report(e, handled: false)
|
64
|
+
transaction&.done(:error)
|
65
|
+
raise
|
66
|
+
ensure
|
67
|
+
Atatus.end_transaction
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
register 'Sneakers', 'sneakers', SneakersSpy.new
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# Licensed to Elasticsearch B.V. under one or more contributor
|
2
|
+
# license agreements. See the NOTICE file distributed with
|
3
|
+
# this work for additional information regarding copyright
|
4
|
+
# ownership. Elasticsearch B.V. licenses this file to you under
|
5
|
+
# the Apache License, Version 2.0 (the "License"); you may
|
6
|
+
# not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing,
|
12
|
+
# software distributed under the License is distributed on an
|
13
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14
|
+
# KIND, either express or implied. See the License for the
|
15
|
+
# specific language governing permissions and limitations
|
16
|
+
# under the License.
|
17
|
+
|
18
|
+
# frozen_string_literal: true
|
19
|
+
|
20
|
+
module Atatus
|
21
|
+
# @api private
|
22
|
+
module Spies
|
23
|
+
# @api private
|
24
|
+
class SuckerPunchSpy
|
25
|
+
TYPE = 'sucker_punch'
|
26
|
+
|
27
|
+
def install
|
28
|
+
if defined?(::SuckerPunch) && defined?(::SuckerPunch::Job) && defined?(::SuckerPunch::Job::ClassMethods)
|
29
|
+
|
30
|
+
::SuckerPunch::Job::ClassMethods.class_eval do
|
31
|
+
alias :__run_perform_without_atatus :__run_perform
|
32
|
+
|
33
|
+
def __run_perform(*args)
|
34
|
+
# This method is reached via JobClass#async_perform
|
35
|
+
# or JobClass#perform_in.
|
36
|
+
name = to_s
|
37
|
+
transaction = Atatus.start_transaction(name, TYPE)
|
38
|
+
__run_perform_without_atatus(*args)
|
39
|
+
transaction.done 'success'
|
40
|
+
rescue ::Exception => e
|
41
|
+
# Note that SuckerPunch by default doesn't raise the errors from
|
42
|
+
# the user-defined JobClass#perform method as it uses an error
|
43
|
+
# handler, accessed via `SuckerPunch.exception_handler`.
|
44
|
+
Atatus.report(e, handled: false)
|
45
|
+
transaction.done 'error'
|
46
|
+
raise
|
47
|
+
ensure
|
48
|
+
Atatus.end_transaction
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
register 'SuckerPunch', 'sucker_punch', SuckerPunchSpy.new
|
57
|
+
end
|
58
|
+
end
|
data/lib/atatus/spies/tilt.rb
CHANGED
@@ -1,3 +1,20 @@
|
|
1
|
+
# Licensed to Elasticsearch B.V. under one or more contributor
|
2
|
+
# license agreements. See the NOTICE file distributed with
|
3
|
+
# this work for additional information regarding copyright
|
4
|
+
# ownership. Elasticsearch B.V. licenses this file to you under
|
5
|
+
# the Apache License, Version 2.0 (the "License"); you may
|
6
|
+
# not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing,
|
12
|
+
# software distributed under the License is distributed on an
|
13
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14
|
+
# KIND, either express or implied. See the License for the
|
15
|
+
# specific language governing permissions and limitations
|
16
|
+
# under the License.
|
17
|
+
|
1
18
|
# frozen_string_literal: true
|
2
19
|
|
3
20
|
module Atatus
|
@@ -8,7 +25,8 @@ module Atatus
|
|
8
25
|
TYPE = 'template.tilt'
|
9
26
|
|
10
27
|
def install
|
11
|
-
if defined?(::Tilt) && defined?(::Tilt::Template)
|
28
|
+
if defined?(::Tilt) && defined?(::Tilt::Template)
|
29
|
+
|
12
30
|
::Tilt::Template.class_eval do
|
13
31
|
alias render_without_apm render
|
14
32
|
|
@@ -20,6 +38,7 @@ module Atatus
|
|
20
38
|
end
|
21
39
|
end
|
22
40
|
end
|
41
|
+
|
23
42
|
end
|
24
43
|
end
|
25
44
|
end
|
data/lib/atatus/spies.rb
CHANGED
@@ -1,6 +1,22 @@
|
|
1
|
+
# Licensed to Elasticsearch B.V. under one or more contributor
|
2
|
+
# license agreements. See the NOTICE file distributed with
|
3
|
+
# this work for additional information regarding copyright
|
4
|
+
# ownership. Elasticsearch B.V. licenses this file to you under
|
5
|
+
# the Apache License, Version 2.0 (the "License"); you may
|
6
|
+
# not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing,
|
12
|
+
# software distributed under the License is distributed on an
|
13
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14
|
+
# KIND, either express or implied. See the License for the
|
15
|
+
# specific language governing permissions and limitations
|
16
|
+
# under the License.
|
17
|
+
|
1
18
|
# frozen_string_literal: true
|
2
19
|
|
3
|
-
require 'forwardable'
|
4
20
|
require 'atatus/util/inflector'
|
5
21
|
|
6
22
|
module Atatus
|
@@ -64,23 +80,25 @@ module Atatus
|
|
64
80
|
end
|
65
81
|
end
|
66
82
|
|
67
|
-
|
68
|
-
|
69
|
-
|
83
|
+
unless ENV['ATATUS_SKIP_REQUIRE_PATCH'] == '1'
|
84
|
+
# @api private
|
85
|
+
module Kernel
|
86
|
+
private
|
70
87
|
|
71
|
-
|
88
|
+
alias require_without_apm require
|
72
89
|
|
73
|
-
|
74
|
-
|
90
|
+
def require(path)
|
91
|
+
res = require_without_apm(path)
|
75
92
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
93
|
+
begin
|
94
|
+
Atatus::Spies.hook_into(path)
|
95
|
+
rescue ::Exception => e
|
96
|
+
puts "Failed hooking into '#{path}'. Please report this at " \
|
97
|
+
'success@atatus.com'
|
98
|
+
puts e.backtrace.join("\n")
|
99
|
+
end
|
83
100
|
|
84
|
-
|
101
|
+
res
|
102
|
+
end
|
85
103
|
end
|
86
104
|
end
|
@@ -0,0 +1,169 @@
|
|
1
|
+
# Licensed to Elasticsearch B.V. under one or more contributor
|
2
|
+
# license agreements. See the NOTICE file distributed with
|
3
|
+
# this work for additional information regarding copyright
|
4
|
+
# ownership. Elasticsearch B.V. licenses this file to you under
|
5
|
+
# the Apache License, Version 2.0 (the "License"); you may
|
6
|
+
# not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing,
|
12
|
+
# software distributed under the License is distributed on an
|
13
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14
|
+
# KIND, either express or implied. See the License for the
|
15
|
+
# specific language governing permissions and limitations
|
16
|
+
# under the License.
|
17
|
+
|
18
|
+
# frozen_string_literal: true
|
19
|
+
|
20
|
+
require 'atatus/sql/tokenizer'
|
21
|
+
|
22
|
+
module Atatus
|
23
|
+
module Sql
|
24
|
+
# @api private
|
25
|
+
class Signature
|
26
|
+
include Tokens
|
27
|
+
|
28
|
+
# Mostly here to provide a similar API to new SqlSummarizer for easier
|
29
|
+
# swapping out
|
30
|
+
#
|
31
|
+
# @api private
|
32
|
+
class Summarizer
|
33
|
+
def summarize(sql)
|
34
|
+
Signature.parse(sql)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def initialize(sql)
|
39
|
+
@sql = sql
|
40
|
+
@tokenizer = Tokenizer.new(sql)
|
41
|
+
end
|
42
|
+
|
43
|
+
def parse
|
44
|
+
@tokenizer.scan # until tokenizer.token != COMMENT
|
45
|
+
|
46
|
+
parsed = parse_tokens
|
47
|
+
return parsed if parsed
|
48
|
+
|
49
|
+
# If all else fails, just return the first token of the query.
|
50
|
+
parts = @sql.split
|
51
|
+
return '' unless parts.any?
|
52
|
+
|
53
|
+
parts.first.upcase
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.parse(sql)
|
57
|
+
new(sql).parse
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
63
|
+
# rubocop:disable Metrics/PerceivedComplexity
|
64
|
+
def parse_tokens
|
65
|
+
t = @tokenizer
|
66
|
+
|
67
|
+
case t.token
|
68
|
+
|
69
|
+
when CALL
|
70
|
+
return unless scan_until IDENT
|
71
|
+
"CALL #{t.text}"
|
72
|
+
|
73
|
+
when DELETE
|
74
|
+
return unless scan_until FROM
|
75
|
+
return unless scan_token IDENT
|
76
|
+
table = scan_dotted_identifier
|
77
|
+
"DELETE FROM #{table}"
|
78
|
+
|
79
|
+
when INSERT, REPLACE
|
80
|
+
action = t.text
|
81
|
+
return unless scan_until INTO
|
82
|
+
return unless scan_token IDENT
|
83
|
+
table = scan_dotted_identifier
|
84
|
+
"#{action} INTO #{table}"
|
85
|
+
|
86
|
+
when SELECT
|
87
|
+
level = 0
|
88
|
+
while t.scan
|
89
|
+
case t.token
|
90
|
+
when LPAREN then level += 1
|
91
|
+
when RPAREN then level -= 1
|
92
|
+
when FROM
|
93
|
+
next unless level == 0
|
94
|
+
break unless scan_token IDENT
|
95
|
+
table = scan_dotted_identifier
|
96
|
+
return "SELECT FROM #{table}"
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
when UPDATE
|
101
|
+
# Scan for the table name. Some dialects allow option keywords before
|
102
|
+
# the table name.
|
103
|
+
return 'UPDATE' unless scan_token IDENT
|
104
|
+
|
105
|
+
table = t.text
|
106
|
+
|
107
|
+
period = false
|
108
|
+
first_period = false
|
109
|
+
|
110
|
+
while t.scan
|
111
|
+
case t.token
|
112
|
+
when IDENT
|
113
|
+
if period
|
114
|
+
table += t.text
|
115
|
+
period = false
|
116
|
+
end
|
117
|
+
|
118
|
+
unless first_period
|
119
|
+
table = t.text
|
120
|
+
end
|
121
|
+
|
122
|
+
# Two adjacent identifiers found after the first period. Ignore
|
123
|
+
# the secondary ones, in case they are unknown keywords.
|
124
|
+
when PERIOD
|
125
|
+
period = true
|
126
|
+
first_period = true
|
127
|
+
table += '.'
|
128
|
+
else
|
129
|
+
return "UPDATE #{table}"
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
135
|
+
# rubocop:enable Metrics/PerceivedComplexity
|
136
|
+
|
137
|
+
# Scans until finding token of `kind`
|
138
|
+
def scan_until(kind)
|
139
|
+
while @tokenizer.scan
|
140
|
+
break true if @tokenizer.token == kind
|
141
|
+
false
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
# Scans next token, ignoring comments
|
146
|
+
# Returns whether next token is of `kind`
|
147
|
+
def scan_token(kind)
|
148
|
+
while @tokenizer.scan
|
149
|
+
next if @tokenizer.token == COMMENT
|
150
|
+
break
|
151
|
+
end
|
152
|
+
|
153
|
+
return true if @tokenizer.token == kind
|
154
|
+
|
155
|
+
false
|
156
|
+
end
|
157
|
+
|
158
|
+
def scan_dotted_identifier
|
159
|
+
table = @tokenizer.text
|
160
|
+
|
161
|
+
while scan_token(PERIOD) && scan_token(IDENT)
|
162
|
+
table += ".#{@tokenizer.text}"
|
163
|
+
end
|
164
|
+
|
165
|
+
table
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|