-
Notifications
You must be signed in to change notification settings - Fork 678
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
Metrics instrumentation flask #1186
Changes from 10 commits
e91b396
139a1cc
02d7f29
528efb0
8278b7a
615bf12
aea88d4
754e062
cade131
d6026f8
41ab66f
ea828be
4273177
6bbfea2
ab37e05
22b11a5
9286231
a758eab
1a33b21
dda7548
9557035
27cdbc7
536bf25
bc89aa0
ab2bed1
0ba053f
9fe8a18
3516ec4
f45ca51
7cc4a50
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,15 @@ | |
get_global_response_propagator, | ||
set_global_response_propagator, | ||
) | ||
from opentelemetry.instrumentation.wsgi import OpenTelemetryMiddleware | ||
from opentelemetry.instrumentation.wsgi import ( | ||
OpenTelemetryMiddleware, | ||
_active_requests_count_attrs, | ||
_duration_attrs, | ||
) | ||
from opentelemetry.sdk.metrics.export import ( | ||
HistogramDataPoint, | ||
NumberDataPoint, | ||
) | ||
from opentelemetry.sdk.resources import Resource | ||
from opentelemetry.semconv.trace import SpanAttributes | ||
from opentelemetry.test.wsgitestutil import WsgiTestBase | ||
|
@@ -49,6 +57,16 @@ def expected_attributes(override_attributes): | |
return default_attributes | ||
|
||
|
||
_expected_metric_names = [ | ||
"http.server.active_requests", | ||
"http.server.duration", | ||
] | ||
_recommended_attrs = { | ||
"http.server.active_requests": _active_requests_count_attrs, | ||
"http.server.duration": _duration_attrs, | ||
} | ||
|
||
|
||
class TestProgrammatic(InstrumentationTest, WsgiTestBase): | ||
def setUp(self): | ||
super().setUp() | ||
|
@@ -250,6 +268,34 @@ def test_exclude_lists_from_explicit(self): | |
span_list = self.memory_exporter.get_finished_spans() | ||
self.assertEqual(len(span_list), 1) | ||
|
||
def test_wsgi_metrics(self): | ||
TheAnshul756 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.client.get("/hello/123") | ||
self.client.get("/hello/321") | ||
self.client.get("/hello/756") | ||
metrics_list = self.memory_metrics_reader.get_metrics_data() | ||
number_data_point_seen = False | ||
histogram_data_point_seen = False | ||
self.assertTrue(len(metrics_list.resource_metrics) != 0) | ||
for resource_metric in metrics_list.resource_metrics: | ||
self.assertTrue(len(resource_metric.scope_metrics) != 0) | ||
for scope_metric in resource_metric.scope_metrics: | ||
self.assertTrue(len(scope_metric.metrics) != 0) | ||
for metric in scope_metric.metrics: | ||
self.assertIn(metric.name, _expected_metric_names) | ||
data_points = list(metric.data.data_points) | ||
self.assertEqual(len(data_points), 1) | ||
for point in data_points: | ||
if isinstance(point, HistogramDataPoint): | ||
self.assertEqual(point.count, 3) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we have a test for the actual values in the histogram? This will test that what we are recording for duration value is actually accurate. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sorry, I do not understand how can we get the value of time duration to test. Can you please give me an example? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we can get the actual value, but what will be the expected value? For three get requests There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You do not have to make it exact hardcoded value. It can be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. trigger a request -> operation takes some time -> expected duration should be within x+/-∆ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added the test. Please have a look. |
||
histogram_data_point_seen = True | ||
if isinstance(point, NumberDataPoint): | ||
number_data_point_seen = True | ||
for attr in point.attributes: | ||
self.assertIn( | ||
attr, _recommended_attrs[metric.name] | ||
) | ||
self.assertTrue(number_data_point_seen and histogram_data_point_seen) | ||
|
||
|
||
class TestProgrammaticHooks(InstrumentationTest, WsgiTestBase): | ||
def setUp(self): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -326,14 +326,30 @@ def collect_custom_response_headers_attributes(response_headers): | |
return attributes | ||
|
||
|
||
def _parse_status_code(resp_status): | ||
def parse_status_code(resp_status): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would still keep this and other functions private. They are not intended to be user accessible. If there is really need we can always change from private to public but not vice versa. |
||
status_code, _ = resp_status.split(" ", 1) | ||
try: | ||
return int(status_code) | ||
except ValueError: | ||
return None | ||
|
||
|
||
def parse_active_request_count_attrs(req_attrs): | ||
TheAnshul756 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
active_requests_count_attrs = {} | ||
for attr_key in _active_requests_count_attrs: | ||
if req_attrs.get(attr_key) is not None: | ||
active_requests_count_attrs[attr_key] = req_attrs[attr_key] | ||
return active_requests_count_attrs | ||
|
||
|
||
def parse_duration_attrs(req_attrs): | ||
duration_attrs = {} | ||
for attr_key in _duration_attrs: | ||
if req_attrs.get(attr_key) is not None: | ||
duration_attrs[attr_key] = req_attrs[attr_key] | ||
return duration_attrs | ||
|
||
|
||
def add_response_attributes( | ||
span, start_response_status, response_headers | ||
): # pylint: disable=unused-argument | ||
|
@@ -412,7 +428,7 @@ def _create_start_response( | |
@functools.wraps(start_response) | ||
def _start_response(status, response_headers, *args, **kwargs): | ||
add_response_attributes(span, status, response_headers) | ||
status_code = _parse_status_code(status) | ||
status_code = parse_status_code(status) | ||
if status_code is not None: | ||
duration_attrs[SpanAttributes.HTTP_STATUS_CODE] = status_code | ||
if span.is_recording() and span.kind == trace.SpanKind.SERVER: | ||
|
@@ -436,15 +452,10 @@ def __call__(self, environ, start_response): | |
start_response: The WSGI start_response callable. | ||
""" | ||
req_attrs = collect_request_attributes(environ) | ||
active_requests_count_attrs = {} | ||
for attr_key in _active_requests_count_attrs: | ||
if req_attrs.get(attr_key) is not None: | ||
active_requests_count_attrs[attr_key] = req_attrs[attr_key] | ||
|
||
duration_attrs = {} | ||
for attr_key in _duration_attrs: | ||
if req_attrs.get(attr_key) is not None: | ||
duration_attrs[attr_key] = req_attrs[attr_key] | ||
active_requests_count_attrs = parse_active_request_count_attrs( | ||
TheAnshul756 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
req_attrs | ||
) | ||
duration_attrs = parse_duration_attrs(req_attrs) | ||
|
||
span, token = _start_internal_or_server_span( | ||
tracer=self.tracer, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't all this logic be in
_start_response
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, that is also a possible way. But we are starting span in before_request and ending it in teardown_request so I did the same for metrics too. Also here it says that, it is discouraged, I'm also not sure what to prefer. If doing it in start_response is more acceptable then I can make the changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, please do. That comment is relevant to only tracing API and they are referring to
update_name
call on span once it is started. It is discouraged because sampling decision would have already been made based on it and such edge cases.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.