opentelemetry.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # frozen_string_literal: true
  2. # Set OTEL_* environment variables according to OTel docs:
  3. # https://opentelemetry.io/docs/concepts/sdk-configuration/
  4. if ENV.keys.any? { |name| name.match?(/OTEL_.*_ENDPOINT/) }
  5. require 'opentelemetry/sdk'
  6. require 'opentelemetry/exporter/otlp'
  7. require 'opentelemetry/instrumentation/active_job'
  8. require 'opentelemetry/instrumentation/active_model_serializers'
  9. require 'opentelemetry/instrumentation/concurrent_ruby'
  10. require 'opentelemetry/instrumentation/excon'
  11. require 'opentelemetry/instrumentation/faraday'
  12. require 'opentelemetry/instrumentation/http'
  13. require 'opentelemetry/instrumentation/http_client'
  14. require 'opentelemetry/instrumentation/net/http'
  15. require 'opentelemetry/instrumentation/pg'
  16. require 'opentelemetry/instrumentation/rack'
  17. require 'opentelemetry/instrumentation/rails'
  18. require 'opentelemetry/instrumentation/redis'
  19. require 'opentelemetry/instrumentation/sidekiq'
  20. OpenTelemetry::SDK.configure do |c|
  21. # use_all() attempts to load ALL the auto-instrumentations
  22. # currently loaded by Ruby requires.
  23. #
  24. # Load attempts will emit an INFO or WARN to the console
  25. # about the success/failure to wire up an auto-instrumentation.
  26. # "WARN -- : Instrumentation: <X> failed to install" is most
  27. # likely caused by <X> not being a Ruby library loaded by
  28. # the application or the instrumentation has been explicitly
  29. # disabled.
  30. #
  31. # To disable an instrumentation, set an environment variable
  32. # along this pattern:
  33. #
  34. # OTEL_RUBY_INSTRUMENTATION_<X>_ENABLED=false
  35. #
  36. # For example, PostgreSQL and Redis produce a lot of child spans
  37. # in the course of this application doing its business. To turn
  38. # them off, set the env vars below, but recognize that you will
  39. # be missing details about what particular calls to the
  40. # datastores are slow.
  41. #
  42. # OTEL_RUBY_INSTRUMENTATION_PG_ENABLED=false
  43. # OTEL_RUBY_INSTRUMENTATION_REDIS_ENABLED=false
  44. c.use_all({
  45. 'OpenTelemetry::Instrumentation::Rack' => {
  46. use_rack_events: false, # instead of events, use middleware; allows for untraced_endpoints to ignore child spans
  47. untraced_endpoints: ['/health'],
  48. },
  49. 'OpenTelemetry::Instrumentation::Sidekiq' => {
  50. span_naming: :job_class, # Use the job class as the span name, otherwise this is the queue name and not very helpful
  51. },
  52. })
  53. prefix = ENV.fetch('OTEL_SERVICE_NAME_PREFIX', 'mastodon')
  54. separator = ENV.fetch('OTEL_SERVICE_NAME_SEPARATOR', '/')
  55. c.service_name = case $PROGRAM_NAME
  56. when /puma/ then "#{prefix}#{separator}web"
  57. else
  58. "#{prefix}#{separator}#{$PROGRAM_NAME.split('/').last}"
  59. end
  60. c.service_version = Mastodon::Version.to_s
  61. if Mastodon::Version.source_commit.present?
  62. c.resource = OpenTelemetry::SDK::Resources::Resource.create(
  63. 'vcs.repository.ref.revision' => Mastodon::Version.source_commit,
  64. 'vcs.repository.url.full' => Mastodon::Version.source_base_url
  65. )
  66. end
  67. end
  68. end
  69. MastodonOTELTracer = OpenTelemetry.tracer_provider.tracer('mastodon')