Skip to content

[WIP] Fix tornado server (request) duration metric calculation #3489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#3447](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3447))
- `opentelemetry-instrumentation-botocore` Capture server attributes for botocore API calls
([#3448](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3448))
- `opentelemetry-instrumentation-tornado` Fix server (request) duration metric calculation
([#3486](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3486))


## Version 1.32.0/0.53b0 (2025-04-10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ def client_response_hook(span, future):

_logger = getLogger(__name__)
_TraceContext = namedtuple("TraceContext", ["activation", "span", "token"])
_HANDLER_STATE_KEY = "_otel_state_key"
_HANDLER_CONTEXT_KEY = "_otel_trace_context_key"
_OTEL_PATCHED_KEY = "_otel_patched_key"

Expand Down Expand Up @@ -379,7 +380,10 @@ def _wrap(cls, method_name, wrapper):
def _prepare(
tracer, server_histograms, request_hook, func, handler, args, kwargs
):
server_histograms[_START_TIME] = default_timer()
otel_handler_state = {
_START_TIME: default_timer()
}
setattr(handler, _HANDLER_STATE_KEY, otel_handler_state)

request = handler.request
if _excluded_urls.url_disabled(request.uri):
Expand Down Expand Up @@ -593,8 +597,10 @@ def _record_prepare_metrics(server_histograms, handler):


def _record_on_finish_metrics(server_histograms, handler, error=None):
otel_handler_state = getattr(handler, _HANDLER_STATE_KEY, None) or {}
start_time = otel_handler_state.get(_START_TIME, None) or default_timer()
elapsed_time = round(
(default_timer() - server_histograms[_START_TIME]) * 1000
(default_timer() - start_time) * 1000
)

response_size = int(handler._headers.get("Content-Length", 0))
Expand Down