-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix!: refactor propagators to add #fields (#638)
* fix!: refactor propagators to add #fields * fix API tests * fix SDK tests * fix B3 tests * update ottrace propagator * add fields method to OTTrace propagator * Update Jaeger propagator. (#663) * test fixes * fix tests & composite prop * fix composite prop test * fix: b3 extract priority * fix: feedback * fix: merge conflicts * fix: update XRay propagator Co-authored-by: Francis Hwang <[email protected]>
- Loading branch information
Showing
81 changed files
with
2,458 additions
and
2,988 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 0 additions & 58 deletions
58
api/lib/opentelemetry/baggage/propagation/text_map_extractor.rb
This file was deleted.
Oops, something went wrong.
76 changes: 0 additions & 76 deletions
76
api/lib/opentelemetry/baggage/propagation/text_map_injector.rb
This file was deleted.
Oops, something went wrong.
109 changes: 109 additions & 0 deletions
109
api/lib/opentelemetry/baggage/propagation/text_map_propagator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright The OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
require 'cgi' | ||
|
||
module OpenTelemetry | ||
module Baggage | ||
module Propagation | ||
# Propagates baggage using the W3C Baggage format | ||
class TextMapPropagator | ||
# Maximums according to W3C Baggage spec | ||
MAX_ENTRIES = 180 | ||
MAX_ENTRY_LENGTH = 4096 | ||
MAX_TOTAL_LENGTH = 8192 | ||
|
||
BAGGAGE_KEY = 'baggage' | ||
FIELDS = [BAGGAGE_KEY].freeze | ||
|
||
private_constant :BAGGAGE_KEY, :FIELDS, :MAX_ENTRIES, :MAX_ENTRY_LENGTH, :MAX_TOTAL_LENGTH | ||
|
||
# Inject in-process baggage into the supplied carrier. | ||
# | ||
# @param [Carrier] carrier The mutable carrier to inject baggage into | ||
# @param [Context] context The context to read baggage from | ||
# @param [optional Setter] setter If the optional setter is provided, it | ||
# will be used to write context into the carrier, otherwise the default | ||
# text map setter will be used. | ||
def inject(carrier, context: Context.current, setter: Context::Propagation.text_map_setter) | ||
baggage = OpenTelemetry.baggage.raw_entries(context: context) | ||
|
||
return if baggage.nil? || baggage.empty? | ||
|
||
encoded_baggage = encode(baggage) | ||
setter.set(carrier, BAGGAGE_KEY, encoded_baggage) unless encoded_baggage&.empty? | ||
nil | ||
end | ||
|
||
# Extract remote baggage from the supplied carrier. | ||
# If extraction fails, the original context will be returned | ||
# | ||
# @param [Carrier] carrier The carrier to get the header from | ||
# @param [optional Context] context Context to be updated with the baggage | ||
# extracted from the carrier. Defaults to +Context.current+. | ||
# @param [optional Getter] getter If the optional getter is provided, it | ||
# will be used to read the header from the carrier, otherwise the default | ||
# text map getter will be used. | ||
# | ||
# @return [Context] context updated with extracted baggage, or the original context | ||
# if extraction fails | ||
def extract(carrier, context: Context.current, getter: Context::Propagation.text_map_getter) | ||
header = getter.get(carrier, BAGGAGE_KEY) | ||
|
||
entries = header.gsub(/\s/, '').split(',') | ||
|
||
OpenTelemetry.baggage.build(context: context) do |builder| | ||
entries.each do |entry| | ||
# Note metadata is currently unused in OpenTelemetry, but is part | ||
# the W3C spec where it's referred to as properties. We preserve | ||
# the properties (as-is) so that they can be propagated elsewhere. | ||
kv, meta = entry.split(';', 2) | ||
k, v = kv.split('=').map!(&CGI.method(:unescape)) | ||
builder.set_value(k, v, metadata: meta) | ||
end | ||
end | ||
rescue StandardError => e | ||
OpenTelemetry.logger.debug "Error extracting W3C baggage: #{e.message}" | ||
context | ||
end | ||
|
||
# Returns the predefined propagation fields. If your carrier is reused, you | ||
# should delete the fields returned by this method before calling +inject+. | ||
# | ||
# @return [Array<String>] a list of fields that will be used by this propagator. | ||
def fields | ||
FIELDS | ||
end | ||
|
||
private | ||
|
||
def encode(baggage) | ||
result = +'' | ||
encoded_count = 0 | ||
baggage.each_pair do |key, entry| | ||
break unless encoded_count < MAX_ENTRIES | ||
|
||
encoded_entry = encode_value(key, entry) | ||
next unless encoded_entry.size <= MAX_ENTRY_LENGTH && | ||
encoded_entry.size + result.size <= MAX_TOTAL_LENGTH | ||
|
||
result << encoded_entry << ',' | ||
encoded_count += 1 | ||
end | ||
result.chop! | ||
end | ||
|
||
def encode_value(key, entry) | ||
result = +"#{CGI.escape(key.to_s)}=#{CGI.escape(entry.value.to_s)}" | ||
# We preserve metadata recieved on extract and assume it's already formatted | ||
# for transport. It's sent as-is without further processing. | ||
result << ";#{entry.metadata}" if entry.metadata | ||
result | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.