Skip to content

Commit 94da548

Browse files
committed
Optimize scope attribute setting
1 parent 8f385ee commit 94da548

2 files changed

Lines changed: 13 additions & 22 deletions

File tree

sentry-ruby/lib/sentry/log_event.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,13 @@ def serialize_body
5858
end
5959

6060
def serialize_attributes
61-
sentry_attributes.update(@attributes).transform_values! { |v| attribute_hash(v) }
61+
populate_sentry_attributes!
62+
@attributes.transform_values! { |v| attribute_hash(v) }
6263
end
6364

64-
def sentry_attributes
65-
sentry_attributes = {}
66-
sentry_attributes["sentry.origin"] = @origin if @origin
67-
sentry_attributes["sentry.message.template"] = template if has_parameters?
68-
sentry_attributes
65+
def populate_sentry_attributes!
66+
@attributes["sentry.origin"] ||= @origin if @origin
67+
@attributes["sentry.message.template"] ||= template if has_parameters?
6968
end
7069

7170
def parameters

sentry-ruby/lib/sentry/scope.rb

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,24 +101,16 @@ def apply_to_telemetry(telemetry)
101101
configuration = Sentry.configuration
102102
return telemetry unless configuration
103103

104-
default_attributes = {
105-
"sentry.environment" => configuration.environment,
106-
"sentry.release" => configuration.release,
107-
"sentry.sdk.name" => Sentry.sdk_meta["name"],
108-
"sentry.sdk.version" => Sentry.sdk_meta["version"],
109-
"server.address" => configuration.server_name
110-
}.compact
111-
112-
default_attributes.each { |k, v| telemetry.attributes[k] ||= v }
104+
telemetry.attributes["sentry.sdk.name"] ||= Sentry.sdk_meta["name"]
105+
telemetry.attributes["sentry.sdk.version"] ||= Sentry.sdk_meta["version"]
106+
telemetry.attributes["sentry.environment"] ||= configuration.environment if configuration.environment
107+
telemetry.attributes["sentry.release"] ||= configuration.release if configuration.release
108+
telemetry.attributes["server.address"] ||= configuration.server_name if configuration.server_name
113109

114110
if configuration.send_default_pii && !user.empty?
115-
user_attributes = {
116-
"user.id" => user[:id],
117-
"user.name" => user[:username],
118-
"user.email" => user[:email]
119-
}.compact
120-
121-
user_attributes.each { |k, v| telemetry.attributes[k] ||= v }
111+
telemetry.attributes["user.id"] ||= user[:id] if user[:id]
112+
telemetry.attributes["user.name"] ||= user[:username] if user[:username]
113+
telemetry.attributes["user.email"] ||= user[:email] if user[:email]
122114
end
123115

124116
telemetry

0 commit comments

Comments
 (0)