Skip to content

Commit 4ab76d3

Browse files
committed
use existing sem conv.
1 parent 2672bd5 commit 4ab76d3

File tree

3 files changed

+31
-80
lines changed

3 files changed

+31
-80
lines changed

instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,18 +129,17 @@ def response_hook(span, request_obj, response):
129129
TEST_PATTERNS,
130130
)
131131
from opentelemetry.instrumentation.requests.package import _instruments
132-
from opentelemetry.instrumentation.requests.semconv import (
133-
ATTR_USER_AGENT_SYNTHETIC_TYPE,
134-
USER_AGENT_SYNTHETIC_TYPE_VALUE_BOT,
135-
USER_AGENT_SYNTHETIC_TYPE_VALUE_TEST,
136-
)
137132
from opentelemetry.instrumentation.requests.version import __version__
138133
from opentelemetry.instrumentation.utils import (
139134
is_http_instrumentation_enabled,
140135
suppress_http_instrumentation,
141136
)
142137
from opentelemetry.metrics import Histogram, get_meter
143138
from opentelemetry.propagate import inject
139+
from opentelemetry.semconv._incubating.attributes.user_agent_attributes import (
140+
USER_AGENT_SYNTHETIC_TYPE,
141+
UserAgentSyntheticTypeValues,
142+
)
144143
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
145144
from opentelemetry.semconv.attributes.network_attributes import (
146145
NETWORK_PEER_ADDRESS,
@@ -175,8 +174,8 @@ def _detect_synthetic_user_agent(user_agent: str) -> Optional[str]:
175174
user_agent: The user agent string to analyze
176175
177176
Returns:
178-
USER_AGENT_SYNTHETIC_TYPE_VALUE_TEST if user agent contains any pattern from TEST_PATTERNS
179-
USER_AGENT_SYNTHETIC_TYPE_VALUE_BOT if user agent contains any pattern from BOT_PATTERNS
177+
UserAgentSyntheticTypeValues.TEST if user agent contains any pattern from TEST_PATTERNS
178+
UserAgentSyntheticTypeValues.BOT if user agent contains any pattern from BOT_PATTERNS
180179
None otherwise
181180
182181
Note: Test patterns take priority over bot patterns.
@@ -187,9 +186,9 @@ def _detect_synthetic_user_agent(user_agent: str) -> Optional[str]:
187186
user_agent_lower = user_agent.lower()
188187

189188
if any(test_pattern in user_agent_lower for test_pattern in TEST_PATTERNS):
190-
return USER_AGENT_SYNTHETIC_TYPE_VALUE_TEST
189+
return UserAgentSyntheticTypeValues.TEST.value
191190
if any(bot_pattern in user_agent_lower for bot_pattern in BOT_PATTERNS):
192-
return USER_AGENT_SYNTHETIC_TYPE_VALUE_BOT
191+
return UserAgentSyntheticTypeValues.BOT.value
193192

194193
return None
195194

@@ -286,7 +285,7 @@ def get_or_create_headers():
286285
user_agent = headers.get("User-Agent")
287286
synthetic_type = _detect_synthetic_user_agent(user_agent)
288287
if synthetic_type:
289-
span_attributes[ATTR_USER_AGENT_SYNTHETIC_TYPE] = synthetic_type
288+
span_attributes[USER_AGENT_SYNTHETIC_TYPE] = synthetic_type
290289

291290
metric_labels = {}
292291
_set_http_method(

instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/semconv.py

Lines changed: 0 additions & 47 deletions
This file was deleted.

instrumentation/opentelemetry-instrumentation-requests/tests/test_user_agent_synthetic.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
import requests
1717

1818
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,
19+
from opentelemetry.semconv._incubating.attributes.user_agent_attributes import (
20+
USER_AGENT_SYNTHETIC_TYPE,
21+
UserAgentSyntheticTypeValues,
2322
)
2423
from opentelemetry.test.test_base import TestBase
2524

@@ -56,8 +55,8 @@ def test_user_agent_bot_googlebot(self):
5655

5756
span = self.assert_span()
5857
self.assertEqual(
59-
span.attributes.get(ATTR_USER_AGENT_SYNTHETIC_TYPE),
60-
USER_AGENT_SYNTHETIC_TYPE_VALUE_BOT,
58+
span.attributes.get(USER_AGENT_SYNTHETIC_TYPE),
59+
UserAgentSyntheticTypeValues.BOT.value,
6160
)
6261

6362
def test_user_agent_bot_bingbot(self):
@@ -69,8 +68,8 @@ def test_user_agent_bot_bingbot(self):
6968

7069
span = self.assert_span()
7170
self.assertEqual(
72-
span.attributes.get(ATTR_USER_AGENT_SYNTHETIC_TYPE),
73-
USER_AGENT_SYNTHETIC_TYPE_VALUE_BOT,
71+
span.attributes.get(USER_AGENT_SYNTHETIC_TYPE),
72+
UserAgentSyntheticTypeValues.BOT.value,
7473
)
7574

7675
def test_user_agent_test_alwayson(self):
@@ -80,8 +79,8 @@ def test_user_agent_test_alwayson(self):
8079

8180
span = self.assert_span()
8281
self.assertEqual(
83-
span.attributes.get(ATTR_USER_AGENT_SYNTHETIC_TYPE),
84-
USER_AGENT_SYNTHETIC_TYPE_VALUE_TEST,
82+
span.attributes.get(USER_AGENT_SYNTHETIC_TYPE),
83+
UserAgentSyntheticTypeValues.TEST.value,
8584
)
8685

8786
def test_user_agent_case_insensitive(self):
@@ -91,8 +90,8 @@ def test_user_agent_case_insensitive(self):
9190

9291
span = self.assert_span()
9392
self.assertEqual(
94-
span.attributes.get(ATTR_USER_AGENT_SYNTHETIC_TYPE),
95-
USER_AGENT_SYNTHETIC_TYPE_VALUE_BOT,
93+
span.attributes.get(USER_AGENT_SYNTHETIC_TYPE),
94+
UserAgentSyntheticTypeValues.BOT.value,
9695
)
9796

9897
self.memory_exporter.clear()
@@ -102,8 +101,8 @@ def test_user_agent_case_insensitive(self):
102101

103102
span = self.assert_span()
104103
self.assertEqual(
105-
span.attributes.get(ATTR_USER_AGENT_SYNTHETIC_TYPE),
106-
USER_AGENT_SYNTHETIC_TYPE_VALUE_TEST,
104+
span.attributes.get(USER_AGENT_SYNTHETIC_TYPE),
105+
UserAgentSyntheticTypeValues.TEST.value,
107106
)
108107

109108
def test_user_agent_normal_browser(self):
@@ -114,22 +113,22 @@ def test_user_agent_normal_browser(self):
114113
requests.get(self.URL, headers=headers, timeout=5)
115114

116115
span = self.assert_span()
117-
self.assertNotIn(ATTR_USER_AGENT_SYNTHETIC_TYPE, span.attributes)
116+
self.assertNotIn(USER_AGENT_SYNTHETIC_TYPE, span.attributes)
118117

119118
def test_no_user_agent_header(self):
120119
"""Test that requests without user agent don't get synthetic type"""
121120
requests.get(self.URL, timeout=5)
122121

123122
span = self.assert_span()
124-
self.assertNotIn(ATTR_USER_AGENT_SYNTHETIC_TYPE, span.attributes)
123+
self.assertNotIn(USER_AGENT_SYNTHETIC_TYPE, span.attributes)
125124

126125
def test_empty_user_agent_header(self):
127126
"""Test that empty user agent doesn't get synthetic type"""
128127
headers = {"User-Agent": ""}
129128
requests.get(self.URL, headers=headers, timeout=5)
130129

131130
span = self.assert_span()
132-
self.assertNotIn(ATTR_USER_AGENT_SYNTHETIC_TYPE, span.attributes)
131+
self.assertNotIn(USER_AGENT_SYNTHETIC_TYPE, span.attributes)
133132

134133
def test_user_agent_substring_match(self):
135134
"""Test that substrings are detected correctly"""
@@ -139,8 +138,8 @@ def test_user_agent_substring_match(self):
139138

140139
span = self.assert_span()
141140
self.assertEqual(
142-
span.attributes.get(ATTR_USER_AGENT_SYNTHETIC_TYPE),
143-
USER_AGENT_SYNTHETIC_TYPE_VALUE_BOT,
141+
span.attributes.get(USER_AGENT_SYNTHETIC_TYPE),
142+
UserAgentSyntheticTypeValues.BOT.value,
144143
)
145144

146145
self.memory_exporter.clear()
@@ -151,8 +150,8 @@ def test_user_agent_substring_match(self):
151150

152151
span = self.assert_span()
153152
self.assertEqual(
154-
span.attributes.get(ATTR_USER_AGENT_SYNTHETIC_TYPE),
155-
USER_AGENT_SYNTHETIC_TYPE_VALUE_TEST,
153+
span.attributes.get(USER_AGENT_SYNTHETIC_TYPE),
154+
UserAgentSyntheticTypeValues.TEST.value,
156155
)
157156

158157
def test_user_agent_priority_alwayson_over_bot(self):
@@ -163,6 +162,6 @@ def test_user_agent_priority_alwayson_over_bot(self):
163162
span = self.assert_span()
164163
# alwayson should be checked first and return 'test'
165164
self.assertEqual(
166-
span.attributes.get(ATTR_USER_AGENT_SYNTHETIC_TYPE),
167-
USER_AGENT_SYNTHETIC_TYPE_VALUE_TEST,
165+
span.attributes.get(USER_AGENT_SYNTHETIC_TYPE),
166+
UserAgentSyntheticTypeValues.TEST.value,
168167
)

0 commit comments

Comments
 (0)