|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import httpretty |
| 16 | +import requests |
| 17 | + |
| 18 | +from opentelemetry.instrumentation.requests import RequestsInstrumentor |
| 19 | +from opentelemetry.instrumentation.requests.semconv import ( |
| 20 | + ATTR_USER_AGENT_SYNTHETIC_TYPE, |
| 21 | + USER_AGENT_SYNTHETIC_TYPE_VALUE_BOT, |
| 22 | + USER_AGENT_SYNTHETIC_TYPE_VALUE_TEST, |
| 23 | +) |
| 24 | +from opentelemetry.test.test_base import TestBase |
| 25 | + |
| 26 | + |
| 27 | +class TestUserAgentSynthetic(TestBase): |
| 28 | + URL = "http://mock/status/200" |
| 29 | + |
| 30 | + def setUp(self): |
| 31 | + super().setUp() |
| 32 | + RequestsInstrumentor().instrument() |
| 33 | + httpretty.enable() |
| 34 | + httpretty.register_uri(httpretty.GET, self.URL, body="Hello!") |
| 35 | + |
| 36 | + def tearDown(self): |
| 37 | + super().tearDown() |
| 38 | + RequestsInstrumentor().uninstrument() |
| 39 | + httpretty.disable() |
| 40 | + |
| 41 | + def assert_span(self, num_spans=1): |
| 42 | + span_list = self.memory_exporter.get_finished_spans() |
| 43 | + self.assertEqual(num_spans, len(span_list)) |
| 44 | + if num_spans == 0: |
| 45 | + return None |
| 46 | + if num_spans == 1: |
| 47 | + return span_list[0] |
| 48 | + return span_list |
| 49 | + |
| 50 | + def test_user_agent_bot_googlebot(self): |
| 51 | + """Test that googlebot user agent is marked as 'bot'""" |
| 52 | + headers = { |
| 53 | + "User-Agent": "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" |
| 54 | + } |
| 55 | + requests.get(self.URL, headers=headers, timeout=5) |
| 56 | + |
| 57 | + span = self.assert_span() |
| 58 | + self.assertEqual( |
| 59 | + span.attributes.get(ATTR_USER_AGENT_SYNTHETIC_TYPE), |
| 60 | + USER_AGENT_SYNTHETIC_TYPE_VALUE_BOT, |
| 61 | + ) |
| 62 | + |
| 63 | + def test_user_agent_bot_bingbot(self): |
| 64 | + """Test that bingbot user agent is marked as 'bot'""" |
| 65 | + headers = { |
| 66 | + "User-Agent": "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)" |
| 67 | + } |
| 68 | + requests.get(self.URL, headers=headers, timeout=5) |
| 69 | + |
| 70 | + span = self.assert_span() |
| 71 | + self.assertEqual( |
| 72 | + span.attributes.get(ATTR_USER_AGENT_SYNTHETIC_TYPE), |
| 73 | + USER_AGENT_SYNTHETIC_TYPE_VALUE_BOT, |
| 74 | + ) |
| 75 | + |
| 76 | + def test_user_agent_test_alwayson(self): |
| 77 | + """Test that alwayson user agent is marked as 'test'""" |
| 78 | + headers = {"User-Agent": "AlwaysOn-Monitor/1.0"} |
| 79 | + requests.get(self.URL, headers=headers, timeout=5) |
| 80 | + |
| 81 | + span = self.assert_span() |
| 82 | + self.assertEqual( |
| 83 | + span.attributes.get(ATTR_USER_AGENT_SYNTHETIC_TYPE), |
| 84 | + USER_AGENT_SYNTHETIC_TYPE_VALUE_TEST, |
| 85 | + ) |
| 86 | + |
| 87 | + def test_user_agent_case_insensitive(self): |
| 88 | + """Test that detection is case insensitive""" |
| 89 | + headers = {"User-Agent": "GOOGLEBOT/2.1"} |
| 90 | + requests.get(self.URL, headers=headers, timeout=5) |
| 91 | + |
| 92 | + span = self.assert_span() |
| 93 | + self.assertEqual( |
| 94 | + span.attributes.get(ATTR_USER_AGENT_SYNTHETIC_TYPE), |
| 95 | + USER_AGENT_SYNTHETIC_TYPE_VALUE_BOT, |
| 96 | + ) |
| 97 | + |
| 98 | + self.memory_exporter.clear() |
| 99 | + |
| 100 | + headers = {"User-Agent": "ALWAYSON-Monitor/1.0"} |
| 101 | + requests.get(self.URL, headers=headers, timeout=5) |
| 102 | + |
| 103 | + span = self.assert_span() |
| 104 | + self.assertEqual( |
| 105 | + span.attributes.get(ATTR_USER_AGENT_SYNTHETIC_TYPE), |
| 106 | + USER_AGENT_SYNTHETIC_TYPE_VALUE_TEST, |
| 107 | + ) |
| 108 | + |
| 109 | + def test_user_agent_normal_browser(self): |
| 110 | + """Test that normal browser user agents don't get synthetic type""" |
| 111 | + headers = { |
| 112 | + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" |
| 113 | + } |
| 114 | + requests.get(self.URL, headers=headers, timeout=5) |
| 115 | + |
| 116 | + span = self.assert_span() |
| 117 | + self.assertNotIn(ATTR_USER_AGENT_SYNTHETIC_TYPE, span.attributes) |
| 118 | + |
| 119 | + def test_no_user_agent_header(self): |
| 120 | + """Test that requests without user agent don't get synthetic type""" |
| 121 | + requests.get(self.URL, timeout=5) |
| 122 | + |
| 123 | + span = self.assert_span() |
| 124 | + self.assertNotIn(ATTR_USER_AGENT_SYNTHETIC_TYPE, span.attributes) |
| 125 | + |
| 126 | + def test_empty_user_agent_header(self): |
| 127 | + """Test that empty user agent doesn't get synthetic type""" |
| 128 | + headers = {"User-Agent": ""} |
| 129 | + requests.get(self.URL, headers=headers, timeout=5) |
| 130 | + |
| 131 | + span = self.assert_span() |
| 132 | + self.assertNotIn(ATTR_USER_AGENT_SYNTHETIC_TYPE, span.attributes) |
| 133 | + |
| 134 | + def test_user_agent_substring_match(self): |
| 135 | + """Test that substrings are detected correctly""" |
| 136 | + # Test googlebot in middle of string |
| 137 | + headers = {"User-Agent": "MyApp/1.0 googlebot crawler"} |
| 138 | + requests.get(self.URL, headers=headers, timeout=5) |
| 139 | + |
| 140 | + span = self.assert_span() |
| 141 | + self.assertEqual( |
| 142 | + span.attributes.get(ATTR_USER_AGENT_SYNTHETIC_TYPE), |
| 143 | + USER_AGENT_SYNTHETIC_TYPE_VALUE_BOT, |
| 144 | + ) |
| 145 | + |
| 146 | + self.memory_exporter.clear() |
| 147 | + |
| 148 | + # Test alwayson in middle of string |
| 149 | + headers = {"User-Agent": "TestFramework/1.0 alwayson monitoring"} |
| 150 | + requests.get(self.URL, headers=headers, timeout=5) |
| 151 | + |
| 152 | + span = self.assert_span() |
| 153 | + self.assertEqual( |
| 154 | + span.attributes.get(ATTR_USER_AGENT_SYNTHETIC_TYPE), |
| 155 | + USER_AGENT_SYNTHETIC_TYPE_VALUE_TEST, |
| 156 | + ) |
| 157 | + |
| 158 | + def test_user_agent_priority_alwayson_over_bot(self): |
| 159 | + """Test that alwayson takes priority if both patterns match""" |
| 160 | + headers = {"User-Agent": "alwayson-googlebot/1.0"} |
| 161 | + requests.get(self.URL, headers=headers, timeout=5) |
| 162 | + |
| 163 | + span = self.assert_span() |
| 164 | + # alwayson should be checked first and return 'test' |
| 165 | + self.assertEqual( |
| 166 | + span.attributes.get(ATTR_USER_AGENT_SYNTHETIC_TYPE), |
| 167 | + USER_AGENT_SYNTHETIC_TYPE_VALUE_TEST, |
| 168 | + ) |
0 commit comments