From 68b9fe824d6cc8154efe46829fa0fcd26a5b9a5d Mon Sep 17 00:00:00 2001 From: Renaud Chaput Date: Fri, 10 May 2024 14:40:20 +0200 Subject: [PATCH] Add OpenTelemetry instrumentation (#30130) Co-authored-by: Juliano Costa Co-authored-by: Robb Kidd --- Gemfile | 18 +++++ Gemfile.lock | 108 +++++++++++++++++++++++++++ config/initializers/opentelemetry.rb | 63 ++++++++++++++++ 3 files changed, 189 insertions(+) create mode 100644 config/initializers/opentelemetry.rb diff --git a/Gemfile b/Gemfile index c7e07f964..247865aac 100644 --- a/Gemfile +++ b/Gemfile @@ -103,6 +103,24 @@ gem 'rdf-normalize', '~> 0.5' gem 'private_address_check', '~> 0.5' +group :opentelemetry do + gem 'opentelemetry-exporter-otlp', '~> 0.26.3', require: false + gem 'opentelemetry-instrumentation-active_job', '~> 0.7.1', require: false + gem 'opentelemetry-instrumentation-active_model_serializers', '~> 0.20.1', require: false + gem 'opentelemetry-instrumentation-concurrent_ruby', '~> 0.21.2', require: false + gem 'opentelemetry-instrumentation-excon', '~> 0.22.0', require: false + gem 'opentelemetry-instrumentation-faraday', '~> 0.24.1', require: false + gem 'opentelemetry-instrumentation-http', '~> 0.23.2', require: false + gem 'opentelemetry-instrumentation-http_client', '~> 0.22.3', require: false + gem 'opentelemetry-instrumentation-net_http', '~> 0.22.4', require: false + gem 'opentelemetry-instrumentation-pg', '~> 0.27.1', require: false + gem 'opentelemetry-instrumentation-rack', '~> 0.24.1', require: false + gem 'opentelemetry-instrumentation-rails', '~> 0.30.0', require: false + gem 'opentelemetry-instrumentation-redis', '~> 0.25.3', require: false + gem 'opentelemetry-instrumentation-sidekiq', '~> 0.25.2', require: false + gem 'opentelemetry-sdk', '~> 1.4', require: false +end + group :test do # Adds RSpec Error/Warning annotations to GitHub PRs on the Files tab gem 'rspec-github', '~> 2.4', require: false diff --git a/Gemfile.lock b/Gemfile.lock index b4db2b9a0..b96afef2c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -284,6 +284,9 @@ GEM ruby-progressbar (~> 1.4) globalid (1.2.1) activesupport (>= 6.1) + google-protobuf (3.25.3) + googleapis-common-protos-types (1.14.0) + google-protobuf (~> 3.18) haml (6.3.0) temple (>= 0.8.2) thor @@ -483,6 +486,96 @@ GEM openssl (3.2.0) openssl-signature_algorithm (1.3.0) openssl (> 2.0) + opentelemetry-api (1.2.5) + opentelemetry-common (0.20.1) + opentelemetry-api (~> 1.0) + opentelemetry-exporter-otlp (0.26.3) + google-protobuf (~> 3.14) + googleapis-common-protos-types (~> 1.3) + opentelemetry-api (~> 1.1) + opentelemetry-common (~> 0.20) + opentelemetry-sdk (~> 1.2) + opentelemetry-semantic_conventions + opentelemetry-helpers-sql-obfuscation (0.1.0) + opentelemetry-common (~> 0.20) + opentelemetry-instrumentation-action_pack (0.9.0) + opentelemetry-api (~> 1.0) + opentelemetry-instrumentation-base (~> 0.22.1) + opentelemetry-instrumentation-rack (~> 0.21) + opentelemetry-instrumentation-action_view (0.7.0) + opentelemetry-api (~> 1.0) + opentelemetry-instrumentation-active_support (~> 0.1) + opentelemetry-instrumentation-base (~> 0.22.1) + opentelemetry-instrumentation-active_job (0.7.1) + opentelemetry-api (~> 1.0) + opentelemetry-instrumentation-base (~> 0.22.1) + opentelemetry-instrumentation-active_model_serializers (0.20.1) + opentelemetry-api (~> 1.0) + opentelemetry-instrumentation-base (~> 0.22.1) + opentelemetry-instrumentation-active_record (0.7.1) + opentelemetry-api (~> 1.0) + opentelemetry-instrumentation-base (~> 0.22.1) + opentelemetry-instrumentation-active_support (0.5.1) + opentelemetry-api (~> 1.0) + opentelemetry-instrumentation-base (~> 0.22.1) + opentelemetry-instrumentation-base (0.22.3) + opentelemetry-api (~> 1.0) + opentelemetry-registry (~> 0.1) + opentelemetry-instrumentation-concurrent_ruby (0.21.2) + opentelemetry-api (~> 1.0) + opentelemetry-instrumentation-base (~> 0.22.1) + opentelemetry-instrumentation-excon (0.22.0) + opentelemetry-api (~> 1.0) + opentelemetry-common (~> 0.20.0) + opentelemetry-instrumentation-base (~> 0.22.1) + opentelemetry-instrumentation-faraday (0.24.1) + opentelemetry-api (~> 1.0) + opentelemetry-common (~> 0.20.0) + opentelemetry-instrumentation-base (~> 0.22.1) + opentelemetry-instrumentation-http (0.23.2) + opentelemetry-api (~> 1.0) + opentelemetry-instrumentation-base (~> 0.22.1) + opentelemetry-instrumentation-http_client (0.22.3) + opentelemetry-api (~> 1.0) + opentelemetry-common (~> 0.20.0) + opentelemetry-instrumentation-base (~> 0.22.1) + opentelemetry-instrumentation-net_http (0.22.4) + opentelemetry-api (~> 1.0) + opentelemetry-common (~> 0.20.0) + opentelemetry-instrumentation-base (~> 0.22.1) + opentelemetry-instrumentation-pg (0.27.1) + opentelemetry-api (~> 1.0) + opentelemetry-helpers-sql-obfuscation + opentelemetry-instrumentation-base (~> 0.22.1) + opentelemetry-instrumentation-rack (0.24.1) + opentelemetry-api (~> 1.0) + opentelemetry-common (~> 0.20.0) + opentelemetry-instrumentation-base (~> 0.22.1) + opentelemetry-instrumentation-rails (0.30.0) + opentelemetry-api (~> 1.0) + opentelemetry-instrumentation-action_pack (~> 0.9.0) + opentelemetry-instrumentation-action_view (~> 0.7.0) + opentelemetry-instrumentation-active_job (~> 0.7.0) + opentelemetry-instrumentation-active_record (~> 0.7.0) + opentelemetry-instrumentation-active_support (~> 0.5.0) + opentelemetry-instrumentation-base (~> 0.22.1) + opentelemetry-instrumentation-redis (0.25.3) + opentelemetry-api (~> 1.0) + opentelemetry-common (~> 0.20.0) + opentelemetry-instrumentation-base (~> 0.22.1) + opentelemetry-instrumentation-sidekiq (0.25.2) + opentelemetry-api (~> 1.0) + opentelemetry-common (~> 0.20.0) + opentelemetry-instrumentation-base (~> 0.22.1) + opentelemetry-registry (0.3.1) + opentelemetry-api (~> 1.1) + opentelemetry-sdk (1.4.1) + opentelemetry-api (~> 1.1) + opentelemetry-common (~> 0.20) + opentelemetry-registry (~> 0.2) + opentelemetry-semantic_conventions + opentelemetry-semantic_conventions (1.10.0) + opentelemetry-api (~> 1.0) orm_adapter (0.5.0) ox (2.14.18) parallel (1.24.0) @@ -880,6 +973,21 @@ DEPENDENCIES omniauth-rails_csrf_protection (~> 1.0) omniauth-saml (~> 2.0) omniauth_openid_connect (~> 0.6.1) + opentelemetry-exporter-otlp (~> 0.26.3) + opentelemetry-instrumentation-active_job (~> 0.7.1) + opentelemetry-instrumentation-active_model_serializers (~> 0.20.1) + opentelemetry-instrumentation-concurrent_ruby (~> 0.21.2) + opentelemetry-instrumentation-excon (~> 0.22.0) + opentelemetry-instrumentation-faraday (~> 0.24.1) + opentelemetry-instrumentation-http (~> 0.23.2) + opentelemetry-instrumentation-http_client (~> 0.22.3) + opentelemetry-instrumentation-net_http (~> 0.22.4) + opentelemetry-instrumentation-pg (~> 0.27.1) + opentelemetry-instrumentation-rack (~> 0.24.1) + opentelemetry-instrumentation-rails (~> 0.30.0) + opentelemetry-instrumentation-redis (~> 0.25.3) + opentelemetry-instrumentation-sidekiq (~> 0.25.2) + opentelemetry-sdk (~> 1.4) ox (~> 2.14) parslet pg (~> 1.5) diff --git a/config/initializers/opentelemetry.rb b/config/initializers/opentelemetry.rb new file mode 100644 index 000000000..e50132d46 --- /dev/null +++ b/config/initializers/opentelemetry.rb @@ -0,0 +1,63 @@ +# frozen_string_literal: true + +# Set OTEL_* environment variables according to OTel docs: +# https://opentelemetry.io/docs/concepts/sdk-configuration/ + +if ENV.keys.any? { |name| name.match?(/OTEL_.*_ENDPOINT/) } + require 'opentelemetry/sdk' + require 'opentelemetry/exporter/otlp' + + require 'opentelemetry/instrumentation/active_job' + require 'opentelemetry/instrumentation/active_model_serializers' + require 'opentelemetry/instrumentation/concurrent_ruby' + require 'opentelemetry/instrumentation/excon' + require 'opentelemetry/instrumentation/faraday' + require 'opentelemetry/instrumentation/http' + require 'opentelemetry/instrumentation/http_client' + require 'opentelemetry/instrumentation/net/http' + require 'opentelemetry/instrumentation/pg' + require 'opentelemetry/instrumentation/rack' + require 'opentelemetry/instrumentation/rails' + require 'opentelemetry/instrumentation/redis' + require 'opentelemetry/instrumentation/sidekiq' + + OpenTelemetry::SDK.configure do |c| + # use_all() attempts to load ALL the auto-instrumentations + # currently loaded by Ruby requires. + # + # Load attempts will emit an INFO or WARN to the console + # about the success/failure to wire up an auto-instrumentation. + # "WARN -- : Instrumentation: failed to install" is most + # likely caused by not being a Ruby library loaded by + # the application or the instrumentation has been explicitly + # disabled. + # + # To disable an instrumentation, set an environment variable + # along this pattern: + # + # OTEL_RUBY_INSTRUMENTATION__ENABLED=false + # + # For example, PostgreSQL and Redis produce a lot of child spans + # in the course of this application doing its business. To turn + # them off, set the env vars below, but recognize that you will + # be missing details about what particular calls to the + # datastores are slow. + # + # OTEL_RUBY_INSTRUMENTATION_PG_ENABLED=false + # OTEL_RUBY_INSTRUMENTATION_REDIS_ENABLED=false + + c.use_all({ + 'OpenTelemetry::Instrumentation::Rack' => { + use_rack_events: false, # instead of events, use middleware; allows for untraced_endpoints to ignore child spans + untraced_endpoints: ['/health'], + }, + }) + + c.service_name = case $PROGRAM_NAME + when /puma/ then 'mastodon/web' + else + "mastodon/#{$PROGRAM_NAME.split('/').last}" + end + c.service_version = Mastodon::Version.to_s + end +end