-
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 all 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 |
---|---|---|
|
@@ -14,3 +14,5 @@ | |
|
||
|
||
_instruments = ("flask >= 1.0, < 3.0",) | ||
|
||
_supports_metrics = True |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from timeit import default_timer | ||
from unittest.mock import Mock, patch | ||
|
||
from flask import Flask, request | ||
|
@@ -23,7 +24,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 +58,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 +269,106 @@ def test_exclude_lists_from_explicit(self): | |
span_list = self.memory_exporter.get_finished_spans() | ||
self.assertEqual(len(span_list), 1) | ||
|
||
def test_flask_metrics(self): | ||
start = default_timer() | ||
self.client.get("/hello/123") | ||
self.client.get("/hello/321") | ||
self.client.get("/hello/756") | ||
duration = max(round((default_timer() - start) * 1000), 0) | ||
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. |
||
self.assertAlmostEqual( | ||
duration, point.sum, delta=10 | ||
) | ||
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) | ||
|
||
def test_flask_metric_values(self): | ||
start = default_timer() | ||
self.client.post("/hello/756") | ||
self.client.post("/hello/756") | ||
self.client.post("/hello/756") | ||
duration = max(round((default_timer() - start) * 1000), 0) | ||
metrics_list = self.memory_metrics_reader.get_metrics_data() | ||
for resource_metric in metrics_list.resource_metrics: | ||
for scope_metric in resource_metric.scope_metrics: | ||
for metric in scope_metric.metrics: | ||
for point in list(metric.data.data_points): | ||
if isinstance(point, HistogramDataPoint): | ||
self.assertEqual(point.count, 3) | ||
self.assertAlmostEqual( | ||
duration, point.sum, delta=10 | ||
) | ||
if isinstance(point, NumberDataPoint): | ||
self.assertEqual(point.value, 0) | ||
|
||
def test_basic_metric_success(self): | ||
self.client.get("/hello/756") | ||
expected_duration_attributes = { | ||
"http.method": "GET", | ||
"http.host": "localhost", | ||
"http.scheme": "http", | ||
"http.flavor": "1.1", | ||
"http.server_name": "localhost", | ||
"net.host.port": 80, | ||
"http.status_code": 200, | ||
} | ||
expected_requests_count_attributes = { | ||
"http.method": "GET", | ||
"http.host": "localhost", | ||
"http.scheme": "http", | ||
"http.flavor": "1.1", | ||
"http.server_name": "localhost", | ||
} | ||
metrics_list = self.memory_metrics_reader.get_metrics_data() | ||
for resource_metric in metrics_list.resource_metrics: | ||
for scope_metrics in resource_metric.scope_metrics: | ||
for metric in scope_metrics.metrics: | ||
for point in list(metric.data.data_points): | ||
if isinstance(point, HistogramDataPoint): | ||
self.assertDictEqual( | ||
expected_duration_attributes, | ||
dict(point.attributes), | ||
) | ||
self.assertEqual(point.count, 1) | ||
elif isinstance(point, NumberDataPoint): | ||
self.assertDictEqual( | ||
expected_requests_count_attributes, | ||
dict(point.attributes), | ||
) | ||
self.assertEqual(point.value, 0) | ||
|
||
def test_metric_uninstrument(self): | ||
self.client.delete("/hello/756") | ||
FlaskInstrumentor().uninstrument_app(self.app) | ||
self.client.delete("/hello/756") | ||
metrics_list = self.memory_metrics_reader.get_metrics_data() | ||
for resource_metric in metrics_list.resource_metrics: | ||
for scope_metric in resource_metric.scope_metrics: | ||
for metric in scope_metric.metrics: | ||
for point in list(metric.data.data_points): | ||
if isinstance(point, HistogramDataPoint): | ||
self.assertEqual(point.count, 1) | ||
|
||
|
||
class TestProgrammaticHooks(InstrumentationTest, WsgiTestBase): | ||
def setUp(self): | ||
|
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.
I'm wondering if this is really the correct place to start the timer. I'm not too sure when
_wrapped_app
is called but is it invoked everytime a request is made or is only_start_response
called upon each request?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, it is invoked every time.