Skip to content

Commit

Permalink
Feat add manticore instrumentation (#3)
Browse files Browse the repository at this point in the history
* initial code complete

* chore: fix tests for sidekiq to support inline lua changes (open-telemetry#1011)

* chore: fix tests for sidekiq to support inline lua changes to redis calls

* chore: add additional sidekiq appraisal

* feat: add dalli obfuscation for db_statement (open-telemetry#1013)

* feat: add dalli obfuscation for db_statement

* chore: linting

* Link to website spec pages (open-telemetry#1018)

Co-authored-by: Matthew Wear <matthew.wear@gmail.com>

* Feat: Add custom getters to common (open-telemetry#1006)

Co-authored-by: Matthew Wear <matthew.wear@gmail.com>

Co-authored-by: Eric Mustin <eric.mustin@shopify.com>
Co-authored-by: Patrice Chalin <chalin@users.noreply.github.com>
Co-authored-by: Matthew Wear <matthew.wear@gmail.com>
Co-authored-by: Robert <robertlaurin@users.noreply.github.com>
  • Loading branch information
5 people committed Nov 20, 2021
1 parent eab3631 commit b8eaddb
Show file tree
Hide file tree
Showing 34 changed files with 1,045 additions and 7 deletions.
1 change: 1 addition & 0 deletions common/lib/opentelemetry/common.rb
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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
Expand Up @@ -20,7 +20,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
end

option :peer_service, default: nil, validate: :string
option :db_statement, default: :include, validate: ->(opt) { %I[omit include].include?(opt) }
option :db_statement, default: :include, validate: ->(opt) { %I[omit obfuscate include].include?(opt) }

private

Expand Down
Expand Up @@ -17,7 +17,12 @@ def request(op, *args)
'net.peer.name' => hostname,
'net.peer.port' => port
}
attributes['db.statement'] = Utils.format_command(operation, args) if config[:db_statement] == :include
if config[:db_statement] == :include
attributes['db.statement'] = Utils.format_command(operation, args)
elsif config[:db_statement] == :obfuscate
attributes['db.statement'] = "#{operation} ?"
end

attributes['peer.service'] = config[:peer_service] if config[:peer_service]
tracer.in_span(operation, attributes: attributes, kind: :client) do
super
Expand Down
Expand Up @@ -107,5 +107,16 @@
_(span.name).must_equal 'set'
_(span.attributes).wont_include 'db.statement'
end

it 'obfuscates db.statement' do
instrumentation.instance_variable_set(:@installed, false)
instrumentation.install(db_statement: :obfuscate)

dalli.set('foo', 'bar')

_(exporter.finished_spans.size).must_equal 1
_(span.name).must_equal 'set'
_(span.attributes['db.statement']).must_equal 'set ?'
end
end
end unless ENV['OMIT_SERVICES']
5 changes: 5 additions & 0 deletions instrumentation/manticore/.rubocop.yml
@@ -0,0 +1,5 @@
inherit_from: ../.rubocop-examples.yml

Naming/FileName:
Exclude:
- "lib/opentelemetry-instrumentation-manticore.rb"
9 changes: 9 additions & 0 deletions instrumentation/manticore/.yardopts
@@ -0,0 +1,9 @@
--no-private
--title=OpenTelemetry Manticore Instrumentation
--markup=markdown
--main=README.md
./lib/opentelemetry/instrumentation/**/*.rb
./lib/opentelemetry/instrumentation.rb
-
README.md
CHANGELOG.md
1 change: 1 addition & 0 deletions instrumentation/manticore/CHANGELOG.md
@@ -0,0 +1 @@
# Release History: opentelemetry-instrumentation-manticore
17 changes: 17 additions & 0 deletions instrumentation/manticore/Gemfile
@@ -0,0 +1,17 @@
# frozen_string_literal: true

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

source 'https://rubygems.org'

gemspec

gem 'opentelemetry-api', path: '../../api'
gem 'opentelemetry-instrumentation-base', path: '../base'

group :test do
gem 'opentelemetry-common', path: '../../common'
gem 'opentelemetry-sdk', path: '../../sdk'
end

0 comments on commit b8eaddb

Please sign in to comment.