Skip to content

Commit

Permalink
Refactored: gRPC to use Datadog::Contrib::Integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
delner committed Sep 24, 2018
1 parent 16d9782 commit c94ce0b
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 52 deletions.
2 changes: 1 addition & 1 deletion lib/ddtrace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def configure(target = configuration, opts = {})
require 'ddtrace/contrib/faraday/patcher'
require 'ddtrace/contrib/grape/patcher'
require 'ddtrace/contrib/graphql/patcher'
require 'ddtrace/contrib/grpc/patcher'
require 'ddtrace/contrib/grpc/integration'
require 'ddtrace/contrib/http/patcher'
require 'ddtrace/contrib/integration'
require 'ddtrace/contrib/mongodb/patcher'
Expand Down
16 changes: 16 additions & 0 deletions lib/ddtrace/contrib/grpc/configuration/settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'ddtrace/contrib/configuration/settings'
require 'ddtrace/contrib/grpc/ext'

module Datadog
module Contrib
module GRPC
module Configuration
# Custom settings for the gRPC integration
class Settings < Contrib::Configuration::Settings
option :service_name, default: Ext::SERVICE_NAME
option :tracer, default: Datadog.tracer
end
end
end
end
end
7 changes: 5 additions & 2 deletions lib/ddtrace/contrib/grpc/datadog_interceptor/client.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require 'ddtrace/ext/http'
require 'ddtrace/contrib/grpc/ext'

module Datadog
module Contrib
module GRPC
Expand All @@ -11,12 +14,12 @@ def trace(keywords)
keywords[:metadata] ||= {}

options = {
span_type: Datadog::Ext::GRPC::TYPE,
span_type: Datadog::Ext::HTTP::TYPE,
service: datadog_pin.service_name,
resource: format_resource(keywords[:method])
}

tracer.trace('grpc.client', options) do |span|
tracer.trace(Ext::SPAN_CLIENT, options) do |span|
annotate!(span, keywords[:metadata])

yield
Expand Down
7 changes: 5 additions & 2 deletions lib/ddtrace/contrib/grpc/datadog_interceptor/server.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require 'ddtrace/ext/http'
require 'ddtrace/contrib/grpc/ext'

module Datadog
module Contrib
module GRPC
Expand All @@ -10,15 +13,15 @@ module DatadogInterceptor
class Server < Base
def trace(keywords)
options = {
span_type: Datadog::Ext::GRPC::TYPE,
span_type: Datadog::Ext::HTTP::TYPE,
service: datadog_pin.service_name,
resource: format_resource(keywords[:method])
}
metadata = keywords[:call].metadata

set_distributed_context!(tracer, metadata)

tracer.trace('grpc.service', options) do |span|
tracer.trace(Ext::SPAN_SERVICE, options) do |span|
annotate!(span, metadata)

yield
Expand Down
14 changes: 14 additions & 0 deletions lib/ddtrace/contrib/grpc/ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Datadog
module Contrib
module GRPC
# gRPC integration constants
module Ext
APP = 'grpc'.freeze
SERVICE_NAME = 'grpc'.freeze

SPAN_CLIENT = 'grpc.client'.freeze
SPAN_SERVICE = 'grpc.service'.freeze
end
end
end
end
38 changes: 38 additions & 0 deletions lib/ddtrace/contrib/grpc/integration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'ddtrace/contrib/integration'
require 'ddtrace/contrib/grpc/configuration/settings'
require 'ddtrace/contrib/grpc/patcher'

module Datadog
module Contrib
module GRPC
# Description of gRPC integration
class Integration
include Contrib::Integration

register_as :grpc, auto_patch: true

def self.version
Gem.loaded_specs['grpc'] && Gem.loaded_specs['grpc'].version
end

def self.present?
super && defined?(::GRPC)
end

def self.compatible?
super \
&& Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.0') \
&& version >= Gem::Version.new('0.10.0')
end

def default_configuration
Configuration::Settings.new
end

def patcher
Patcher
end
end
end
end
end
60 changes: 25 additions & 35 deletions lib/ddtrace/contrib/grpc/patcher.rb
Original file line number Diff line number Diff line change
@@ -1,60 +1,50 @@
# requirements should be kept minimal as Patcher is a shared requirement.
require 'ddtrace/contrib/patcher'
require 'ddtrace/contrib/grpc/ext'

