-
Notifications
You must be signed in to change notification settings - Fork 375
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
Add SNS/SQS trace propagation #3842
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,38 @@ module Aws | |
module Service | ||
# Base class for all AWS service-specific tag handlers. | ||
class Base | ||
def before_span(config, context, response); end | ||
def process(config, trace, context); end | ||
def add_tags(span, params); end | ||
|
||
MESSAGE_ATTRIBUTES_LIMIT = 10 # Can't set more than 10 message attributes | ||
|
||
# Extract the `_datadog` message attribute and decode its JSON content. | ||
def extract_propagation!(response, data_type) | ||
messages = response.data.messages | ||
|
||
# DEV: Extract the context from the first message today. | ||
# DEV: Use span links in the future to support multiple messages related to a single span. | ||
return unless (message = messages[0]) | ||
|
||
message_attributes = message.message_attributes | ||
|
||
return unless message_attributes && (datadog = message_attributes['_datadog']) | ||
|
||
if (data = datadog[data_type]) && (parsed_data = JSON.parse(data)) | ||
Tracing.continue_trace!(Distributed.extract(parsed_data)) | ||
end | ||
end | ||
|
||
def inject_propagation(trace, params, data_type) | ||
message_attributes = (params[:message_attributes] ||= {}) | ||
return if message_attributes.size >= MESSAGE_ATTRIBUTES_LIMIT | ||
|
||
data = {} | ||
if Distributed.inject(trace.to_digest, data) | ||
message_attributes['_datadog'] = { :data_type => data_type, :binary_value => data.to_json } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⚪ Code Quality ViolationUse newer syntax for a hash with symbols as keys (...read more)The rule "Use new syntax when keys are symbols" is a coding standard in modern Ruby development. It encourages the use of the new hash syntax, introduced in Ruby 1.9, where symbols are used as keys. The old hash rocket syntax ( This rule is important as it promotes a cleaner, more readable code. The new syntax is more concise and less cluttered, making it easier to understand the structure and purpose of the hash. This is particularly beneficial in large codebases or when hashes are nested or complex. To adhere to this rule, always use the new syntax when defining hashes where keys are symbols. Instead of defining a hash with |
||
end | ||
end | ||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'distributed/b3_multi' | ||
require_relative 'distributed/b3_single' | ||
require_relative 'distributed/datadog' | ||
require_relative 'distributed/none' | ||
require_relative 'distributed/propagation' | ||
require_relative 'distributed/trace_context' | ||
require_relative 'contrib/component' | ||
|
||
module Datadog | ||
module Tracing | ||
# Namespace for distributed tracing propagation and correlation | ||
module Distributed | ||
module_function | ||
|
||
# Inject distributed headers into the given request | ||
# @param digest [Datadog::Tracing::TraceDigest] the trace to inject | ||
# @param data [Hash] the request to inject | ||
def inject(digest, data) | ||
raise 'Please invoke Datadog.configure at least once before calling this method' unless @propagation | ||
|
||
@propagation.inject!(digest, data) | ||
end | ||
|
||
# Extract distributed headers from the given request | ||
# @param data [Hash] the request to extract from | ||
# @return [Datadog::Tracing::TraceDigest,nil] the extracted trace digest or nil if none was found | ||
def extract(data) | ||
raise 'Please invoke Datadog.configure at least once before calling this method' unless @propagation | ||
|
||
@propagation.extract(data) | ||
end | ||
|
||
Contrib::Component.register('distributed') do |config| | ||
tracing = config.tracing | ||
# DEV: evaluate propagation_style in case it overrides propagation_style_extract & propagation_extract_first | ||
tracing.propagation_style | ||
|
||
@propagation = Propagation.new( | ||
propagation_styles: { | ||
Configuration::Ext::Distributed::PROPAGATION_STYLE_B3_MULTI_HEADER => | ||
B3Multi.new(fetcher: Fetcher), | ||
Configuration::Ext::Distributed::PROPAGATION_STYLE_B3_SINGLE_HEADER => | ||
B3Single.new(fetcher: Fetcher), | ||
Configuration::Ext::Distributed::PROPAGATION_STYLE_DATADOG => | ||
Datadog.new(fetcher: Fetcher), | ||
Configuration::Ext::Distributed::PROPAGATION_STYLE_TRACE_CONTEXT => | ||
TraceContext.new(fetcher: Fetcher), | ||
Configuration::Ext::Distributed::PROPAGATION_STYLE_NONE => None.new | ||
}, | ||
propagation_style_inject: tracing.propagation_style_inject, | ||
propagation_style_extract: tracing.propagation_style_extract, | ||
propagation_extract_first: tracing.propagation_extract_first | ||
) | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⚪ Code Quality Violation
Improve readability with first (...read more)
This rule encourages the use of
first
andlast
methods over array indexing to access the first and last elements of an array, respectively. The primary reason behind this rule is to improve code readability. Usingfirst
andlast
makes it immediately clear that you are accessing the first or last element of the array, which might not be immediately obvious with array indexing, especially for developers who are new to Ruby.The use of these methods also helps to make your code more idiomatic, which is a crucial aspect of writing effective Ruby code. Idiomatic code is easier to read, understand, and maintain. It also tends to be more efficient, as idioms often reflect patterns that are optimized for the language.
To adhere to this rule, replace the use of array indexing with
first
orlast
methods when you want to access the first and last elements of an array. For instance, instead ofarr[0]
usearr.first
and instead ofarr[-1]
usearr.last
. However, note that this rule should be applied only when reading values. When modifying the first or last elements, array indexing should still be used. For example,arr[0] = 'new_value'
andarr[-1] = 'new_value'
.