Skip to content

Commit 1da1c2d

Browse files
authored
Skip sentry.message.template when there are no parameters (#2700)
* Skip sentry.message.template when there are no parameters * Update CHANGELOG
1 parent aa4741c commit 1da1c2d

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## Unreleased
2+
3+
### Bug Fixes
4+
5+
- Skip including `sentry.message.template` in the log event attributes if there are no interpolation parameters provided ([#2700](https://github.com/getsentry/sentry-ruby/pull/2700))
6+
17
## 5.27.0
28

39
### Feature

sentry-ruby/lib/sentry/log_event.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class LogEvent
3232
"sentry.message.template" => :template
3333
}
3434

35+
PARAMETER_PREFIX = "sentry.message.parameter"
36+
3537
USER_ATTRIBUTES = {
3638
"user.id" => :user_id,
3739
"user.name" => :user_username,
@@ -51,6 +53,7 @@ class LogEvent
5153
parent_span_id
5254
sdk_name
5355
sdk_version
56+
template
5457
timestamp
5558
trace_id
5659
user_id
@@ -146,6 +149,10 @@ def serialize_user_email
146149
user[:email]
147150
end
148151

152+
def serialize_template
153+
template if has_parameters?
154+
end
155+
149156
def serialize_attributes
150157
hash = {}
151158

@@ -185,11 +192,11 @@ def parameters
185192

186193
if parameters.is_a?(Hash)
187194
parameters.each do |key, value|
188-
attributes["sentry.message.parameter.#{key}"] = value
195+
attributes["#{PARAMETER_PREFIX}.#{key}"] = value
189196
end
190197
else
191198
parameters.each_with_index do |param, index|
192-
attributes["sentry.message.parameter.#{index}"] = param
199+
attributes["#{PARAMETER_PREFIX}.#{index}"] = param
193200
end
194201
end
195202
end
@@ -202,5 +209,9 @@ def template_tokens
202209
def is_template?
203210
body.include?("%s") || TOKEN_REGEXP.match?(body)
204211
end
212+
213+
def has_parameters?
214+
attributes.keys.any? { |key| key.start_with?(PARAMETER_PREFIX) }
215+
end
205216
end
206217
end

sentry-ruby/spec/sentry/log_event_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,39 @@
9696
expect(hash[:attributes]).not_to have_key("sentry.message.template")
9797
end
9898

99+
it "doesn't set message.template when template has no parameters" do
100+
event = described_class.new(
101+
configuration: configuration,
102+
level: :info,
103+
body: "Hello %{name}, today is %{day}"
104+
)
105+
106+
hash = event.to_hash
107+
108+
expect(hash[:attributes]).not_to have_key("sentry.message.template")
109+
expect(hash[:attributes]).not_to have_key("sentry.message.parameter.name")
110+
expect(hash[:attributes]).not_to have_key("sentry.message.parameter.day")
111+
end
112+
113+
it "sets message.template only when parameters are present" do
114+
attributes = {
115+
"sentry.message.parameter.0" => "John"
116+
}
117+
118+
event = described_class.new(
119+
configuration: configuration,
120+
level: :info,
121+
body: "User %s has logged in!",
122+
attributes: attributes
123+
)
124+
125+
hash = event.to_hash
126+
127+
expect(hash[:attributes]).to have_key("sentry.message.template")
128+
expect(hash[:attributes]["sentry.message.template"]).to eq({ value: "User %s has logged in!", type: "string" })
129+
expect(hash[:attributes]["sentry.message.parameter.0"]).to eq({ value: "John", type: "string" })
130+
end
131+
99132
it "serializes different attribute types correctly" do
100133
attributes = {
101134
"string_attr" => "string value",

0 commit comments

Comments
 (0)