module Datadog
module Contrib
module GRPC
SERVICE = 'grpc'.freeze

# Patcher enables patching of 'grpc' module.
module Patcher
include Base
register_as :grpc, auto_patch: true
option :tracer, default: Datadog.tracer
option :service_name, default: SERVICE

@patched = false
include Contrib::Patcher

module_function

def patch
return false unless compatible?
return @patched if @patched

require 'ddtrace/ext/grpc'
require 'ddtrace/propagation/grpc_propagator'
require 'ddtrace/contrib/grpc/datadog_interceptor'
require 'ddtrace/contrib/grpc/intercept_with_datadog'

add_pin
prepend_interceptor

@patched = true
rescue StandardError => e
Datadog::Tracer.log.error("Unable to apply gRPC integration: #{e}")
ensure
@patched
end

def compatible?
defined?(::GRPC::VERSION) && Gem::Version.new(::GRPC::VERSION) >= Gem::Version.new('0.10.0')
def patched?
done?(:grpc)
end

def patched?
@patched
def patch
do_once(:grpc) do
begin
require 'ddtrace/propagation/grpc_propagator'
require 'ddtrace/contrib/grpc/datadog_interceptor'
require 'ddtrace/contrib/grpc/intercept_with_datadog'

add_pin
prepend_interceptor
rescue StandardError => e
Datadog::Tracer.log.error("Unable to apply gRPC integration: #{e}")
end
end
end

def add_pin
Pin.new(
get_option(:service_name),
app: 'grpc',
app_type: 'grpc',
app: Ext::APP,
app_type: Datadog::Ext::AppTypes::WEB,
tracer: get_option(:tracer)
).onto(::GRPC)
end

def prepend_interceptor
::GRPC::InterceptionContext
.prepend(Datadog::Contrib::GRPC::InterceptWithDatadog)
.send(:prepend, Datadog::Contrib::GRPC::InterceptWithDatadog)
end

def get_option(option)
Datadog.configuration[:grpc].get_option(option)
end
end
end
Expand Down
7 changes: 0 additions & 7 deletions lib/ddtrace/ext/grpc.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

shared_examples 'span data contents' do
specify { expect(span.name).to eq 'grpc.client' }
specify { expect(span.span_type).to eq 'grpc' }
specify { expect(span.span_type).to eq 'http' }
specify { expect(span.service).to eq 'rspec' }
specify { expect(span.resource).to eq 'myservice.endpoint' }
specify { expect(span.get_tag('error.stack')).to be_nil }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

shared_examples 'span data contents' do
specify { expect(span.name).to eq 'grpc.service' }
specify { expect(span.span_type).to eq 'grpc' }
specify { expect(span.span_type).to eq 'http' }
specify { expect(span.service).to eq 'rspec' }
specify { expect(span.resource).to eq 'my.server.endpoint' }
specify { expect(span.get_tag('error.stack')).to be_nil }
Expand Down
6 changes: 3 additions & 3 deletions spec/ddtrace/contrib/grpc/interception_context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
context 'when intercepting on the client' do
shared_examples 'span data contents' do
specify { expect(span.name).to eq 'grpc.client' }
specify { expect(span.span_type).to eq 'grpc' }
specify { expect(span.span_type).to eq 'http' }
specify { expect(span.service).to eq 'rspec' }
specify { expect(span.resource).to eq 'myservice.endpoint' }
specify { expect(span.get_tag('error.stack')).to be_nil }
Expand Down Expand Up @@ -60,7 +60,7 @@

specify do
expect(span.name).to eq 'grpc.client'
expect(span.span_type).to eq 'grpc'
expect(span.span_type).to eq 'http'
expect(span.service).to eq 'rspec'
expect(span.resource).to eq 'myservice.endpoint'
expect(span.get_tag('error.stack')).to be_nil
Expand All @@ -84,7 +84,7 @@
context 'when intercepting on the server' do
shared_examples 'span data contents' do
specify { expect(span.name).to eq 'grpc.service' }
specify { expect(span.span_type).to eq 'grpc' }
specify { expect(span.span_type).to eq 'http' }
specify { expect(span.service).to eq 'rspec' }
specify { expect(span.resource).to eq 'my.server.endpoint' }
specify { expect(span.get_tag('error.stack')).to be_nil }
Expand Down

0 comments on commit c94ce0b

Please sign in to comment.