Skip to content

Commit

Permalink
feat: migrate gRPC instrumentation from standardrb to Rubocop (#1341)
Browse files Browse the repository at this point in the history
* fix: migrate grpc instrumentation from standardrb to rubocop

* remove standardrb

* fix: remove conflicting CODE_OF_CONDUCT

* fix: remove rbs

* include the gRPC instrumentation in the general CI

* fix(grpc instrumentation): skip JRuby tests since gRPC is not compatible with JRuby

reference grpc/grpc#6705

---------

Co-authored-by: Ariel Valentin <arielvalentin@users.noreply.github.com>
  • Loading branch information
michal-kazmierczak and arielvalentin authored Jan 16, 2025
1 parent a25cd4b commit 07c9cf4
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 126 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci-instrumentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
- faraday
- grape
- graphql
- grpc
- gruf
- http
- http_client
Expand Down Expand Up @@ -102,6 +103,7 @@ jobs:
[[ "${{ matrix.gem }}" == "lmdb" ]] && echo "skip=true" >> $GITHUB_OUTPUT
[[ "${{ matrix.gem }}" == "rack" ]] && echo "skip=true" >> $GITHUB_OUTPUT
[[ "${{ matrix.gem }}" == "rails" ]] && echo "skip=true" >> $GITHUB_OUTPUT
[[ "${{ matrix.gem }}" == "grpc" ]] && echo "skip=true" >> $GITHUB_OUTPUT
[[ "${{ matrix.gem }}" == "gruf" ]] && echo "skip=true" >> $GITHUB_OUTPUT
# This is essentially a bash script getting evaluated, so we need to return true or the whole job fails.
true
Expand Down
5 changes: 5 additions & 0 deletions instrumentation/grpc/.rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
inherit_from: ../../.rubocop.yml

AllCops:
Exclude:
- 'test/support/proto/*.rb'
3 changes: 0 additions & 3 deletions instrumentation/grpc/.standard.yml

This file was deleted.

84 changes: 0 additions & 84 deletions instrumentation/grpc/CODE_OF_CONDUCT.md

This file was deleted.

2 changes: 1 addition & 1 deletion instrumentation/grpc/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# SPDX-License-Identifier: Apache-2.0

source "https://rubygems.org"
source 'https://rubygems.org'

# Specify your gem's dependencies in opentelemetry-instrumentation-grpc.gemspec
gemspec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
#
# SPDX-License-Identifier: Apache-2.0

require_relative "opentelemetry/instrumentation"
require_relative 'opentelemetry/instrumentation'
12 changes: 11 additions & 1 deletion instrumentation/grpc/lib/opentelemetry/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@
#
# SPDX-License-Identifier: Apache-2.0

# OpenTelemetry is an open source observability framework, providing a
# general-purpose API, SDK, and related tools required for the instrumentation
# of cloud-native software, frameworks, and libraries.
#
# The OpenTelemetry module provides global accessors for telemetry objects.
# See the documentation for the `opentelemetry-api` gem for details.
module OpenTelemetry
# "Instrumentation" are specified by
# https://github.com/open-telemetry/opentelemetry-specification/blob/784635d01d8690c8f5fcd1f55bdbc8a13cf2f4f2/specification/glossary.md#instrumentation-library
#
# Instrumentation should be able to handle the case when the library is not installed on a user's system.
module Instrumentation
end
end

require_relative "instrumentation/grpc"
require_relative 'instrumentation/grpc'
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#
# SPDX-License-Identifier: Apache-2.0

require "opentelemetry"
require "opentelemetry-instrumentation-base"
require 'opentelemetry'
require 'opentelemetry-instrumentation-base'

module OpenTelemetry
module Instrumentation
Expand All @@ -16,6 +16,6 @@ class Error < StandardError; end
end
end

require_relative "grpc/instrumentation"
require_relative "grpc/version"
require_relative 'grpc/instrumentation'
require_relative 'grpc/version'
require_relative 'grpc/interceptors/client_tracer'
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,45 @@ module Interceptors
# 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)
call(type: 'request_response', requests: [request], call: call, method: method, metadata: metadata, &blk)
end

def client_streamer(requests: nil, call: nil, method: nil, metadata: nil, &blk)
call(type: "client_streamer", requests: requests, call: call, method: method, metadata: metadata, &blk)
call(type: 'client_streamer', requests: requests, call: call, method: method, metadata: metadata, &blk)
end

def server_streamer(request: nil, call: nil, method: nil, metadata: nil, &blk)
call(type: "server_streamer", requests: [request], call: call, method: method, metadata: metadata, &blk)
call(type: 'server_streamer', requests: [request], call: call, method: method, metadata: metadata, &blk)
end

def bidi_streamer(requests: nil, call: nil, method: nil, metadata: nil, &blk)
call(type: "client_streamer", requests: requests, call: call, method: method, metadata: metadata, &blk)
call(type: 'client_streamer', requests: requests, call: call, method: method, metadata: metadata, &blk)
end

private

def call(type:, requests: nil, call: nil, method: nil, metadata: nil)
return yield if instrumentation_config.empty?

method_parts = method.to_s.split("/")
method_parts = method.to_s.split('/')
service = method_parts[1]
method_name = method_parts.last

attributes = {
OpenTelemetry::SemanticConventions::Trace::RPC_SYSTEM => "grpc",
OpenTelemetry::SemanticConventions::Trace::RPC_SYSTEM => 'grpc',
OpenTelemetry::SemanticConventions::Trace::RPC_SERVICE => service,
OpenTelemetry::SemanticConventions::Trace::RPC_METHOD => method_name,
OpenTelemetry::SemanticConventions::Trace::PEER_SERVICE => instrumentation_config[:peer_service],
"rpc.type" => type,
"net.sock.peer.addr" => call.instance_variable_get(:@wrapped)&.peer
'rpc.type' => type,
'net.sock.peer.addr' => call.instance_variable_get(:@wrapped)&.peer
}.compact

attributes.merge!(allowed_metadata_headers(metadata.transform_keys(&:to_s)))

instrumentation_tracer.in_span(
# The specification requires there be no leading slash
# https://github.com/open-telemetry/semantic-conventions/blob/5a2836bbea0b6e105b98370f331a7661bcf19540/docs/rpc/rpc-spans.md?plain=1#L63-L69
method.to_s.delete_prefix("/"),
method.to_s.delete_prefix('/'),
attributes: attributes,
kind: OpenTelemetry::Trace::SpanKind::CLIENT
) do |span|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ module Patches
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
interceptors.unshift(Interceptors::ClientTracer.new) unless interceptors.any?(Interceptors::ClientTracer)
args[:interceptors] = interceptors

super
Expand Down
10 changes: 5 additions & 5 deletions instrumentation/grpc/opentelemetry-instrumentation-grpc.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
#
# SPDX-License-Identifier: Apache-2.0

require_relative "lib/opentelemetry/instrumentation/grpc/version"
require_relative 'lib/opentelemetry/instrumentation/grpc/version'

Gem::Specification.new do |spec|
spec.name = "opentelemetry-instrumentation-grpc"
spec.name = 'opentelemetry-instrumentation-grpc'
spec.version = OpenTelemetry::Instrumentation::Grpc::VERSION
spec.authors = ['OpenTelemetry Authors']
spec.email = ['cncf-opentelemetry-contributors@lists.cncf.io']
Expand All @@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
spec.homepage = 'https://github.com/open-telemetry/opentelemetry-ruby-contrib'
spec.license = 'Apache-2.0'

spec.required_ruby_version = ">= 3.0.0"
spec.required_ruby_version = '>= 3.0.0'

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
Expand All @@ -28,9 +28,9 @@ Gem::Specification.new do |spec|
f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
end
end
spec.bindir = "exe"
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.require_paths = ['lib']

# Uncomment to register a new dependency of your gem
spec.add_dependency 'opentelemetry-api', '~> 1.2'
Expand Down
14 changes: 0 additions & 14 deletions instrumentation/grpc/sig/opentelemetry/instrumentation/grpc.rbs

This file was deleted.

2 changes: 1 addition & 1 deletion instrumentation/grpc/test/support/grpc_server_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# inspired by https://github.com/grpc/grpc/blob/master/src/ruby/spec/support/helpers.rb

require_relative './ping_server_impl'
require_relative 'ping_server_impl'

module Support
class GrpcServerRunner
Expand Down

0 comments on commit 07c9cf4

Please sign in to comment.