forked from open-telemetry/opentelemetry-python-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
214 lines (179 loc) · 7.15 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# 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 os
import threading
import urllib.parse
from enum import Enum
from re import escape, sub
from typing import Dict, Sequence
from wrapt import ObjectProxy
from opentelemetry import context, trace
# pylint: disable=unused-import
# pylint: disable=E0611
from opentelemetry.context import _SUPPRESS_INSTRUMENTATION_KEY # noqa: F401
from opentelemetry.propagate import extract
from opentelemetry.trace import StatusCode
from opentelemetry.trace.propagation.tracecontext import (
TraceContextTextMapPropagator,
)
propagator = TraceContextTextMapPropagator()
def extract_attributes_from_object(
obj: any, attributes: Sequence[str], existing: Dict[str, str] = None
) -> Dict[str, str]:
extracted = {}
if existing:
extracted.update(existing)
for attr in attributes:
value = getattr(obj, attr, None)
if value is not None:
extracted[attr] = str(value)
return extracted
def http_status_to_status_code(
status: int,
allow_redirect: bool = True,
server_span: bool = False,
) -> StatusCode:
"""Converts an HTTP status code to an OpenTelemetry canonical status code
Args:
status (int): HTTP status code
"""
# See: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#status
if not isinstance(status, int):
return StatusCode.UNSET
if status < 100:
return StatusCode.ERROR
if status <= 299:
return StatusCode.UNSET
if status <= 399 and allow_redirect:
return StatusCode.UNSET
if status <= 499 and server_span:
return StatusCode.UNSET
return StatusCode.ERROR
def unwrap(obj, attr: str):
"""Given a function that was wrapped by wrapt.wrap_function_wrapper, unwrap it
Args:
obj: Object that holds a reference to the wrapped function
attr (str): Name of the wrapped function
"""
func = getattr(obj, attr, None)
if func and isinstance(func, ObjectProxy) and hasattr(func, "__wrapped__"):
setattr(obj, attr, func.__wrapped__)
def _start_internal_or_server_span(
tracer,
span_name,
start_time,
context_carrier,
context_getter,
attributes=None,
):
"""Returns internal or server span along with the token which can be used by caller to reset context
Args:
tracer : tracer in use by given instrumentation library
span_name (string): name of the span
start_time : start time of the span
context_carrier : object which contains values that are
used to construct a Context. This object
must be paired with an appropriate getter
which understands how to extract a value from it.
context_getter : an object which contains a get function that can retrieve zero
or more values from the carrier and a keys function that can get all the keys
from carrier.
"""
token = ctx = span_kind = None
if trace.get_current_span() is trace.INVALID_SPAN:
ctx = extract(context_carrier, getter=context_getter)
token = context.attach(ctx)
span_kind = trace.SpanKind.SERVER
else:
ctx = context.get_current()
span_kind = trace.SpanKind.INTERNAL
span = tracer.start_span(
name=span_name,
context=ctx,
kind=span_kind,
start_time=start_time,
attributes=attributes,
)
return span, token
def _url_quote(s) -> str: # pylint: disable=invalid-name
if not isinstance(s, (str, bytes)):
return s
quoted = urllib.parse.quote(s)
# Since SQL uses '%' as a keyword, '%' is a by-product of url quoting
# e.g. foo,bar --> foo%2Cbar
# thus in our quoting, we need to escape it too to finally give
# foo,bar --> foo%%2Cbar
return quoted.replace("%", "%%")
def _get_opentelemetry_values() -> dict:
"""
Return the OpenTelemetry Trace and Span IDs if Span ID is set in the
OpenTelemetry execution context.
"""
# Insert the W3C TraceContext generated
_headers = {}
propagator.inject(_headers)
return _headers
def _python_path_without_directory(python_path, directory, path_separator):
return sub(
rf"{escape(directory)}{path_separator}(?!$)",
"",
python_path,
)
_OTEL_SEMCONV_STABILITY_OPT_IN_KEY = "OTEL_SEMCONV_STABILITY_OPT_IN"
class _OpenTelemetryStabilitySignalType:
HTTP = "http"
class _OpenTelemetryStabilityMode(Enum):
# http - emit the new, stable HTTP and networking conventions ONLY
HTTP = "http"
# http/dup - emit both the old and the stable HTTP and networking conventions
HTTP_DUP = "http/dup"
# default - continue emitting old experimental HTTP and networking conventions
DEFAULT = "default"
class _OpenTelemetrySemanticConventionStability:
_initialized = False
_lock = threading.Lock()
_OTEL_SEMCONV_STABILITY_SIGNAL_MAPPING = {}
@classmethod
def _initialize(cls):
with _OpenTelemetrySemanticConventionStability._lock:
if not _OpenTelemetrySemanticConventionStability._initialized:
# Users can pass in comma delimited string for opt-in options
# Only values for http stability are supported for now
opt_in = os.environ.get(_OTEL_SEMCONV_STABILITY_OPT_IN_KEY, "")
opt_in_list = []
if opt_in:
opt_in_list = [s.strip() for s in opt_in.split(",")]
http_opt_in = _OpenTelemetryStabilityMode.DEFAULT
if opt_in_list:
# Process http opt-in
# http/dup takes priority over http
if (
_OpenTelemetryStabilityMode.HTTP_DUP.value
in opt_in_list
):
http_opt_in = _OpenTelemetryStabilityMode.HTTP_DUP
elif _OpenTelemetryStabilityMode.HTTP.value in opt_in_list:
http_opt_in = _OpenTelemetryStabilityMode.HTTP
_OpenTelemetrySemanticConventionStability._OTEL_SEMCONV_STABILITY_SIGNAL_MAPPING[
_OpenTelemetryStabilitySignalType.HTTP
] = http_opt_in
_OpenTelemetrySemanticConventionStability._initialized = True
@classmethod
def _get_opentelemetry_stability_opt_in(
type: _OpenTelemetryStabilitySignalType,
) -> _OpenTelemetryStabilityMode:
with _OpenTelemetrySemanticConventionStability._lock:
return _OpenTelemetrySemanticConventionStability._OTEL_SEMCONV_STABILITY_SIGNAL_MAPPING.get(
type, _OpenTelemetryStabilityMode.DEFAULT
)