Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor:Move distributed propagation to Contrib #2352

Merged
merged 56 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
76dfe37
Merge branch 'master' into refactor-propagation-2
marcotc Nov 3, 2022
135e58e
Avoid duplication in distributed tracing strategies
marcotc Nov 1, 2022
b621972
Merge branch 'feat-Sampling-Propagation' into refactor-propagation-2
marcotc Nov 8, 2022
243050e
Merge branch 'feat-Sampling-Propagation' into refactor-propagation-2
marcotc Nov 8, 2022
0166025
Simplity test constants
marcotc Nov 8, 2022
af7c159
GRPC fixes
marcotc Nov 8, 2022
a5c5513
Expand comment
marcotc Nov 8, 2022
7f9e59e
Merge branch 'master' into refactor-propagation-2
marcotc Nov 9, 2022
6d2d00f
Move core distributed to tracing
marcotc Nov 9, 2022
c918273
Clean up language around headers
marcotc Nov 9, 2022
fe38b24
Remove comment
marcotc Nov 10, 2022
72bc3a6
Freeze string literals
marcotc Nov 10, 2022
f8ecf3a
Add String refinement for unary plus
marcotc Nov 10, 2022
f05e27c
Simplify test
marcotc Nov 10, 2022
6eecfbc
Sorbet
marcotc Nov 10, 2022
13ef267
Add comment on public api method not raising error
marcotc Nov 14, 2022
cedfe2b
Use duck typying
marcotc Nov 14, 2022
5650505
Remove misleading comment about nil returns
marcotc Nov 14, 2022
8f86955
Document mismatched context extraction
marcotc Nov 14, 2022
2830b3a
Move debug log to block
marcotc Nov 14, 2022
e261e8e
Remove one-line classes
marcotc Nov 15, 2022
62fcea8
Make fetcher mandatory
marcotc Nov 15, 2022
17bfa34
Update lib/datadog/tracing/distributed/b3_single.rb
marcotc Nov 15, 2022
c81fd81
Update lib/datadog/tracing/distributed/datadog.rb
marcotc Nov 15, 2022
e877d99
Add _key to propagation key names
marcotc Nov 15, 2022
998ffb3
Sacrifice for the linting gods ☠️
marcotc Nov 15, 2022
70dd590
Use keyword argument for base
marcotc Nov 15, 2022
1374774
Fix log message tests
marcotc Nov 15, 2022
076a784
Apply suggestion
marcotc Nov 15, 2022
7c394e0
Copy dev comment
marcotc Nov 15, 2022
6462cf8
Example constant comment
marcotc Nov 15, 2022
b4c8239
B3 single header test shared variable
marcotc Nov 15, 2022
52da1c1
Update spec/datadog/tracing/distributed/fetcher_spec.rb
marcotc Nov 15, 2022
bb60a3a
Update last b3 key
marcotc Nov 15, 2022
df12297
Remove unused files after refactor
marcotc Nov 15, 2022
98bf97f
Add return value to inject!
marcotc Nov 15, 2022
998f35b
Revert mistakenly moved metadata file
marcotc Nov 15, 2022
61c3b9a
Revert mistakenly moved metadata file
marcotc Nov 15, 2022
bb08ca8
Remove unused rquire
marcotc Nov 15, 2022
5efac8a
Remove unused require
marcotc Nov 15, 2022
35dddbc
Consolidate fetcher and helper tests
marcotc Nov 16, 2022
b39ce1c
Refactor lib/datadog/tracing/distributed/datadog.rb
marcotc Nov 16, 2022
658f497
Rubocop
marcotc Nov 16, 2022
1bbeb5e
Simplify rack header logic
marcotc Nov 16, 2022
bf39755
Revert refinement changes
marcotc Nov 16, 2022
6c69871
Make ext headers its own copy
marcotc Nov 16, 2022
0975630
Move constants closer to ownership
marcotc Nov 16, 2022
23f75b5
Fix typo
marcotc Nov 16, 2022
11d3103
Update lib/datadog/tracing/distributed/datadog.rb
marcotc Nov 16, 2022
ebf82b8
Update lib/datadog/tracing/distributed/propagation.rb
marcotc Nov 16, 2022
51ea9f4
Inline x-datadog-origin in tests
marcotc Nov 16, 2022
8af6f31
Add test to protect to_i
marcotc Nov 16, 2022
2150c88
Fix ext migration
marcotc Nov 16, 2022
129d823
Disable sorbet due to *args
marcotc Nov 16, 2022
9df6d9d
Fix no style injection test
marcotc Nov 16, 2022
573eaf1
Change description on to_i invalid format
marcotc Nov 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions lib/datadog/core/utils/refinement.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true
# typed: false

module Datadog
module Core
module Utils
# A collection of refinements for core Ruby features.
# Backports future features to old rubies.
module Refinement
# rubocop:disable Style/Documentation
module Regexp
refine ::Regexp do
# `Regexp::match?` is measurably the most performant
# way to check if a String matches a regular expression.
#
# Introduced in Ruby 2.4.
def match?(*args)
!match(*args).nil?
end
end
end

