elastic-apm 3.3.0 → 3.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ci/.jenkins_exclude.yml +43 -24
- data/.ci/.jenkins_framework.yml +2 -2
- data/.ci/.jenkins_ruby.yml +1 -1
- data/.ci/Jenkinsfile +288 -170
- data/.ci/docker/jruby/11-jdk/Dockerfile +40 -0
- data/.ci/docker/jruby/12-jdk/Dockerfile +40 -0
- data/.ci/docker/jruby/13-jdk/Dockerfile +40 -0
- data/.ci/docker/jruby/7-jdk/Dockerfile +40 -0
- data/.ci/docker/jruby/8-jdk/Dockerfile +40 -0
- data/.ci/docker/jruby/README.md +31 -0
- data/.ci/docker/jruby/run.sh +73 -0
- data/.ci/docker/jruby/test.sh +13 -0
- data/.ci/jobs/apm-agent-ruby-downstream.yml +1 -0
- data/.ci/jobs/apm-agent-ruby-linting-mbp.yml +1 -0
- data/.ci/jobs/apm-agent-ruby-mbp.yml +1 -0
- data/.ci/jobs/defaults.yml +1 -1
- data/.ci/packer_cache.sh +16 -0
- data/.github/ISSUE_TEMPLATE/Bug_report.md +40 -0
- data/.github/ISSUE_TEMPLATE/Feature_request.md +17 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +60 -0
- data/.gitignore +8 -0
- data/.rspec +0 -1
- data/.rubocop.yml +18 -0
- data/CHANGELOG.asciidoc +104 -1
- data/CONTRIBUTING.md +6 -48
- data/Gemfile +38 -10
- data/README.md +62 -13
- data/Rakefile +37 -5
- data/bench/sql.rb +49 -0
- data/bin/build_docs +1 -1
- data/bin/run-tests +4 -1
- data/docker-compose.yml +7 -0
- data/docs/api.asciidoc +52 -3
- data/docs/configuration.asciidoc +171 -22
- data/docs/getting-started-rails.asciidoc +2 -0
- data/docs/graphql.asciidoc +23 -0
- data/docs/images/dynamic-config.svg +1 -0
- data/docs/index.asciidoc +6 -1
- data/docs/introduction.asciidoc +2 -1
- data/docs/performance-tuning.asciidoc +106 -0
- data/docs/set-up.asciidoc +5 -2
- data/docs/supported-technologies.asciidoc +86 -1
- data/docs/upgrading.asciidoc +45 -0
- data/elastic-apm.gemspec +17 -0
- data/lib/elastic-apm.rb +17 -0
- data/lib/elastic_apm.rb +58 -5
- data/lib/elastic_apm/agent.rb +55 -4
- data/lib/elastic_apm/central_config.rb +27 -8
- data/lib/elastic_apm/central_config/cache_control.rb +17 -0
- data/lib/elastic_apm/child_durations.rb +26 -4
- data/lib/elastic_apm/config.rb +86 -5
- data/lib/elastic_apm/config/bytes.rb +17 -0
- data/lib/elastic_apm/config/duration.rb +17 -0
- data/lib/elastic_apm/config/options.rb +21 -5
- data/lib/elastic_apm/config/regexp_list.rb +17 -0
- data/lib/elastic_apm/config/wildcard_pattern_list.rb +20 -1
- data/lib/elastic_apm/context.rb +17 -0
- data/lib/elastic_apm/context/request.rb +17 -0
- data/lib/elastic_apm/context/request/socket.rb +17 -0
- data/lib/elastic_apm/context/request/url.rb +17 -0
- data/lib/elastic_apm/context/response.rb +27 -2
- data/lib/elastic_apm/context/user.rb +17 -0
- data/lib/elastic_apm/context_builder.rb +17 -0
- data/lib/elastic_apm/deprecations.rb +17 -0
- data/lib/elastic_apm/error.rb +17 -0
- data/lib/elastic_apm/error/exception.rb +17 -0
- data/lib/elastic_apm/error/log.rb +17 -0
- data/lib/elastic_apm/error_builder.rb +17 -0
- data/lib/elastic_apm/grape.rb +17 -0
- data/lib/elastic_apm/graphql.rb +91 -0
- data/lib/elastic_apm/grpc.rb +99 -0
- data/lib/elastic_apm/instrumenter.rb +49 -15
- data/lib/elastic_apm/internal_error.rb +17 -0
- data/lib/elastic_apm/logging.rb +17 -0
- data/lib/elastic_apm/metadata.rb +17 -0
- data/lib/elastic_apm/metadata/process_info.rb +17 -0
- data/lib/elastic_apm/metadata/service_info.rb +22 -2
- data/lib/elastic_apm/metadata/system_info.rb +17 -0
- data/lib/elastic_apm/metadata/system_info/container_info.rb +17 -0
- data/lib/elastic_apm/metrics.rb +33 -1
- data/lib/elastic_apm/metrics/breakdown_set.rb +17 -0
- data/lib/elastic_apm/metrics/cpu_mem_set.rb +17 -0
- data/lib/elastic_apm/metrics/metric.rb +23 -4
- data/lib/elastic_apm/metrics/set.rb +17 -0
- data/lib/elastic_apm/metrics/span_scoped_set.rb +17 -0
- data/lib/elastic_apm/metrics/transaction_set.rb +17 -0
- data/lib/elastic_apm/metrics/vm_set.rb +17 -0
- data/lib/elastic_apm/metricset.rb +17 -0
- data/lib/elastic_apm/middleware.rb +20 -4
- data/lib/elastic_apm/naively_hashable.rb +17 -0
- data/lib/elastic_apm/normalizers.rb +17 -0
- data/lib/elastic_apm/normalizers/grape.rb +17 -0
- data/lib/elastic_apm/normalizers/grape/endpoint_run.rb +18 -1
- data/lib/elastic_apm/normalizers/rails.rb +17 -0
- data/lib/elastic_apm/normalizers/rails/action_controller.rb +17 -0
- data/lib/elastic_apm/normalizers/rails/action_mailer.rb +17 -0
- data/lib/elastic_apm/normalizers/rails/action_view.rb +17 -0
- data/lib/elastic_apm/normalizers/rails/active_record.rb +29 -5
- data/lib/elastic_apm/opentracing.rb +66 -24
- data/lib/elastic_apm/rails.rb +18 -10
- data/lib/elastic_apm/railtie.rb +18 -1
- data/lib/elastic_apm/resque.rb +29 -0
- data/lib/elastic_apm/sinatra.rb +17 -0
- data/lib/elastic_apm/span.rb +20 -2
- data/lib/elastic_apm/span/context.rb +43 -44
- data/lib/elastic_apm/span/context/db.rb +43 -0
- data/lib/elastic_apm/span/context/destination.rb +77 -0
- data/lib/elastic_apm/span/context/http.rb +43 -0
- data/lib/elastic_apm/span_helpers.rb +17 -0
- data/lib/elastic_apm/spies.rb +33 -14
- data/lib/elastic_apm/spies/action_dispatch.rb +17 -0
- data/lib/elastic_apm/spies/delayed_job.rb +17 -0
- data/lib/elastic_apm/spies/elasticsearch.rb +49 -5
- data/lib/elastic_apm/spies/faraday.rb +53 -18
- data/lib/elastic_apm/spies/http.rb +35 -3
- data/lib/elastic_apm/spies/json.rb +17 -0
- data/lib/elastic_apm/spies/mongo.rb +23 -1
- data/lib/elastic_apm/spies/net_http.rb +50 -8
- data/lib/elastic_apm/spies/rake.rb +17 -0
- data/lib/elastic_apm/spies/redis.rb +17 -0
- data/lib/elastic_apm/spies/resque.rb +53 -0
- data/lib/elastic_apm/spies/sequel.rb +52 -15
- data/lib/elastic_apm/spies/shoryuken.rb +65 -0
- data/lib/elastic_apm/spies/sidekiq.rb +17 -0
- data/lib/elastic_apm/spies/sinatra.rb +17 -0
- data/lib/elastic_apm/spies/sneakers.rb +74 -0
- data/lib/elastic_apm/spies/sucker_punch.rb +54 -0
- data/lib/elastic_apm/spies/tilt.rb +17 -0
- data/lib/elastic_apm/sql.rb +36 -0
- data/lib/elastic_apm/sql/signature.rb +169 -0
- data/lib/elastic_apm/sql/tokenizer.rb +264 -0
- data/lib/elastic_apm/sql/tokens.rb +63 -0
- data/lib/elastic_apm/sql_summarizer.rb +18 -2
- data/lib/elastic_apm/stacktrace.rb +17 -0
- data/lib/elastic_apm/stacktrace/frame.rb +17 -0
- data/lib/elastic_apm/stacktrace_builder.rb +23 -1
- data/lib/elastic_apm/subscriber.rb +17 -0
- data/lib/elastic_apm/trace_context.rb +85 -49
- data/lib/elastic_apm/trace_context/traceparent.rb +113 -0
- data/lib/elastic_apm/trace_context/tracestate.rb +43 -0
- data/lib/elastic_apm/transaction.rb +43 -15
- data/lib/elastic_apm/transport/base.rb +39 -23
- data/lib/elastic_apm/transport/connection.rb +17 -0
- data/lib/elastic_apm/transport/connection/http.rb +17 -0
- data/lib/elastic_apm/transport/connection/proxy_pipe.rb +24 -2
- data/lib/elastic_apm/transport/filters.rb +17 -0
- data/lib/elastic_apm/transport/filters/hash_sanitizer.rb +77 -0
- data/lib/elastic_apm/transport/filters/secrets_filter.rb +29 -53
- data/lib/elastic_apm/transport/headers.rb +21 -0
- data/lib/elastic_apm/transport/serializers.rb +17 -0
- data/lib/elastic_apm/transport/serializers/context_serializer.rb +17 -0
- data/lib/elastic_apm/transport/serializers/error_serializer.rb +17 -0
- data/lib/elastic_apm/transport/serializers/metadata_serializer.rb +44 -20
- data/lib/elastic_apm/transport/serializers/metricset_serializer.rb +17 -0
- data/lib/elastic_apm/transport/serializers/span_serializer.rb +45 -8
- data/lib/elastic_apm/transport/serializers/transaction_serializer.rb +17 -0
- data/lib/elastic_apm/transport/user_agent.rb +17 -0
- data/lib/elastic_apm/transport/worker.rb +17 -0
- data/lib/elastic_apm/util.rb +17 -0
- data/lib/elastic_apm/util/inflector.rb +17 -0
- data/lib/elastic_apm/util/lru_cache.rb +17 -0
- data/lib/elastic_apm/util/throttle.rb +17 -0
- data/lib/elastic_apm/version.rb +18 -1
- metadata +36 -7
- data/.ci/downstreamTests.groovy +0 -185
- data/.ci/prepare-git-context.sh +0 -24
- data/.github/workflows/main.yml +0 -14
- data/CHANGELOG.md +0 -1
@@ -0,0 +1,63 @@
|
|
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 ElasticAPM
|
21
|
+
module Sql
|
22
|
+
# @api private
|
23
|
+
module Tokens
|
24
|
+
OTHER = :OTHER
|
25
|
+
COMMENT = :COMMENT
|
26
|
+
IDENT = :IDENT
|
27
|
+
NUMBER = :NUMBER
|
28
|
+
STRING = :STRING
|
29
|
+
|
30
|
+
PERIOD = :PERIOD
|
31
|
+
COMMA = :COMMA
|
32
|
+
LPAREN = :LPAREN
|
33
|
+
RPAREN = :RPAREN
|
34
|
+
|
35
|
+
AS = :AS
|
36
|
+
CALL = :CALL
|
37
|
+
DELETE = :DELETE
|
38
|
+
FROM = :FROM
|
39
|
+
INSERT = :INSERT
|
40
|
+
INTO = :INTO
|
41
|
+
OR = :OR
|
42
|
+
REPLACE = :REPLACE
|
43
|
+
SELECT = :SELECT
|
44
|
+
SET = :SET
|
45
|
+
TABLE = :TABLE
|
46
|
+
TRUNCATE = :TRUNCATE
|
47
|
+
UPDATE = :UPDATE
|
48
|
+
|
49
|
+
KEYWORDS = {
|
50
|
+
2 => [AS, OR],
|
51
|
+
3 => [SET],
|
52
|
+
4 => [CALL, FROM, INTO],
|
53
|
+
5 => [TABLE],
|
54
|
+
6 => [DELETE, INSERT, SELECT, UPDATE],
|
55
|
+
7 => [REPLACE],
|
56
|
+
8 => [TRUNCATE]
|
57
|
+
}.freeze
|
58
|
+
|
59
|
+
KEYWORD_MIN_LENGTH = KEYWORDS.keys.first
|
60
|
+
KEYWORD_MAX_LENGTH = KEYWORDS.keys.last
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -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
|
require 'elastic_apm/util/lru_cache'
|
@@ -18,14 +35,13 @@ module ElasticAPM
|
|
18
35
|
}.freeze
|
19
36
|
|
20
37
|
FORMAT = '%s%s'
|
21
|
-
UTF8 = 'UTF-8'
|
22
38
|
|
23
39
|
def self.cache
|
24
40
|
@cache ||= Util::LruCache.new
|
25
41
|
end
|
26
42
|
|
27
43
|
def summarize(sql)
|
28
|
-
sql = sql.encode(
|
44
|
+
sql = sql.encode('utf-8', invalid: :replace, undef: :replace)
|
29
45
|
self.class.cache[sql] ||=
|
30
46
|
REGEXES.find do |regex, sig|
|
31
47
|
if (match = sql[0...1000].match(regex))
|
@@ -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 ElasticAPM
|
@@ -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
|
require 'elastic_apm/naively_hashable'
|
@@ -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
|
require 'elastic_apm/stacktrace/frame'
|
@@ -12,7 +29,12 @@ module ElasticAPM
|
|
12
29
|
RUBY_VERS_REGEX = %r{ruby(/gems)?[-/](\d+\.)+\d}.freeze
|
13
30
|
JRUBY_ORG_REGEX = %r{org/jruby}.freeze
|
14
31
|
|
15
|
-
GEMS_PATH =
|
32
|
+
GEMS_PATH =
|
33
|
+
if defined?(Bundler) && Bundler.default_bundle_dir
|
34
|
+
Bundler.bundle_path.to_s
|
35
|
+
else
|
36
|
+
Gem.dir
|
37
|
+
end
|
16
38
|
|
17
39
|
def initialize(config)
|
18
40
|
@config = config
|
@@ -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
|
require 'active_support/notifications'
|
@@ -1,82 +1,118 @@
|
|
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
|
|
20
|
+
require 'elastic_apm/trace_context/tracestate'
|
21
|
+
require 'elastic_apm/trace_context/traceparent'
|
22
|
+
|
3
23
|
module ElasticAPM
|
4
24
|
# @api private
|
5
25
|
class TraceContext
|
6
|
-
|
26
|
+
extend Forwardable
|
7
27
|
|
8
|
-
|
9
|
-
HEX_REGEX = /[^[:xdigit:]]/.freeze
|
10
|
-
|
11
|
-
TRACE_ID_LENGTH = 16
|
12
|
-
ID_LENGTH = 8
|
28
|
+
class InvalidTraceparentHeader < StandardError; end
|
13
29
|
|
14
30
|
def initialize(
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
id: nil,
|
19
|
-
recorded: true
|
31
|
+
traceparent: nil,
|
32
|
+
tracestate: nil,
|
33
|
+
**legacy_traceparent_attrs
|
20
34
|
)
|
21
|
-
@
|
22
|
-
@
|
23
|
-
# TODO: rename to parent_id with next major version bump
|
24
|
-
@parent_id = span_id
|
25
|
-
@id = id || hex(ID_LENGTH)
|
26
|
-
@recorded = recorded
|
35
|
+
@traceparent = traceparent || Traceparent.new(**legacy_traceparent_attrs)
|
36
|
+
@tracestate = tracestate
|
27
37
|
end
|
28
38
|
|
29
|
-
attr_accessor :
|
39
|
+
attr_accessor :traceparent, :tracestate
|
40
|
+
|
41
|
+
def_delegators :traceparent,
|
42
|
+
:version, :trace_id, :id, :parent_id, :ensure_parent_id, :recorded?
|
43
|
+
|
44
|
+
class << self
|
45
|
+
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
46
|
+
def parse(legacy_header = nil, env: nil, metadata: nil)
|
47
|
+
unless legacy_header || env || metadata
|
48
|
+
raise ArgumentError, 'TraceContext expects env:, metadata: ' \
|
49
|
+
'or single argument header string'
|
50
|
+
end
|
51
|
+
|
52
|
+
if legacy_header
|
53
|
+
legacy_parse_from_header(legacy_header)
|
54
|
+
elsif env
|
55
|
+
trace_context_from_env(env)
|
56
|
+
elsif metadata
|
57
|
+
trace_context_from_metadata(metadata)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
61
|
+
|
62
|
+
private
|
30
63
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
64
|
+
def trace_context_from_env(env)
|
65
|
+
return unless (
|
66
|
+
header =
|
67
|
+
env['HTTP_ELASTIC_APM_TRACEPARENT'] || env['HTTP_TRACEPARENT']
|
68
|
+
)
|
35
69
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
70
|
+
parent = TraceContext::Traceparent.parse(header)
|
71
|
+
|
72
|
+
state =
|
73
|
+
if (header = env['HTTP_TRACESTATE'])
|
74
|
+
TraceContext::Tracestate.parse(header)
|
40
75
|
end
|
41
76
|
|
42
|
-
|
43
|
-
raise InvalidTraceparentHeader if HEX_REGEX =~ t.parent_id
|
77
|
+
new(traceparent: parent, tracestate: state)
|
44
78
|
end
|
45
|
-
end
|
46
79
|
|
47
|
-
|
48
|
-
|
80
|
+
def trace_context_from_metadata(metadata)
|
81
|
+
return unless (header = metadata['elastic-apm-traceparent'] ||
|
82
|
+
metadata['traceparent'])
|
49
83
|
|
50
|
-
|
51
|
-
end
|
84
|
+
parent = TraceContext::Traceparent.parse(header)
|
52
85
|
|
53
|
-
|
54
|
-
|
55
|
-
|
86
|
+
state =
|
87
|
+
if (header = metadata['tracestate'])
|
88
|
+
TraceContext::Tracestate.parse(header)
|
89
|
+
end
|
56
90
|
|
57
|
-
|
58
|
-
|
59
|
-
end
|
91
|
+
new(traceparent: parent, tracestate: state)
|
92
|
+
end
|
60
93
|
|
61
|
-
|
62
|
-
|
94
|
+
def legacy_parse_from_header(header)
|
95
|
+
parent = Traceparent.parse(header)
|
96
|
+
new(traceparent: parent)
|
97
|
+
end
|
63
98
|
end
|
64
99
|
|
65
100
|
def child
|
66
101
|
dup.tap do |tc|
|
67
|
-
tc.
|
68
|
-
tc.id = hex(ID_LENGTH)
|
102
|
+
tc.traceparent = tc.traceparent.child
|
69
103
|
end
|
70
104
|
end
|
71
105
|
|
72
|
-
def
|
73
|
-
|
74
|
-
|
106
|
+
def apply_headers
|
107
|
+
yield 'Traceparent', traceparent.to_header
|
108
|
+
|
109
|
+
if tracestate
|
110
|
+
yield 'Tracestate', tracestate.to_header
|
111
|
+
end
|
75
112
|
|
76
|
-
|
113
|
+
return unless ElasticAPM.agent.config.use_elastic_traceparent_header
|
77
114
|
|
78
|
-
|
79
|
-
SecureRandom.hex(len)
|
115
|
+
yield 'Elastic-Apm-Traceparent', traceparent.to_header
|
80
116
|
end
|
81
117
|
end
|
82
118
|
end
|
@@ -0,0 +1,113 @@
|
|
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 ElasticAPM
|
21
|
+
class TraceContext
|
22
|
+
# @api private
|
23
|
+
class Traceparent
|
24
|
+
VERSION = '00'
|
25
|
+
HEX_REGEX = /[^[:xdigit:]]/.freeze
|
26
|
+
|
27
|
+
TRACE_ID_LENGTH = 16
|
28
|
+
ID_LENGTH = 8
|
29
|
+
|
30
|
+
# rubocop:disable Metrics/ParameterLists
|
31
|
+
def initialize(
|
32
|
+
version: VERSION,
|
33
|
+
trace_id: nil,
|
34
|
+
span_id: nil,
|
35
|
+
id: nil,
|
36
|
+
recorded: true,
|
37
|
+
tracestate: nil
|
38
|
+
)
|
39
|
+
@version = version
|
40
|
+
@trace_id = trace_id || hex(TRACE_ID_LENGTH)
|
41
|
+
# TODO: rename span_id kw arg to parent_id with next major version bump
|
42
|
+
@parent_id = span_id
|
43
|
+
@id = id || hex(ID_LENGTH)
|
44
|
+
@recorded = recorded
|
45
|
+
@tracestate = tracestate
|
46
|
+
end
|
47
|
+
# rubocop:enable Metrics/ParameterLists
|
48
|
+
|
49
|
+
attr_accessor :version, :id, :trace_id, :parent_id, :recorded, :tracestate
|
50
|
+
|
51
|
+
alias :recorded? :recorded
|
52
|
+
|
53
|
+
def self.parse(header)
|
54
|
+
raise_invalid(header) unless header.length == 55
|
55
|
+
raise_invalid(header) unless header[0..1] == VERSION
|
56
|
+
|
57
|
+
new.tap do |t|
|
58
|
+
t.version, t.trace_id, t.parent_id, t.flags =
|
59
|
+
header.split('-').tap do |values|
|
60
|
+
values[-1] = Util.hex_to_bits(values[-1])
|
61
|
+
end
|
62
|
+
|
63
|
+
raise_invalid(header) if HEX_REGEX =~ t.trace_id
|
64
|
+
raise_invalid(header) if HEX_REGEX =~ t.parent_id
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
class << self
|
69
|
+
private
|
70
|
+
|
71
|
+
def raise_invalid(header)
|
72
|
+
raise InvalidTraceparentHeader,
|
73
|
+
"Couldn't parse invalid traceparent header: #{header.inspect}"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def flags=(flags)
|
78
|
+
@flags = flags
|
79
|
+
|
80
|
+
self.recorded = flags[7] == '1'
|
81
|
+
end
|
82
|
+
|
83
|
+
def flags
|
84
|
+
format('0000000%d', recorded? ? 1 : 0)
|
85
|
+
end
|
86
|
+
|
87
|
+
def hex_flags
|
88
|
+
format('%02x', flags.to_i(2))
|
89
|
+
end
|
90
|
+
|
91
|
+
def ensure_parent_id
|
92
|
+
@parent_id ||= hex(ID_LENGTH)
|
93
|
+
end
|
94
|
+
|
95
|
+
def child
|
96
|
+
dup.tap do |copy|
|
97
|
+
copy.parent_id = id
|
98
|
+
copy.id = hex(ID_LENGTH)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def to_header
|
103
|
+
format('%s-%s-%s-%s', version, trace_id, id, hex_flags)
|
104
|
+
end
|
105
|
+
|
106
|
+
private
|
107
|
+
|
108
|
+
def hex(len)
|
109
|
+
SecureRandom.hex(len)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|