Skip to content

Commit

Permalink
metric instrumentation Tornado (open-telemetry#1252)
Browse files Browse the repository at this point in the history
  • Loading branch information
shalevr authored and CircleCI committed Nov 13, 2022
1 parent 0fe6c77 commit bc03a37
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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],
),
)

Expand All @@ -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",
),
Expand All @@ -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)",
),
Expand Down Expand Up @@ -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
)

Expand All @@ -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
)

Expand Down

0 comments on commit bc03a37

Please sign in to comment.