diff --git a/CHANGELOG.md b/CHANGELOG.md index e1d0927117..44d69fe454 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.13.0-0.34b0...HEAD) +- Add metric instrumentation for tornado + ([#1252](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1252)) + ### Added diff --git a/instrumentation/opentelemetry-instrumentation-tornado/src/opentelemetry/instrumentation/tornado/__init__.py b/instrumentation/opentelemetry-instrumentation-tornado/src/opentelemetry/instrumentation/tornado/__init__.py index dd8da74f3f..513302f8bc 100644 --- a/instrumentation/opentelemetry-instrumentation-tornado/src/opentelemetry/instrumentation/tornado/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-tornado/src/opentelemetry/instrumentation/tornado/__init__.py @@ -202,6 +202,13 @@ def client_resposne_hook(span, future): _OTEL_PATCHED_KEY = "_otel_patched_key" _START_TIME = "start_time" +_CLIENT_DURATION_HISTOGRAM = "http.client.duration" +_CLIENT_REQUEST_SIZE_HISTOGRAM = "http.client.request.size" +_CLIENT_RESPONSE_SIZE_HISTOGRAM = "http.client.response.size" +_SERVER_DURATION_HISTOGRAM = "http.server.duration" +_SERVER_REQUEST_SIZE_HISTOGRAM = "http.server.request.size" +_SERVER_RESPONSE_SIZE_HISTOGRAM = "http.server.response.size" +_SERVER_ACTIVE_REQUESTS_HISTOGRAM = "http.server.active_requests" _excluded_urls = get_excluded_urls("TORNADO") _traced_request_attrs = get_traced_request_attrs("TORNADO") @@ -267,9 +274,9 @@ def handler_init(init, handler, args, kwargs): tracer, client_request_hook, client_response_hook, - client_histograms[MetricInstruments.HTTP_CLIENT_DURATION], - client_histograms[MetricInstruments.HTTP_CLIENT_REQUEST_SIZE], - client_histograms[MetricInstruments.HTTP_CLIENT_RESPONSE_SIZE], + client_histograms[_CLIENT_DURATION_HISTOGRAM], + client_histograms[_CLIENT_REQUEST_SIZE_HISTOGRAM], + client_histograms[_CLIENT_RESPONSE_SIZE_HISTOGRAM], ), ) @@ -283,23 +290,23 @@ def _uninstrument(self, **kwargs): def _create_server_histograms(meter) -> Dict[str, Histogram]: histograms = { - MetricInstruments.HTTP_SERVER_DURATION: meter.create_histogram( - name=MetricInstruments.HTTP_SERVER_DURATION, + _SERVER_DURATION_HISTOGRAM: meter.create_histogram( + name="http.server.duration", unit="ms", description="measures the duration outbound HTTP requests", ), - MetricInstruments.HTTP_SERVER_REQUEST_SIZE: meter.create_histogram( - name=MetricInstruments.HTTP_SERVER_REQUEST_SIZE, + _SERVER_REQUEST_SIZE_HISTOGRAM: meter.create_histogram( + name="http.server.request.size", unit="By", description="measures the size of HTTP request messages (compressed)", ), - MetricInstruments.HTTP_SERVER_RESPONSE_SIZE: meter.create_histogram( - name=MetricInstruments.HTTP_SERVER_RESPONSE_SIZE, + _SERVER_RESPONSE_SIZE_HISTOGRAM: meter.create_histogram( + name="http.server.response.size", unit="By", description="measures the size of HTTP response messages (compressed)", ), - MetricInstruments.HTTP_SERVER_ACTIVE_REQUESTS: meter.create_up_down_counter( - name=MetricInstruments.HTTP_SERVER_ACTIVE_REQUESTS, + _SERVER_ACTIVE_REQUESTS_HISTOGRAM: meter.create_up_down_counter( + name="http.server.active_requests", unit="requests", description="measures the number of concurrent HTTP requests that are currently in-flight", ), @@ -310,18 +317,18 @@ def _create_server_histograms(meter) -> Dict[str, Histogram]: def _create_client_histograms(meter) -> Dict[str, Histogram]: histograms = { - MetricInstruments.HTTP_CLIENT_DURATION: meter.create_histogram( - name=MetricInstruments.HTTP_CLIENT_DURATION, + _CLIENT_DURATION_HISTOGRAM: meter.create_histogram( + name="http.client.duration", unit="ms", description="measures the duration outbound HTTP requests", ), - MetricInstruments.HTTP_CLIENT_REQUEST_SIZE: meter.create_histogram( - name=MetricInstruments.HTTP_CLIENT_REQUEST_SIZE, + _CLIENT_REQUEST_SIZE_HISTOGRAM: meter.create_histogram( + name="http.client.request.size", unit="By", description="measures the size of HTTP request messages (compressed)", ), - MetricInstruments.HTTP_CLIENT_RESPONSE_SIZE: meter.create_histogram( - name=MetricInstruments.HTTP_CLIENT_RESPONSE_SIZE, + _CLIENT_RESPONSE_SIZE_HISTOGRAM: meter.create_histogram( + name="http.client.response.size", unit="By", description="measures the size of HTTP response messages (compressed)", ), @@ -556,14 +563,14 @@ def _record_prepare_metrics(server_histograms, handler): request_size = int(handler.request.headers.get("Content-Length", 0)) metric_attributes = _create_metric_attributes(handler) - server_histograms[MetricInstruments.HTTP_SERVER_REQUEST_SIZE].record( + server_histograms[_SERVER_REQUEST_SIZE_HISTOGRAM].record( request_size, attributes=metric_attributes ) active_requests_attributes = _create_active_requests_attributes( handler.request ) - server_histograms[MetricInstruments.HTTP_SERVER_ACTIVE_REQUESTS].add( + server_histograms[_SERVER_ACTIVE_REQUESTS_HISTOGRAM].add( 1, attributes=active_requests_attributes ) @@ -579,18 +586,18 @@ def _record_on_finish_metrics(server_histograms, handler, error=None): if isinstance(error, tornado.web.HTTPError): metric_attributes[SpanAttributes.HTTP_STATUS_CODE] = error.status_code - server_histograms[MetricInstruments.HTTP_SERVER_RESPONSE_SIZE].record( + server_histograms[_SERVER_RESPONSE_SIZE_HISTOGRAM].record( response_size, attributes=metric_attributes ) - server_histograms[MetricInstruments.HTTP_SERVER_DURATION].record( + server_histograms[_SERVER_DURATION_HISTOGRAM].record( elapsed_time, attributes=metric_attributes ) active_requests_attributes = _create_active_requests_attributes( handler.request ) - server_histograms[MetricInstruments.HTTP_SERVER_ACTIVE_REQUESTS].add( + server_histograms[_SERVER_ACTIVE_REQUESTS_HISTOGRAM].add( -1, attributes=active_requests_attributes )