Skip to content

feat(logs): Add user attributes to logs #4423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

szokeasaurusrex
Copy link
Member

@szokeasaurusrex szokeasaurusrex marked this pull request as ready for review May 28, 2025 16:18
@szokeasaurusrex szokeasaurusrex requested a review from a team as a code owner May 28, 2025 16:18
Copy link

codecov bot commented May 28, 2025

❌ 24 Tests Failed:

Tests completed Failed Passed Skipped
8326 24 8302 1551
View the top 3 failed test(s) by shortest run time
tests.integrations.aiohttp.test_aiohttp::test_crumb_capture
Stack Traces | 0.002s run time
file .../integrations/aiohttp/test_aiohttp.py, line 491
  @pytest.mark.asyncio
  async def test_crumb_capture(
      sentry_init, aiohttp_raw_server, aiohttp_client, event_loop, capture_events
  ):
      def before_breadcrumb(crumb, hint):
E       fixture 'event_loop' not found
>       available fixtures: DictionaryContaining, ObjectDescribedBy, StringContaining, _capture_internal_warnings, _class_event_loop, _function_event_loop, _module_event_loop, _package_event_loop, _session_event_loop, aiohttp_client, aiohttp_client_cls, aiohttp_raw_server, aiohttp_server, benchmark, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, capture_envelopes, capture_events, capture_events_forksafe, capture_exceptions, capture_record_lost_event_calls, clean_scopes, cov, doctest_namespace, event_loop_policy, httpserver, httpsserver, internal_exceptions, maybe_monkeypatched_threading, monkeypatch, no_cover, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, render_span_tree, reset_integrations, sentry_init, smtpserver, suppress_deprecation_warnings, teardown_profiling, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory, uninstall_integration, unused_tcp_port, unused_tcp_port_factory, unused_udp_port, unused_udp_port_factory, validate_event_schema
>       use 'pytest --fixtures [testpath]' for help on them.

.../integrations/aiohttp/test_aiohttp.py:491
tests.integrations.aiohttp.test_aiohttp::test_crumb_capture_client_error[403-warning]
Stack Traces | 0.002s run time
file .../integrations/aiohttp/test_aiohttp.py, line 534
  @pytest.mark.parametrize(
      "status_code,level",
      [
          (200, None),
          (301, None),
          (403, "warning"),
          (405, "warning"),
          (500, "error"),
      ],
  )
  @pytest.mark.asyncio
  async def test_crumb_capture_client_error(
      sentry_init,
      aiohttp_raw_server,
      aiohttp_client,
      event_loop,
      capture_events,
      status_code,
      level,
  ):
      sentry_init(integrations=[AioHttpIntegration()])

      async def handler(request):
          return web.Response(status=status_code)

      raw_server = await aiohttp_raw_server(handler)

      with start_transaction():
          events = capture_events()

          client = await aiohttp_client(raw_server)
          resp = await client.get("/")
          assert resp.status == status_code
          capture_message("Testing!")

          (event,) = events

          crumb = event["breadcrumbs"]["values"][0]
          assert crumb["type"] == "http"
          if level is None:
              assert "level" not in crumb
          else:
              assert crumb["level"] == level
          assert crumb["category"] == "httplib"
          assert crumb["data"] == ApproxDict(
              {
                  "url": "http://127.0.0.1:{}/".format(raw_server.port),
                  "http.fragment": "",
                  "http.method": "GET",
                  "http.query": "",
                  "http.response.status_code": status_code,
              }
          )
E       fixture 'event_loop' not found
>       available fixtures: DictionaryContaining, ObjectDescribedBy, StringContaining, _capture_internal_warnings, _class_event_loop, _function_event_loop, _module_event_loop, _package_event_loop, _session_event_loop, aiohttp_client, aiohttp_client_cls, aiohttp_raw_server, aiohttp_server, benchmark, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, capture_envelopes, capture_events, capture_events_forksafe, capture_exceptions, capture_record_lost_event_calls, clean_scopes, cov, doctest_namespace, event_loop_policy, httpserver, httpsserver, internal_exceptions, maybe_monkeypatched_threading, monkeypatch, no_cover, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, render_span_tree, reset_integrations, sentry_init, smtpserver, suppress_deprecation_warnings, teardown_profiling, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory, uninstall_integration, unused_tcp_port, unused_tcp_port_factory, unused_udp_port, unused_udp_port_factory, validate_event_schema
>       use 'pytest --fixtures [testpath]' for help on them.

.../integrations/aiohttp/test_aiohttp.py:534
tests.integrations.aiohttp.test_aiohttp::test_crumb_capture_client_error[500-error]
Stack Traces | 0.002s run time
file .../integrations/aiohttp/test_aiohttp.py, line 534
  @pytest.mark.parametrize(
      "status_code,level",
      [
          (200, None),
          (301, None),
          (403, "warning"),
          (405, "warning"),
          (500, "error"),
      ],
  )
  @pytest.mark.asyncio
  async def test_crumb_capture_client_error(
      sentry_init,
      aiohttp_raw_server,
      aiohttp_client,
      event_loop,
      capture_events,
      status_code,
      level,
  ):
      sentry_init(integrations=[AioHttpIntegration()])

      async def handler(request):
          return web.Response(status=status_code)

      raw_server = await aiohttp_raw_server(handler)

      with start_transaction():
          events = capture_events()

          client = await aiohttp_client(raw_server)
          resp = await client.get("/")
          assert resp.status == status_code
          capture_message("Testing!")

          (event,) = events

          crumb = event["breadcrumbs"]["values"][0]
          assert crumb["type"] == "http"
          if level is None:
              assert "level" not in crumb
          else:
              assert crumb["level"] == level
          assert crumb["category"] == "httplib"
          assert crumb["data"] == ApproxDict(
              {
                  "url": "http://127.0.0.1:{}/".format(raw_server.port),
                  "http.fragment": "",
                  "http.method": "GET",
                  "http.query": "",
                  "http.response.status_code": status_code,
              }
          )
E       fixture 'event_loop' not found
>       available fixtures: DictionaryContaining, ObjectDescribedBy, StringContaining, _capture_internal_warnings, _class_event_loop, _function_event_loop, _module_event_loop, _package_event_loop, _session_event_loop, aiohttp_client, aiohttp_client_cls, aiohttp_raw_server, aiohttp_server, benchmark, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, capture_envelopes, capture_events, capture_events_forksafe, capture_exceptions, capture_record_lost_event_calls, clean_scopes, cov, doctest_namespace, event_loop_policy, httpserver, httpsserver, internal_exceptions, maybe_monkeypatched_threading, monkeypatch, no_cover, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, render_span_tree, reset_integrations, sentry_init, smtpserver, suppress_deprecation_warnings, teardown_profiling, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory, uninstall_integration, unused_tcp_port, unused_tcp_port_factory, unused_udp_port, unused_udp_port_factory, validate_event_schema
>       use 'pytest --fixtures [testpath]' for help on them.

.../integrations/aiohttp/test_aiohttp.py:534

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

@@ -927,6 +927,20 @@ def _capture_experimental_log(self, current_scope, log):
elif propagation_context is not None:
log["trace_id"] = propagation_context.trace_id

if self.should_send_default_pii() and isolation_scope._user is not None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we also check the global and current scope?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add user attributes to Python logs
2 participants