Skip to content

Commit e3c020f

Browse files
feat: parts of the gRPC istrumentation (#1330)
* 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]>
1 parent 190aa9d commit e3c020f

28 files changed

+297
-320
lines changed

instrumentation/all/Gemfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ group :test do
1515
.sort
1616
.each { |dir| gem "opentelemetry-helpers-#{dir}", path: "../../helpers/#{dir}" }
1717

18+
excluded_instrumentations = %w[. .. all grpc]
1819
Dir.entries('../')
1920
.select { |entry| File.directory?(File.join('../', entry)) }
20-
.reject { |entry| %w[. .. all].include?(entry) } # rubocop:disable Performance/CollectionLiteralInLoop
21+
.reject { |entry| excluded_instrumentations.include?(entry) }
2122
.sort
2223
.each { |dir| gem "opentelemetry-instrumentation-#{dir}", path: "../#{dir}" }
2324
end

instrumentation/grpc/.rspec

-3
This file was deleted.

instrumentation/grpc/Appraisals

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright The OpenTelemetry Authors
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
appraise 'grpc-1.68' do
8+
gem 'grpc', '~> 1.68.0'
9+
end
10+
11+
appraise 'grpc-latest' do
12+
gem 'grpc'
13+
end

instrumentation/grpc/Gemfile

+4-6
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ source "https://rubygems.org"
99
# Specify your gem's dependencies in opentelemetry-instrumentation-grpc.gemspec
1010
gemspec
1111

12-
gem "rake", "~> 13.0"
13-
gem "rspec", "~> 3.0"
14-
gem "standard", "~> 1.3"
15-
gem "opentelemetry-sdk", "~> 1.0"
16-
gem "opentelemetry-test-helpers"
17-
gem "grpc"
12+
group :test do
13+
gem 'opentelemetry-instrumentation-base', path: '../base'
14+
gem 'pry'
15+
end

instrumentation/grpc/README.md

