File tree Expand file tree Collapse file tree 3 files changed +52
-2
lines changed Expand file tree Collapse file tree 3 files changed +52
-2
lines changed Original file line number Diff line number Diff line change
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
+
1
7
## 5.27.0
2
8
3
9
### Feature
Original file line number Diff line number Diff line change @@ -32,6 +32,8 @@ class LogEvent
32
32
"sentry.message.template" => :template
33
33
}
34
34
35
+ PARAMETER_PREFIX = "sentry.message.parameter"
36
+
35
37
USER_ATTRIBUTES = {
36
38
"user.id" => :user_id ,
37
39
"user.name" => :user_username ,
@@ -51,6 +53,7 @@ class LogEvent
51
53
parent_span_id
52
54
sdk_name
53
55
sdk_version
56
+ template
54
57
timestamp
55
58
trace_id
56
59
user_id
@@ -146,6 +149,10 @@ def serialize_user_email
146
149
user [ :email ]
147
150
end
148
151
152
+ def serialize_template
153
+ template if has_parameters?
154
+ end
155
+
149
156
def serialize_attributes
150
157
hash = { }
151
158
@@ -185,11 +192,11 @@ def parameters
185
192
186
193
if parameters . is_a? ( Hash )
187
194
parameters . each do |key , value |
188
- attributes [ "sentry.message.parameter .#{ key } " ] = value
195
+ attributes [ "#{ PARAMETER_PREFIX } .#{ key } " ] = value
189
196
end
190
197
else
191
198
parameters . each_with_index do |param , index |
192
- attributes [ "sentry.message.parameter .#{ index } " ] = param
199
+ attributes [ "#{ PARAMETER_PREFIX } .#{ index } " ] = param
193
200
end
194
201
end
195
202
end
@@ -202,5 +209,9 @@ def template_tokens
202
209
def is_template?
203
210
body . include? ( "%s" ) || TOKEN_REGEXP . match? ( body )
204
211
end
212
+
213
+ def has_parameters?
214
+ attributes . keys . any? { |key | key . start_with? ( PARAMETER_PREFIX ) }
215
+ end
205
216
end
206
217
end
Original file line number Diff line number Diff line change 96
96
expect ( hash [ :attributes ] ) . not_to have_key ( "sentry.message.template" )
97
97
end
98
98
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
+
99
132
it "serializes different attribute types correctly" do
100
133
attributes = {
101
134
"string_attr" => "string value" ,
You can’t perform that action at this time.
0 commit comments