Skip to content

Commit

Permalink
Add instrumentation to the search services (#30350)
Browse files Browse the repository at this point in the history
  • Loading branch information
renchap committed May 24, 2024
1 parent 8394a15 commit acc77c3
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 21 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ gem 'rdf-normalize', '~> 0.5'

gem 'private_address_check', '~> 0.5'

gem 'opentelemetry-api', '~> 1.2.5'

group :opentelemetry do
gem 'opentelemetry-exporter-otlp', '~> 0.26.3', require: false
gem 'opentelemetry-instrumentation-active_job', '~> 0.7.1', require: false
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ DEPENDENCIES
omniauth-rails_csrf_protection (~> 1.0)
omniauth-saml (~> 2.0)
omniauth_openid_connect (~> 0.6.1)
opentelemetry-api (~> 1.2.5)
opentelemetry-exporter-otlp (~> 0.26.3)
opentelemetry-instrumentation-active_job (~> 0.7.1)
opentelemetry-instrumentation-active_model_serializers (~> 0.20.1)
Expand Down
22 changes: 16 additions & 6 deletions app/services/account_search_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,23 @@ def core_query
end

def call(query, account = nil, options = {})
@query = query&.strip&.gsub(/\A@/, '')
@limit = options[:limit].to_i
@offset = options[:offset].to_i
@options = options
@account = account
MastodonOTELTracer.in_span('AccountSearchService#call') do |span|
@query = query&.strip&.gsub(/\A@/, '')
@limit = options[:limit].to_i
@offset = options[:offset].to_i
@options = options
@account = account

span.add_attributes(
'search.offset' => @offset,
'search.limit' => @limit,
'search.backend' => Chewy.enabled? ? 'elasticsearch' : 'database'
)

search_service_results.compact.uniq
search_service_results.compact.uniq.tap do |results|
span.set_attribute('search.results.count', results.size)
end
end
end

private
Expand Down
26 changes: 18 additions & 8 deletions app/services/statuses_search_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@

class StatusesSearchService < BaseService
def call(query, account = nil, options = {})
@query = query&.strip
@account = account
@options = options
@limit = options[:limit].to_i
@offset = options[:offset].to_i

convert_deprecated_options!
status_search_results
MastodonOTELTracer.in_span('StatusesSearchService#call') do |span|
@query = query&.strip
@account = account
@options = options
@limit = options[:limit].to_i
@offset = options[:offset].to_i
convert_deprecated_options!

span.add_attributes(
'search.offset' => @offset,
'search.limit' => @limit,
'search.backend' => Chewy.enabled? ? 'elasticsearch' : 'database'
)

status_search_results.tap do |results|
span.set_attribute('search.results.count', results.size)
end
end
end

private
Expand Down
24 changes: 17 additions & 7 deletions app/services/tag_search_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@

class TagSearchService < BaseService
def call(query, options = {})
@query = query.strip.delete_prefix('#')
@offset = options.delete(:offset).to_i
@limit = options.delete(:limit).to_i
@options = options
MastodonOTELTracer.in_span('TagSearchService#call') do |span|
@query = query.strip.delete_prefix('#')
@offset = options.delete(:offset).to_i
@limit = options.delete(:limit).to_i
@options = options

results = from_elasticsearch if Chewy.enabled?
results ||= from_database
span.add_attributes(
'search.offset' => @offset,
'search.limit' => @limit,
'search.backend' => Chewy.enabled? ? 'elasticsearch' : 'database'
)

results
results = from_elasticsearch if Chewy.enabled?
results ||= from_database

span.set_attribute('search.results.count', results.size)

results
end
end

private
Expand Down
2 changes: 2 additions & 0 deletions config/initializers/opentelemetry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,5 @@
c.service_version = Mastodon::Version.to_s
end
end

MastodonOTELTracer = OpenTelemetry.tracer_provider.tracer('mastodon')

0 comments on commit acc77c3

Please sign in to comment.