File tree Expand file tree Collapse file tree 3 files changed +24
-13
lines changed
lib/semantic_logger/formatters Expand file tree Collapse file tree 3 files changed +24
-13
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
5
5
6
6
## [ unreleased]
7
7
8
+ - Fix missing trace.id/span.id in NewRelic logs
9
+
8
10
## [ 4.17.0]
9
11
10
12
- Correct ` source_code_uri ` URL
Original file line number Diff line number Diff line change @@ -47,7 +47,7 @@ def call(log, logger)
47
47
hash = super
48
48
49
49
result = {
50
- **newrelic_metadata ,
50
+ **log . newrelic_metadata ,
51
51
message : hash [ :message ] . to_s ,
52
52
tags : hash [ :tags ] ,
53
53
metric : hash [ :metric ] ,
@@ -112,15 +112,19 @@ def call(log, logger)
112
112
113
113
result
114
114
end
115
+ end
115
116
116
- private
117
+ module NewRelicMetadata
118
+ attr_reader :newrelic_metadata
117
119
118
- # NOTE: This function will already include trace.id and span.id if they
119
- # are available so I believe the previous implementation of this is redundant
120
- # https://rubydoc.info/gems/newrelic_rpm/NewRelic/Agent#linking_metadata-instance_method
121
- def newrelic_metadata
122
- NewRelic ::Agent . linking_metadata . transform_keys ( &:to_sym )
120
+ def initialize ( *)
121
+ super
122
+ # Record NewRelic's "trace.id"/"entity.name"/"hostname"/etc, so we can include them later in the formatted output.
123
+ # These are thread-local, so need to be captured as soon as the log-message is created.
124
+ # https://rubydoc.info/gems/newrelic_rpm/NewRelic/Agent#linking_metadata-instance_method
125
+ @newrelic_metadata = NewRelic ::Agent . linking_metadata
123
126
end
124
127
end
128
+ Log . prepend NewRelicMetadata
125
129
end
126
130
end
Original file line number Diff line number Diff line change @@ -174,19 +174,24 @@ class NewRelicLogsTest < Minitest::Test
174
174
175
175
describe "metadata" do
176
176
it "includes trace.id and span.id if present" do
177
+ # Simulate recording a log message within a Rails transaction, where trace.id has been set on the current thread
177
178
NewRelic ::Agent . stub ( :linking_metadata , { "trace.id" => "trace123" , "span.id" => "span456" } ) do
178
- result = formatted_log
179
- assert_equal "trace123" , result [ :"trace.id" ]
180
- assert_equal "span456" , result [ :"span.id" ]
179
+ log
181
180
end
181
+ # ... which is then formatted on the async appender thread
182
+ result = formatted_log
183
+ assert_equal "trace123" , result [ "trace.id" ]
184
+ assert_equal "span456" , result [ "span.id" ]
182
185
end
183
186
184
187
it "omits trace.id and span.id if absent" do
185
188
NewRelic ::Agent . stub ( :linking_metadata , { } ) do
186
- result = formatted_log
187
- refute result . key? ( :"trace.id" )
188
- refute result . key? ( :"span.id" )
189
+ log
189
190
end
191
+
192
+ result = formatted_log
193
+ refute result . key? ( "trace.id" )
194
+ refute result . key? ( "span.id" )
190
195
end
191
196
end
192
197
end
You can’t perform that action at this time.
0 commit comments