Skip to content

Add optional http.client.response.body.size span attribute & metric to Aiohttp-client instrumentation. - #4572

Open
weizhikuan wants to merge 6 commits into
open-telemetry:mainfrom
weizhikuan:i3623
Open

Add optional http.client.response.body.size span attribute & metric to Aiohttp-client instrumentation.#4572
weizhikuan wants to merge 6 commits into
open-telemetry:mainfrom
weizhikuan:i3623

Conversation

@weizhikuan

@weizhikuan weizhikuan commented May 12, 2026

Copy link
Copy Markdown

Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.

Fixes #3623

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?

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.

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

@weizhikuan
weizhikuan requested a review from a team as a code owner May 12, 2026 03:34
@github-actions
github-actions Bot requested a review from herin049 May 12, 2026 03:34
@weizhikuan

Copy link
Copy Markdown
Author

As per the semconv spec v1.41.0, the span attribute and metric are both optional. So I added both of them and only enabled when semconv stability contains http and the env var OTEL_PYTHON_INSTRUMENTATION_HTTP_RESPONSE_BODY_SIZE is set as true.

@emdneto

emdneto commented May 12, 2026

Copy link
Copy Markdown
Member

Thanks for the PR!

Just a heads-up: we no longer update CHANGELOG.md directly. The changelog is now generated from changelog fragments using Towncrier.

Please add the appropriate changelog fragment for this change instead of editing CHANGELOG.md manually. You can find the instructions and expected format in CONTRIBUTING.md.

@tammy-baylis-swi

Copy link
Copy Markdown
Contributor

Thank you for this @weizhikuan . The way to opt into this makes sense. Please do a quick tox -e precommit to fix some formatting.

@weizhikuan

weizhikuan commented May 14, 2026

Copy link
Copy Markdown
Author

Thanks for the PR!

Just a heads-up: we no longer update CHANGELOG.md directly. The changelog is now generated from changelog fragments using Towncrier.

Please add the appropriate changelog fragment for this change instead of editing CHANGELOG.md manually. You can find the instructions and expected format in CONTRIBUTING.md.

Gotcha, will add the fragment.

@aabmass aabmass moved this to Ready for review in Python PR digest May 14, 2026
****************************
To capture the ``http.response.body.size`` span attribute and record the
``http.client.response.body.size`` metric histogram, set the environment variable
``OTEL_PYTHON_INSTRUMENTATION_HTTP_RESPONSE_BODY_SIZE`` to ``"true"``.

@herin049 herin049 May 17, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Personally, I don't think that having an opt-in is really needed here. From my understanding, when the Spec states that a metric or attribute is "optional", it usually indicates that library authors can optionally add it - not necessarily that users need the ability to enable/disable it. Typically, the spec will explicitly state if behavior should be opt-in only.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same understanding here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

So... this is suggesting removing the env var and have this enabled directly? But this is what I get from the spec for the definition of 'Opt-In' level requirement, which sounds requesting such attribute cannot be emitted if user s did not explicitly enable.

@herin049 herin049 moved this from Ready for review to Reviewed PRs that need fixes in Python PR digest May 17, 2026
):
return

trace_config_ctx.start_time = default_timer()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should probably add the following, just to ensure that the response body size doesn't accidentally leak into another request (e.g. if follow-up requests don't end up setting the attribute)

Suggested change
trace_config_ctx.start_time = default_timer()
trace_config_ctx.response_body_size = None

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for pointing this out. Just trying to understand the issue better. From my understanding, the aiohttp will create a new trace_config_ctx per request via the factory function. Why and how could it gets leaked to another request?

@weizhikuan
weizhikuan requested a review from herin049 May 20, 2026 03:34
@github-actions

github-actions Bot commented Jun 3, 2026

Copy link
Copy Markdown

This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment.
If you're still working on this, please add a comment or push new commits.

@github-actions github-actions Bot added the Stale label Jun 3, 2026
@weizhikuan

Copy link
Copy Markdown
Author

Hello @herin049, may I get another look for the review comment discussion? Thanks!

@github-actions

Copy link
Copy Markdown

This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment.
If you're still working on this, please add a comment or push new commits.

@github-actions github-actions Bot added the Stale label Jun 19, 2026
@github-actions github-actions Bot removed the Stale label Jun 20, 2026
@github-actions

github-actions Bot commented Jul 5, 2026

Copy link
Copy Markdown

This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment.
If you're still working on this, please add a comment or push new commits.

@github-actions github-actions Bot added the Stale label Jul 5, 2026
@github-actions

Copy link
Copy Markdown

This PR has been closed due to inactivity. Please reopen if you would like to continue working on it.

@github-actions github-actions Bot closed this Jul 20, 2026
@github-project-automation github-project-automation Bot moved this from Reviewed PRs that need fixes to Done in Python PR digest Jul 20, 2026
@aabmass aabmass reopened this Aug 3, 2026
@aabmass
aabmass requested a review from Copilot August 3, 2026 20:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR extends opentelemetry-instrumentation-aiohttp-client to optionally emit the new HTTP semantic convention response body size signal: it sets the http.response.body.size span attribute and records the http.client.response.body.size histogram metric when explicitly enabled via an environment variable (and when new HTTP semconv is active).

Changes:

  • Add a shared util-http env var toggle (OTEL_PYTHON_INSTRUMENTATION_HTTP_RESPONSE_BODY_SIZE) and helper to check it.
  • In aiohttp-client instrumentation, when enabled + new semconv active, populate http.response.body.size from Content-Length and record the http.client.response.body.size metric.
  • Add integration tests covering default-off behavior, enabled behavior, and semconv gating; add a changelog entry.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
util/opentelemetry-util-http/src/opentelemetry/util/http/init.py Adds env var constant + helper to opt-in to response body size capture.
instrumentation/opentelemetry-instrumentation-aiohttp-client/src/opentelemetry/instrumentation/aiohttp_client/init.py Implements optional http.response.body.size span attribute + http.client.response.body.size metric recording based on Content-Length.
instrumentation/opentelemetry-instrumentation-aiohttp-client/tests/test_aiohttp_client_integration.py Adds tests for span attribute presence/default-off, semconv gating, and metric emission when enabled.
.changelog/4572.added Documents the new optional span attribute + metric for aiohttp-client instrumentation.

Comment thread .changelog/4572.added
@@ -0,0 +1 @@
`opentelemetry-instrumentation-aiohttp-client`: add optional `http.client.response.body.size` span attribute & metric to the aiohttp client instrumentation
Comment on lines +1652 to +1655
span = self._assert_spans(1)
self.assertIn(HTTP_RESPONSE_BODY_SIZE, span.attributes)
self.assertIsInstance(span.attributes[HTTP_RESPONSE_BODY_SIZE], int)

Comment on lines +1667 to +1679
run_with_test_server(
self.get_default_request(), self.URL, self.handler_with_body
)
metrics = self._assert_metrics(2)
metric_names = {m.name for m in metrics}
self.assertIn("http.client.response.body.size", metric_names)
body_size_metric = next(
m for m in metrics if m.name == "http.client.response.body.size"
)
data_point = body_size_metric.data.data_points[0]
self.assertEqual(data_point.count, 1)
self.assertTrue(data_point.sum > 0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

aiohttp-client add http.response.body.size span attribute

6 participants