From d4f07c6b328a29c9075c16295d9d52b1cf7437b8 Mon Sep 17 00:00:00 2001 From: Hazel Bachrach Date: Wed, 11 Sep 2024 22:59:38 -0700 Subject: [PATCH 1/4] feat: add grpc instrumentation See for previous git history --- instrumentation/grpc/.rspec | 3 + instrumentation/grpc/.standard.yml | 3 + instrumentation/grpc/CHANGELOG.md | 18 ++ instrumentation/grpc/CODE_OF_CONDUCT.md | 84 ++++++++ instrumentation/grpc/Gemfile | 17 ++ instrumentation/grpc/LICENSE | 201 ++++++++++++++++++ instrumentation/grpc/README.md | 66 ++++++ instrumentation/grpc/Rakefile | 14 ++ .../lib/opentelemetry-instrumentation-grpc.rb | 7 + .../grpc/lib/opentelemetry/instrumentation.rb | 12 ++ .../lib/opentelemetry/instrumentation/grpc.rb | 28 +++ .../instrumentation/grpc/instrumentation.rb | 31 +++ .../grpc/interceptors/client.rb | 84 ++++++++ .../instrumentation/grpc/version.rb | 13 ++ ...opentelemetry-instrumentation-grpc.gemspec | 49 +++++ .../opentelemetry/instrumentation/grpc.rbs | 14 ++ instrumentation/grpc/spec/example/Gemfile | 14 ++ instrumentation/grpc/spec/example/base.rb | 13 ++ .../controllers/example_api_controller.rb | 15 ++ .../grpc/spec/example/proto/example_api.proto | 16 ++ .../grpc/spec/example/proto/example_api_pb.rb | 25 +++ .../example/proto/example_api_services_pb.rb | 25 +++ .../grpc/spec/example/trace_demonstration.rb | 46 ++++ .../grpc/interceptors/client_spec.rb | 82 +++++++ .../instrumentation/grpc_spec.rb | 15 ++ instrumentation/grpc/spec/spec_helper.rb | 31 +++ 26 files changed, 926 insertions(+) create mode 100644 instrumentation/grpc/.rspec create mode 100644 instrumentation/grpc/.standard.yml create mode 100644 instrumentation/grpc/CHANGELOG.md create mode 100644 instrumentation/grpc/CODE_OF_CONDUCT.md create mode 100644 instrumentation/grpc/Gemfile create mode 100644 instrumentation/grpc/LICENSE create mode 100644 instrumentation/grpc/README.md create mode 100644 instrumentation/grpc/Rakefile create mode 100644 instrumentation/grpc/lib/opentelemetry-instrumentation-grpc.rb create mode 100644 instrumentation/grpc/lib/opentelemetry/instrumentation.rb create mode 100644 instrumentation/grpc/lib/opentelemetry/instrumentation/grpc.rb create mode 100644 instrumentation/grpc/lib/opentelemetry/instrumentation/grpc/instrumentation.rb create mode 100644 instrumentation/grpc/lib/opentelemetry/instrumentation/grpc/interceptors/client.rb create mode 100644 instrumentation/grpc/lib/opentelemetry/instrumentation/grpc/version.rb create mode 100644 instrumentation/grpc/opentelemetry-instrumentation-grpc.gemspec create mode 100644 instrumentation/grpc/sig/opentelemetry/instrumentation/grpc.rbs create mode 100644 instrumentation/grpc/spec/example/Gemfile create mode 100644 instrumentation/grpc/spec/example/base.rb create mode 100644 instrumentation/grpc/spec/example/controllers/example_api_controller.rb create mode 100644 instrumentation/grpc/spec/example/proto/example_api.proto create mode 100644 instrumentation/grpc/spec/example/proto/example_api_pb.rb create mode 100644 instrumentation/grpc/spec/example/proto/example_api_services_pb.rb create mode 100644 instrumentation/grpc/spec/example/trace_demonstration.rb create mode 100644 instrumentation/grpc/spec/opentelemetry/instrumentation/grpc/interceptors/client_spec.rb create mode 100644 instrumentation/grpc/spec/opentelemetry/instrumentation/grpc_spec.rb create mode 100644 instrumentation/grpc/spec/spec_helper.rb diff --git a/instrumentation/grpc/.rspec b/instrumentation/grpc/.rspec new file mode 100644 index 000000000..34c5164d9 --- /dev/null +++ b/instrumentation/grpc/.rspec @@ -0,0 +1,3 @@ +--format documentation +--color +--require spec_helper diff --git a/instrumentation/grpc/.standard.yml b/instrumentation/grpc/.standard.yml new file mode 100644 index 000000000..6d67c99eb --- /dev/null +++ b/instrumentation/grpc/.standard.yml @@ -0,0 +1,3 @@ +# For available configuration options, see: +# https://github.com/standardrb/standard +ruby_version: 3.0 diff --git a/instrumentation/grpc/CHANGELOG.md b/instrumentation/grpc/CHANGELOG.md new file mode 100644 index 000000000..4b7b650f9 --- /dev/null +++ b/instrumentation/grpc/CHANGELOG.md @@ -0,0 +1,18 @@ +## [Unreleased] + +## [0.1.3] - 2024-09-11 + +- Fix error in handling of non-gRPC errors +- Fix error in method signature for OpenTelemetry::Instrumentation::Grpc.client_interceptor [#1](https://github.com/hibachrach/opentelemetry-instrumentation-grpc/pull/1) + +## [0.1.2] - 2024-06-26 + +- Align span naming with spec + +## [0.1.1] - 2024-06-26 + +- Fix `uninitialized constant Interceptors` error + +## [0.1.0] - 2024-06-18 + +- Initial release diff --git a/instrumentation/grpc/CODE_OF_CONDUCT.md b/instrumentation/grpc/CODE_OF_CONDUCT.md new file mode 100644 index 000000000..e478f902c --- /dev/null +++ b/instrumentation/grpc/CODE_OF_CONDUCT.md @@ -0,0 +1,84 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience +* Focusing on what is best not just for us as individuals, but for the overall community + +Examples of unacceptable behavior include: + +* The use of sexualized language or imagery, and sexual attention or + advances of any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email + address, without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at hbachrach@dropbox.com. All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series of actions. + +**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within the community. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0, +available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. + +Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity). + +[homepage]: https://www.contributor-covenant.org + +For answers to common questions about this code of conduct, see the FAQ at +https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations. diff --git a/instrumentation/grpc/Gemfile b/instrumentation/grpc/Gemfile new file mode 100644 index 000000000..091fd2d84 --- /dev/null +++ b/instrumentation/grpc/Gemfile @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +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" diff --git a/instrumentation/grpc/LICENSE b/instrumentation/grpc/LICENSE new file mode 100644 index 000000000..1ef7dad2c --- /dev/null +++ b/instrumentation/grpc/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright The OpenTelemetry Authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/instrumentation/grpc/README.md b/instrumentation/grpc/README.md new file mode 100644 index 000000000..f49cfbf89 --- /dev/null +++ b/instrumentation/grpc/README.md @@ -0,0 +1,66 @@ +# OpenTelemetry::Instrumentation::Grpc + +[![Gem Version](https://badge.fury.io/rb/opentelemetry-instrumentation-grpc.svg)](https://badge.fury.io/rb/opentelemetry-instrumentation-grpc) +[![GitHub Actions CI Status](https://github.com/hibachrach/opentelemetry-instrumentation-grpc/actions/workflows/main.yml/badge.svg)](https://github.com/hibachrach/opentelemetry-instrumentation-grpc/actions?query=branch%3Amain) + +OpenTelemetry instrumentation for users of the `grpc` gem + +> [!WARNING] +> Right now, the gem only instruments outbound requests to gRPC services + +## Installation + +Install the gem and add to the application's Gemfile by executing: + + $ bundle add opentelemetry-instrumentation-grpc + +If bundler is not being used to manage dependencies, install the gem by executing: + + $ gem install opentelemetry-instrumentation-grpc + +## Usage + +To use the instrumentation, call `use` with the name of the instrumentation: + +```ruby +OpenTelemetry::SDK.configure do |c| + c.use 'OpenTelemetry::Instrumentation::Grpc', { + peer_service: "Example", + allowed_metadata_headers: [], + } +end +``` + +Alternatively, you can also call `use_all` to install all the available +instrumentation. + +```ruby +OpenTelemetry::SDK.configure do |c| + c.use_all +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. + +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). + +## Contributing + +Bug reports and pull requests are welcome on GitHub at https://github.com/hibachrach/opentelemetry-instrumentation-grpc. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/opentelemetry-instrumentation-grpc/blob/main/CODE_OF_CONDUCT.md). + +## License + +The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). + +## Code of Conduct + +Everyone interacting in the OpenTelemetry::Instrumentation::Grpc project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/opentelemetry-instrumentation-grpc/blob/main/CODE_OF_CONDUCT.md). diff --git a/instrumentation/grpc/Rakefile b/instrumentation/grpc/Rakefile new file mode 100644 index 000000000..011031f8f --- /dev/null +++ b/instrumentation/grpc/Rakefile @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +require "bundler/gem_tasks" +require "rspec/core/rake_task" + +RSpec::Core::RakeTask.new(:spec) + +require "standard/rake" + +task default: %i[spec standard] diff --git a/instrumentation/grpc/lib/opentelemetry-instrumentation-grpc.rb b/instrumentation/grpc/lib/opentelemetry-instrumentation-grpc.rb new file mode 100644 index 000000000..c6f1e5014 --- /dev/null +++ b/instrumentation/grpc/lib/opentelemetry-instrumentation-grpc.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +require_relative "opentelemetry/instrumentation" diff --git a/instrumentation/grpc/lib/opentelemetry/instrumentation.rb b/instrumentation/grpc/lib/opentelemetry/instrumentation.rb new file mode 100644 index 000000000..e036eea9d --- /dev/null +++ b/instrumentation/grpc/lib/opentelemetry/instrumentation.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +module OpenTelemetry + module Instrumentation + end +end + +require_relative "instrumentation/grpc" diff --git a/instrumentation/grpc/lib/opentelemetry/instrumentation/grpc.rb b/instrumentation/grpc/lib/opentelemetry/instrumentation/grpc.rb new file mode 100644 index 000000000..3672d3d05 --- /dev/null +++ b/instrumentation/grpc/lib/opentelemetry/instrumentation/grpc.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +require "opentelemetry" +require "opentelemetry-instrumentation-base" +require "active_support/inflector" + +module OpenTelemetry + module Instrumentation + # Contains the OpenTelemetry instrumentation for the Gruf 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" diff --git a/instrumentation/grpc/lib/opentelemetry/instrumentation/grpc/instrumentation.rb b/instrumentation/grpc/lib/opentelemetry/instrumentation/grpc/instrumentation.rb new file mode 100644 index 000000000..222797670 --- /dev/null +++ b/instrumentation/grpc/lib/opentelemetry/instrumentation/grpc/instrumentation.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +module OpenTelemetry + module Instrumentation + module Grpc + # The Instrumentation class contains logic to detect and install the Grpc instrumentation + class Instrumentation < OpenTelemetry::Instrumentation::Base + install do |_config| + require_dependencies + end + + option :allowed_metadata_headers, default: [], validate: :array + option :peer_service, default: nil, validate: :string + + present do + defined?(::GRPC) + end + + private + + def require_dependencies + require_relative "interceptors/client" + end + end + end + end +end diff --git a/instrumentation/grpc/lib/opentelemetry/instrumentation/grpc/interceptors/client.rb b/instrumentation/grpc/lib/opentelemetry/instrumentation/grpc/interceptors/client.rb new file mode 100644 index 000000000..988ff0db6 --- /dev/null +++ b/instrumentation/grpc/lib/opentelemetry/instrumentation/grpc/interceptors/client.rb @@ -0,0 +1,84 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +module OpenTelemetry + module Instrumentation + module Grpc + module Interceptors + class Client < ::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 + + 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) + 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) + 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) + 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("/") + service = method_parts[1] + method_name = method_parts.last + + attributes = { + 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 + }.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("/"), + attributes: attributes, + kind: OpenTelemetry::Trace::SpanKind::CLIENT + ) do |span| + OpenTelemetry.propagation.inject(metadata) + yield.tap do + span&.set_attribute(OpenTelemetry::SemanticConventions::Trace::RPC_GRPC_STATUS_CODE, 0) + end + rescue ::GRPC::BadStatus => e + span&.set_attribute(OpenTelemetry::SemanticConventions::Trace::RPC_GRPC_STATUS_CODE, e.code) + raise e + end + end + + def allowed_metadata_headers(metadata) + instrumentation_config[:allowed_metadata_headers].each_with_object({}) do |k, h| + if (v = metadata[k.to_s]) + h["rpc.request.metadata.#{k}"] = v + end + end + end + + def instrumentation_config + Grpc::Instrumentation.instance.config + end + + def instrumentation_tracer + Grpc::Instrumentation.instance.tracer + end + end + end + end + end +end diff --git a/instrumentation/grpc/lib/opentelemetry/instrumentation/grpc/version.rb b/instrumentation/grpc/lib/opentelemetry/instrumentation/grpc/version.rb new file mode 100644 index 000000000..1549ed154 --- /dev/null +++ b/instrumentation/grpc/lib/opentelemetry/instrumentation/grpc/version.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +module OpenTelemetry + module Instrumentation + module Grpc + VERSION = "0.1.3" + end + end +end diff --git a/instrumentation/grpc/opentelemetry-instrumentation-grpc.gemspec b/instrumentation/grpc/opentelemetry-instrumentation-grpc.gemspec new file mode 100644 index 000000000..446a414a1 --- /dev/null +++ b/instrumentation/grpc/opentelemetry-instrumentation-grpc.gemspec @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +require_relative "lib/opentelemetry/instrumentation/grpc/version" + +Gem::Specification.new do |spec| + spec.name = "opentelemetry-instrumentation-grpc" + spec.version = OpenTelemetry::Instrumentation::Grpc::VERSION + spec.authors = ['OpenTelemetry Authors'] + spec.email = ['cncf-opentelemetry-contributors@lists.cncf.io'] + + spec.summary = 'Gruf instrumentation for the OpenTelemetry framework' + spec.description = 'Gruf instrumentation for the OpenTelemetry framework' + spec.homepage = 'https://github.com/open-telemetry/opentelemetry-ruby-contrib' + spec.license = 'Apache-2.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. + gemspec = File.basename(__FILE__) + spec.files = IO.popen(%w[git ls-files -z], chdir: __dir__, err: IO::NULL) do |ls| + ls.readlines("\x0", chomp: true).reject do |f| + (f == gemspec) || + f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile]) + end + end + spec.bindir = "exe" + spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } + 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" + + # For more information and examples about making a new gem, check out our + # guide at: https://bundler.io/guides/creating_gem.html + + if spec.respond_to?(:metadata) + spec.metadata['changelog_uri'] = "https://open-telemetry.github.io/opentelemetry-ruby-contrib/opentelemetry-instrumentation-grpc/v#{OpenTelemetry::Instrumentation::Grpc::VERSION}/file.CHANGELOG.html" + spec.metadata['source_code_uri'] = 'https://github.com/open-telemetry/opentelemetry-ruby-contrib/tree/main/instrumentation/grpc' + spec.metadata['bug_tracker_uri'] = 'https://github.com/open-telemetry/opentelemetry-ruby-contrib/issues' + spec.metadata['documentation_uri'] = "https://open-telemetry.github.io/opentelemetry-ruby-contrib/opentelemetry-instrumentation-grpc/v#{OpenTelemetry::Instrumentation::Grpc::VERSION}" + end +end diff --git a/instrumentation/grpc/sig/opentelemetry/instrumentation/grpc.rbs b/instrumentation/grpc/sig/opentelemetry/instrumentation/grpc.rbs new file mode 100644 index 000000000..03f8245de --- /dev/null +++ b/instrumentation/grpc/sig/opentelemetry/instrumentation/grpc.rbs @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +module OpenTelemetry + module Instrumentation + module Grpc + VERSION: String + # See the writing guide of rbs: https://github.com/ruby/rbs#guides + end + end +end diff --git a/instrumentation/grpc/spec/example/Gemfile b/instrumentation/grpc/spec/example/Gemfile new file mode 100644 index 000000000..fafb31a31 --- /dev/null +++ b/instrumentation/grpc/spec/example/Gemfile @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +source "https://rubygems.org" + +gem "opentelemetry-api" +gem "opentelemetry-common" +gem "opentelemetry-instrumentation-gruf", path: "../../gruf" +gem "opentelemetry-sdk" +gem "gruf" +gem "google-protobuf" diff --git a/instrumentation/grpc/spec/example/base.rb b/instrumentation/grpc/spec/example/base.rb new file mode 100644 index 000000000..e66891270 --- /dev/null +++ b/instrumentation/grpc/spec/example/base.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +require "rubygems" +require "bundler/setup" +require "json" +require_relative "proto/example_api_pb" +require_relative "proto/example_api_services_pb" + +Bundler.require diff --git a/instrumentation/grpc/spec/example/controllers/example_api_controller.rb b/instrumentation/grpc/spec/example/controllers/example_api_controller.rb new file mode 100644 index 000000000..7bf6f14be --- /dev/null +++ b/instrumentation/grpc/spec/example/controllers/example_api_controller.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +require_relative "../base" + +class ExampleApiController < Gruf::Controllers::Base + bind Proto::Example::ExampleAPI::Service + + def example + Proto::Example::ExampleResponse.new(response_name: "Done") + end +end diff --git a/instrumentation/grpc/spec/example/proto/example_api.proto b/instrumentation/grpc/spec/example/proto/example_api.proto new file mode 100644 index 000000000..d28ba90f3 --- /dev/null +++ b/instrumentation/grpc/spec/example/proto/example_api.proto @@ -0,0 +1,16 @@ +syntax = "proto3"; + +package proto.example; + +service ExampleAPI { + rpc Example(ExampleRequest) returns (ExampleResponse); +} + +message ExampleRequest { + uint64 id = 1; + string name = 2; +} + +message ExampleResponse { + string response_name = 1; +} \ No newline at end of file diff --git a/instrumentation/grpc/spec/example/proto/example_api_pb.rb b/instrumentation/grpc/spec/example/proto/example_api_pb.rb new file mode 100644 index 000000000..4e4649924 --- /dev/null +++ b/instrumentation/grpc/spec/example/proto/example_api_pb.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: example/example_api.proto + +require "google/protobuf" + +Google::Protobuf::DescriptorPool.generated_pool.build do + add_file("example/example_api.proto", syntax: :proto3) do + add_message "proto.example.ExampleRequest" do + optional :id, :uint64, 1 + optional :name, :string, 2 + end + add_message "proto.example.ExampleResponse" do + optional :response_name, :string, 1 + end + end +end + +module Proto + module Example + ExampleRequest = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("proto.example.ExampleRequest").msgclass + ExampleResponse = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("proto.example.ExampleResponse").msgclass + end +end diff --git a/instrumentation/grpc/spec/example/proto/example_api_services_pb.rb b/instrumentation/grpc/spec/example/proto/example_api_services_pb.rb new file mode 100644 index 000000000..8fb75965b --- /dev/null +++ b/instrumentation/grpc/spec/example/proto/example_api_services_pb.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +# Generated by the protocol buffer compiler. DO NOT EDIT! +# Source: example/example_api.proto for package 'proto.example' + +require "grpc" +require_relative "example_api_pb" + +module Proto + module Example + module ExampleAPI + class Service + include ::GRPC::GenericService + + self.marshal_class_method = :encode + self.unmarshal_class_method = :decode + self.service_name = "proto.example.ExampleAPI" + + rpc :Example, ::Proto::Example::ExampleRequest, ::Proto::Example::ExampleResponse + end + + Stub = Service.rpc_stub_class + end + end +end diff --git a/instrumentation/grpc/spec/example/trace_demonstration.rb b/instrumentation/grpc/spec/example/trace_demonstration.rb new file mode 100644 index 000000000..9e4c38a28 --- /dev/null +++ b/instrumentation/grpc/spec/example/trace_demonstration.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +require_relative "base" + +# Export traces to console by default +ENV["OTEL_TRACES_EXPORTER"] = "console" +# Configure OpenTelemetry::Instrumentation::Gruf +OpenTelemetry::SDK.configure do |c| + c.use "OpenTelemetry::Instrumentation::Gruf", { + peer_service: "Example", + grpc_ignore_methods_on_client: [], # ["proto.example.example_api.example"] + grpc_ignore_methods_on_server: [], # ["proto.example.example_api.example"] + allowed_metadata_headers: [] + } +end + +# Configure Gruf::Server +Gruf.configure do |config| + config.controllers_path = ("./controllers") + config.interceptors.clear + config.interceptors.use(OpenTelemetry::Instrumentation::Gruf::Interceptors::Server) +end +Gruf.autoloaders.load!(controllers_path: Gruf.controllers_path) + +Thread.new do + # Configure Gruf::Client: send request after 5 seconds + sleep 5 + client_options = {interceptors: [OpenTelemetry::Instrumentation::Gruf::Interceptors::Client.new]} + client = Gruf::Client.new( + service: Proto::Example::ExampleAPI, options: {}, client_options: client_options + ) + metadata = {project_name: "Example project", authorization: "authorization_token"} + client.call(:Example, {id: 1, name: "Example"}, metadata) + + # Kill process after 3 seconds + sleep 3 + exit +end + +server = Gruf::Server.new +# Demonstrate tracing for client and server (span output to console): +server.start! diff --git a/instrumentation/grpc/spec/opentelemetry/instrumentation/grpc/interceptors/client_spec.rb b/instrumentation/grpc/spec/opentelemetry/instrumentation/grpc/interceptors/client_spec.rb new file mode 100644 index 000000000..c536120e5 --- /dev/null +++ b/instrumentation/grpc/spec/opentelemetry/instrumentation/grpc/interceptors/client_spec.rb @@ -0,0 +1,82 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +require "spec_helper" + +require "opentelemetry/instrumentation/grpc/interceptors/client" + +RSpec.describe OpenTelemetry::Instrumentation::Grpc::Interceptors::Client do + before do + instrumentation.install(config) + exporter.reset + end + + after do + instrumentation.instance_variable_set(:@installed, false) + end + + let(:config) { {} } + let(:exporter) { EXPORTER } + let(:instrumentation) { OpenTelemetry::Instrumentation::Grpc::Instrumentation.instance } + let(:span) { exporter.finished_spans.first } + let(:response) { Proto::Example::ExampleResponse.new(response_name: "Done") } + let(:block) { proc { response } } + let(:client_call) do + OpenTelemetry::Instrumentation::Grpc::Interceptors::Client + .new.request_response(request: Proto::Example::ExampleRequest.new, call: proc { true }, method: "/proto.example.ExampleAPI/Example", metadata: {foo: "bar"}, &block) + end + + describe "success request" do + it "gets response and finish span" do + expect(client_call).to eq(response) + expect(exporter.finished_spans.size).to eq(1) + expect(span.kind).to eq(:client) + expect(span.attributes["rpc.system"]).to eq("grpc") + expect(span.attributes["rpc.type"]).to eq("request_response") + expect(span.name).to eq("proto.example.ExampleAPI/Example") + end + + describe "with allowed_metadata_headers" do + let(:config) { {allowed_metadata_headers: [:foo]} } + + it do + client_call + + expect(exporter.finished_spans.size).to eq(1) + expect(span.attributes["rpc.request.metadata.foo"]).to eq("bar") + end + end + end + + describe "raise non-gRPC related error" do + let(:error_class) { Class.new(StandardError) } + let(:block) { proc { raise error_class } } + + it "gets response and finish span" do + expect { client_call }.to raise_error(error_class) + expect(exporter.finished_spans.size).to eq(1) + expect(span.kind).to eq(:client) + expect(span.attributes["rpc.system"]).to eq("grpc") + expect(span.attributes["rpc.type"]).to eq("request_response") + expect(span.events.size).to eq(1) + expect(span.events.first.name).to eq("exception") + end + end + + describe "raise gRPC related error" do + let(:block) { proc { raise ::GRPC::NotFound } } + + it "gets response and finish span, setting code correctly" do + expect { client_call }.to raise_error(::GRPC::NotFound) + expect(exporter.finished_spans.size).to eq(1) + expect(span.kind).to eq(:client) + expect(span.attributes["rpc.system"]).to eq("grpc") + expect(span.attributes["rpc.type"]).to eq("request_response") + expect(span.attributes["rpc.grpc.status_code"]).to eq(::GRPC::NotFound.new.code) + expect(span.events.size).to eq(1) + end + end +end diff --git a/instrumentation/grpc/spec/opentelemetry/instrumentation/grpc_spec.rb b/instrumentation/grpc/spec/opentelemetry/instrumentation/grpc_spec.rb new file mode 100644 index 000000000..0a805138c --- /dev/null +++ b/instrumentation/grpc/spec/opentelemetry/instrumentation/grpc_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +RSpec.describe OpenTelemetry::Instrumentation::Grpc do + it "has a version number" do + expect(OpenTelemetry::Instrumentation::Grpc::VERSION).not_to be nil + end + + describe ".client_interceptor" do + it { expect(described_class.client_interceptor).to be_a(OpenTelemetry::Instrumentation::Grpc::Interceptors::Client) } + end +end diff --git a/instrumentation/grpc/spec/spec_helper.rb b/instrumentation/grpc/spec/spec_helper.rb new file mode 100644 index 000000000..7a082ee74 --- /dev/null +++ b/instrumentation/grpc/spec/spec_helper.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +require "opentelemetry/sdk" +require "grpc" + +require "opentelemetry/instrumentation/grpc" +require "example/proto/example_api_pb" +require "example/proto/example_api_services_pb" + +RSpec.configure do |config| + # Enable flags like --only-failures and --next-failure + config.example_status_persistence_file_path = ".rspec_status" + + # Disable RSpec exposing methods globally on `Module` and `main` + config.disable_monkey_patching! + + config.expect_with :rspec do |c| + c.syntax = :expect + end +end + +EXPORTER = OpenTelemetry::SDK::Trace::Export::InMemorySpanExporter.new +span_processor = OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(EXPORTER) + +OpenTelemetry::SDK.configure do |c| + c.add_span_processor span_processor +end From 59b313ebbf483d07bb2372f87a6dcd2bc548c006 Mon Sep 17 00:00:00 2001 From: Kayla Reopelle <87386821+kaylareopelle@users.noreply.github.com> Date: Wed, 20 Nov 2024 17:18:56 -0800 Subject: [PATCH 2/4] Apply suggestions from code review --- instrumentation/grpc/README.md | 26 +++++++++---------- .../lib/opentelemetry/instrumentation/grpc.rb | 2 +- ...opentelemetry-instrumentation-grpc.gemspec | 4 +-- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/instrumentation/grpc/README.md b/instrumentation/grpc/README.md index f49cfbf89..082dc4906 100644 --- a/instrumentation/grpc/README.md +++ b/instrumentation/grpc/README.md @@ -23,28 +23,28 @@ If bundler is not being used to manage dependencies, install the gem by executin To use the instrumentation, call `use` with the name of the instrumentation: ```ruby -OpenTelemetry::SDK.configure do |c| - c.use 'OpenTelemetry::Instrumentation::Grpc', { - peer_service: "Example", - allowed_metadata_headers: [], - } -end + OpenTelemetry::SDK.configure do |c| + c.use 'OpenTelemetry::Instrumentation::Grpc', { + peer_service: "Example", + allowed_metadata_headers: [], + } + end ``` Alternatively, you can also call `use_all` to install all the available instrumentation. ```ruby -OpenTelemetry::SDK.configure do |c| - c.use_all -end + OpenTelemetry::SDK.configure do |c| + c.use_all + 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]) + otel = OpenTelemetry::Instrumentation::Grpc.client_interceptor + SomeService::Stub.new(host, credentials, *args, **kwargs, interceptors: [otel]) ``` ## Development @@ -55,7 +55,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To ## Contributing -Bug reports and pull requests are welcome on GitHub at https://github.com/hibachrach/opentelemetry-instrumentation-grpc. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/opentelemetry-instrumentation-grpc/blob/main/CODE_OF_CONDUCT.md). +Bug reports and pull requests are welcome on GitHub at https://github.com/open-telemetry/opentelemetry-ruby-contrib. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/open-telemetry/opentelemetry-ruby-contrib/blob/main/CODE_OF_CONDUCT.md). ## License @@ -63,4 +63,4 @@ The gem is available as open source under the terms of the [MIT License](https:/ ## Code of Conduct -Everyone interacting in the OpenTelemetry::Instrumentation::Grpc project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/opentelemetry-instrumentation-grpc/blob/main/CODE_OF_CONDUCT.md). +Everyone interacting in the OpenTelemetry::Instrumentation::Grpc project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/open-telemetry/opentelemetry-ruby-contrib/blob/main/CODE_OF_CONDUCT.md). diff --git a/instrumentation/grpc/lib/opentelemetry/instrumentation/grpc.rb b/instrumentation/grpc/lib/opentelemetry/instrumentation/grpc.rb index 3672d3d05..582b53fe3 100644 --- a/instrumentation/grpc/lib/opentelemetry/instrumentation/grpc.rb +++ b/instrumentation/grpc/lib/opentelemetry/instrumentation/grpc.rb @@ -10,7 +10,7 @@ module OpenTelemetry module Instrumentation - # Contains the OpenTelemetry instrumentation for the Gruf gem + # Contains the OpenTelemetry instrumentation for the gRPC gem module Grpc class Error < StandardError; end diff --git a/instrumentation/grpc/opentelemetry-instrumentation-grpc.gemspec b/instrumentation/grpc/opentelemetry-instrumentation-grpc.gemspec index 446a414a1..e2c18e6a3 100644 --- a/instrumentation/grpc/opentelemetry-instrumentation-grpc.gemspec +++ b/instrumentation/grpc/opentelemetry-instrumentation-grpc.gemspec @@ -12,8 +12,8 @@ Gem::Specification.new do |spec| spec.authors = ['OpenTelemetry Authors'] spec.email = ['cncf-opentelemetry-contributors@lists.cncf.io'] - spec.summary = 'Gruf instrumentation for the OpenTelemetry framework' - spec.description = 'Gruf instrumentation for the OpenTelemetry framework' + spec.summary = 'gRPC instrumentation for the OpenTelemetry framework' + spec.description = 'gRPC instrumentation for the OpenTelemetry framework' spec.homepage = 'https://github.com/open-telemetry/opentelemetry-ruby-contrib' spec.license = 'Apache-2.0' From 8ba7ca3ff6ee0d0b5d4521862d9192b579fb24e5 Mon Sep 17 00:00:00 2001 From: Kayla Reopelle Date: Thu, 21 Nov 2024 12:24:43 -0800 Subject: [PATCH 3/4] chore: Markdown lint, class updates for CI --- instrumentation/grpc/README.md | 11 +++++++++-- .../instrumentation/grpc/instrumentation.rb | 5 +++++ .../instrumentation/grpc/interceptors/client.rb | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/instrumentation/grpc/README.md b/instrumentation/grpc/README.md index 082dc4906..8d3c1165c 100644 --- a/instrumentation/grpc/README.md +++ b/instrumentation/grpc/README.md @@ -8,15 +8,22 @@ OpenTelemetry instrumentation for users of the `grpc` gem > [!WARNING] > Right now, the gem only instruments outbound requests to gRPC services +> [!WARNING] +> This gem is not ready for release by the open-telemetry org yet, but other versions are available on RubyGems + ## Installation Install the gem and add to the application's Gemfile by executing: - $ bundle add opentelemetry-instrumentation-grpc +```sh + $ bundle add opentelemetry-instrumentation-grpc +``` If bundler is not being used to manage dependencies, install the gem by executing: - $ gem install opentelemetry-instrumentation-grpc +```sh + $ gem install opentelemetry-instrumentation-grpc +``` ## Usage diff --git a/instrumentation/grpc/lib/opentelemetry/instrumentation/grpc/instrumentation.rb b/instrumentation/grpc/lib/opentelemetry/instrumentation/grpc/instrumentation.rb index 222797670..5287f44b8 100644 --- a/instrumentation/grpc/lib/opentelemetry/instrumentation/grpc/instrumentation.rb +++ b/instrumentation/grpc/lib/opentelemetry/instrumentation/grpc/instrumentation.rb @@ -11,6 +11,7 @@ module Grpc class Instrumentation < OpenTelemetry::Instrumentation::Base install do |_config| require_dependencies + patch end option :allowed_metadata_headers, default: [], validate: :array @@ -22,6 +23,10 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base private + def patch + ::GRPC::ClientInterceptor.prepend(Interceptors::Client) + end + def require_dependencies require_relative "interceptors/client" end diff --git a/instrumentation/grpc/lib/opentelemetry/instrumentation/grpc/interceptors/client.rb b/instrumentation/grpc/lib/opentelemetry/instrumentation/grpc/interceptors/client.rb index 988ff0db6..2cc03459d 100644 --- a/instrumentation/grpc/lib/opentelemetry/instrumentation/grpc/interceptors/client.rb +++ b/instrumentation/grpc/lib/opentelemetry/instrumentation/grpc/interceptors/client.rb @@ -8,7 +8,7 @@ module OpenTelemetry module Instrumentation module Grpc module Interceptors - class Client < ::GRPC::ClientInterceptor + class Client 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 From 2b6c708c2f9810e7bbb5e3fa45cceaca6c8dda0a Mon Sep 17 00:00:00 2001 From: Kayla Reopelle <87386821+kaylareopelle@users.noreply.github.com> Date: Mon, 25 Nov 2024 13:43:17 -0800 Subject: [PATCH 4/4] Update instrumentation/grpc/README.md --- instrumentation/grpc/README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/instrumentation/grpc/README.md b/instrumentation/grpc/README.md index 8d3c1165c..8377b9d57 100644 --- a/instrumentation/grpc/README.md +++ b/instrumentation/grpc/README.md @@ -1,4 +1,6 @@ # OpenTelemetry::Instrumentation::Grpc +> [!CAUTION] +> This gem is not ready for release by the open-telemetry org yet, but other versions are available on RubyGems [![Gem Version](https://badge.fury.io/rb/opentelemetry-instrumentation-grpc.svg)](https://badge.fury.io/rb/opentelemetry-instrumentation-grpc) [![GitHub Actions CI Status](https://github.com/hibachrach/opentelemetry-instrumentation-grpc/actions/workflows/main.yml/badge.svg)](https://github.com/hibachrach/opentelemetry-instrumentation-grpc/actions?query=branch%3Amain) @@ -8,9 +10,6 @@ OpenTelemetry instrumentation for users of the `grpc` gem > [!WARNING] > Right now, the gem only instruments outbound requests to gRPC services -> [!WARNING] -> This gem is not ready for release by the open-telemetry org yet, but other versions are available on RubyGems - ## Installation Install the gem and add to the application's Gemfile by executing: