Skip to content

Commit bc03a37

Browse files
shalevrCircleCI
authored and
CircleCI
committed
metric instrumentation Tornado (open-telemetry#1252)
1 parent 0fe6c77 commit bc03a37

File tree

2 files changed

+32
-22
lines changed
  • instrumentation/opentelemetry-instrumentation-tornado/src/opentelemetry/instrumentation/tornado

2 files changed

+32
-22
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.13.0-0.34b0...HEAD)
9+
- Add metric instrumentation for tornado
10+
([#1252](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1252))
11+
912

1013
### Added
1114

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

+29-22
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,13 @@ def client_resposne_hook(span, future):
202202
_OTEL_PATCHED_KEY = "_otel_patched_key"
203203

204204
_START_TIME = "start_time"
205+
_CLIENT_DURATION_HISTOGRAM = "http.client.duration"
206+
_CLIENT_REQUEST_SIZE_HISTOGRAM = "http.client.request.size"
207+
_CLIENT_RESPONSE_SIZE_HISTOGRAM = "http.client.response.size"
208+
_SERVER_DURATION_HISTOGRAM = "http.server.duration"
209+
_SERVER_REQUEST_SIZE_HISTOGRAM = "http.server.request.size"
210+
_SERVER_RESPONSE_SIZE_HISTOGRAM = "http.server.response.size"
211+
_SERVER_ACTIVE_REQUESTS_HISTOGRAM = "http.server.active_requests"
205212

206213
_excluded_urls = get_excluded_urls("TORNADO")
207214
_traced_request_attrs = get_traced_request_attrs("TORNADO")
@@ -267,9 +274,9 @@ def handler_init(init, handler, args, kwargs):
267274
tracer,
268275
client_request_hook,
269276
client_response_hook,
270-
client_histograms[MetricInstruments.HTTP_CLIENT_DURATION],
271-
client_histograms[MetricInstruments.HTTP_CLIENT_REQUEST_SIZE],
272-
client_histograms[MetricInstruments.HTTP_CLIENT_RESPONSE_SIZE],
277+
client_histograms[_CLIENT_DURATION_HISTOGRAM],
278+
client_histograms[_CLIENT_REQUEST_SIZE_HISTOGRAM],
279+
client_histograms[_CLIENT_RESPONSE_SIZE_HISTOGRAM],
273280
),
274281
)
275282

@@ -283,23 +290,23 @@ def _uninstrument(self, **kwargs):
283290

