Skip to content

Commit 8cfb235

Browse files
committed
Move const values to a constants file.
1 parent 10353ad commit 8cfb235

File tree

2 files changed

+42
-17
lines changed
  • instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests

2 files changed

+42
-17
lines changed

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

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ def response_hook(span, request_obj, response):
124124
_StabilityMode,
125125
)
126126
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
127+
from opentelemetry.instrumentation.requests.constants import (
128+
BOT_PATTERNS,
129+
TEST_PATTERNS,
130+
)
127131
from opentelemetry.instrumentation.requests.package import _instruments
128132
from opentelemetry.instrumentation.requests.semconv import (
129133
ATTR_USER_AGENT_SYNTHETIC_TYPE,
@@ -162,17 +166,6 @@ def response_hook(span, request_obj, response):
162166
_RequestHookT = Optional[Callable[[Span, PreparedRequest], None]]
163167
_ResponseHookT = Optional[Callable[[Span, PreparedRequest, Response], None]]
164168

165-
# Test patterns to detect (case-insensitive)
166-
_TEST_PATTERNS = [
167-
"alwayson",
168-
]
169-
170-
# Bot patterns to detect (case-insensitive)
171-
_BOT_PATTERNS = [
172-
"googlebot",
173-
"bingbot",
174-
]
175-
176169

177170
def _detect_synthetic_user_agent(user_agent: str) -> Optional[str]:
178171
"""
@@ -182,8 +175,8 @@ def _detect_synthetic_user_agent(user_agent: str) -> Optional[str]:
182175
user_agent: The user agent string to analyze
183176
184177
Returns:
185-
USER_AGENT_SYNTHETIC_TYPE_VALUE_TEST if user agent contains any pattern from _TEST_PATTERNS
186-
USER_AGENT_SYNTHETIC_TYPE_VALUE_BOT if user agent contains any pattern from _BOT_PATTERNS
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
187180
None otherwise
188181
189182
Note: Test patterns take priority over bot patterns.
@@ -193,11 +186,9 @@ def _detect_synthetic_user_agent(user_agent: str) -> Optional[str]:
193186

194187
user_agent_lower = user_agent.lower()
195188

196-
if any(
197-
test_pattern in user_agent_lower for test_pattern in _TEST_PATTERNS
198-
):
189+
if any(test_pattern in user_agent_lower for test_pattern in TEST_PATTERNS):
199190
return USER_AGENT_SYNTHETIC_TYPE_VALUE_TEST
200-
if any(bot_pattern in user_agent_lower for bot_pattern in _BOT_PATTERNS):
191+
if any(bot_pattern in user_agent_lower for bot_pattern in BOT_PATTERNS):
201192
return USER_AGENT_SYNTHETIC_TYPE_VALUE_BOT
202193

203194
return None
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
"""
16+
Constants for OpenTelemetry requests instrumentation.
17+
18+
This module contains configuration constants and pattern definitions used
19+
by the requests instrumentation for various features like synthetic user
20+
agent detection.
21+
"""
22+
23+
# Test patterns to detect in user agent strings (case-insensitive)
24+
# These patterns indicate synthetic test traffic
25+
TEST_PATTERNS = [
26+
"alwayson",
27+
]
28+
29+
# Bot patterns to detect in user agent strings (case-insensitive)
30+
# These patterns indicate automated bot traffic
31+
BOT_PATTERNS = [
32+
"googlebot",
33+
"bingbot",
34+
]

0 commit comments

Comments
 (0)