-
Notifications
You must be signed in to change notification settings - Fork 810
Fix FlaskInstrumentor exemplars generation for http.server.(request.)duration #3912
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
Open
tammy-baylis-swi
wants to merge
12
commits into
open-telemetry:main
Choose a base branch
from
tammy-baylis-swi:exemplars-flask
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+189
−2
Open
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ea25349
Fix FlaskInstrumentor http.server.request.duration exemplars
tammy-baylis-swi f82f6a2
Fix FlaskInstrumentor http.server.duration exemplars
tammy-baylis-swi c4341a8
Add unit test
tammy-baylis-swi 4c64b46
Changelog
tammy-baylis-swi 37ac4fb
lint
tammy-baylis-swi e8ce689
Add flask functional test for exemplars
tammy-baylis-swi 5adc274
lint
tammy-baylis-swi 7384b8b
More docker-test deps
tammy-baylis-swi 91aedd8
Merge branch 'main' into exemplars-flask
tammy-baylis-swi 6e38a77
More concise
tammy-baylis-swi 0a52355
Merge branch 'main' into exemplars-flask
tammy-baylis-swi ac2aace
Merge branch 'main' into exemplars-flask
tammy-baylis-swi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
tests/opentelemetry-docker-tests/tests/flask/test_flask_functional.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from flask import Flask | ||
|
|
||
| from opentelemetry import metrics as metrics_api | ||
| from opentelemetry.instrumentation.flask import FlaskInstrumentor | ||
| from opentelemetry.sdk.metrics import AlwaysOnExemplarFilter | ||
| from opentelemetry.test.globals_test import ( | ||
| reset_metrics_globals, | ||
| ) | ||
| from opentelemetry.test.test_base import TestBase | ||
| from opentelemetry.trace import ( | ||
| INVALID_SPAN_ID, | ||
| INVALID_TRACE_ID, | ||
| ) | ||
|
|
||
|
|
||
| class TestFunctionalFlask(TestBase): | ||
| def setUp(self): | ||
| super().setUp() | ||
| self.memory_exporter.clear() | ||
| # This is done because set_meter_provider cannot override the | ||
| # current meter provider. | ||
| reset_metrics_globals() | ||
| ( | ||
| self.meter_provider, | ||
| self.memory_metrics_reader, | ||
| ) = self.create_meter_provider( | ||
| exemplar_filter=AlwaysOnExemplarFilter(), | ||
| ) | ||
| metrics_api.set_meter_provider(self.meter_provider) | ||
|
|
||
| self._app = Flask(__name__) | ||
|
|
||
| @self._app.route("/test/") | ||
| def test_route(): | ||
| return "Test response" | ||
|
|
||
| self._client = self._app.test_client() | ||
|
|
||
| FlaskInstrumentor().instrument_app( | ||
| self._app, | ||
| meter_provider=self.meter_provider, | ||
| ) | ||
|
|
||
| def tearDown(self): | ||
| FlaskInstrumentor().uninstrument() | ||
| super().tearDown() | ||
|
|
||
| def test_duration_metrics_exemplars(self): | ||
| """Should generate exemplars with trace and span IDs for Flask HTTP requests.""" | ||
| self._client.get("/test/") | ||
| self._client.get("/test/") | ||
| self._client.get("/test/") | ||
|
|
||
| metrics_data = self.memory_metrics_reader.get_metrics_data() | ||
| self.assertIsNotNone(metrics_data) | ||
| self.assertTrue(len(metrics_data.resource_metrics) > 0) | ||
|
|
||
| duration_metric = None | ||
| metric_names = [] | ||
| for resource_metric in metrics_data.resource_metrics: | ||
| for scope_metric in resource_metric.scope_metrics: | ||
| for metric in scope_metric.metrics: | ||
| metric_names.append(metric.name) | ||
| if metric.name in [ | ||
| "http.server.request.duration", | ||
| "http.server.duration", | ||
| ]: | ||
| duration_metric = metric | ||
| break | ||
| if duration_metric: | ||
| break | ||
| if duration_metric: | ||
| break | ||
|
|
||
| self.assertIsNotNone(duration_metric) | ||
| data_points = list(duration_metric.data.data_points) | ||
| self.assertTrue(len(data_points) > 0) | ||
|
|
||
| exemplar_count = 0 | ||
| for data_point in data_points: | ||
| if hasattr(data_point, "exemplars") and data_point.exemplars: | ||
| for exemplar in data_point.exemplars: | ||
| exemplar_count += 1 | ||
| # Exemplar has required fields and valid span context | ||
| self.assertIsNotNone(exemplar.value) | ||
| self.assertIsNotNone(exemplar.time_unix_nano) | ||
| self.assertIsNotNone(exemplar.span_id) | ||
| self.assertNotEqual(exemplar.span_id, INVALID_SPAN_ID) | ||
| self.assertIsNotNone(exemplar.trace_id) | ||
| self.assertNotEqual(exemplar.trace_id, INVALID_TRACE_ID) | ||
|
|
||
| # Trace and span ID of exemplar are part of finished spans | ||
| finished_spans = self.memory_exporter.get_finished_spans() | ||
| finished_span_ids = [ | ||
| span.context.span_id for span in finished_spans | ||
| ] | ||
| finished_trace_ids = [ | ||
| span.context.trace_id for span in finished_spans | ||
| ] | ||
| self.assertIn(exemplar.span_id, finished_span_ids) | ||
| self.assertIn(exemplar.trace_id, finished_trace_ids) | ||
|
|
||
| # At least one exemplar was generated | ||
| self.assertGreater(exemplar_count, 0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
What do you think of doing something like
metrics_context = context.set_value(_SPAN_KEY, span) if span else Noneand reuse the same record call to keep the code more compact?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! Changed in 6e38a77