Skip to content

fix: Fix missing metric response attributes in HTTPX instrumentation #3615

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

Conversation

diogosilva30
Copy link

@diogosilva30 diogosilva30 commented Jul 4, 2025

Description

This PR fixes an issue where HTTP response attributes (status code, http version) are only recorded in metrics if span are recording. I believe this should not be the case because metrics should not be coupled with tracing. Based myself of requests instrumentation here, where it even states:

Also the decision to include status code on a histogram should
not be dependent on tracing decisions.

Fixes #3614

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Added unit tests for both old and new sem conv
  • Ran script provided in minimum reproducible code and saw after this change behaviour is as expected

Does This PR Require a Core Repo Change?

  • 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

Copy link

linux-foundation-easycla bot commented Jul 4, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@diogosilva30 diogosilva30 requested a review from a team as a code owner July 4, 2025 17:18
@diogosilva30
Copy link
Author

From manual testing this looks good and it's a simple change. Still need to figure out unit test updates tho

@diogosilva30 diogosilva30 marked this pull request as draft July 5, 2025 13:13
@diogosilva30 diogosilva30 changed the title (WIP) fix: Fix missing metric response attributes in HTTPX instrumentation fix: Fix missing metric response attributes in HTTPX instrumentation Jul 5, 2025
@diogosilva30 diogosilva30 marked this pull request as ready for review July 5, 2025 14:35
@diogosilva30
Copy link
Author

Added unit tests for both old and new sem conv ✅
Should be ready for review!

@diogosilva30
Copy link
Author

@xrmx let me know if further changes are required

@xrmx
Copy link
Contributor

xrmx commented Jul 9, 2025

@xrmx let me know if further changes are required

I think you missed this comment #3615 (comment)

@diogosilva30
Copy link
Author

diogosilva30 commented Jul 9, 2025

@xrmx Indeed I've missed, apologize.

If you check previously existing tests those attributes are only asserted when test ends in both_semconv which means its running with opt-in as http/dup. For "default" behaviour (old semantic) only HTTP_STATUS_CODE, HTTP_METHOD and HTTP_SCHEME are added. To have HTTP_FLAVOR, HTTP_HOST , NET_PEER_NAME , NET_PEER_PORT it needs to run in http/dup (both sem conv).

We can see this explanation in original implementation by @emdneto : #2631

Don't know if it makes sense, but it's the current behaviour. I've added a new test case so it covers all 3 scenarios according to logic previously implemented by @emdneto :

  • old sem conv: test_metrics_have_response_attributes_with_disabled_tracing
  • both sem conv: test_metrics_have_response_attributes_with_disabled_tracing_both_semconv
  • new sem conv: test_metrics_have_response_attributes_with_disabled_tracing_new_semconv

@@ -76,6 +87,7 @@


HTTP_RESPONSE_BODY = "http.response.body"
SCHEMA_URL = "https://opentelemetry.io/schemas/1.21.0"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
SCHEMA_URL = "https://opentelemetry.io/schemas/1.21.0"
SCHEMA_URL = Schemas.V1_21_0

Copy link
Author

Choose a reason for hiding this comment

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

Schemas does not have V1_21_0
I added this because I could not find any macro for schema 1.21.0.

Other instrumentations like requests also have this version and they just repeat the raw string https://opentelemetry.io/schemas/1.21.0 in the asserts.

Example: https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py#L201

Copy link
Contributor

@xrmx xrmx Jul 10, 2025

Choose a reason for hiding this comment

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

I've added it right before writing that comment :)

Copy link
Author

Choose a reason for hiding this comment

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

Oh I see your commit open-telemetry/opentelemetry-python@0e4cb44

But not very clear how to upgrade semconv package to include that change. Sorry, first time contributing in this project :)

Copy link
Author

@diogosilva30 diogosilva30 Jul 10, 2025

Choose a reason for hiding this comment

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

Should I re-create uv lock file? That does the trick, but not sure if that's the correct procedure

>>> rm uv.lock
>>> uv sync # new file is generated and semconv package includes new commit from opentelemetry-python repo

Copy link
Author

@diogosilva30 diogosilva30 Jul 10, 2025

Choose a reason for hiding this comment

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

In the meantime, I've pushed your suggestions. Let me know if new lock file should be commited

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah tht should have been Schemas.V1_21_0.value or something, please run tests locally :)

Copy link
Author

@diogosilva30 diogosilva30 Jul 11, 2025

Choose a reason for hiding this comment

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

Fixed and test passing locally ✅ . But could you clarify on the uv lock file question? Interested in understanding the process.
In tests I see it pulls latest version (main branch) of semantic convention repo. But trying to run locally with fresh uv sync install does not pull that version and pulls from commit c9ad4bc2cefc64a918f0cda3cf76c64379c since it's specified in uv lock file.

(opentelemetry-python-contrib) diosil1 % rm -rf .venv .ruff_cache
(opentelemetry-python-contrib) diosil1 % uv sync > /dev/null 2>&1
(opentelemetry-python-contrib) diosil1 % uv run python3
Python 3.12.11 (main, Jul  1 2025, 18:16:32) [Clang 20.1.4 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from opentelemetry.semconv.schemas import Schemas
>>> Schemas.V1_21_0.value
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'Schemas' has no attribute 'V1_21_0'. Did you mean: 'V1_25_0'?

Is this expected in a fresh local install?

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.

Missing HTTP response attributes in HTTPX instrumentation
2 participants