Skip to content

Commit 12331fd

Browse files
committed
Add utils file in opentelemetry-instrumentation
1 parent f5a8cc3 commit 12331fd

3 files changed

Lines changed: 126 additions & 0 deletions

File tree

opentelemetry-instrumentation/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
- Rename opentelemetry-auto-instrumentation to opentelemetry-instrumentation,
6+
and console script `opentelemetry-auto-instrumentation` to `opentelemetry-instrument`
7+
58
## 0.8b0
69

710
Released 2020-05-27
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
from wrapt import ObjectProxy
16+
17+
from opentelemetry.trace.status import Status, StatusCanonicalCode
18+
19+
20+
def http_status_to_canonical_code(
21+
status: int, allow_redirect: bool = True
22+
) -> StatusCanonicalCode:
23+
"""Converts an HTTP status code to an OpenTelemetry canonical status code
24+
25+
Args:
26+
status (int): HTTP status code
27+
"""
28+
# pylint:disable=too-many-branches,too-many-return-statements
29+
if status < 100:
30+
return StatusCanonicalCode.UNKNOWN
31+
if status <= 299:
32+
return StatusCanonicalCode.OK
33+
if status <= 399:
34+
if allow_redirect:
35+
return StatusCanonicalCode.OK
36+
return StatusCanonicalCode.DEADLINE_EXCEEDED
37+
if status <= 499:
38+
if status == 401: # HTTPStatus.UNAUTHORIZED:
39+
return StatusCanonicalCode.UNAUTHENTICATED
40+
if status == 403: # HTTPStatus.FORBIDDEN:
41+
return StatusCanonicalCode.PERMISSION_DENIED
42+
if status == 404: # HTTPStatus.NOT_FOUND:
43+
return StatusCanonicalCode.NOT_FOUND
44+
if status == 429: # HTTPStatus.TOO_MANY_REQUESTS:
45+
return StatusCanonicalCode.RESOURCE_EXHAUSTED
46+
return StatusCanonicalCode.INVALID_ARGUMENT
47+
if status <= 599:
48+
if status == 501: # HTTPStatus.NOT_IMPLEMENTED:
49+
return StatusCanonicalCode.UNIMPLEMENTED
50+
if status == 503: # HTTPStatus.SERVICE_UNAVAILABLE:
51+
return StatusCanonicalCode.UNAVAILABLE
52+
if status == 504: # HTTPStatus.GATEWAY_TIMEOUT:
53+
return StatusCanonicalCode.DEADLINE_EXCEEDED
54+
return StatusCanonicalCode.INTERNAL
55+
return StatusCanonicalCode.UNKNOWN
56+
57+
58+
def unwrap(obj, attr: str):
59+
"""Given a function that was wrapped by wrapt.wrap_function_wrapper, unwrap it
60+
61+
Args:
62+
obj: Object that holds a reference to the wrapped function
63+
attr (str): Name of the wrapped function
64+
"""
65+
func = getattr(obj, attr, None)
66+
if func and isinstance(func, ObjectProxy) and hasattr(func, "__wrapped__"):
67+
setattr(obj, attr, func.__wrapped__)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2020, 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+
from http import HTTPStatus
16+
17+
from opentelemetry.instrumentation.utils import http_status_to_canonical_code
18+
from opentelemetry.test.test_base import TestBase
19+
from opentelemetry.trace.status import StatusCanonicalCode
20+
21+
22+
class TestUtils(TestBase):
23+
def test_http_status_to_canonical_code(self):
24+
for status_code, expected in (
25+
(HTTPStatus.OK, StatusCanonicalCode.OK),
26+
(HTTPStatus.ACCEPTED, StatusCanonicalCode.OK),
27+
(HTTPStatus.IM_USED, StatusCanonicalCode.OK),
28+
(HTTPStatus.MULTIPLE_CHOICES, StatusCanonicalCode.OK),
29+
(HTTPStatus.BAD_REQUEST, StatusCanonicalCode.INVALID_ARGUMENT),
30+
(HTTPStatus.UNAUTHORIZED, StatusCanonicalCode.UNAUTHENTICATED),
31+
(HTTPStatus.FORBIDDEN, StatusCanonicalCode.PERMISSION_DENIED),
32+
(HTTPStatus.NOT_FOUND, StatusCanonicalCode.NOT_FOUND),
33+
(
34+
HTTPStatus.UNPROCESSABLE_ENTITY,
35+
StatusCanonicalCode.INVALID_ARGUMENT,
36+
),
37+
(
38+
HTTPStatus.TOO_MANY_REQUESTS,
39+
StatusCanonicalCode.RESOURCE_EXHAUSTED,
40+
),
41+
(HTTPStatus.NOT_IMPLEMENTED, StatusCanonicalCode.UNIMPLEMENTED),
42+
(HTTPStatus.SERVICE_UNAVAILABLE, StatusCanonicalCode.UNAVAILABLE),
43+
(
44+
HTTPStatus.GATEWAY_TIMEOUT,
45+
StatusCanonicalCode.DEADLINE_EXCEEDED,
46+
),
47+
(
48+
HTTPStatus.HTTP_VERSION_NOT_SUPPORTED,
49+
StatusCanonicalCode.INTERNAL,
50+
),
51+
(600, StatusCanonicalCode.UNKNOWN),
52+
(99, StatusCanonicalCode.UNKNOWN),
53+
):
54+
with self.subTest(status_code=status_code):
55+
actual = http_status_to_canonical_code(int(status_code))
56+
self.assertEqual(actual, expected, status_code)

0 commit comments

Comments
 (0)