Skip to content

Commit 8b281b2

Browse files
author
Filip Nikolovski
committed
update tests
1 parent e12ace6 commit 8b281b2

File tree

1 file changed

+60
-20
lines changed
  • instrumentation/opentelemetry-instrumentation-falcon/tests

1 file changed

+60
-20
lines changed

instrumentation/opentelemetry-instrumentation-falcon/tests/test_falcon.py

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
#
15+
from timeit import default_timer
1516
from unittest.mock import Mock, patch
1617

1718
import pytest
@@ -39,6 +40,24 @@
3940
NumberDataPoint,
4041
)
4142
from opentelemetry.sdk.resources import Resource
43+
from opentelemetry.semconv.attributes.client_attributes import (
44+
CLIENT_PORT,
45+
)
46+
from opentelemetry.semconv.attributes.http_attributes import (
47+
HTTP_REQUEST_METHOD,
48+
HTTP_RESPONSE_STATUS_CODE,
49+
)
50+
from opentelemetry.semconv.attributes.network_attributes import (
51+
NETWORK_PROTOCOL_VERSION,
52+
)
53+
from opentelemetry.semconv.attributes.server_attributes import (
54+
SERVER_ADDRESS,
55+
SERVER_PORT,
56+
)
57+
from opentelemetry.semconv.attributes.url_attributes import (
58+
URL_PATH,
59+
URL_SCHEME,
60+
)
4261
from opentelemetry.semconv.trace import SpanAttributes
4362
from opentelemetry.test.test_base import TestBase
4463
from opentelemetry.test.wsgitestutil import WsgiTestBase
@@ -219,17 +238,15 @@ def _test_method(self, method, old_semconv=True, new_semconv=False):
219238
SpanAttributes.HTTP_ROUTE: "/hello",
220239
}
221240
expected_attributes_new = {
222-
SpanAttributes.HTTP_REQUEST_METHOD: method,
223-
SpanAttributes.SERVER_ADDRESS: "falconframework.org",
224-
SpanAttributes.URL_SCHEME: "http",
225-
SpanAttributes.SERVER_PORT: 80,
226-
SpanAttributes.URL_PATH: "/"
227-
if self._has_fixed_http_target
228-
else "/hello",
229-
SpanAttributes.CLIENT_PORT: 65133,
230-
SpanAttributes.NETWORK_PROTOCOL_VERSION: "1.1",
241+
HTTP_REQUEST_METHOD: method,
242+
SERVER_ADDRESS: "falconframework.org",
243+
URL_SCHEME: "http",
244+
SERVER_PORT: 80,
245+
URL_PATH: "/" if self._has_fixed_http_target else "/hello",
246+
CLIENT_PORT: 65133,
247+
NETWORK_PROTOCOL_VERSION: "1.1",
231248
"falcon.resource": "HelloWorldResource",
232-
SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 201,
249+
HTTP_RESPONSE_STATUS_CODE: 201,
233250
SpanAttributes.HTTP_ROUTE: "/hello",
234251
}
235252

@@ -342,17 +359,15 @@ def test_url_template_new_semconv(self):
342359
self.assertSpanHasAttributes(
343360
span,
344361
{
345-
SpanAttributes.HTTP_REQUEST_METHOD: "GET",
346-
SpanAttributes.SERVER_ADDRESS: "falconframework.org",
347-
SpanAttributes.URL_SCHEME: "http",
348-
SpanAttributes.SERVER_PORT: 80,
349-
SpanAttributes.URL_PATH: "/"
350-
if self._has_fixed_http_target
351-
else "/user/123",
352-
SpanAttributes.CLIENT_PORT: 65133,
353-
SpanAttributes.NETWORK_PROTOCOL_VERSION: "1.1",
362+
HTTP_REQUEST_METHOD: "GET",
363+
SERVER_ADDRESS: "falconframework.org",
364+
URL_SCHEME: "http",
365+
SERVER_PORT: 80,
366+
URL_PATH: "/" if self._has_fixed_http_target else "/user/123",
367+
CLIENT_PORT: 65133,
368+
NETWORK_PROTOCOL_VERSION: "1.1",
354369
"falcon.resource": "UserResource",
355-
SpanAttributes.HTTP_RESPONSE_STATUS_CODE: 200,
370+
HTTP_RESPONSE_STATUS_CODE: 200,
356371
SpanAttributes.HTTP_ROUTE: "/user/{user_id}",
357372
},
358373
)
@@ -519,7 +534,10 @@ def test_falcon_metric_values_new_semconv(self):
519534
number_data_point_seen = False
520535
histogram_data_point_seen = False
521536

537+
start = default_timer()
522538
self.client().simulate_get("/hello/756")
539+
duration = max(default_timer() - start, 0)
540+
523541
metrics_list = self.memory_metrics_reader.get_metrics_data()
524542
for resource_metric in metrics_list.resource_metrics:
525543
for scope_metric in resource_metric.scope_metrics:
@@ -530,6 +548,9 @@ def test_falcon_metric_values_new_semconv(self):
530548
if isinstance(point, HistogramDataPoint):
531549
self.assertEqual(point.count, 1)
532550
histogram_data_point_seen = True
551+
self.assertAlmostEqual(
552+
duration, point.sum, delta=10
553+
)
533554
if isinstance(point, NumberDataPoint):
534555
self.assertEqual(point.value, 0)
535556
number_data_point_seen = True
@@ -545,7 +566,10 @@ def test_falcon_metric_values_both_semconv(self):
545566
number_data_point_seen = False
546567
histogram_data_point_seen = False
547568

569+
start = default_timer()
548570
self.client().simulate_get("/hello/756")
571+
duration_s = default_timer() - start
572+
549573
metrics_list = self.memory_metrics_reader.get_metrics_data()
550574
for resource_metric in metrics_list.resource_metrics:
551575
for scope_metric in resource_metric.scope_metrics:
@@ -565,6 +589,16 @@ def test_falcon_metric_values_both_semconv(self):
565589
for point in list(metric.data.data_points):
566590
if isinstance(point, HistogramDataPoint):
567591
self.assertEqual(point.count, 1)
592+
if metric.unit == "ms":
593+
self.assertAlmostEqual(
594+
max(round(duration_s * 1000), 0),
595+
point.sum,
596+
delta=10,
597+
)
598+
elif metric.unit == "s":
599+
self.assertAlmostEqual(
600+
max(duration_s, 0), point.sum, delta=10
601+
)
568602
histogram_data_point_seen = True
569603
if isinstance(point, NumberDataPoint):
570604
self.assertEqual(point.value, 0)
@@ -580,7 +614,10 @@ def test_falcon_metric_values(self):
580614
number_data_point_seen = False
581615
histogram_data_point_seen = False
582616

617+
start = default_timer()
583618
self.client().simulate_get("/hello/756")
619+
duration = max(round((default_timer() - start) * 1000), 0)
620+
584621
metrics_list = self.memory_metrics_reader.get_metrics_data()
585622
for resource_metric in metrics_list.resource_metrics:
586623
for scope_metric in resource_metric.scope_metrics:
@@ -591,6 +628,9 @@ def test_falcon_metric_values(self):
591628
if isinstance(point, HistogramDataPoint):
592629
self.assertEqual(point.count, 1)
593630
histogram_data_point_seen = True
631+
self.assertAlmostEqual(
632+
duration, point.sum, delta=10
633+
)
594634
if isinstance(point, NumberDataPoint):
595635
self.assertEqual(point.value, 0)
596636
number_data_point_seen = True

0 commit comments

Comments
 (0)