module String
refine ::String do
# When not sure if a String is mutable and but it's necessary to perform
# changes to it, `+@` is measurable faster than a possibly unnecessary `.dup`.
#
# Introduced in Ruby 2.3.
def +@
frozen? ? dup : self
end
end
end
# rubocop:enable Style/Documentation
end
end
end
end
4 changes: 2 additions & 2 deletions lib/datadog/opentracer/distributed_headers.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# typed: true

require_relative '../tracing/span'
require_relative '../tracing/distributed/headers/ext'
require_relative '../tracing/distributed/ext'

module Datadog
module OpenTracer
# DistributedHeaders provides easy access and validation to headers
# @public_api
class DistributedHeaders
include Tracing::Distributed::Headers::Ext
include Tracing::Distributed::Ext

def initialize(carrier)
@carrier = carrier
Expand Down
6 changes: 3 additions & 3 deletions lib/datadog/opentracer/rack_propagator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# typed: true

require_relative '../tracing/context'
require_relative '../tracing/distributed/headers/ext'
require_relative '../tracing/distributed/ext'
require_relative '../tracing/propagation/http'
require_relative '../tracing/trace_operation'
require_relative 'propagator'
Expand All @@ -11,8 +11,8 @@ module OpenTracer
# OpenTracing propagator for Datadog::OpenTracer::Tracer
module RackPropagator
extend Propagator
extend Tracing::Distributed::Headers::Ext
include Tracing::Distributed::Headers::Ext
extend Tracing::Distributed::Ext
include Tracing::Distributed::Ext

BAGGAGE_PREFIX = 'ot-baggage-'.freeze
BAGGAGE_PREFIX_FORMATTED = 'HTTP_OT_BAGGAGE_'.freeze
Expand Down
6 changes: 3 additions & 3 deletions lib/datadog/opentracer/text_map_propagator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# typed: true

require_relative '../tracing/context'
require_relative '../tracing/distributed/headers/ext'
require_relative '../tracing/distributed/ext'
require_relative '../tracing/trace_operation'
require_relative 'propagator'

Expand All @@ -10,8 +10,8 @@ module OpenTracer
# OpenTracing propagator for Datadog::OpenTracer::Tracer
module TextMapPropagator
extend Propagator
extend Tracing::Distributed::Headers::Ext
include Tracing::Distributed::Headers::Ext
extend Tracing::Distributed::Ext
include Tracing::Distributed::Ext

BAGGAGE_PREFIX = 'ot-baggage-'.freeze

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_relative '../../../../tracing'
require_relative '../../../metadata/ext'
require_relative '../distributed/propagation'
require_relative '../../analytics'
require_relative '../ext'
require_relative '../../ext'
Expand Down Expand Up @@ -55,7 +56,7 @@ def annotate!(trace, span, metadata, call)
# Set analytics sample rate
Contrib::Analytics.set_sample_rate(span, analytics_sample_rate) if analytics_enabled?

Tracing::Propagation::GRPC.inject!(trace, metadata) if distributed_tracing?
Distributed::Propagation::INSTANCE.inject!(trace, metadata) if distributed_tracing?
rescue StandardError => e
Datadog.logger.debug("GRPC client trace failed: #{e}")
end
Expand Down
17 changes: 5 additions & 12 deletions lib/datadog/tracing/contrib/grpc/datadog_interceptor/server.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# typed: ignore

require_relative '../../../../tracing'
require_relative '../../../distributed/headers/ext'
require_relative '../../../distributed/ext'
require_relative '../../../metadata/ext'
require_relative '../../../propagation/grpc'
require_relative '../distributed/propagation'
require_relative '../../analytics'
require_relative '../ext'
require_relative '../../ext'
Expand Down Expand Up @@ -45,7 +45,7 @@ def trace(keywords)
private

