diff --git a/.github/dependabot.yml b/.github/dependabot.yml index bc622e2e7..5485d59f1 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -173,10 +173,6 @@ updates: directory: "/instrumentation/rails" schedule: interval: weekly -- package-ecosystem: bundler - directory: "/resource_detectors" - schedule: - interval: weekly - package-ecosystem: bundler directory: "/resources/azure" schedule: diff --git a/.github/workflows/ci-contrib-canary.yml b/.github/workflows/ci-contrib-canary.yml index ebf4f534a..214a516a7 100644 --- a/.github/workflows/ci-contrib-canary.yml +++ b/.github/workflows/ci-contrib-canary.yml @@ -63,7 +63,6 @@ jobs: fail-fast: false matrix: gem: - - resource_detectors - resource-detector-azure - resource-detector-container - resource-detector-google_cloud_platform diff --git a/.github/workflows/ci-contrib.yml b/.github/workflows/ci-contrib.yml index c2a86e1b0..75e970310 100644 --- a/.github/workflows/ci-contrib.yml +++ b/.github/workflows/ci-contrib.yml @@ -54,7 +54,6 @@ jobs: fail-fast: false matrix: gem: - - resource_detectors - resource-detector-azure - resource-detector-container - resource-detector-google_cloud_platform diff --git a/.github/workflows/ci-instrumentation-canary.yml b/.github/workflows/ci-instrumentation-canary.yml index 4dd45a722..ae32dd9bb 100644 --- a/.github/workflows/ci-instrumentation-canary.yml +++ b/.github/workflows/ci-instrumentation-canary.yml @@ -33,6 +33,7 @@ jobs: - koala - lmdb - net_http + - httpx - rack - rails - restclient diff --git a/.github/workflows/ci-instrumentation.yml b/.github/workflows/ci-instrumentation.yml index b5bacdeb2..d06fe5d5b 100644 --- a/.github/workflows/ci-instrumentation.yml +++ b/.github/workflows/ci-instrumentation.yml @@ -34,6 +34,7 @@ jobs: - gruf - http - http_client + - httpx - koala - lmdb - net_http diff --git a/.toys/.data/releases.yml b/.toys/.data/releases.yml index 8b78685f3..7d689c7a4 100644 --- a/.toys/.data/releases.yml +++ b/.toys/.data/releases.yml @@ -106,6 +106,10 @@ gems: directory: instrumentation/http_client version_constant: [OpenTelemetry, Instrumentation, HttpClient, VERSION] + - name: opentelemetry-instrumentation-httpx + directory: instrumentation/httpx + version_constant: [OpenTelemetry, Instrumentation, HTTPX, VERSION] + - name: opentelemetry-instrumentation-koala directory: instrumentation/koala version_constant: [OpenTelemetry, Instrumentation, Koala, VERSION] @@ -203,11 +207,6 @@ gems: directory: propagator/xray version_constant: [OpenTelemetry, Propagator, XRay, VERSION] - - name: opentelemetry-resource_detectors - directory: resource_detectors - version_rb_path: lib/opentelemetry/resource/detectors/version.rb - version_constant: [OpenTelemetry, Resource, Detectors, VERSION] - - name: opentelemetry-resource-detector-azure directory: resources/azure version_rb_path: lib/opentelemetry/resource/detector/azure/version.rb diff --git a/CODEOWNERS b/CODEOWNERS index ff2a9eb21..8c2435bb4 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -22,6 +22,8 @@ instrumentation/grape/ @muripic @fbogsany @mwear @robertlaurin @dazuma @ericmust instrumentation/graphql/ @swalkinshaw @rmosolgo @fbogsany @mwear @robertlaurin @dazuma @ericmustin @arielvalentin @ahayworth @plantfansam @robbkidd +instrumentation/httpx/ @HoneyryderChuck @fbogsany @mwear @robertlaurin @dazuma @ericmustin @arielvalentin @ahayworth @plantfansam @robbkidd + instrumentation/mongo/ @johnnyshields @fbogsany @mwear @robertlaurin @dazuma @ericmustin @arielvalentin @ahayworth @plantfansam @robbkidd instrumentation/racecar/ @chrisholmes @fbogsany @mwear @robertlaurin @dazuma @ericmustin @arielvalentin @ahayworth @plantfansam @robbkidd diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 391929061..aad37a276 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -67,15 +67,15 @@ _Setting up a running Ruby environment is outside the scope of this document._ This repository contains multiple Ruby gems: * Various instrumentation gems located in subdirectories of `instrumentation` + * Various resource detector gems located in subdirectories of `resources` * `opentelemetry-propagator-xray` located in the `propagator/xray` directory * `opentelemetry-propagator-ottrace` located in the `propagator/ottrace` directory - * `opentelemetry-resource_detectors` located in the `resource_detectors` directory Each of these gems has its configuration and tests. -For example, to test `opentelemetry-resource_detectors` you would: +For example, to test `opentelemetry-instrumentation-action_pack` you would: - 1. Change directory to `resource_detectors` + 1. Change directory to `instrumentation/action_pack` 2. Install the bundle with `bundle install` 3. Run the tests with `bundle exec rake` diff --git a/README.md b/README.md index 3dbcec330..d7de85310 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ using OpenTelemetry with minimal changes to your application. See the This repository also contains libraries to aid with interoperablity with vendor specific tracing solutions: - [Context Propagation](propagator/): OTTrace and Amazon X-Ray -- [Resource Detectors](resource_detectors/): +- [Resource Detectors](resources/): - Azure - Container - Google Cloud Platform diff --git a/instrumentation/active_support/CHANGELOG.md b/instrumentation/active_support/CHANGELOG.md index 9093048a0..6254326a6 100644 --- a/instrumentation/active_support/CHANGELOG.md +++ b/instrumentation/active_support/CHANGELOG.md @@ -1,5 +1,9 @@ # Release History: opentelemetry-instrumentation-active_support +### v0.4.4 / 2023-10-31 + +* FIXED: Remove call to ActiveSupport::Notifications.notifier#synchronize deprecated in Rails 7.2 + ### v0.4.3 / 2023-10-16 * FIXED: Add Rails 7.1 compatibility diff --git a/instrumentation/active_support/lib/opentelemetry/instrumentation/active_support/span_subscriber.rb b/instrumentation/active_support/lib/opentelemetry/instrumentation/active_support/span_subscriber.rb index 72e8f04a9..e2ab1d700 100644 --- a/instrumentation/active_support/lib/opentelemetry/instrumentation/active_support/span_subscriber.rb +++ b/instrumentation/active_support/lib/opentelemetry/instrumentation/active_support/span_subscriber.rb @@ -30,19 +30,23 @@ def self.subscribe( subscriber_object = ::ActiveSupport::Notifications.subscribe(pattern, subscriber) - ::ActiveSupport::Notifications.notifier.synchronize do - subscribers = ::ActiveSupport::Notifications.notifier.instance_variable_get(:@string_subscribers)[pattern] - - if subscribers.nil? - OpenTelemetry.handle_error( - message: 'Unable to move OTEL ActiveSupport Notifications subscriber to the front of the notifications list which may cause incomplete traces.' \ - 'Please report an issue here: ' \ - 'https://github.com/open-telemetry/opentelemetry-ruby-contrib/issues/new?labels=bug&template=bug_report.md&title=ActiveSupport%20Notifications%20subscribers%20list%20is%20nil' - ) - else - subscribers.unshift( - subscribers.delete(subscriber_object) - ) + # this can be removed once we drop support for Rails < 7.2 + # see https://github.com/open-telemetry/opentelemetry-ruby-contrib/pull/707 for more context + if ::ActiveSupport::Notifications.notifier.respond_to?(:synchronize) + ::ActiveSupport::Notifications.notifier.synchronize do + subscribers = ::ActiveSupport::Notifications.notifier.instance_variable_get(:@string_subscribers)[pattern] + + if subscribers.nil? + OpenTelemetry.handle_error( + message: 'Unable to move OTEL ActiveSupport Notifications subscriber to the front of the notifications list which may cause incomplete traces.' \ + 'Please report an issue here: ' \ + 'https://github.com/open-telemetry/opentelemetry-ruby-contrib/issues/new?labels=bug&template=bug_report.md&title=ActiveSupport%20Notifications%20subscribers%20list%20is%20nil' + ) + else + subscribers.unshift( + subscribers.delete(subscriber_object) + ) + end end end subscriber_object diff --git a/instrumentation/active_support/lib/opentelemetry/instrumentation/active_support/version.rb b/instrumentation/active_support/lib/opentelemetry/instrumentation/active_support/version.rb index c159609b9..96965c3e7 100644 --- a/instrumentation/active_support/lib/opentelemetry/instrumentation/active_support/version.rb +++ b/instrumentation/active_support/lib/opentelemetry/instrumentation/active_support/version.rb @@ -7,7 +7,7 @@ module OpenTelemetry module Instrumentation module ActiveSupport - VERSION = '0.4.3' + VERSION = '0.4.4' end end end diff --git a/instrumentation/grape/CHANGELOG.md b/instrumentation/grape/CHANGELOG.md index f1fe30822..a6a55b96e 100644 --- a/instrumentation/grape/CHANGELOG.md +++ b/instrumentation/grape/CHANGELOG.md @@ -1,5 +1,9 @@ # Release History: opentelemetry-instrumentation-grape +### v0.1.5 / 2023-10-31 + +* FIXED: Remove dependency on ActiveSupport core extensions from Grape instrumentation + ### v0.1.4 / 2023-08-02 * FIXED: Fix opentelemetry-api version constraint in grape gemspec diff --git a/instrumentation/grape/lib/opentelemetry/instrumentation/grape/version.rb b/instrumentation/grape/lib/opentelemetry/instrumentation/grape/version.rb index 48400673f..4bf132dcd 100644 --- a/instrumentation/grape/lib/opentelemetry/instrumentation/grape/version.rb +++ b/instrumentation/grape/lib/opentelemetry/instrumentation/grape/version.rb @@ -8,7 +8,7 @@ module OpenTelemetry module Instrumentation module Grape # Current gem version - VERSION = '0.1.4' + VERSION = '0.1.5' end end end diff --git a/instrumentation/httpx/.yardopts b/instrumentation/httpx/.yardopts new file mode 100644 index 000000000..66530b20f --- /dev/null +++ b/instrumentation/httpx/.yardopts @@ -0,0 +1,9 @@ +--no-private +--title=OpenTelemetry HTTPX Instrumentation +--markup=markdown +--main=README.md +./lib/opentelemetry/instrumentation/**/*.rb +./lib/opentelemetry/instrumentation.rb +- +README.md +CHANGELOG.md diff --git a/instrumentation/httpx/Appraisals b/instrumentation/httpx/Appraisals new file mode 100644 index 000000000..6d5b4793c --- /dev/null +++ b/instrumentation/httpx/Appraisals @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +appraise 'httpx-1' do + gem 'httpx', '~> 1.0' +end + +appraise 'httpx-0' do + gem 'httpx', '~> 0.24' +end diff --git a/instrumentation/httpx/CHANGELOG.md b/instrumentation/httpx/CHANGELOG.md new file mode 100644 index 000000000..47ddf384d --- /dev/null +++ b/instrumentation/httpx/CHANGELOG.md @@ -0,0 +1 @@ +# Release History: opentelemetry-instrumentation-httpx diff --git a/resource_detectors/Gemfile b/instrumentation/httpx/Gemfile similarity index 57% rename from resource_detectors/Gemfile rename to instrumentation/httpx/Gemfile index 6ae9e48bc..8b038cdcb 100644 --- a/resource_detectors/Gemfile +++ b/instrumentation/httpx/Gemfile @@ -8,7 +8,7 @@ source 'https://rubygems.org' gemspec -group :development, :test do - gem 'byebug' unless RUBY_PLATFORM == 'java' - gem 'pry' +group :test do + gem 'opentelemetry-instrumentation-base', path: '../base' + gem 'pry-byebug', platform: 'ruby' end diff --git a/resource_detectors/LICENSE b/instrumentation/httpx/LICENSE similarity index 100% rename from resource_detectors/LICENSE rename to instrumentation/httpx/LICENSE diff --git a/instrumentation/httpx/README.md b/instrumentation/httpx/README.md new file mode 100644 index 000000000..2053e8ab1 --- /dev/null +++ b/instrumentation/httpx/README.md @@ -0,0 +1,49 @@ +# OpenTelemetry Http Instrumentation + +The HTTPX instrumentation is a community-maintained instrumentation for the [HTTPX][httpx-home] gem. + +## How do I get started? + +Install the gem using: + +``` +gem install opentelemetry-instrumentation-httpx +``` + +Or, if you use [bundler][bundler-home], include `opentelemetry-instrumentation-httpx` in your `Gemfile`. + +## Usage + +To use the instrumentation, call `use` with the name of the instrumentation: + +```ruby +OpenTelemetry::SDK.configure do |c| + c.use 'OpenTelemetry::Instrumentation::HTTPX' +end +``` + +Alternatively, you can also call `use_all` to install all the available instrumentation. + +```ruby +OpenTelemetry::SDK.configure do |c| + c.use_all +end +``` + +## How can I get involved? + +The `opentelemetry-instrumentation-httpx` gem source is [on github][repo-github], along with related gems including `opentelemetry-api` and `opentelemetry-sdk`. + +The OpenTelemetry Ruby gems are maintained by the OpenTelemetry-Ruby special interest group (SIG). You can get involved by joining us in [GitHub Discussions][discussions-url] or attending our weekly meeting. See the [meeting calendar][community-meetings] for dates and times. For more information on this and other language SIGs, see the OpenTelemetry [community page][ruby-sig]. + +## License + +The `opentelemetry-instrumentation-httpx` gem is distributed under the Apache 2.0 license. See [LICENSE][license-github] for more information. + +[http-home]: https://gitlab.com/os85/httpx +[bundler-home]: https://bundler.io +[repo-github]: https://github.com/open-telemetry/opentelemetry-ruby +[license-github]: https://github.com/open-telemetry/opentelemetry-ruby-contrib/blob/main/LICENSE +[ruby-sig]: https://github.com/open-telemetry/community#ruby-sig +[community-meetings]: https://github.com/open-telemetry/community#community-meetings +[discussions-url]: https://github.com/open-telemetry/opentelemetry-ruby/discussions diff --git a/resource_detectors/Rakefile b/instrumentation/httpx/Rakefile similarity index 100% rename from resource_detectors/Rakefile rename to instrumentation/httpx/Rakefile diff --git a/instrumentation/httpx/example/trace_demonstration.rb b/instrumentation/httpx/example/trace_demonstration.rb new file mode 100644 index 000000000..f0afdbf11 --- /dev/null +++ b/instrumentation/httpx/example/trace_demonstration.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require 'bundler/inline' + +gemfile(true) do + source 'https://rubygems.org' + gem 'opentelemetry-api' + gem 'opentelemetry-instrumentation-base' + gem 'opentelemetry-instrumentation-httpx' + gem 'opentelemetry-sdk' + gem 'httpx' +end + +require 'opentelemetry-api' +require 'opentelemetry-sdk' +require 'opentelemetry-instrumentation-httpx' +require 'httpx' + +# Export traces to console by default +ENV['OTEL_TRACES_EXPORTER'] ||= 'console' + +OpenTelemetry::SDK.configure do |c| + c.use 'OpenTelemetry::Instrumentation::HTTPX' +end + +# A basic HTTP example +HTTPX.get('https://github.com') diff --git a/resource_detectors/lib/opentelemetry-resource_detectors.rb b/instrumentation/httpx/lib/opentelemetry-instrumentation-httpx.rb similarity index 67% rename from resource_detectors/lib/opentelemetry-resource_detectors.rb rename to instrumentation/httpx/lib/opentelemetry-instrumentation-httpx.rb index c84a217e4..c034f140f 100644 --- a/resource_detectors/lib/opentelemetry-resource_detectors.rb +++ b/instrumentation/httpx/lib/opentelemetry-instrumentation-httpx.rb @@ -4,4 +4,4 @@ # # SPDX-License-Identifier: Apache-2.0 -require_relative 'opentelemetry/resource/detectors' +require_relative 'opentelemetry/instrumentation' diff --git a/resource_detectors/lib/opentelemetry/resource/detectors.rb b/instrumentation/httpx/lib/opentelemetry/instrumentation.rb similarity index 52% rename from resource_detectors/lib/opentelemetry/resource/detectors.rb rename to instrumentation/httpx/lib/opentelemetry/instrumentation.rb index 78494b65a..b9459a853 100644 --- a/resource_detectors/lib/opentelemetry/resource/detectors.rb +++ b/instrumentation/httpx/lib/opentelemetry/instrumentation.rb @@ -4,12 +4,6 @@ # # SPDX-License-Identifier: Apache-2.0 -require 'opentelemetry/sdk' -require 'opentelemetry/resource/detectors/version' -require 'opentelemetry/resource/detectors/google_cloud_platform' -require 'opentelemetry/resource/detectors/azure' -require 'opentelemetry/resource/detectors/auto_detector' - # 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. @@ -17,10 +11,9 @@ # The OpenTelemetry module provides global accessors for telemetry objects. # See the documentation for the `opentelemetry-api` gem for details. module OpenTelemetry - module Resource - # Detectors contains the resource detectors as well as the AutoDetector - # that can run all the detectors and return an accumlated resource - module Detectors - end + # 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/httpx' diff --git a/instrumentation/httpx/lib/opentelemetry/instrumentation/httpx.rb b/instrumentation/httpx/lib/opentelemetry/instrumentation/httpx.rb new file mode 100644 index 000000000..70e00f6cc --- /dev/null +++ b/instrumentation/httpx/lib/opentelemetry/instrumentation/httpx.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +require 'opentelemetry' +require 'opentelemetry-instrumentation-base' + +module OpenTelemetry + module Instrumentation + # Contains the OpenTelemetry instrumentation for the Http gem + module HTTPX + end + end +end + +require_relative 'httpx/instrumentation' +require_relative 'httpx/version' diff --git a/instrumentation/httpx/lib/opentelemetry/instrumentation/httpx/instrumentation.rb b/instrumentation/httpx/lib/opentelemetry/instrumentation/httpx/instrumentation.rb new file mode 100644 index 000000000..9174343b4 --- /dev/null +++ b/instrumentation/httpx/lib/opentelemetry/instrumentation/httpx/instrumentation.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +module OpenTelemetry + module Instrumentation + module HTTPX + # The Instrumentation class contains logic to detect and install the Http instrumentation + class Instrumentation < OpenTelemetry::Instrumentation::Base + install do |_config| + require_dependencies + patch + end + + compatible do + Gem::Version.new(::HTTPX::VERSION) >= Gem::Version.new('0.24.7') + end + + present do + defined?(::HTTPX) + end + + option :peer_service, default: nil, validate: :string + + def patch + otel_session = ::HTTPX.plugin(Plugin) + + ::HTTPX.send(:remove_const, :Session) + ::HTTPX.send(:const_set, :Session, otel_session.class) + end + + def require_dependencies + require_relative 'plugin' + end + end + end + end +end diff --git a/instrumentation/httpx/lib/opentelemetry/instrumentation/httpx/plugin.rb b/instrumentation/httpx/lib/opentelemetry/instrumentation/httpx/plugin.rb new file mode 100644 index 000000000..10738e1ed --- /dev/null +++ b/instrumentation/httpx/lib/opentelemetry/instrumentation/httpx/plugin.rb @@ -0,0 +1,92 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +module OpenTelemetry + module Instrumentation + module HTTPX + module Plugin + # Instruments around HTTPX's request/response lifecycle in order to generate + # an OTEL trace. + class RequestTracer + def initialize(request) + @request = request + end + + def call + @request.on(:response, &method(:finish)) + + uri = @request.uri + request_method = @request.verb + span_name = "HTTP #{request_method}" + + attributes = { + OpenTelemetry::SemanticConventions::Trace::HTTP_HOST => uri.host, + OpenTelemetry::SemanticConventions::Trace::HTTP_METHOD => request_method, + OpenTelemetry::SemanticConventions::Trace::HTTP_SCHEME => uri.scheme, + OpenTelemetry::SemanticConventions::Trace::HTTP_TARGET => uri.path, + OpenTelemetry::SemanticConventions::Trace::HTTP_URL => "#{uri.scheme}://#{uri.host}", + OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME => uri.host, + OpenTelemetry::SemanticConventions::Trace::NET_PEER_PORT => uri.port + } + config = HTTPX::Instrumentation.instance.config + attributes[OpenTelemetry::SemanticConventions::Trace::PEER_SERVICE] = config[:peer_service] if config[:peer_service] + attributes.merge!( + OpenTelemetry::Common::HTTP::ClientContext.attributes + ) + + @span = tracer.start_span(span_name, attributes: attributes, kind: :client) + trace_ctx = OpenTelemetry::Trace.context_with_span(@span) + @trace_token = OpenTelemetry::Context.attach(trace_ctx) + + OpenTelemetry.propagation.inject(@request.headers) + rescue StandardError => e + OpenTelemetry.handle_error(exception: e) + end + + def finish(response) + return unless @span + + if response.is_a?(::HTTPX::ErrorResponse) + @span.record_exception(response.error) + @span.status = Trace::Status.error("Unhandled exception of type: #{response.error.class}") + else + @span.set_attribute(OpenTelemetry::SemanticConventions::Trace::HTTP_STATUS_CODE, response.status) + @span.status = Trace::Status.error unless (100..399).include?(response.status) + end + + OpenTelemetry::Context.detach(@trace_token) if @trace_token + @span.finish + end + + private + + def tracer + HTTPX::Instrumentation.instance.tracer + end + end + + # HTTPX::Request overrides + module RequestMethods + def __otel_enable_trace! + return if @__otel_enable_trace + + RequestTracer.new(self).call + @__otel_enable_trace = true + end + end + + # HTTPX::Connection overrides + module ConnectionMethods + def send(request) + request.__otel_enable_trace! + + super + end + end + end + end + end +end diff --git a/resource_detectors/lib/opentelemetry/resource/detectors/version.rb b/instrumentation/httpx/lib/opentelemetry/instrumentation/httpx/version.rb similarity index 69% rename from resource_detectors/lib/opentelemetry/resource/detectors/version.rb rename to instrumentation/httpx/lib/opentelemetry/instrumentation/httpx/version.rb index d500bc012..b05472d1e 100644 --- a/resource_detectors/lib/opentelemetry/resource/detectors/version.rb +++ b/instrumentation/httpx/lib/opentelemetry/instrumentation/httpx/version.rb @@ -5,9 +5,9 @@ # SPDX-License-Identifier: Apache-2.0 module OpenTelemetry - module Resource - module Detectors - VERSION = '0.24.2' + module Instrumentation + module HTTPX + VERSION = '0.1.0' end end end diff --git a/resource_detectors/opentelemetry-resource_detectors.gemspec b/instrumentation/httpx/opentelemetry-instrumentation-httpx.gemspec similarity index 59% rename from resource_detectors/opentelemetry-resource_detectors.gemspec rename to instrumentation/httpx/opentelemetry-instrumentation-httpx.gemspec index 3613cf69f..6531619a7 100644 --- a/resource_detectors/opentelemetry-resource_detectors.gemspec +++ b/instrumentation/httpx/opentelemetry-instrumentation-httpx.gemspec @@ -6,16 +6,16 @@ lib = File.expand_path('lib', __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'opentelemetry/resource/detectors/version' +require 'opentelemetry/instrumentation/httpx/version' Gem::Specification.new do |spec| - spec.name = 'opentelemetry-resource_detectors' - spec.version = OpenTelemetry::Resource::Detectors::VERSION + spec.name = 'opentelemetry-instrumentation-httpx' + spec.version = OpenTelemetry::Instrumentation::HTTPX::VERSION spec.authors = ['OpenTelemetry Authors'] spec.email = ['cncf-opentelemetry-contributors@lists.cncf.io'] - spec.summary = 'Resource detection helpers for OpenTelemetry' - spec.description = 'Resource detection helpers for OpenTelemetry' + spec.summary = 'HTTPX instrumentation for the OpenTelemetry framework' + spec.description = 'HTTPX instrumentation for the OpenTelemetry framework' spec.homepage = 'https://github.com/open-telemetry/opentelemetry-ruby-contrib' spec.license = 'Apache-2.0' @@ -25,23 +25,25 @@ Gem::Specification.new do |spec| spec.require_paths = ['lib'] spec.required_ruby_version = '>= 3.0' - spec.add_dependency 'google-cloud-env' - spec.add_dependency 'opentelemetry-sdk', '~> 1.0' + spec.add_dependency 'opentelemetry-api', '~> 1.0' + spec.add_dependency 'opentelemetry-instrumentation-base', '~> 0.22.1' + spec.add_development_dependency 'appraisal', '~> 2.5' spec.add_development_dependency 'bundler', '~> 2.4' + spec.add_development_dependency 'httpx' spec.add_development_dependency 'minitest', '~> 5.0' + spec.add_development_dependency 'opentelemetry-sdk', '~> 1.1' + spec.add_development_dependency 'opentelemetry-test-helpers', '~> 0.3' spec.add_development_dependency 'rake', '~> 13.0' - spec.add_development_dependency 'rubocop', '~> 1.57.1' - spec.add_development_dependency 'simplecov', '~> 0.17' + spec.add_development_dependency 'rspec-mocks' + spec.add_development_dependency 'rubocop', '~> 1.56.1' + spec.add_development_dependency 'simplecov', '~> 0.17.1' spec.add_development_dependency 'webmock', '~> 3.19' spec.add_development_dependency 'yard', '~> 0.9' - spec.post_install_message = 'This gem has been deprecated. Please use opentelemetry-resource-detector-azure ' \ - 'or opentelemetry-resource-detector-google_cloud_platform onwards.' - if spec.respond_to?(:metadata) spec.metadata['changelog_uri'] = "https://rubydoc.info/gems/#{spec.name}/#{spec.version}/file/CHANGELOG.md" - spec.metadata['source_code_uri'] = 'https://github.com/open-telemetry/opentelemetry-ruby-contrib/tree/main/resource_detectors' + spec.metadata['source_code_uri'] = 'https://github.com/open-telemetry/opentelemetry-ruby-contrib/tree/main/instrumentation/http' spec.metadata['bug_tracker_uri'] = 'https://github.com/open-telemetry/opentelemetry-ruby-contrib/issues' spec.metadata['documentation_uri'] = "https://rubydoc.info/gems/#{spec.name}/#{spec.version}" end diff --git a/instrumentation/httpx/test/instrumentation/httpx/instrumentation_test.rb b/instrumentation/httpx/test/instrumentation/httpx/instrumentation_test.rb new file mode 100644 index 000000000..70dda9013 --- /dev/null +++ b/instrumentation/httpx/test/instrumentation/httpx/instrumentation_test.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +require 'test_helper' + +require_relative '../../../lib/opentelemetry/instrumentation/httpx' + +describe OpenTelemetry::Instrumentation::HTTPX do + let(:instrumentation) { OpenTelemetry::Instrumentation::HTTPX::Instrumentation.instance } + + it 'has #name' do + _(instrumentation.name).must_equal 'OpenTelemetry::Instrumentation::HTTPX' + end + + it 'has #version' do + _(instrumentation.version).wont_be_nil + _(instrumentation.version).wont_be_empty + end + + describe '#install' do + it 'accepts argument' do + _(instrumentation.install({})).must_equal(true) + end + end +end diff --git a/instrumentation/httpx/test/instrumentation/plugin_test.rb b/instrumentation/httpx/test/instrumentation/plugin_test.rb new file mode 100644 index 000000000..585e8f50f --- /dev/null +++ b/instrumentation/httpx/test/instrumentation/plugin_test.rb @@ -0,0 +1,139 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +require 'test_helper' + +require_relative '../../lib/opentelemetry/instrumentation/httpx' +require_relative '../../lib/opentelemetry/instrumentation/httpx/plugin' + +describe OpenTelemetry::Instrumentation::HTTPX::Plugin do + let(:instrumentation) { OpenTelemetry::Instrumentation::HTTPX::Instrumentation.instance } + let(:exporter) { EXPORTER } + let(:span) { exporter.finished_spans.first } + + before do + exporter.reset + stub_request(:get, 'http://example.com/success').to_return(status: 200) + stub_request(:get, 'http://example.com/failure').to_return(status: 500) + stub_request(:get, 'http://example.com/timeout').to_timeout + end + + # Force re-install of instrumentation + after { instrumentation.instance_variable_set(:@installed, false) } + + describe 'tracing' do + before do + instrumentation.install + end + + it 'before request' do + _(exporter.finished_spans.size).must_equal 0 + end + + it 'after request with success code' do + HTTPX.get('http://example.com/success') + + _(exporter.finished_spans.size).must_equal 1 + _(span.name).must_equal 'HTTP GET' + _(span.attributes['http.method']).must_equal 'GET' + _(span.attributes['http.status_code']).must_equal 200 + _(span.attributes['http.scheme']).must_equal 'http' + _(span.attributes['http.host']).must_equal 'example.com' + _(span.attributes['http.target']).must_equal '/success' + assert_requested( + :get, + 'http://example.com/success', + headers: { 'Traceparent' => "00-#{span.hex_trace_id}-#{span.hex_span_id}-01" } + ) + end + + it 'after request with failure code' do + HTTPX.get('http://example.com/failure') + + _(exporter.finished_spans.size).must_equal 1 + _(span.name).must_equal 'HTTP GET' + _(span.attributes['http.method']).must_equal 'GET' + _(span.attributes['http.status_code']).must_equal 500 + _(span.attributes['http.scheme']).must_equal 'http' + _(span.attributes['http.host']).must_equal 'example.com' + _(span.attributes['http.target']).must_equal '/failure' + assert_requested( + :get, + 'http://example.com/failure', + headers: { 'Traceparent' => "00-#{span.hex_trace_id}-#{span.hex_span_id}-01" } + ) + end + + it 'after request timeout' do + response = HTTPX.get('http://example.com/timeout') + assert response.is_a?(HTTPX::ErrorResponse) + assert response.error.is_a?(HTTPX::TimeoutError) + + _(exporter.finished_spans.size).must_equal 1 + _(span.name).must_equal 'HTTP GET' + _(span.attributes['http.method']).must_equal 'GET' + _(span.attributes['http.scheme']).must_equal 'http' + _(span.attributes['http.host']).must_equal 'example.com' + _(span.attributes['http.target']).must_equal '/timeout' + _(span.status.code).must_equal( + OpenTelemetry::Trace::Status::ERROR + ) + _(span.status.description).must_equal( + 'Unhandled exception of type: HTTPX::TimeoutError' + ) + assert_requested( + :get, + 'http://example.com/timeout', + headers: { 'Traceparent' => "00-#{span.hex_trace_id}-#{span.hex_span_id}-01" } + ) + end + + it 'merges HTTP client context' do + client_context_attrs = { + 'test.attribute' => 'test.value', 'http.method' => 'OVERRIDE' + } + + OpenTelemetry::Common::HTTP::ClientContext.with_attributes(client_context_attrs) do + HTTPX.get('http://example.com/success') + end + + _(exporter.finished_spans.size).must_equal 1 + _(span.name).must_equal 'HTTP GET' + _(span.attributes['http.method']).must_equal 'OVERRIDE' + _(span.attributes['http.status_code']).must_equal 200 + _(span.attributes['http.scheme']).must_equal 'http' + _(span.attributes['http.host']).must_equal 'example.com' + _(span.attributes['http.target']).must_equal '/success' + _(span.attributes['test.attribute']).must_equal 'test.value' + assert_requested( + :get, + 'http://example.com/success', + headers: { 'Traceparent' => "00-#{span.hex_trace_id}-#{span.hex_span_id}-01" } + ) + end + + it 'accepts peer service name from config' do + instrumentation.instance_variable_set(:@installed, false) + instrumentation.install(peer_service: 'example:httpx') + + HTTPX.get('http://example.com/success') + + _(span.attributes['peer.service']).must_equal 'example:httpx' + end + + it 'prioritizes context attributes over config for peer service name' do + instrumentation.instance_variable_set(:@installed, false) + instrumentation.install(peer_service: 'example:static') + + client_context_attrs = { 'peer.service' => 'example:custom' } + OpenTelemetry::Common::HTTP::ClientContext.with_attributes(client_context_attrs) do + HTTPX.get('http://example.com/success') + end + + _(span.attributes['peer.service']).must_equal 'example:custom' + end + end +end diff --git a/instrumentation/httpx/test/test_helper.rb b/instrumentation/httpx/test/test_helper.rb new file mode 100644 index 000000000..10c09f2da --- /dev/null +++ b/instrumentation/httpx/test/test_helper.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +require 'bundler/setup' +Bundler.require(:default, :development, :test) + +require 'minitest/autorun' +require 'httpx/adapters/webmock' +require 'webmock/minitest' + +# global opentelemetry-sdk setup: +EXPORTER = OpenTelemetry::SDK::Trace::Export::InMemorySpanExporter.new +span_processor = OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(EXPORTER) + +OpenTelemetry::SDK.configure do |c| + c.error_handler = ->(exception:, message:) { raise(exception || message) } + c.logger = Logger.new($stderr, level: ENV.fetch('OTEL_LOG_LEVEL', 'fatal').to_sym) + c.add_span_processor span_processor +end diff --git a/releases/Gemfile b/releases/Gemfile index bfc49848d..c9cd38765 100644 --- a/releases/Gemfile +++ b/releases/Gemfile @@ -36,7 +36,6 @@ gem 'trilogy' gem 'opentelemetry-api' gem 'opentelemetry-sdk' -gem 'opentelemetry-resource_detectors' Dir['../propagator/**/version.rb'].each do |f| name = f.match(%r{propagator/(\w+)/lib})[1] diff --git a/resource_detectors/.rubocop.yml b/resource_detectors/.rubocop.yml deleted file mode 100644 index fc2019d46..000000000 --- a/resource_detectors/.rubocop.yml +++ /dev/null @@ -1 +0,0 @@ -inherit_from: ../.rubocop.yml diff --git a/resource_detectors/.yardopts b/resource_detectors/.yardopts deleted file mode 100644 index 1b25699e2..000000000 --- a/resource_detectors/.yardopts +++ /dev/null @@ -1,9 +0,0 @@ ---no-private ---title=OpenTelemetry Resource Detectors ---markup=markdown ---main=README.md -./lib/opentelemetry/resource/detectors/**/*.rb -./lib/opentelemetry/resource/detectors.rb -- -README.md -CHANGELOG.md diff --git a/resource_detectors/CHANGELOG.md b/resource_detectors/CHANGELOG.md deleted file mode 100644 index 5d6b2c0e6..000000000 --- a/resource_detectors/CHANGELOG.md +++ /dev/null @@ -1,106 +0,0 @@ -# Release History: opentelemetry-resource_detectors - -### v0.24.2 / 2023-09-07 - -* CHANGED: split resource_detectors into their own gems - -### v0.24.1 / 2023-08-03 - -* FIXED: Remove inline linter rules - -### v0.24.0 / 2023-08-02 - -* ADDED: Add container resource detector - -### v0.23.0 / 2023-04-17 - -* BREAKING CHANGE: Drop support for EoL Ruby 2.7 - -* ADDED: Drop support for EoL Ruby 2.7 - -### v0.22.0 / 2023-01-14 - -* ADDED: Add azure resource detector. -* DOCS: Fix gem homepage -* DOCS: More gem documentation fixes - -### v0.21.0 / 2022-06-09 - -* BREAKING CHANGE: This requires upgrading both the SDK and Instrumentation gem in tandem - - -### v0.20.0 / 2022-05-02 - -* ADDED: Added Google Cloud Function Resource Detection - -### v0.19.1 / 2021-09-29 - -* (No significant changes) - -### v0.19.0 / 2021-08-12 - -* BREAKING CHANGE: Use auto-generated resource constants in sdk and resource_detectors - -* ADDED: Use auto-generated resource constants in sdk and resource_detectors - -### v0.18.1 / 2021-06-23 - -* (No significant changes) - -### v0.18.0 / 2021-05-21 - -* FIXED: Rename cloud.zone to cloud.availability_zone - -### v0.17.0 / 2021-04-22 - -* (No significant changes) - -### v0.16.0 / 2021-03-17 - -* ADDED: Add k8s node to gcp resource detector -* DOCS: Replace Gitter with GitHub Discussions - -### v0.15.0 / 2021-02-18 - -* (No significant changes) - -### v0.14.0 / 2021-02-03 - -* DOCS: Updated gem name to match gemspec - -### v0.13.0 / 2021-01-29 - -* (No significant changes) - -### v0.12.0 / 2020-12-24 - -* (No significant changes) - -### v0.11.0 / 2020-12-11 - -* FIXED: Copyright comments to not reference year - -### v0.10.0 / 2020-12-03 - -* (No significant changes) - -### v0.9.0 / 2020-11-27 - -* BREAKING CHANGE: Add timeout for force_flush and shutdown - -* ADDED: Add timeout for force_flush and shutdown - -### v0.8.0 / 2020-10-27 - -* (No significant changes) - -### v0.7.0 / 2020-10-07 - -* DOCS: Standardize toplevel docs structure and readme - -### v0.6.0 / 2020-09-10 - -* BREAKING CHANGE: Rename Resource labels to attributes - -* FIXED: Rename Resource labels to attributes -* ADDED: Environment variable resource detection diff --git a/resource_detectors/README.md b/resource_detectors/README.md deleted file mode 100644 index a0ffaa622..000000000 --- a/resource_detectors/README.md +++ /dev/null @@ -1,51 +0,0 @@ -# Opentelemetry::Resource::Detectors - -The `opentelemetry-resource_detectors` gem provides resource detectors for OpenTelemetry. - -## What is OpenTelemetry? - -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. - -OpenTelemetry provides a single set of APIs, libraries, agents, and collector services to capture distributed traces and metrics from your application. You can analyze them using Prometheus, Jaeger, and other observability tools. - -## How does this gem fit in? - -The `opentelemetry-resource-detectors` gem provides a means of retrieving a resource for supported environments following the resource semantic conventions. - -## How do I get started? - -Install the gem using: - -``` -gem install opentelemetry-sdk -gem install opentelemetry-resource_detectors -``` - -Or, if you use Bundler, include `opentelemetry-sdk` and `opentelemetry-resource_detectors` in your `Gemfile`. - -```rb -require 'opentelemetry/sdk' -require 'opentelemetry/resource/detectors' - -# For a specific platform -OpenTelemetry::SDK.configure do |c| - c.resource = OpenTelemetry::Resource::Detectors::GoogleCloudPlatform.detect -end - -# Or if you would like for it to run all detectors available -OpenTelemetry::SDK.configure do |c| - c.resource = OpenTelemetry::Resource::Detectors::AutoDetector.detect -end -``` - -## How can I get involved? - -The `opentelemetry-resource_detectors` gem source is on github, along with related gems. - -The OpenTelemetry Ruby gems are maintained by the OpenTelemetry-Ruby special interest group (SIG). You can get involved by joining us in [GitHub Discussions][discussions-url] or attending our weekly meeting. See the meeting calendar for dates and times. For more information on this and other language SIGs, see the OpenTelemetry community page. - -## License - -The `opentelemetry-resource_detectors` gem is distributed under the Apache 2.0 license. See LICENSE for more information. - -[discussions-url]: https://github.com/open-telemetry/opentelemetry-ruby/discussions diff --git a/resource_detectors/lib/opentelemetry/resource/detectors/auto_detector.rb b/resource_detectors/lib/opentelemetry/resource/detectors/auto_detector.rb deleted file mode 100644 index 2eb873bdc..000000000 --- a/resource_detectors/lib/opentelemetry/resource/detectors/auto_detector.rb +++ /dev/null @@ -1,25 +0,0 @@ -# frozen_string_literal: true - -# Copyright The OpenTelemetry Authors -# -# SPDX-License-Identifier: Apache-2.0 - -module OpenTelemetry - module Resource - module Detectors - # AutoDetector contains detect class method for running all detectors - module AutoDetector - extend self - - DETECTORS = [ - OpenTelemetry::Resource::Detectors::Azure, - OpenTelemetry::Resource::Detectors::GoogleCloudPlatform - ].freeze - - def detect - DETECTORS.map(&:detect).reduce(:merge) - end - end - end - end -end diff --git a/resource_detectors/lib/opentelemetry/resource/detectors/azure.rb b/resource_detectors/lib/opentelemetry/resource/detectors/azure.rb deleted file mode 100644 index c27ccc012..000000000 --- a/resource_detectors/lib/opentelemetry/resource/detectors/azure.rb +++ /dev/null @@ -1,77 +0,0 @@ -# frozen_string_literal: true - -# Copyright The OpenTelemetry Authors -# -# SPDX-License-Identifier: Apache-2.0 - -require 'net/http' - -module OpenTelemetry - module Resource - module Detectors - # Azure contains detect class method for determining Azure environment resource attributes - # - # This gem has been moved into a separate gem: - # opentelemetry-resource-detector-azure - # - # Log a warning if someone still uses this gem for Azure Resource Detection - module Azure - extend self - - AZURE_METADATA_URI = 'http://169.254.169.254/metadata/instance/compute?api-version=2019-08-15' - - def detect - OpenTelemetry.logger.warn('Azure resource detector - The Azure resource detector has been moved to a separate gem. ' \ - 'Please use the "opentelemetry-resource-detector-azure" gem onwards.') - - metadata = azure_metadata - resource_attributes = {} - - unless metadata.nil? - resource_attributes[OpenTelemetry::SemanticConventions::Resource::CLOUD_PROVIDER] = 'azure' - resource_attributes[OpenTelemetry::SemanticConventions::Resource::CLOUD_ACCOUNT_ID] = metadata['subscriptionId'] - resource_attributes[OpenTelemetry::SemanticConventions::Resource::CLOUD_PLATFORM] = cloud_platform(metadata['provider']) - resource_attributes[OpenTelemetry::SemanticConventions::Resource::CLOUD_REGION] = metadata['location'] - resource_attributes[OpenTelemetry::SemanticConventions::Resource::CLOUD_AVAILABILITY_ZONE] = metadata['zone'] - - resource_attributes[OpenTelemetry::SemanticConventions::Resource::HOST_ID] = metadata['vmId'] - resource_attributes[OpenTelemetry::SemanticConventions::Resource::HOST_IMAGE_ID] = metadata.dig('storageProfile', 'imageReference', 'id') - resource_attributes[OpenTelemetry::SemanticConventions::Resource::HOST_TYPE] = metadata['vmSize'] - resource_attributes[OpenTelemetry::SemanticConventions::Resource::HOST_NAME] = metadata['name'] - end - - resource_attributes.delete_if { |_key, value| value.nil? || value.empty? } - OpenTelemetry::SDK::Resources::Resource.create(resource_attributes) - end - - private - - def azure_metadata - uri = URI(AZURE_METADATA_URI) - - req = Net::HTTP::Get.new(uri) - req['Metadata'] = 'true' - - response = Net::HTTP.start(uri.hostname, uri.port, open_timeout: 2) do |http| - http.request(req) - end - - return unless response.code == '200' - - JSON.parse(response.body) - rescue Errno::EHOSTDOWN, Net::OpenTimeout, SocketError - nil - end - - def cloud_platform(metadata) - case metadata - when 'Microsoft.Compute' - 'azure_vm' - else - '' - end - end - end - end - end -end diff --git a/resource_detectors/lib/opentelemetry/resource/detectors/google_cloud_platform.rb b/resource_detectors/lib/opentelemetry/resource/detectors/google_cloud_platform.rb deleted file mode 100644 index 17795ebf4..000000000 --- a/resource_detectors/lib/opentelemetry/resource/detectors/google_cloud_platform.rb +++ /dev/null @@ -1,80 +0,0 @@ -# frozen_string_literal: true - -# Copyright The OpenTelemetry Authors -# -# SPDX-License-Identifier: Apache-2.0 - -require 'google-cloud-env' - -module OpenTelemetry - module Resource - module Detectors - # GoogleCloudPlatform contains detect class method for determining gcp environment resource attributes - # - # This gem has been moved into a separate gem: - # opentelemetry-resource-detector-google_cloud_platform - # - # Log a warning if someone still uses this gem for GoogleCloudPlatform Resource Detection - module GoogleCloudPlatform - extend self - - def detect - OpenTelemetry.logger.warn('GoogleCloudPlatform resource detector - The GoogleCloudPlatform resource detector has been moved to a separate gem. ' \ - 'Please use the "opentelemetry-resource-detector-google_cloud_platform" gem onwards.') - - gcp_env = Google::Cloud::Env.new - resource_attributes = {} - - if gcp_env.compute_engine? - resource_attributes[OpenTelemetry::SemanticConventions::Resource::CLOUD_PROVIDER] = 'gcp' - resource_attributes[OpenTelemetry::SemanticConventions::Resource::CLOUD_ACCOUNT_ID] = gcp_env.project_id - resource_attributes[OpenTelemetry::SemanticConventions::Resource::CLOUD_REGION] = gcp_env.instance_attribute('cluster-location') - resource_attributes[OpenTelemetry::SemanticConventions::Resource::CLOUD_AVAILABILITY_ZONE] = gcp_env.instance_zone - - resource_attributes[OpenTelemetry::SemanticConventions::Resource::HOST_ID] = gcp_env.lookup_metadata('instance', 'id') - resource_attributes[OpenTelemetry::SemanticConventions::Resource::HOST_NAME] = ENV['HOSTNAME'] || - gcp_env.lookup_metadata('instance', 'hostname') || - safe_gethostname - end - - if gcp_env.kubernetes_engine? - resource_attributes[OpenTelemetry::SemanticConventions::Resource::K8S_CLUSTER_NAME] = gcp_env.instance_attribute('cluster-name') - resource_attributes[OpenTelemetry::SemanticConventions::Resource::K8S_NAMESPACE_NAME] = gcp_env.kubernetes_engine_namespace_id - resource_attributes[OpenTelemetry::SemanticConventions::Resource::K8S_POD_NAME] = ENV['HOSTNAME'] || safe_gethostname - resource_attributes[OpenTelemetry::SemanticConventions::Resource::K8S_NODE_NAME] = gcp_env.lookup_metadata('instance', 'hostname') - - resource_attributes[OpenTelemetry::SemanticConventions::Resource::CONTAINER_NAME] = ENV.fetch('CONTAINER_NAME', nil) - end - - if gcp_env.knative? - resource_attributes[OpenTelemetry::SemanticConventions::Resource::CLOUD_PROVIDER] = 'gcp' - resource_attributes[OpenTelemetry::SemanticConventions::Resource::CLOUD_ACCOUNT_ID] = gcp_env.project_id - resource_attributes[OpenTelemetry::SemanticConventions::Resource::FAAS_NAME] = gcp_env.knative_service_id - resource_attributes[OpenTelemetry::SemanticConventions::Resource::FAAS_VERSION] = gcp_env.knative_service_revision - zone = gcp_env.instance_zone - resource_attributes[OpenTelemetry::SemanticConventions::Resource::CLOUD_REGION] = get_region zone - resource_attributes[OpenTelemetry::SemanticConventions::Resource::CLOUD_AVAILABILITY_ZONE] = zone - end - - resource_attributes.delete_if { |_key, value| value.nil? || value.empty? } - OpenTelemetry::SDK::Resources::Resource.create(resource_attributes) - end - - private - - def get_region(zone) - return if zone.nil? || zone.empty? - - split_arr = zone.split('-', 3) - split_arr[0].concat('-', split_arr[1]) - end - - def safe_gethostname - Socket.gethostname - rescue StandardError - '' - end - end - end - end -end diff --git a/resource_detectors/test/opentelemetry/detectors/auto_detector_test.rb b/resource_detectors/test/opentelemetry/detectors/auto_detector_test.rb deleted file mode 100644 index c9b583601..000000000 --- a/resource_detectors/test/opentelemetry/detectors/auto_detector_test.rb +++ /dev/null @@ -1,52 +0,0 @@ -# frozen_string_literal: true - -# Copyright The OpenTelemetry Authors -# -# SPDX-License-Identifier: Apache-2.0 - -require 'test_helper' - -describe OpenTelemetry::Resource::Detectors::AutoDetector do - before do - WebMock.disable_net_connect! - # Azure stub - stub_request(:get, 'http://169.254.169.254/metadata/instance/compute?api-version=2019-08-15') - .with( - headers: { - 'Accept' => '*/*', - 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'Host' => '169.254.169.254', - 'Metadata' => 'true', - 'User-Agent' => 'Ruby' - } - ).to_raise(SocketError) - - # GCP stub - stub_request(:get, 'http://169.254.169.254/') - .with( - headers: { - 'Accept' => '*/*', - 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'Metadata-Flavor' => 'Google', - 'User-Agent' => 'Ruby' - } - ) - .to_return(status: 200, body: '', headers: {}) - end - - after do - WebMock.allow_net_connect! - end - - let(:auto_detector) { OpenTelemetry::Resource::Detectors::AutoDetector } - let(:detected_resource) { auto_detector.detect } - let(:detected_resource_attributes) { detected_resource.attribute_enumerator.to_h } - let(:expected_resource_attributes) { {} } - - describe '.detect' do - it 'returns detected resources' do - _(detected_resource).must_be_instance_of(OpenTelemetry::SDK::Resources::Resource) - _(detected_resource_attributes).must_equal(expected_resource_attributes) - end - end -end diff --git a/resource_detectors/test/opentelemetry/detectors/azure_test.rb b/resource_detectors/test/opentelemetry/detectors/azure_test.rb deleted file mode 100644 index 8da61cb20..000000000 --- a/resource_detectors/test/opentelemetry/detectors/azure_test.rb +++ /dev/null @@ -1,120 +0,0 @@ -# frozen_string_literal: true - -# Copyright The OpenTelemetry Authors -# -# SPDX-License-Identifier: Apache-2.0 - -require 'test_helper' - -describe OpenTelemetry::Resource::Detectors::Azure do - before do - WebMock.disable_net_connect! - stub_request(:get, 'http://169.254.169.254/metadata/instance/compute?api-version=2019-08-15') - .with( - headers: { - 'Accept' => '*/*', - 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'Host' => '169.254.169.254', - 'Metadata' => 'true', - 'User-Agent' => 'Ruby' - } - ).to_raise(SocketError) - end - - after do - WebMock.allow_net_connect! - end - - let(:detector) { OpenTelemetry::Resource::Detectors::Azure } - - describe '.detect' do - let(:detected_resource) { detector.detect } - let(:detected_resource_attributes) { detected_resource.attribute_enumerator.to_h } - let(:expected_resource_attributes) { {} } - - describe 'when NOT in an azure environment' do - it 'returns an empty resource' do - _(detected_resource).must_be_instance_of(OpenTelemetry::SDK::Resources::Resource) - _(detected_resource_attributes).must_equal(expected_resource_attributes) - end - end - - describe 'when in an azure VM environment' do - let(:project_id) { 'opentelemetry' } - let(:azure_metadata) do - { - 'subscriptionId' => project_id, - 'provider' => 'Microsoft.Compute', - 'location' => 'westeurope', - 'zone' => '2', - 'vmId' => '012345671234-abcd-1234-0123456789ab', - 'storageProfile' => { - 'imageReference' => { - 'id' => '/subscriptions/12345678-abcd-1234-abcd-0123456789ab/resourceGroups/AKS-Ubuntu/providers/Microsoft.Compute/galleries/AKSUbuntu/images/1804gen2containerd/versions/2022.06.22' - } - }, - 'vmSize' => 'Standard_D2s_v3', - 'name' => 'opentelemetry' - }.to_json - end - - before do - metadata = Minitest::Mock.new - metadata.expect(:code, 200) - metadata.expect(:body, azure_metadata) - metadata.expect(:nil?, false) - - WebMock.disable_net_connect! - stub_request(:get, 'http://169.254.169.254/metadata/instance/compute?api-version=2019-08-15') - .with( - headers: { - 'Accept' => '*/*', - 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'Host' => '169.254.169.254', - 'Metadata' => 'true', - 'User-Agent' => 'Ruby' - } - ).to_return(status: 200, body: azure_metadata, headers: {}) - end - - after do - WebMock.allow_net_connect! - end - - let(:expected_resource_attributes) do - { - 'cloud.provider' => 'azure', - 'cloud.account.id' => 'opentelemetry', - 'cloud.platform' => 'azure_vm', - 'cloud.region' => 'westeurope', - 'cloud.availability_zone' => '2', - 'host.id' => '012345671234-abcd-1234-0123456789ab', - 'host.image.id' => '/subscriptions/12345678-abcd-1234-abcd-0123456789ab/resourceGroups/AKS-Ubuntu/providers/Microsoft.Compute/galleries/AKSUbuntu/images/1804gen2containerd/versions/2022.06.22', - 'host.name' => 'opentelemetry', - 'host.type' => 'Standard_D2s_v3' - } - end - - it 'returns a resource with azure attributes' do - _(detected_resource).must_be_instance_of(OpenTelemetry::SDK::Resources::Resource) - _(detected_resource_attributes).must_equal(expected_resource_attributes) - end - - describe 'and a nil resource value is detected' do - let(:project_id) { nil } - - it 'returns a resource without that attribute' do - _(detected_resource_attributes.key?('cloud.account.id')).must_equal(false) - end - end - - describe 'and an empty string resource value is detected' do - let(:project_id) { '' } - - it 'returns a resource without that attribute' do - _(detected_resource_attributes.key?('cloud.account.id')).must_equal(false) - end - end - end - end -end diff --git a/resource_detectors/test/opentelemetry/detectors/google_cloud_platform_test.rb b/resource_detectors/test/opentelemetry/detectors/google_cloud_platform_test.rb deleted file mode 100644 index c537bef2d..000000000 --- a/resource_detectors/test/opentelemetry/detectors/google_cloud_platform_test.rb +++ /dev/null @@ -1,110 +0,0 @@ -# frozen_string_literal: true - -# Copyright The OpenTelemetry Authors -# -# SPDX-License-Identifier: Apache-2.0 - -require 'test_helper' - -describe OpenTelemetry::Resource::Detectors::GoogleCloudPlatform do - let(:detector) { OpenTelemetry::Resource::Detectors::GoogleCloudPlatform } - - describe '.detect' do - before do - WebMock.disable_net_connect! - stub_request(:get, 'http://169.254.169.254/') - .with( - headers: { - 'Accept' => '*/*', - 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'Metadata-Flavor' => 'Google', - 'User-Agent' => 'Ruby' - } - ) - .to_return(status: 200, body: '', headers: {}) - end - - after do - WebMock.allow_net_connect! - end - - let(:detected_resource) { detector.detect } - let(:detected_resource_attributes) { detected_resource.attribute_enumerator.to_h } - let(:expected_resource_attributes) { {} } - - it 'returns an empty resource' do - _(detected_resource).must_be_instance_of(OpenTelemetry::SDK::Resources::Resource) - _(detected_resource_attributes).must_equal(expected_resource_attributes) - end - - describe 'when in a gcp environment' do - let(:project_id) { 'opentelemetry' } - - before do - gcp_env_mock = Minitest::Mock.new - gcp_env_mock.expect(:compute_engine?, true) - gcp_env_mock.expect(:project_id, project_id) - gcp_env_mock.expect(:instance_attribute, 'us-central1', %w[cluster-location]) - gcp_env_mock.expect(:instance_zone, 'us-central1-a') - gcp_env_mock.expect(:lookup_metadata, 'opentelemetry-test', %w[instance id]) - gcp_env_mock.expect(:lookup_metadata, 'opentelemetry-node-1', %w[instance hostname]) - gcp_env_mock.expect(:instance_attribute, 'opentelemetry-cluster', %w[cluster-name]) - gcp_env_mock.expect(:kubernetes_engine?, true) - gcp_env_mock.expect(:kubernetes_engine_namespace_id, 'default') - gcp_env_mock.expect(:knative?, true) - gcp_env_mock.expect(:project_id, project_id) - gcp_env_mock.expect(:knative_service_id, 'test-google-cloud-function') - gcp_env_mock.expect(:knative_service_revision, '2') - gcp_env_mock.expect(:instance_zone, 'us-central1-a') - - Socket.stub(:gethostname, 'opentelemetry-test') do - old_hostname = ENV.fetch('HOSTNAME', nil) - ENV['HOSTNAME'] = 'opentelemetry-host-name-1' - begin - Google::Cloud::Env.stub(:new, gcp_env_mock) { detected_resource } - ensure - ENV['HOSTNAME'] = old_hostname - end - end - end - - let(:expected_resource_attributes) do - { - 'cloud.provider' => 'gcp', - 'cloud.account.id' => 'opentelemetry', - 'cloud.region' => 'us-central1', - 'cloud.availability_zone' => 'us-central1-a', - 'host.id' => 'opentelemetry-test', - 'host.name' => 'opentelemetry-host-name-1', - 'k8s.cluster.name' => 'opentelemetry-cluster', - 'k8s.namespace.name' => 'default', - 'k8s.pod.name' => 'opentelemetry-host-name-1', - 'k8s.node.name' => 'opentelemetry-node-1', - 'faas.name' => 'test-google-cloud-function', - 'faas.version' => '2' - } - end - - it 'returns a resource with gcp attributes' do - _(detected_resource).must_be_instance_of(OpenTelemetry::SDK::Resources::Resource) - _(detected_resource_attributes).must_equal(expected_resource_attributes) - end - - describe 'and a nil resource value is detected' do - let(:project_id) { nil } - - it 'returns a resource without that attribute' do - _(detected_resource_attributes.key?('cloud.account.id')).must_equal(false) - end - end - - describe 'and an empty string resource value is detected' do - let(:project_id) { '' } - - it 'returns a resource without that attribute' do - _(detected_resource_attributes.key?('cloud.account.id')).must_equal(false) - end - end - end - end -end diff --git a/resource_detectors/test/test_helper.rb b/resource_detectors/test/test_helper.rb deleted file mode 100644 index aee3060a7..000000000 --- a/resource_detectors/test/test_helper.rb +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true - -# Copyright The OpenTelemetry Authors -# -# SPDX-License-Identifier: Apache-2.0 - -require 'bundler/setup' -Bundler.require(:default, :development, :test) - -SimpleCov.minimum_coverage 85 -SimpleCov.start - -require 'opentelemetry-resource_detectors' -require 'minitest/autorun' -require 'webmock/minitest' - -OpenTelemetry.logger = Logger.new($stderr, level: ENV.fetch('OTEL_LOG_LEVEL', 'fatal').to_sym)