+4-9
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,13 @@ instrumentation.
4646
end
4747
```
4848

49-
You *also* need to make sure your stubs are using the interceptor, e.g.
50-
51-
```ruby
52-
otel = OpenTelemetry::Instrumentation::Grpc.client_interceptor
53-
SomeService::Stub.new(host, credentials, *args, **kwargs, interceptors: [otel])
54-
```
55-
5649
## Development
5750

58-
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.
51+
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:
5952

60-
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).
53+
```sh
54+
bundle exec grpc_tools_ruby_protoc --ruby_out=. --grpc_out=. test/support/proto/ping.proto
55+
```
6156

6257
## Contributing
6358

instrumentation/grpc/Rakefile

+19-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,25 @@
44
#
55
# SPDX-License-Identifier: Apache-2.0
66

7-
require "bundler/gem_tasks"
8-
require "rspec/core/rake_task"
7+
require 'bundler/gem_tasks'
8+
require 'rake/testtask'
9+
require 'yard'
10+
require 'rubocop/rake_task'
911

10-
RSpec::Core::RakeTask.new(:spec)
12+
RuboCop::RakeTask.new
1113

12-
require "standard/rake"
14+
Rake::TestTask.new :test do |t|
15+
t.libs << 'test'
16+
t.libs << 'lib'
17+
t.test_files = FileList['test/**/*_test.rb']
18+
end
1319

14-
task default: %i[spec standard]
20+
YARD::Rake::YardocTask.new do |t|
21+
t.stats_options = ['--list-undoc']
22+
end
23+
24+
if RUBY_ENGINE == 'truffleruby'
25+
task default: %i[test]
26+
else
27+
task default: %i[test rubocop yard]
28+
end

instrumentation/grpc/lib/opentelemetry/instrumentation/grpc.rb

+1-8
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,16 @@
66

77
require "opentelemetry"
88
require "opentelemetry-instrumentation-base"
9-
require "active_support/inflector"
109

1110
module OpenTelemetry
1211
module Instrumentation
1312
# Contains the OpenTelemetry instrumentation for the gRPC gem
1413
module Grpc
1514
class Error < StandardError; end
16-
17-
module_function
18-
19-
def client_interceptor
20-
Interceptors::Client.new
21-
end
2215
end
2316
end
2417
end
2518

2619
require_relative "grpc/instrumentation"
27-
require_relative "grpc/interceptors/client"
2820
require_relative "grpc/version"
21+
require_relative 'grpc/interceptors/client_tracer'

instrumentation/grpc/lib/opentelemetry/instrumentation/grpc/instrumentation.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
2424
private
2525

2626
def patch
27-
::GRPC::ClientInterceptor.prepend(Interceptors::Client)
27+
::GRPC::ClientStub.prepend(Patches::ClientStub)
2828
end
2929

3030
def require_dependencies
31-
require_relative "interceptors/client"
31+
require_relative 'interceptors/client_tracer'
32+
require_relative 'patches/client_stub'
3233
end
3334
end
3435
end

instrumentation/grpc/lib/opentelemetry/instrumentation/grpc/interceptors/client.rb instrumentation/grpc/lib/opentelemetry/instrumentation/grpc/interceptors/client_tracer.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ module OpenTelemetry
88
module Instrumentation
99
module Grpc
1010
module Interceptors
11-
class Client
11+
# ClientTracer is a gRPC client interceptor which instrument gRPC calls with OpenTelemetry tracing
12+
class ClientTracer < ::GRPC::ClientInterceptor
1213
def request_response(request: nil, call: nil, method: nil, metadata: nil, &blk)
1314
call(type: "request_response", requests: [request], call: call, method: method, metadata: metadata, &blk)
1415
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright The OpenTelemetry Authors
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
module OpenTelemetry
8+
module Instrumentation
9+
module Grpc
10+
module Patches
11+
# Module to be prepended to force gRPC to use the client interceptor by
12+
# default so the user doesn't have to manually add it when initializing a client.
13+
module ClientStub
14+
def initialize(host, creds, **args)
15+
interceptors = args[:interceptors] || []
16+
interceptors.unshift(Interceptors::ClientTracer.new) unless interceptors.any? do |interceptor|
17+
interceptor.is_a?(Interceptors::ClientTracer)
18+
end
19+
args[:interceptors] = interceptors
20+
21+
super
22+
end
23+
end
24+
end
25+
end
26+
end
27+
end

instrumentation/grpc/opentelemetry-instrumentation-grpc.gemspec

+15-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,21 @@ Gem::Specification.new do |spec|
3333
spec.require_paths = ["lib"]
3434

3535
# Uncomment to register a new dependency of your gem
36-
spec.add_dependency "opentelemetry-instrumentation-base", "~> 0.22.4"
37-
spec.add_dependency "opentelemetry-api", "~> 1.2"
38-
spec.add_runtime_dependency "activesupport", ">= 4"
36+
spec.add_dependency 'opentelemetry-api', '~> 1.2'
37+
spec.add_dependency 'opentelemetry-instrumentation-base', '~> 0.22.4'
38+
39+
spec.add_development_dependency 'appraisal', '~> 2.5'
40+
spec.add_development_dependency 'bundler', '~> 2.4'
41+
spec.add_development_dependency 'google-protobuf', '~> 4.29'
42+
spec.add_development_dependency 'grpc-tools', '~> 1.64'
43+
spec.add_development_dependency 'minitest', '~> 5.0'
44+
spec.add_development_dependency 'opentelemetry-sdk', '~> 1.1'
45+
spec.add_development_dependency 'opentelemetry-test-helpers', '~> 0.4'
46+
spec.add_development_dependency 'rake', '~> 13.2'
47+
spec.add_development_dependency 'rubocop', '~> 1.69.1'
48+
spec.add_development_dependency 'rubocop-performance', '~> 1.23.0'
49+
spec.add_development_dependency 'simplecov', '~> 0.17.1'
50+
spec.add_development_dependency 'yard', '~> 0.9'
3951

4052
# For more information and examples about making a new gem, check out our
4153
# guide at: https://bundler.io/guides/creating_gem.html

instrumentation/grpc/spec/example/Gemfile

-14
This file was deleted.

instrumentation/grpc/spec/example/base.rb

-13
This file was deleted.

instrumentation/grpc/spec/example/controllers/example_api_controller.rb

-15
This file was deleted.

instrumentation/grpc/spec/example/proto/example_api.proto

-16
This file was deleted.

instrumentation/grpc/spec/example/proto/example_api_pb.rb

-25
This file was deleted.

instrumentation/grpc/spec/example/proto/example_api_services_pb.rb

-25
This file was deleted.

instrumentation/grpc/spec/example/trace_demonstration.rb

-46
This file was deleted.

0 commit comments

Comments
 (0)