Skip to content

Fix for FASTAPI unable to record AppService URL (Issue #3654) #3670

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 9 commits into
base: main
Choose a base branch
from

Conversation

rads-1996
Copy link
Contributor

Description

Fixes #3654

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

  • Added tests in both asgi and fastapi instrumentation to ensure the host value is fetched.

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@rads-1996 rads-1996 requested a review from a team as a code owner August 1, 2025 15:07
@rads-1996 rads-1996 marked this pull request as draft August 1, 2025 15:07
@rads-1996
Copy link
Contributor Author

Before Change -

Screenshot 2025-08-01 143500

After Change -

Screenshot 2025-08-01 143511

@rads-1996 rads-1996 marked this pull request as ready for review August 1, 2025 23:16
@rads-1996 rads-1996 requested a review from xrmx August 14, 2025 19:57
Copy link
Member

@emdneto emdneto left a comment

Choose a reason for hiding this comment

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

Left some comments. One thought though is we don't even need the HTTP_URL attribute for server spans even for old semantic convention, but I believe it's another discussion.

http.url is usually not readily available on the server side but would have to be assembled in a cumbersome and sometimes lossy process from other information

"""Test that Host header values are properly decoded from bytes."""
# This test verifies the fix for bytes vs string handling in our implementation
resp = self._client.get(
"/foobar", headers={"host": "bytes-test.example.com"}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"/foobar", headers={"host": "bytes-test.example.com"}
"/foobar", headers={"host": b"bytes-test.example.com"}

am I missing something, or are we supposed to send the value as bytes here?

Copy link
Contributor

Choose a reason for hiding this comment

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

No, it doesn't need to be bytes.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not even sure it accepts bytes? 👀

# Find the server span (the main span, not internal middleware spans)
server_span = None
for span in spans:
if HTTP_URL in span.attributes:
Copy link
Member

Choose a reason for hiding this comment

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

I think will be better if we check if the span kind is the server? Same for other tests

host_value = host_header[0]
# Ensure host_value is a string, not bytes
if isinstance(host_value, bytes):
host_value = host_value.decode("utf-8")
Copy link
Member

Choose a reason for hiding this comment

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

Can we use the already created _decode_header_item helper?

# Current behavior: Host header may not affect server.address in new semantic conventions
# This test documents the current behavior rather than enforcing Host header usage
server_address = server_span.attributes.get("server.address", "")
self.assertIsNotNone(server_address) # Should have some value
Copy link
Member

Choose a reason for hiding this comment

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

Can we assert a more deterministic value?

@Kludex
Copy link
Contributor

Kludex commented Aug 15, 2025

I think the current implementation is correct. I don't think the instrumentation should trust the "Host" header.

@rads-1996
Copy link
Contributor Author

rads-1996 commented Aug 15, 2025

I think the current implementation is correct. I don't think the instrumentation should trust the "Host" header.

But then the IP address appearing in the url field instead of the actual application url address is not very desirable. @Kludex Do you think there is another way to overcome this issue.

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.

FastAPI can fail to record App Service URL
4 participants