def set_distributed_context!(metadata)
Tracing.continue_trace!(Tracing::Propagation::GRPC.extract(metadata))
Tracing.continue_trace!(Distributed::Propagation::INSTANCE.extract(metadata))
rescue StandardError => e
Datadog.logger.debug(
"unable to propagate GRPC metadata to context: #{e}"
Expand All @@ -54,7 +54,8 @@ def set_distributed_context!(metadata)

def annotate!(span, metadata)
metadata.each do |header, value|
next if reserved_headers.include?(header)
# Datadog propagation headers are considered internal implementation detail.
next if header.to_s.start_with?(Tracing::Distributed::Ext::DATADOG_PREFIX)

span.set_tag(header, value)
end
Expand All @@ -71,14 +72,6 @@ def annotate!(span, metadata)
Datadog.logger.debug("GRPC client trace failed: #{e}")
end

def reserved_headers
[
Tracing::Distributed::Headers::Ext::GRPC_METADATA_TRACE_ID,
Tracing::Distributed::Headers::Ext::GRPC_METADATA_PARENT_ID,
Tracing::Distributed::Headers::Ext::GRPC_METADATA_SAMPLING_PRIORITY
]
end

def format_resource(proto_method)
proto_method
.owner
Expand Down
24 changes: 24 additions & 0 deletions lib/datadog/tracing/contrib/grpc/distributed/b3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true
# typed: true

require_relative '../../../distributed/b3'
require_relative 'fetcher'

module Datadog
module Tracing
module Contrib
module GRPC
module Distributed
# B3-style trace propagation through gRPC metadata.
# @see https://github.com/openzipkin/b3-propagation#multiple-headers
# @see https://github.com/grpc/grpc-go/blob/v1.50.1/Documentation/grpc-metadata.md gRPC metadata
class B3 < Tracing::Distributed::B3
def initialize
super(fetcher: Fetcher)
end
end
end
end
end
end
end
24 changes: 24 additions & 0 deletions lib/datadog/tracing/contrib/grpc/distributed/b3_single.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true
# typed: true

require_relative '../../../distributed/b3_single'
require_relative 'fetcher'

module Datadog
module Tracing
module Contrib
module GRPC
module Distributed
# B3 single header-style trace propagation through gRPC metadata.
# @see https://github.com/openzipkin/b3-propagation#single-header
# @see https://github.com/grpc/grpc-go/blob/v1.50.1/Documentation/grpc-metadata.md gRPC metadata
class B3Single < Tracing::Distributed::B3Single
def initialize
super(fetcher: Fetcher)
end
end
end
end
end
end
end
23 changes: 23 additions & 0 deletions lib/datadog/tracing/contrib/grpc/distributed/datadog.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true
# typed: true

require_relative '../../../distributed/datadog'
require_relative 'fetcher'

module Datadog
module Tracing
module Contrib
module GRPC
module Distributed
# Datadog-style trace propagation through gRPC metadata.
# @see https://github.com/grpc/grpc-go/blob/v1.50.1/Documentation/grpc-metadata.md gRPC metadata
class Datadog < Tracing::Distributed::Datadog
def initialize
super(fetcher: Fetcher)
end
end
end
end
end
end
end
23 changes: 23 additions & 0 deletions lib/datadog/tracing/contrib/grpc/distributed/fetcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true
# typed: true

require_relative '../../../distributed/fetcher'

module Datadog
module Tracing
module Contrib
module GRPC
module Distributed
# Retrieves values from the gRPC metadata.
class Fetcher < Tracing::Distributed::Fetcher
def [](key)
# Metadata values can be arrays (multiple values for the same key)
value = super(key)
value.is_a?(::Array) ? value[0] : value
end
end
end
end
end
end
end
33 changes: 33 additions & 0 deletions lib/datadog/tracing/contrib/grpc/distributed/propagation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true
# typed: true

require_relative '../../../distributed/propagation'
require_relative 'b3'
require_relative 'b3_single'
require_relative 'datadog'

module Datadog
module Tracing
module Contrib
module GRPC
module Distributed
# Extracts and injects propagation through gRPC metadata.
class Propagation < Tracing::Distributed::Propagation
def initialize
super(
propagation_styles: {
Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_B3 => B3.new,
Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_B3_SINGLE_HEADER => B3Single.new,
Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_DATADOG => Datadog.new,
})
end

# DEV: Singleton kept until a larger refactor is performed.
# DEV: See {Datadog::Tracing::Distributed::Propagation#initialize} for more information.
INSTANCE = Propagation.new
end
end
end
end
end
end
2 changes: 0 additions & 2 deletions lib/datadog/tracing/contrib/grpc/patcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ def target_version
end

def patch
require_relative '../../propagation/grpc'
require_relative 'datadog_interceptor'
Comment on lines -22 to -23
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These were just unnecessary and actually the responsibility of intercept_with_datadog.rb.

require_relative 'intercept_with_datadog'

prepend_interceptor
Expand Down
23 changes: 23 additions & 0 deletions lib/datadog/tracing/contrib/http/distributed/b3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true
# typed: true

require_relative '../../../distributed/b3'
require_relative 'fetcher'

module Datadog
module Tracing
module Contrib
module HTTP
module Distributed
# B3-style trace propagation through HTTP headers.
# @see https://github.com/openzipkin/b3-propagation#multiple-headers
class B3 < Tracing::Distributed::B3
def initialize
super(fetcher: Fetcher)
end
end
end
marcotc marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
end
23 changes: 23 additions & 0 deletions lib/datadog/tracing/contrib/http/distributed/b3_single.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true
# typed: true

require_relative '../../../distributed/b3_single'
require_relative 'fetcher'

module Datadog
module Tracing
module Contrib
module HTTP
module Distributed
# B3 single header-style trace propagation through HTTP headers.
# @see https://github.com/openzipkin/b3-propagation#single-header
class B3Single < Tracing::Distributed::B3Single
def initialize
super(fetcher: Fetcher)
end
end
end
end
end
end
end
22 changes: 22 additions & 0 deletions lib/datadog/tracing/contrib/http/distributed/datadog.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true
# typed: true

require_relative '../../../distributed/datadog'
require_relative 'fetcher'

module Datadog
module Tracing
module Contrib
module HTTP
module Distributed
# Datadog-style trace propagation through HTTP headers.
class Datadog < Tracing::Distributed::Datadog
def initialize
super(fetcher: Fetcher)
end
end
end
end
end
end
end
Loading