Add telemetry for status / bio formatting (#32677)

This commit is contained in:
David Roetzel 2024-10-29 11:10:17 +01:00 committed by GitHub
parent 244aaf9a38
commit 9de3fd60a0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 66 additions and 39 deletions

View file

@ -27,7 +27,14 @@ module FormattingHelper
module_function :extract_status_plain_text module_function :extract_status_plain_text
def status_content_format(status) def status_content_format(status)
html_aware_format(status.text, status.local?, preloaded_accounts: [status.account] + (status.respond_to?(:active_mentions) ? status.active_mentions.map(&:account) : [])) MastodonOTELTracer.in_span('HtmlAwareFormatter rendering') do |span|
span.add_attributes(
'app.formatter.content.type' => 'status',
'app.formatter.content.origin' => status.local? ? 'local' : 'remote'
)
html_aware_format(status.text, status.local?, preloaded_accounts: [status.account] + (status.respond_to?(:active_mentions) ? status.active_mentions.map(&:account) : []))
end
end end
def rss_status_content_format(status) def rss_status_content_format(status)
@ -39,7 +46,14 @@ module FormattingHelper
end end
def account_bio_format(account) def account_bio_format(account)
html_aware_format(account.note, account.local?) MastodonOTELTracer.in_span('HtmlAwareFormatter rendering') do |span|
span.add_attributes(
'app.formatter.content.type' => 'account_bio',
'app.formatter.content.origin' => account.local? ? 'local' : 'remote'
)
html_aware_format(account.note, account.local?)
end
end end
def account_field_value_format(field, with_rel_me: true) def account_field_value_format(field, with_rel_me: true)

View file

@ -33,17 +33,24 @@ class TextFormatter
def to_s def to_s
return ''.html_safe if text.blank? return ''.html_safe if text.blank?
html = rewrite do |entity| html = nil
if entity[:url] MastodonOTELTracer.in_span('TextFormatter#to_s extract_and_rewrite') do
link_to_url(entity) html = rewrite do |entity|
elsif entity[:hashtag] if entity[:url]
link_to_hashtag(entity) link_to_url(entity)
elsif entity[:screen_name] elsif entity[:hashtag]
link_to_mention(entity) link_to_hashtag(entity)
elsif entity[:screen_name]
link_to_mention(entity)
end
end end
end end
html = simple_format(html, {}, sanitize: false).delete("\n") if multiline? if multiline?
MastodonOTELTracer.in_span('TextFormatter#to_s simple_format') do
html = simple_format(html, {}, sanitize: false).delete("\n")
end
end
html.html_safe # rubocop:disable Rails/OutputSafety html.html_safe # rubocop:disable Rails/OutputSafety
end end
@ -93,48 +100,54 @@ class TextFormatter
end end
def link_to_url(entity) def link_to_url(entity)
TextFormatter.shortened_link(entity[:url], rel_me: with_rel_me?) MastodonOTELTracer.in_span('TextFormatter#link_to_url') do
TextFormatter.shortened_link(entity[:url], rel_me: with_rel_me?)
end
end end
def link_to_hashtag(entity) def link_to_hashtag(entity)
hashtag = entity[:hashtag] MastodonOTELTracer.in_span('TextFormatter#link_to_hashtag') do
url = tag_url(hashtag) hashtag = entity[:hashtag]
url = tag_url(hashtag)
<<~HTML.squish <<~HTML.squish
<a href="#{h(url)}" class="mention hashtag" rel="tag">#<span>#{h(hashtag)}</span></a> <a href="#{h(url)}" class="mention hashtag" rel="tag">#<span>#{h(hashtag)}</span></a>
HTML HTML
end
end end
def link_to_mention(entity) def link_to_mention(entity)
username, domain = entity[:screen_name].split('@') MastodonOTELTracer.in_span('TextFormatter#link_to_mention') do
domain = nil if local_domain?(domain) username, domain = entity[:screen_name].split('@')
account = nil domain = nil if local_domain?(domain)
account = nil
if preloaded_accounts? if preloaded_accounts?
same_username_hits = 0 same_username_hits = 0
preloaded_accounts.each do |other_account| preloaded_accounts.each do |other_account|
same_username = other_account.username.casecmp(username).zero? same_username = other_account.username.casecmp(username).zero?
same_domain = other_account.domain.nil? ? domain.nil? : other_account.domain.casecmp(domain)&.zero? same_domain = other_account.domain.nil? ? domain.nil? : other_account.domain.casecmp(domain)&.zero?
if same_username && !same_domain if same_username && !same_domain
same_username_hits += 1 same_username_hits += 1
elsif same_username && same_domain elsif same_username && same_domain
account = other_account account = other_account
end
end end
else
account = entity_cache.mention(username, domain)
end end
else
account = entity_cache.mention(username, domain) return "@#{h(entity[:screen_name])}" if account.nil?
url = ActivityPub::TagManager.instance.url_for(account)
display_username = same_username_hits&.positive? || with_domains? ? account.pretty_acct : account.username
<<~HTML.squish
<span class="h-card" translate="no"><a href="#{h(url)}" class="u-url mention">@<span>#{h(display_username)}</span></a></span>
HTML
end end
return "@#{h(entity[:screen_name])}" if account.nil?
url = ActivityPub::TagManager.instance.url_for(account)
display_username = same_username_hits&.positive? || with_domains? ? account.pretty_acct : account.username
<<~HTML.squish
<span class="h-card" translate="no"><a href="#{h(url)}" class="u-url mention">@<span>#{h(display_username)}</span></a></span>
HTML
end end
def entity_cache def entity_cache