284291
def _create_server_histograms(meter) -> Dict[str, Histogram]:
285292
histograms = {
286-
MetricInstruments.HTTP_SERVER_DURATION: meter.create_histogram(
287-
name=MetricInstruments.HTTP_SERVER_DURATION,
293+
_SERVER_DURATION_HISTOGRAM: meter.create_histogram(
294+
name="http.server.duration",
288295
unit="ms",
289296
description="measures the duration outbound HTTP requests",
290297
),
291-
MetricInstruments.HTTP_SERVER_REQUEST_SIZE: meter.create_histogram(
292-
name=MetricInstruments.HTTP_SERVER_REQUEST_SIZE,
298+
_SERVER_REQUEST_SIZE_HISTOGRAM: meter.create_histogram(
299+
name="http.server.request.size",
293300
unit="By",
294301
description="measures the size of HTTP request messages (compressed)",
295302
),
296-
MetricInstruments.HTTP_SERVER_RESPONSE_SIZE: meter.create_histogram(
297-
name=MetricInstruments.HTTP_SERVER_RESPONSE_SIZE,
303+
_SERVER_RESPONSE_SIZE_HISTOGRAM: meter.create_histogram(
304+
name="http.server.response.size",
298305
unit="By",
299306
description="measures the size of HTTP response messages (compressed)",
300307
),
301-
MetricInstruments.HTTP_SERVER_ACTIVE_REQUESTS: meter.create_up_down_counter(
302-
name=MetricInstruments.HTTP_SERVER_ACTIVE_REQUESTS,
308+
_SERVER_ACTIVE_REQUESTS_HISTOGRAM: meter.create_up_down_counter(
309+
name="http.server.active_requests",
303310
unit="requests",
304311
description="measures the number of concurrent HTTP requests that are currently in-flight",
305312
),
@@ -310,18 +317,18 @@ def _create_server_histograms(meter) -> Dict[str, Histogram]:
310317

311318
def _create_client_histograms(meter) -> Dict[str, Histogram]:
312319
histograms = {
313-
MetricInstruments.HTTP_CLIENT_DURATION: meter.create_histogram(
314-
name=MetricInstruments.HTTP_CLIENT_DURATION,
320+
_CLIENT_DURATION_HISTOGRAM: meter.create_histogram(
321+
name="http.client.duration",
315322
unit="ms",
316323
description="measures the duration outbound HTTP requests",
317324
),
318-
MetricInstruments.HTTP_CLIENT_REQUEST_SIZE: meter.create_histogram(
319-
name=MetricInstruments.HTTP_CLIENT_REQUEST_SIZE,
325+
_CLIENT_REQUEST_SIZE_HISTOGRAM: meter.create_histogram(
326+
name="http.client.request.size",
320327
unit="By",
321328
description="measures the size of HTTP request messages (compressed)",
322329
),
323-
MetricInstruments.HTTP_CLIENT_RESPONSE_SIZE: meter.create_histogram(
324-
name=MetricInstruments.HTTP_CLIENT_RESPONSE_SIZE,
330+
_CLIENT_RESPONSE_SIZE_HISTOGRAM: meter.create_histogram(
331+
name="http.client.response.size",
325332
unit="By",
326333
description="measures the size of HTTP response messages (compressed)",
327334
),
@@ -556,14 +563,14 @@ def _record_prepare_metrics(server_histograms, handler):
556563
request_size = int(handler.request.headers.get("Content-Length", 0))
557564
metric_attributes = _create_metric_attributes(handler)
558565

559-
server_histograms[MetricInstruments.HTTP_SERVER_REQUEST_SIZE].record(
566+
server_histograms[_SERVER_REQUEST_SIZE_HISTOGRAM].record(
560567
request_size, attributes=metric_attributes
561568
)
562569

563570
active_requests_attributes = _create_active_requests_attributes(
564571
handler.request
565572
)
566-
server_histograms[MetricInstruments.HTTP_SERVER_ACTIVE_REQUESTS].add(
573+
server_histograms[_SERVER_ACTIVE_REQUESTS_HISTOGRAM].add(
567574
1, attributes=active_requests_attributes
568575
)
569576

@@ -579,18 +586,18 @@ def _record_on_finish_metrics(server_histograms, handler, error=None):
579586
if isinstance(error, tornado.web.HTTPError):
580587
metric_attributes[SpanAttributes.HTTP_STATUS_CODE] = error.status_code
581588

582-
server_histograms[MetricInstruments.HTTP_SERVER_RESPONSE_SIZE].record(
589+
server_histograms[_SERVER_RESPONSE_SIZE_HISTOGRAM].record(
583590
response_size, attributes=metric_attributes
584591
)
585592

586-
server_histograms[MetricInstruments.HTTP_SERVER_DURATION].record(
593+
server_histograms[_SERVER_DURATION_HISTOGRAM].record(
587594
elapsed_time, attributes=metric_attributes
588595
)
589596

590597
active_requests_attributes = _create_active_requests_attributes(
591598
handler.request
592599
)
593-
server_histograms[MetricInstruments.HTTP_SERVER_ACTIVE_REQUESTS].add(
600+
server_histograms[_SERVER_ACTIVE_REQUESTS_HISTOGRAM].add(
594601
-1, attributes=active_requests_attributes
595602
)
596603

0 commit comments

Comments
 (0)