Skip to content

Commit eec33fc

Browse files
committed
implement fix -- ci should pass now
Signed-off-by: emdneto <[email protected]>
1 parent dc9f7f7 commit eec33fc

File tree

2 files changed

+37
-24
lines changed
  • instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests

2 files changed

+37
-24
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515

1616
- `opentelemetry-instrumentation-openai-v2` Update doc for OpenAI Instrumentation to support OpenAI Compatible Platforms
1717
([#3279](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3279))
18-
- `opentelemetry-instrumentation-system-metrics` Add `process` metrics and deprecated `process.runtime` prefixed ones
18+
- `opentelemetry-instrumentation-system-metrics` Add `process` metrics and deprecated `process.runtime` prefixed ones
1919
([#3250](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3250))
2020
- `opentelemetry-instrumentation-botocore` Add support for GenAI user events and lazy initialize tracer
2121
([#3258](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3258))
@@ -37,6 +37,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3737
([#3247](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3247))
3838
- `opentelemetry-instrumentation-asyncpg` Fix fallback for empty queries.
3939
([#3253](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3253))
40+
- `opentelemetry-instrumentation-requests` always record span status code in duration metric
41+
([#3323](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3323))
4042

4143
## Version 1.30.0/0.51b0 (2025-02-03)
4244

@@ -96,7 +98,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9698

9799
### Breaking changes
98100

99-
- `opentelemetry-exporter-prometheus-remote-write` updated protobuf required version from 4.21 to 5.26 and regenerated protobufs
101+
- `opentelemetry-exporter-prometheus-remote-write` updated protobuf required version from 4.21 to 5.26 and regenerated protobufs
100102
([#3219](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3219))
101103
- `opentelemetry-instrumentation-sqlalchemy` including sqlcomment in `db.statement` span attribute value is now opt-in
102104
([#3112](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3112))

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

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,14 @@ def response_hook(span, request_obj, response)
9999
_set_http_network_protocol_version,
100100
_set_http_peer_port_client,
101101
_set_http_scheme,
102-
_set_http_status_code,
103102
_set_http_url,
103+
_set_status,
104104
_StabilityMode,
105105
)
106106
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
107107
from opentelemetry.instrumentation.requests.package import _instruments
108108
from opentelemetry.instrumentation.requests.version import __version__
109109
from opentelemetry.instrumentation.utils import (
110-
http_status_to_status_code,
111110
is_http_instrumentation_enabled,
112111
suppress_http_instrumentation,
113112
)
@@ -124,7 +123,6 @@ def response_hook(span, request_obj, response)
124123
)
125124
from opentelemetry.trace import SpanKind, Tracer, get_tracer
126125
from opentelemetry.trace.span import Span
127-
from opentelemetry.trace.status import StatusCode
128126
from opentelemetry.util.http import (
129127
ExcludeList,
130128
get_excluded_urls,
@@ -140,6 +138,32 @@ def response_hook(span, request_obj, response)
140138
_ResponseHookT = Optional[Callable[[Span, PreparedRequest, Response], None]]
141139

142140

141+
def _set_http_status_code_attribute(
142+
span,
143+
status_code,
144+
metric_attributes=None,
145+
sem_conv_opt_in_mode=_StabilityMode.DEFAULT,
146+
):
147+
status_code_str = str(status_code)
148+
try:
149+
status_code = int(status_code)
150+
except ValueError:
151+
status_code = -1
152+
if metric_attributes is None:
153+
metric_attributes = {}
154+
# When we have durations we should set metrics only once
155+
# Also the decision to include status code on a histogram should
156+
# not be dependent on tracing decisions.
157+
_set_status(
158+
span,
159+
metric_attributes,
160+
status_code,
161+
status_code_str,
162+
server_span=False,
163+
sem_conv_opt_in_mode=sem_conv_opt_in_mode,
164+
)
165+
166+
143167
# pylint: disable=unused-argument
144168
# pylint: disable=R0915
145169
def _instrument(
@@ -267,25 +291,12 @@ def get_or_create_headers():
267291

268292
if isinstance(result, Response):
269293
span_attributes = {}
270-
if span.is_recording():
271-
_set_http_status_code(
272-
span_attributes,
273-
result.status_code,
274-
sem_conv_opt_in_mode,
275-
)
276-
_set_http_status_code(
277-
metric_labels, result.status_code, sem_conv_opt_in_mode
278-
)
279-
status_code = http_status_to_status_code(
280-
result.status_code
281-
)
282-
span.set_status(status_code)
283-
if (
284-
_report_new(sem_conv_opt_in_mode)
285-
and status_code is StatusCode.ERROR
286-
):
287-
span_attributes[ERROR_TYPE] = str(result.status_code)
288-
metric_labels[ERROR_TYPE] = str(result.status_code)
294+
_set_http_status_code_attribute(
295+
span,
296+
result.status_code,
297+
metric_labels,
298+
sem_conv_opt_in_mode,
299+
)
289300

290301
if result.raw is not None:
291302
version = getattr(result.raw, "version", None)

0 commit comments

Comments
 (0)