Skip to content

Commit

Permalink
name
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen committed Apr 17, 2024
1 parent c440866 commit 60337bc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
_filter_semconv_duration_attrs,
_get_schema_url,
_OpenTelemetrySemanticConventionStability,
_OpenTelemetryStabilityMode,
_HTTPStabilityMode,
_OpenTelemetryStabilitySignalType,
_report_new,
_report_old,
Expand Down Expand Up @@ -119,7 +119,7 @@ def _instrument(
request_hook: _RequestHookT = None,
response_hook: _ResponseHookT = None,
excluded_urls: ExcludeList = None,
sem_conv_opt_in_mode: _OpenTelemetryStabilityMode = _OpenTelemetryStabilityMode.DEFAULT,
sem_conv_opt_in_mode: _HTTPStabilityMode = _HTTPStabilityMode.DEFAULT,
):
"""Enables tracing of all requests calls that go through
:code:`requests.session.Session.request` (this includes
Expand Down Expand Up @@ -290,7 +290,7 @@ def get_or_create_headers():
metric_labels,
_client_duration_attrs_old,
_client_duration_attrs_new,
_OpenTelemetryStabilityMode.DEFAULT
_HTTPStabilityMode.DEFAULT
)
duration_histogram_old.record(
max(round(elapsed_time * 1000), 0),
Expand All @@ -301,7 +301,7 @@ def get_or_create_headers():
metric_labels,
_client_duration_attrs_old,
_client_duration_attrs_new,
_OpenTelemetryStabilityMode.HTTP
_HTTPStabilityMode.HTTP
)
duration_histogram_new.record(
elapsed_time, attributes=duration_attrs_new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def response_hook(span: Span, environ: WSGIEnvironment, status: str, response_he
_OpenTelemetrySemanticConventionStability,
_OpenTelemetryStabilitySignalType,
_METRIC_ATTRIBUTES_SERVER_DURATION_NAME,
_OpenTelemetryStabilityMode,
_HTTPStabilityMode,
_SPAN_ATTRIBUTES_ERROR_TYPE,
_get_schema_url,
_filter_semconv_active_request_count_attr,
Expand Down Expand Up @@ -305,7 +305,7 @@ def setifnotnone(dic, key, value):

def collect_request_attributes(
environ,
sem_conv_opt_in_mode = _OpenTelemetryStabilityMode.DEFAULT,
sem_conv_opt_in_mode = _HTTPStabilityMode.DEFAULT,
):
"""Collects HTTP request attributes from the PEP3333-conforming
WSGI environ and returns a dictionary to be used as span creation attributes.
Expand Down Expand Up @@ -446,7 +446,7 @@ def _parse_status_code(resp_status):
return None


