Skip to content

Commit

Permalink
Feat: Add custom getters to common (open-telemetry#1006)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthew Wear <matthew.wear@gmail.com>
  • Loading branch information
robertlaurin and mwear committed Nov 12, 2021
1 parent 5269b11 commit 61b84ae
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 0 deletions.
1 change: 1 addition & 0 deletions common/lib/opentelemetry/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

require 'opentelemetry'
require 'opentelemetry/common/http'
require 'opentelemetry/common/propagation'
require 'opentelemetry/common/utilities'
require 'opentelemetry/common/version'

Expand Down
33 changes: 33 additions & 0 deletions common/lib/opentelemetry/common/propagation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

require_relative './propagation/rack_env_getter'
require_relative './propagation/symbol_key_getter'

module OpenTelemetry
module Common
# Propagation contains common helpers for context propagation.
module Propagation
extend self

RACK_ENV_GETTER = RackEnvGetter.new
SYMBOL_KEY_GETTER = SymbolKeyGetter.new
private_constant :RACK_ENV_GETTER, :SYMBOL_KEY_GETTER

# Returns a {RackEnvGetter} instance suitable for reading values from a
# Rack environment.
def rack_env_getter
RACK_ENV_GETTER
end

# Returns a {SymbolKeyGetter} instance for reading values from a
# symbol keyed hash.
def symbol_key_getter
SYMBOL_KEY_GETTER
end
end
end
end
48 changes: 48 additions & 0 deletions common/lib/opentelemetry/common/propagation/rack_env_getter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

module OpenTelemetry
module Common
module Propagation
# The RackEnvGetter class provides a common methods for reading
# keys from a rack environment. It abstracts away the rack-normalization
# process so that keys can be looked up without having to transform them
# first. With this class you can get +traceparent+ instead of
# +HTTP_TRACEPARENT+
class RackEnvGetter
# Converts key into a rack-normalized key and reads it from the carrier.
# Useful for extract operations.
def get(carrier, key)
carrier[to_rack_key(key)] || carrier[key]
end

# Reads all keys from a carrier and converts them from the rack-normalized
# form to the original. The resulting keys will be lowercase and
# underscores will be replaced with dashes.
def keys(carrier)
carrier.keys.map(&method(:from_rack_key))
end

private

def to_rack_key(key)
ret = 'HTTP_' + key
ret.tr!('-', '_')
ret.upcase!
ret
end

def from_rack_key(key)
start = key.start_with?('HTTP_') ? 5 : 0
ret = key[start..-1]
ret.tr!('_', '-')
ret.downcase!
ret
end
end
end
end
end
26 changes: 26 additions & 0 deletions common/lib/opentelemetry/common/propagation/symbol_key_getter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

module OpenTelemetry
module Common
module Propagation
# The SymbolKeyGetter class provides a common method for reading
# symbol keys from a hash.
class SymbolKeyGetter
# Converts key into a symbol and reads it from the carrier.
# Useful for extract operations.
def get(carrier, key)
carrier[key.to_sym]
end

# Reads all keys from a carrier
def keys(carrier)
carrier.keys.map(&:to_s)
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

require 'test_helper'

describe OpenTelemetry::Common::Propagation::RackEnvGetter do
let(:getter) do
OpenTelemetry::Context::Propagation::RackEnvGetter.new
end

let(:carrier) do
{
'HTTP_TRACEPARENT' => 'tp',
'HTTP_TRACESTATE' => 'ts',
'HTTP_X_SOURCE_ID' => '123',
'rack.hijack?' => true
}
end

describe '#get' do
it 'reads key from carrier' do
_(getter.get(carrier, 'traceparent')).must_equal('tp')
_(getter.get(carrier, 'tracestate')).must_equal('ts')
_(getter.get(carrier, 'x-source-id')).must_equal('123')
_(getter.get(carrier, 'rack.hijack?')).must_equal(true)
end

it 'returns nil for non-existent key' do
_(getter.get(carrier, 'not-here')).must_be_nil
end
end

describe '#keys' do
it 'returns carrier keys' do
_(getter.keys(carrier)).must_equal(%w[traceparent tracestate x-source-id rack.hijack?])
end

it 'returns empty array for empty carrier' do
_(getter.keys({})).must_equal([])
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

require 'test_helper'

describe OpenTelemetry::Common::Propagation::SymbolKeyGetter do
let(:symbol_key_getter) { OpenTelemetry::Common::Propagation::SymbolKeyGetter.new }
let(:carrier) { { foo: 'bar' } }

describe '#get' do
it 'retrieves the value' do
_(symbol_key_getter.get(carrier, 'foo')).must_equal('bar')
end
end

describe '#keys' do
it 'returns all the keys as strings' do
_(symbol_key_getter.keys(carrier)).must_equal(['foo'])
end
end
end

0 comments on commit 61b84ae

Please sign in to comment.