-
Notifications
You must be signed in to change notification settings - Fork 761
Add Support for Detecting Synthetic Source #3674
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
JacksonWeber
wants to merge
5
commits into
open-telemetry:main
Choose a base branch
from
JacksonWeber:jacksonweber/populate-synthetic-attributes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+299
−1
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6fb98f5
Add support for detecting synthetic source.
JacksonWeber 24537f6
Update CHANGELOG.md
JacksonWeber 10353ad
Update __init__.py
JacksonWeber 8cfb235
Move const values to a constants file.
JacksonWeber 9db1219
Merge branch 'main' into jacksonweber/populate-synthetic-attributes
JacksonWeber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...ntelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/semconv.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Copyright The OpenTelemetry Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
""" | ||
Semantic conventions for user agent synthetic type detection. | ||
|
||
This module defines constants for user agent synthetic type attributes and values | ||
according to OpenTelemetry semantic conventions. | ||
|
||
**EXPERIMENTAL**: This module contains experimental semantic conventions that are not | ||
yet part of the official OpenTelemetry semantic conventions specification. These | ||
attributes and values may experience breaking changes in future versions as the | ||
specification evolves. | ||
|
||
The semantic conventions defined here are used to classify synthetic traffic based | ||
on User-Agent header analysis, helping distinguish between: | ||
- Bot traffic (web crawlers, search engine bots) | ||
- Test traffic (monitoring systems, health checks) | ||
- Regular user traffic | ||
|
||
These experimental conventions may be: | ||
1. Modified with different attribute names or values | ||
2. Moved to official semantic convention packages | ||
3. Deprecated in favor of standardized alternatives | ||
4. Changed based on community feedback and specification updates | ||
|
||
Users should be prepared for potential breaking changes when upgrading and should | ||
monitor OpenTelemetry specification updates for official semantic convention releases. | ||
""" | ||
|
||
# User agent synthetic type attribute | ||
ATTR_USER_AGENT_SYNTHETIC_TYPE = "user_agent.synthetic.type" | ||
|
||
# User agent synthetic type values | ||
USER_AGENT_SYNTHETIC_TYPE_VALUE_BOT = "bot" | ||
USER_AGENT_SYNTHETIC_TYPE_VALUE_TEST = "test" |
168 changes: 168 additions & 0 deletions
168
instrumentation/opentelemetry-instrumentation-requests/tests/test_user_agent_synthetic.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
# Copyright The OpenTelemetry Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import httpretty | ||
import requests | ||
|
||
from opentelemetry.instrumentation.requests import RequestsInstrumentor | ||
from opentelemetry.instrumentation.requests.semconv import ( | ||
ATTR_USER_AGENT_SYNTHETIC_TYPE, | ||
USER_AGENT_SYNTHETIC_TYPE_VALUE_BOT, | ||
USER_AGENT_SYNTHETIC_TYPE_VALUE_TEST, | ||
) | ||
from opentelemetry.test.test_base import TestBase | ||
|
||
|
||
class TestUserAgentSynthetic(TestBase): | ||
URL = "http://mock/status/200" | ||
|
||
def setUp(self): | ||
super().setUp() | ||
RequestsInstrumentor().instrument() | ||
httpretty.enable() | ||
httpretty.register_uri(httpretty.GET, self.URL, body="Hello!") | ||
|
||
def tearDown(self): | ||
super().tearDown() | ||
RequestsInstrumentor().uninstrument() | ||
httpretty.disable() | ||
|
||
def assert_span(self, num_spans=1): | ||
span_list = self.memory_exporter.get_finished_spans() | ||
self.assertEqual(num_spans, len(span_list)) | ||
if num_spans == 0: | ||
return None | ||
if num_spans == 1: | ||
return span_list[0] | ||
return span_list | ||
|
||
def test_user_agent_bot_googlebot(self): | ||
"""Test that googlebot user agent is marked as 'bot'""" | ||
headers = { | ||
"User-Agent": "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" | ||
} | ||
requests.get(self.URL, headers=headers, timeout=5) | ||
|
||
span = self.assert_span() | ||
self.assertEqual( | ||
span.attributes.get(ATTR_USER_AGENT_SYNTHETIC_TYPE), | ||
USER_AGENT_SYNTHETIC_TYPE_VALUE_BOT, | ||
) | ||
|
||
def test_user_agent_bot_bingbot(self): | ||
"""Test that bingbot user agent is marked as 'bot'""" | ||
headers = { | ||
"User-Agent": "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)" | ||
} | ||
requests.get(self.URL, headers=headers, timeout=5) | ||
|
||
span = self.assert_span() | ||
self.assertEqual( | ||
span.attributes.get(ATTR_USER_AGENT_SYNTHETIC_TYPE), | ||
USER_AGENT_SYNTHETIC_TYPE_VALUE_BOT, | ||
) | ||
|
||
def test_user_agent_test_alwayson(self): | ||
"""Test that alwayson user agent is marked as 'test'""" | ||
headers = {"User-Agent": "AlwaysOn-Monitor/1.0"} | ||
requests.get(self.URL, headers=headers, timeout=5) | ||
|
||
span = self.assert_span() | ||
self.assertEqual( | ||
span.attributes.get(ATTR_USER_AGENT_SYNTHETIC_TYPE), | ||
USER_AGENT_SYNTHETIC_TYPE_VALUE_TEST, | ||
) | ||
|
||
def test_user_agent_case_insensitive(self): | ||
"""Test that detection is case insensitive""" | ||
headers = {"User-Agent": "GOOGLEBOT/2.1"} | ||
requests.get(self.URL, headers=headers, timeout=5) | ||
|
||
span = self.assert_span() | ||
self.assertEqual( | ||
span.attributes.get(ATTR_USER_AGENT_SYNTHETIC_TYPE), | ||
USER_AGENT_SYNTHETIC_TYPE_VALUE_BOT, | ||
) | ||
|
||
self.memory_exporter.clear() | ||
|
||
headers = {"User-Agent": "ALWAYSON-Monitor/1.0"} | ||
requests.get(self.URL, headers=headers, timeout=5) | ||
|
||
span = self.assert_span() | ||
self.assertEqual( | ||
span.attributes.get(ATTR_USER_AGENT_SYNTHETIC_TYPE), | ||
USER_AGENT_SYNTHETIC_TYPE_VALUE_TEST, | ||
) | ||
|
||
def test_user_agent_normal_browser(self): | ||
"""Test that normal browser user agents don't get synthetic type""" | ||
headers = { | ||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" | ||
} | ||
requests.get(self.URL, headers=headers, timeout=5) | ||
|
||
span = self.assert_span() | ||
self.assertNotIn(ATTR_USER_AGENT_SYNTHETIC_TYPE, span.attributes) | ||
|
||
def test_no_user_agent_header(self): | ||
"""Test that requests without user agent don't get synthetic type""" | ||
requests.get(self.URL, timeout=5) | ||
|
||
span = self.assert_span() | ||
self.assertNotIn(ATTR_USER_AGENT_SYNTHETIC_TYPE, span.attributes) | ||
|
||
def test_empty_user_agent_header(self): | ||
"""Test that empty user agent doesn't get synthetic type""" | ||
headers = {"User-Agent": ""} | ||
requests.get(self.URL, headers=headers, timeout=5) | ||
|
||
span = self.assert_span() | ||
self.assertNotIn(ATTR_USER_AGENT_SYNTHETIC_TYPE, span.attributes) | ||
|
||
def test_user_agent_substring_match(self): | ||
"""Test that substrings are detected correctly""" | ||
# Test googlebot in middle of string | ||
headers = {"User-Agent": "MyApp/1.0 googlebot crawler"} | ||
requests.get(self.URL, headers=headers, timeout=5) | ||
|
||
span = self.assert_span() | ||
self.assertEqual( | ||
span.attributes.get(ATTR_USER_AGENT_SYNTHETIC_TYPE), | ||
USER_AGENT_SYNTHETIC_TYPE_VALUE_BOT, | ||
) | ||
|
||
self.memory_exporter.clear() | ||
|
||
# Test alwayson in middle of string | ||
headers = {"User-Agent": "TestFramework/1.0 alwayson monitoring"} | ||
requests.get(self.URL, headers=headers, timeout=5) | ||
|
||
span = self.assert_span() | ||
self.assertEqual( | ||
span.attributes.get(ATTR_USER_AGENT_SYNTHETIC_TYPE), | ||
USER_AGENT_SYNTHETIC_TYPE_VALUE_TEST, | ||
) | ||
|
||
def test_user_agent_priority_alwayson_over_bot(self): | ||
JacksonWeber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Test that alwayson takes priority if both patterns match""" | ||
headers = {"User-Agent": "alwayson-googlebot/1.0"} | ||
requests.get(self.URL, headers=headers, timeout=5) | ||
|
||
span = self.assert_span() | ||
# alwayson should be checked first and return 'test' | ||
self.assertEqual( | ||
span.attributes.get(ATTR_USER_AGENT_SYNTHETIC_TYPE), | ||
USER_AGENT_SYNTHETIC_TYPE_VALUE_TEST, | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.