Skip to content

Commit 5bbbc4b

Browse files
committed
Add relevant logging contexts only for the duration of the request
This should prevent some contexts such us UserContext to be present on subsequent logs for requests without users
1 parent 585d0cc commit 5bbbc4b

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

lib/logtail-rack/http_context.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ def call(env)
1818
request_id: request.request_id
1919
)
2020

21-
CurrentContext.add(context.to_hash)
22-
@app.call(env)
21+
CurrentContext.with(context.to_hash) do
22+
@app.call(env)
23+
end
2324
end
2425
end
2526
end

lib/logtail-rack/user_context.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,12 @@ def custom_user_hash
6767
def call(env)
6868
user_hash = get_user_hash(env)
6969
if user_hash
70-
CurrentContext.add({user: user_hash})
70+
CurrentContext.with({user: user_hash}) do
71+
@app.call(env)
72+
end
73+
else
74+
@app.call(env)
7175
end
72-
73-
@app.call(env)
7476
end
7577

7678
private

0 commit comments

Comments
 (0)