def _parse_active_request_count_attrs(req_attrs, sem_conv_opt_in_mode = _OpenTelemetryStabilityMode.DEFAULT):
def _parse_active_request_count_attrs(req_attrs, sem_conv_opt_in_mode = _HTTPStabilityMode.DEFAULT):
return _filter_semconv_active_request_count_attr(
req_attrs,
_server_active_requests_count_attrs_old,
Expand All @@ -455,7 +455,7 @@ def _parse_active_request_count_attrs(req_attrs, sem_conv_opt_in_mode = _OpenTel
)


def _parse_duration_attrs(req_attrs, sem_conv_opt_in_mode = _OpenTelemetryStabilityMode.DEFAULT):
def _parse_duration_attrs(req_attrs, sem_conv_opt_in_mode = _HTTPStabilityMode.DEFAULT):
return _filter_semconv_duration_attrs(
req_attrs,
_server_duration_attrs_old,
Expand All @@ -469,7 +469,7 @@ def add_response_attributes(
start_response_status,
response_headers,
duration_attrs = None,
sem_conv_opt_in_mode = _OpenTelemetryStabilityMode.DEFAULT,
sem_conv_opt_in_mode = _HTTPStabilityMode.DEFAULT,
): # pylint: disable=unused-argument
"""Adds HTTP response attributes to span using the arguments
passed to a PEP3333-conforming start_response callable.
Expand Down Expand Up @@ -648,18 +648,18 @@ def __call__(self, environ, start_response):
if span.is_recording():
if _report_new(self._sem_conv_opt_in_mode):
span.set_attribute(_SPAN_ATTRIBUTES_ERROR_TYPE, type(ex).__qualname__ )
span.set_status(Status(StatusCode.ERROR, str(ex)))
span.set_status(Status(StatusCode.ERROR, str(ex)))
span.end()
if token is not None:
context.detach(token)
raise
finally:
duration_s = default_timer() - start
if self.duration_histogram_old:
duration_attrs_old = _parse_duration_attrs(req_attrs, _OpenTelemetryStabilityMode.DEFAULT)
duration_attrs_old = _parse_duration_attrs(req_attrs, _HTTPStabilityMode.DEFAULT)
self.duration_histogram_old.record(max(round(duration_s * 1000), 0), duration_attrs_old)
if self.duration_histogram_new:
duration_attrs_new = _parse_duration_attrs(req_attrs, _OpenTelemetryStabilityMode.HTTP)
duration_attrs_new = _parse_duration_attrs(req_attrs, _HTTPStabilityMode.HTTP)
self.duration_histogram_new.record(max(round(duration_s * 1000), 0), duration_attrs_new)
self.active_requests_counter.add(-1, active_requests_count_attrs)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
_server_duration_attrs_old,
_OTEL_SEMCONV_STABILITY_OPT_IN_KEY,
_OpenTelemetrySemanticConventionStability,
_OpenTelemetryStabilityMode,
_HTTPStabilityMode,
)
from opentelemetry.sdk.metrics.export import (
HistogramDataPoint,
Expand Down Expand Up @@ -496,7 +496,7 @@ def test_request_attributes_new_semconv(self):

attrs = otel_wsgi.collect_request_attributes(
self.environ,
_OpenTelemetryStabilityMode.HTTP,
_HTTPStabilityMode.HTTP,
)
self.assertDictEqual(
attrs,
Expand Down Expand Up @@ -654,7 +654,7 @@ def test_request_attributes_with_full_request_uri(self):
self.assertGreaterEqual(
otel_wsgi.collect_request_attributes(
self.environ,
_OpenTelemetryStabilityMode.HTTP,
_HTTPStabilityMode.HTTP,
).items(),
expected_new.items(),
)
Expand All @@ -670,7 +670,7 @@ def test_http_user_agent_attribute(self):
self.assertGreaterEqual(
otel_wsgi.collect_request_attributes(
self.environ,
_OpenTelemetryStabilityMode.HTTP,
_HTTPStabilityMode.HTTP,
).items(),
expected_new.items(),
)
Expand All @@ -681,7 +681,7 @@ def test_response_attributes(self):
self.span,
"404 Not Found",
{},
sem_conv_opt_in_mode=_OpenTelemetryStabilityMode.HTTP,
sem_conv_opt_in_mode=_HTTPStabilityMode.HTTP,
)
expected = (mock.call(SpanAttributes.HTTP_STATUS_CODE, 404),)
expected_new = (mock.call(SpanAttributes.HTTP_RESPONSE_STATUS_CODE, 404),)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class _OpenTelemetryStabilitySignalType:
HTTP = "http"


class _OpenTelemetryStabilityMode(Enum):
class _HTTPStabilityMode(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
Expand All @@ -101,11 +101,11 @@ class _OpenTelemetryStabilityMode(Enum):


def _report_new(mode):
return mode.name != _OpenTelemetryStabilityMode.DEFAULT.name
return mode.name != _HTTPStabilityMode.DEFAULT.name


def _report_old(mode):
return mode.name != _OpenTelemetryStabilityMode.HTTP.name
return mode.name != _HTTPStabilityMode.HTTP.name


class _OpenTelemetrySemanticConventionStability:
Expand All @@ -123,17 +123,17 @@ def _initialize(cls):
opt_in_list = []
if opt_in:
opt_in_list = [s.strip() for s in opt_in.split(",")]
http_opt_in = _OpenTelemetryStabilityMode.DEFAULT
http_opt_in = _HTTPStabilityMode.DEFAULT
if opt_in_list:
# Process http opt-in
# http/dup takes priority over http
if (
_OpenTelemetryStabilityMode.HTTP_DUP.value
_HTTPStabilityMode.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
http_opt_in = _HTTPStabilityMode.HTTP_DUP
elif _HTTPStabilityMode.HTTP.value in opt_in_list:
http_opt_in = _HTTPStabilityMode.HTTP
_OpenTelemetrySemanticConventionStability._OTEL_SEMCONV_STABILITY_SIGNAL_MAPPING[
_OpenTelemetryStabilitySignalType.HTTP
] = http_opt_in
Expand All @@ -144,23 +144,23 @@ def _initialize(cls):
def _get_opentelemetry_stability_opt_in_mode(
cls,
signal_type: _OpenTelemetryStabilitySignalType,
) -> _OpenTelemetryStabilityMode:
) -> _HTTPStabilityMode:
return _OpenTelemetrySemanticConventionStability._OTEL_SEMCONV_STABILITY_SIGNAL_MAPPING.get(
signal_type, _OpenTelemetryStabilityMode.DEFAULT
signal_type, _HTTPStabilityMode.DEFAULT
)


def _filter_semconv_duration_attrs(
attrs,
old_attrs,
new_attrs,
sem_conv_opt_in_mode = _OpenTelemetryStabilityMode.DEFAULT,
sem_conv_opt_in_mode = _HTTPStabilityMode.DEFAULT,
):
filtered_attrs = {}
# duration is two different metrics depending on sem_conv_opt_in_mode, so no DUP attributes
allowed_attributes = (
new_attrs
if sem_conv_opt_in_mode == _OpenTelemetryStabilityMode.HTTP
if sem_conv_opt_in_mode == _HTTPStabilityMode.HTTP
else old_attrs
)
for key, val in attrs.items():
Expand All @@ -173,7 +173,7 @@ def _filter_semconv_active_request_count_attr(
attrs,
old_attrs,
new_attrs,
sem_conv_opt_in_mode = _OpenTelemetryStabilityMode.DEFAULT,
sem_conv_opt_in_mode = _HTTPStabilityMode.DEFAULT,
):
filtered_attrs = {}
if _report_old(sem_conv_opt_in_mode):
Expand Down Expand Up @@ -364,7 +364,7 @@ def _set_status(span, metrics_attributes, status_code_str, status_code, sem_conv


# Get schema version based off of opt-in mode
def _get_schema_url(mode: _OpenTelemetryStabilityMode) -> str:
if mode is _OpenTelemetryStabilityMode.DEFAULT:
def _get_schema_url(mode: _HTTPStabilityMode) -> str:
if mode is _HTTPStabilityMode.DEFAULT:
return "https://opentelemetry.io/schemas/1.11.0"
return SpanAttributes.SCHEMA_URL

0 comments on commit 60337bc

Please sign in to comment.