Skip to content

Commit

Permalink
feat: parts of the gRPC istrumentation (#1330)
Browse files Browse the repository at this point in the history
* feat(grpc): write the client tracing instrumentation in the interceptor manner

* feat(grpc): migrate from rspec to minitest

* feat(grpc): introduce Appraisals

* fix(grpc): remove active_support dependency

* Update grpc.rb

* Update README.md

* ci: add (probably) missing grpc gem in the ci-instrumentation

* cI: grpc is not included in the all gem yet

* ci: skip grpc from the CI for now

* a better approach towards client testing

* add an unversioned Appraisal

Co-authored-by: Kayla Reopelle <[email protected]>

* Update instrumentation/grpc/README.md

Co-authored-by: Kayla Reopelle <[email protected]>

* Update instrumentation/grpc/lib/opentelemetry/instrumentation/grpc/patches/client_stub.rb

Co-authored-by: Kayla Reopelle <[email protected]>

* Update instrumentation/grpc/test/test_helper.rb

Co-authored-by: Kayla Reopelle <[email protected]>

* add opentelemetry-test-helpers and rake as development dependencies

---------

Co-authored-by: Kayla Reopelle <[email protected]>
  • Loading branch information
michal-kazmierczak and kaylareopelle authored Jan 8, 2025
1 parent 190aa9d commit e3c020f
Show file tree
Hide file tree
Showing 28 changed files with 297 additions and 320 deletions.
3 changes: 2 additions & 1 deletion instrumentation/all/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ group :test do
.sort
.each { |dir| gem "opentelemetry-helpers-#{dir}", path: "../../helpers/#{dir}" }

excluded_instrumentations = %w[. .. all grpc]
Dir.entries('../')
.select { |entry| File.directory?(File.join('../', entry)) }
.reject { |entry| %w[. .. all].include?(entry) } # rubocop:disable Performance/CollectionLiteralInLoop
.reject { |entry| excluded_instrumentations.include?(entry) }
.sort
.each { |dir| gem "opentelemetry-instrumentation-#{dir}", path: "../#{dir}" }
end
3 changes: 0 additions & 3 deletions instrumentation/grpc/.rspec

This file was deleted.

13 changes: 13 additions & 0 deletions instrumentation/grpc/Appraisals
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

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

appraise 'grpc-1.68' do
gem 'grpc', '~> 1.68.0'
end

appraise 'grpc-latest' do
gem 'grpc'
end
10 changes: 4 additions & 6 deletions instrumentation/grpc/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ source "https://rubygems.org"
# Specify your gem's dependencies in opentelemetry-instrumentation-grpc.gemspec
gemspec

gem "rake", "~> 13.0"
gem "rspec", "~> 3.0"
gem "standard", "~> 1.3"
gem "opentelemetry-sdk", "~> 1.0"
gem "opentelemetry-test-helpers"
gem "grpc"
group :test do
gem 'opentelemetry-instrumentation-base', path: '../base'
gem 'pry'
end
13 changes: 4 additions & 9 deletions instrumentation/grpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,13 @@ instrumentation.
end
```

You *also* need to make sure your stubs are using the interceptor, e.g.

```ruby
otel = OpenTelemetry::Instrumentation::Grpc.client_interceptor
SomeService::Stub.new(host, credentials, *args, **kwargs, interceptors: [otel])
```

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Integration tests rely on a real gRPC server that is started by relevant tests. The proto definition is located in `test/support/proto/ping.proto`. Making changes to the proto definition requires re-creating gRPC-generated code. To do this, run the following command:

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
```sh
bundle exec grpc_tools_ruby_protoc --ruby_out=. --grpc_out=. test/support/proto/ping.proto
```

## Contributing

Expand Down
24 changes: 19 additions & 5 deletions instrumentation/grpc/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,25 @@
#
# SPDX-License-Identifier: Apache-2.0

require "bundler/gem_tasks"
require "rspec/core/rake_task"
require 'bundler/gem_tasks'
require 'rake/testtask'
require 'yard'
require 'rubocop/rake_task'

RSpec::Core::RakeTask.new(:spec)
RuboCop::RakeTask.new

require "standard/rake"
Rake::TestTask.new :test do |t|
t.libs << 'test'
t.libs << 'lib'
t.test_files = FileList['test/**/*_test.rb']
end

task default: %i[spec standard]
YARD::Rake::YardocTask.new do |t|
t.stats_options = ['--list-undoc']
end

if RUBY_ENGINE == 'truffleruby'
task default: %i[test]
else
task default: %i[test rubocop yard]
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,16 @@

require "opentelemetry"
require "opentelemetry-instrumentation-base"
require "active_support/inflector"

module OpenTelemetry
module Instrumentation
# Contains the OpenTelemetry instrumentation for the gRPC gem
module Grpc
class Error < StandardError; end

module_function

def client_interceptor
Interceptors::Client.new
end
end
end
end

require_relative "grpc/instrumentation"
require_relative "grpc/interceptors/client"
require_relative "grpc/version"
require_relative 'grpc/interceptors/client_tracer'
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
private

def patch
::GRPC::ClientInterceptor.prepend(Interceptors::Client)
::GRPC::ClientStub.prepend(Patches::ClientStub)
end

def require_dependencies
require_relative "interceptors/client"
require_relative 'interceptors/client_tracer'
require_relative 'patches/client_stub'
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ module OpenTelemetry
module Instrumentation
module Grpc
module Interceptors
class Client
# ClientTracer is a gRPC client interceptor which instrument gRPC calls with OpenTelemetry tracing
class ClientTracer < ::GRPC::ClientInterceptor
def request_response(request: nil, call: nil, method: nil, metadata: nil, &blk)
call(type: "request_response", requests: [request], call: call, method: method, metadata: metadata, &blk)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

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

module OpenTelemetry
module Instrumentation
module Grpc
module Patches
# Module to be prepended to force gRPC to use the client interceptor by
# default so the user doesn't have to manually add it when initializing a client.
module ClientStub
def initialize(host, creds, **args)
interceptors = args[:interceptors] || []
interceptors.unshift(Interceptors::ClientTracer.new) unless interceptors.any? do |interceptor|
interceptor.is_a?(Interceptors::ClientTracer)
end
args[:interceptors] = interceptors

super
end
end
end
end
end
end
18 changes: 15 additions & 3 deletions instrumentation/grpc/opentelemetry-instrumentation-grpc.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,21 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

# Uncomment to register a new dependency of your gem
spec.add_dependency "opentelemetry-instrumentation-base", "~> 0.22.4"
spec.add_dependency "opentelemetry-api", "~> 1.2"
spec.add_runtime_dependency "activesupport", ">= 4"
spec.add_dependency 'opentelemetry-api', '~> 1.2'
spec.add_dependency 'opentelemetry-instrumentation-base', '~> 0.22.4'

spec.add_development_dependency 'appraisal', '~> 2.5'
spec.add_development_dependency 'bundler', '~> 2.4'
spec.add_development_dependency 'google-protobuf', '~> 4.29'
spec.add_development_dependency 'grpc-tools', '~> 1.64'
spec.add_development_dependency 'minitest', '~> 5.0'
spec.add_development_dependency 'opentelemetry-sdk', '~> 1.1'
spec.add_development_dependency 'opentelemetry-test-helpers', '~> 0.4'
spec.add_development_dependency 'rake', '~> 13.2'
spec.add_development_dependency 'rubocop', '~> 1.69.1'
spec.add_development_dependency 'rubocop-performance', '~> 1.23.0'
spec.add_development_dependency 'simplecov', '~> 0.17.1'
spec.add_development_dependency 'yard', '~> 0.9'

# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
Expand Down
14 changes: 0 additions & 14 deletions instrumentation/grpc/spec/example/Gemfile

This file was deleted.

13 changes: 0 additions & 13 deletions instrumentation/grpc/spec/example/base.rb

This file was deleted.

This file was deleted.

16 changes: 0 additions & 16 deletions instrumentation/grpc/spec/example/proto/example_api.proto

This file was deleted.

25 changes: 0 additions & 25 deletions instrumentation/grpc/spec/example/proto/example_api_pb.rb

This file was deleted.

25 changes: 0 additions & 25 deletions instrumentation/grpc/spec/example/proto/example_api_services_pb.rb

This file was deleted.

46 changes: 0 additions & 46 deletions instrumentation/grpc/spec/example/trace_demonstration.rb

This file was deleted.

Loading

0 comments on commit e3c020f

Please sign in to comment.