Skip to content

Conversation

@JacksonWeber
Copy link

@JacksonWeber JacksonWeber commented Aug 4, 2025

Description

This pull request introduces synthetic user agent detection to OpenTelemetry Python instrumentation for HTTP requests, ASGI, and WSGI. The main goal is to automatically identify and annotate telemetry data with a synthetic type (such as "bot" or "test") based on the request's user agent string. This helps distinguish traffic from bots, monitoring systems, and tests versus real user traffic. The changes include updates to core instrumentation logic and comprehensive new test coverage.

Synthetic user agent detection and annotation:

  • Added logic to detect synthetic user agents (e.g., bots like Googlebot/Bingbot and test agents like AlwaysOn) in opentelemetry-instrumentation-requests, opentelemetry-instrumentation-asgi, and opentelemetry-instrumentation-wsgi. When detected, the span attribute USER_AGENT_SYNTHETIC_TYPE is set accordingly. [1] [2] [3]
  • Imported the new semantic convention attribute USER_AGENT_SYNTHETIC_TYPE and detection utility in the relevant modules. [1] [2] [3] [4] [5] [6]

Testing and validation:

  • Added a comprehensive test suite for synthetic user agent detection in requests instrumentation, covering bots, test agents, case insensitivity, normal browsers, and priority rules.
  • Expanded ASGI middleware tests to validate synthetic user agent detection for bots, test agents, normal agents, and new semantic conventions.

Test infrastructure updates:

  • Added necessary imports and setup for synthetic user agent attribute in ASGI and WSGI middleware tests. [1] [2]
  • Minor test class annotation for WSGI attribute tests.
    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?

Tested via unit tests included in test_user_agent_synthetic.py

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

@JacksonWeber JacksonWeber requested a review from a team as a code owner August 4, 2025 23:48
@JacksonWeber JacksonWeber requested a review from rads-1996 August 21, 2025 18:05
Copy link
Contributor

@xrmx xrmx left a comment

Choose a reason for hiding this comment

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

Since there's nothing requests specific here I think this should go into opentelemetry-util-http instead. Said that I'm not sure we should ship our own experimental semantic conventions. I don't see any PR in the semantic-conventions repo adding this stuff, so could you please elaborate a bit what's your plan?

@xrmx xrmx moved this to Reviewed PR that needs fixing in @xrmx's Python PR digest Aug 22, 2025
@xrmx
Copy link
Contributor

xrmx commented Aug 23, 2025

Since there's nothing requests specific here I think this should go into opentelemetry-util-http instead. Said that I'm not sure we should ship our own experimental semantic conventions. I don't see any PR in the semantic-conventions repo adding this stuff, so could you please elaborate a bit what's your plan?

So I had seen this PR in semconv repo open-telemetry/semantic-conventions#1523 before writing that comment but I only read the PR description with an old attribute name and not the title with the updated one that matches this. So since we already have the attribute in the semconv since a few (https://github.com/open-telemetry/opentelemetry-python/blob/05343a5c8848f5f55a69100a0becf61766b33051/opentelemetry-semantic-conventions/src/opentelemetry/semconv/_incubating/attributes/user_agent_attributes.py#L41) we should just import it from there and not opencode it.

@JacksonWeber
Copy link
Author

Since there's nothing requests specific here I think this should go into opentelemetry-util-http instead. Said that I'm not sure we should ship our own experimental semantic conventions. I don't see any PR in the semantic-conventions repo adding this stuff, so could you please elaborate a bit what's your plan?

  1. This PR does only add this functionality to requests, however I'm happy to move these changes over to opentelemetry-util-http if that'd be more appropriate
  2. Addressing this on your other comment.

@JacksonWeber
Copy link
Author

Since there's nothing requests specific here I think this should go into opentelemetry-util-http instead. Said that I'm not sure we should ship our own experimental semantic conventions. I don't see any PR in the semantic-conventions repo adding this stuff, so could you please elaborate a bit what's your plan?

So I had seen this PR in semconv repo open-telemetry/semantic-conventions#1523 before writing that comment but I only read the PR description with an old attribute name and not the title with the updated one that matches this. So since we already have the attribute in the semconv since a few (https://github.com/open-telemetry/opentelemetry-python/blob/05343a5c8848f5f55a69100a0becf61766b33051/opentelemetry-semantic-conventions/src/opentelemetry/semconv/_incubating/attributes/user_agent_attributes.py#L41) we should just import it from there and not opencode it.

Thanks for the point out here, the guidance in OTel JS works a bit differently (they ask to hard-code these kinds of experimental semantic conventions in order to avoid breaking customers using the old experimental attributes). I'll update these imports.

@JacksonWeber JacksonWeber requested a review from xrmx August 25, 2025 18:33
@JacksonWeber JacksonWeber requested a review from xrmx September 17, 2025 21:55
@xrmx xrmx moved this from Easy to review / merge / close to Ready for review in @xrmx's Python PR digest Oct 14, 2025
@JacksonWeber JacksonWeber requested a review from lzchen November 1, 2025 00:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Ready for review

Development

Successfully merging this pull request may close these issues.

4 participants