Skip to content

Commit 9e0300a

Browse files
GSVarshapvital
authored andcommitted
fix(tests): failing tests after changes
Signed-off-by: Varsha GS <[email protected]>
1 parent 7bf8247 commit 9e0300a

File tree

7 files changed

+39
-37
lines changed

7 files changed

+39
-37
lines changed

src/instana/collector/utils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
from opentelemetry.trace.span import format_span_id
66
from opentelemetry.trace import SpanKind
77

8-
from instana.util.ids import hex_id, internal_id
9-
8+
from instana.util.ids import hex_id
109
if TYPE_CHECKING:
1110
from instana.span.base_span import BaseSpan
1211

@@ -24,7 +23,7 @@ def format_span(
2423
span.t = format_span_id(span.t)
2524
span.s = format_span_id(span.s)
2625
span.p = format_span_id(span.p) if span.p else None
27-
span.lt = hex_id(internal_id(span.lt)) if hasattr(span, "lt") else None
26+
span.lt = hex_id(span.lt) if hasattr(span, "lt") else None
2827
if isinstance(span.k, SpanKind):
2928
span.k = span.k.value if not span.k is SpanKind.INTERNAL else 3
3029
spans.append(span)

src/instana/instrumentation/aws/lambda_inst.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import wrapt
1313
from opentelemetry.semconv.trace import SpanAttributes
14-
from opentelemetry.trace.span import format_span_id
1514

1615
from instana import get_aws_lambda_handler
1716
from instana.instrumentation.aws.triggers import enrich_lambda_span, get_context
@@ -45,7 +44,7 @@ def lambda_handler_with_instana(
4544
result = wrapped(*args, **kwargs)
4645

4746
if isinstance(result, dict):
48-
server_timing_value = define_server_timing(format_span_id(span.context.trace_id))
47+
server_timing_value = define_server_timing(span.context.trace_id)
4948
if "headers" in result:
5049
result["headers"]["Server-Timing"] = server_timing_value
5150
elif "multiValueHeaders" in result:

src/instana/util/ids.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,17 @@ def hex_id(id: Union[int, str]) -> str:
9494
Returns the hexadecimal representation of the given ID.
9595
Left pad with zeros when the length is not equal to 16
9696
"""
97-
98-
hex_id = hex(int(id))[2:]
99-
length = len(hex_id)
100-
# Left pad ID with zeros
101-
if length < 16:
102-
hex_id = hex_id.zfill(16)
103-
elif length > 16 and length < 32:
104-
hex_id = hex_id.zfill(32)
105-
return hex_id
97+
try:
98+
hex_id = hex(int(id))[2:]
99+
length = len(hex_id)
100+
# Left pad ID with zeros
101+
if length < 16:
102+
hex_id = hex_id.zfill(16)
103+
elif length > 16 and length < 32:
104+
hex_id = hex_id.zfill(32)
105+
return hex_id
106+
except ValueError: # Handles ValueError: invalid literal for int() with base 10:
107+
return id
106108

107109
def hex_id_limited(id: Union[int, str]) -> str:
108110
"""
@@ -119,7 +121,7 @@ def hex_id_limited(id: Union[int, str]) -> str:
119121
# Phase 0: Discard everything but the last 16byte
120122
hex_id = hex_id[-16:]
121123
return hex_id
122-
except ValueError: # ValueError: invalid literal for int() with base 10:
124+
except ValueError: # Handles ValueError: invalid literal for int() with base 10:
123125
return id
124126

125127
def define_server_timing(trace_id: Union[int, str]) -> str:

tests/frameworks/test_flask.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def test_get_request_with_suppression(self) -> None:
212212
assert spans == []
213213

214214
def test_get_request_with_suppression_and_w3c(self) -> None:
215-
"""https://github.ibm.com/instana/technical-documentation/tree/master/tracing/specification#incoming-level-0-plus-w3c-trace-context-specification-headers"""
215+
"""Incoming Level 0 Plus W3C Trace Context Specification Headers"""
216216
headers = {
217217
'X-INSTANA-L':'0',
218218
'traceparent': '00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01',

tests/propagators/test_http_propagator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def test_extract_carrier_list(
9898
_traceparent: str,
9999
_tracestate: str,
100100
) -> None:
101+
_trace_id = str(_trace_id)
101102
carrier = [
102103
("user-agent", "python-requests/2.23.0"),
103104
("accept-encoding", "gzip, deflate"),
@@ -213,6 +214,7 @@ def test_extract_carrier_dict_level_header_not_splitable(
213214
_traceparent: str,
214215
_tracestate: str,
215216
) -> None:
217+
_trace_id = str(_trace_id)
216218
carrier = {
217219
"traceparent": _traceparent,
218220
"tracestate": _tracestate,

tests/span/test_base_span.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ def test_basespan_with_synthetic_source_and_kwargs(
5959

6060
assert trace_id == base_span.t
6161
assert span_id == base_span.s
62-
assert base_span.sy
62+
# synthetic should be true only for entry spans
63+
assert not base_span.sy
6364
assert source == base_span.f
6465
assert _kwarg1 == base_span.arg1
6566

@@ -101,7 +102,8 @@ def test_populate_extra_span_attributes_with_values(
101102

102103
assert trace_id == base_span.t
103104
assert span_id == base_span.s
104-
assert base_span.sy
105+
# synthetic should be true only for entry spans
106+
assert not base_span.sy
105107
assert base_span.tp
106108
assert "IDK" == base_span.ia
107109
assert long_id == base_span.lt
Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
# (c) Copyright IBM Corp. 2021
22
# (c) Copyright Instana Inc. 2021
33

4-
import pytest
54
from instana.w3c_trace_context.traceparent import Traceparent
65
import unittest
7-
6+
from instana.util.ids import header_to_long_id, header_to_id
87

98
class TestTraceparent(unittest.TestCase):
109
def setUp(self):
1110
self.tp = Traceparent()
11+
self.w3cTraceId = "4bf92f3577b34da6a3ce929d0e0e4736"
1212

1313
def test_validate_valid(self):
14-
traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"
14+
traceparent = f"00-{self.w3cTraceId}-00f067aa0ba902b7-01"
1515
self.assertEqual(traceparent, self.tp.validate(traceparent))
1616

1717
def test_validate_newer_version(self):
1818
# Although the incoming traceparent header sports a newer version number, we should still be able to parse the
1919
# parts that we understand (and consider it valid).
20-
traceparent = "fe-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01-12345-abcd"
20+
traceparent = f"fe-{self.w3cTraceId}-00f067aa0ba902b7-01-12345-abcd"
2121
self.assertEqual(traceparent, self.tp.validate(traceparent))
2222

2323
def test_validate_unknown_flags(self):
24-
traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-ee"
24+
traceparent = f"00-{self.w3cTraceId}-00f067aa0ba902b7-ee"
2525
self.assertEqual(traceparent, self.tp.validate(traceparent))
2626

2727
def test_validate_invalid_traceparent_version(self):
28-
traceparent = "ff-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"
28+
traceparent = f"ff-{self.w3cTraceId}-00f067aa0ba902b7-01"
2929
self.assertIsNone(self.tp.validate(traceparent))
3030

3131
def test_validate_invalid_traceparent(self):
@@ -37,32 +37,32 @@ def test_validate_traceparent_None(self):
3737
self.assertIsNone(self.tp.validate(traceparent))
3838

3939
def test_get_traceparent_fields(self):
40-
traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"
40+
traceparent = f"00-{self.w3cTraceId}-00f067aa0ba902b7-01"
4141
version, trace_id, parent_id, sampled_flag = self.tp.get_traceparent_fields(traceparent)
42-
self.assertEqual(trace_id, 11803532876627986230)
42+
self.assertEqual(trace_id, header_to_long_id(self.w3cTraceId))
4343
self.assertEqual(parent_id, 67667974448284343)
4444
self.assertTrue(sampled_flag)
4545

4646
def test_get_traceparent_fields_unsampled(self):
47-
traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00"
47+
traceparent = f"00-{self.w3cTraceId}-00f067aa0ba902b7-00"
4848
version, trace_id, parent_id, sampled_flag = self.tp.get_traceparent_fields(traceparent)
49-
self.assertEqual(trace_id, 11803532876627986230)
49+
self.assertEqual(trace_id, header_to_long_id(self.w3cTraceId))
5050
self.assertEqual(parent_id, 67667974448284343)
5151
self.assertFalse(sampled_flag)
5252

5353
def test_get_traceparent_fields_newer_version(self):
5454
# Although the incoming traceparent header sports a newer version number, we should still be able to parse the
5555
# parts that we understand (and consider it valid).
56-
traceparent = "fe-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01-12345-abcd"
56+
traceparent = f"fe-{self.w3cTraceId}-00f067aa0ba902b7-01-12345-abcd"
5757
version, trace_id, parent_id, sampled_flag = self.tp.get_traceparent_fields(traceparent)
58-
self.assertEqual(trace_id, 11803532876627986230)
58+
self.assertEqual(trace_id, header_to_long_id(self.w3cTraceId))
5959
self.assertEqual(parent_id, 67667974448284343)
6060
self.assertTrue(sampled_flag)
6161

6262
def test_get_traceparent_fields_unknown_flags(self):
63-
traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-ff"
63+
traceparent = f"00-{self.w3cTraceId}-00f067aa0ba902b7-ff"
6464
version, trace_id, parent_id, sampled_flag = self.tp.get_traceparent_fields(traceparent)
65-
self.assertEqual(trace_id, 11803532876627986230)
65+
self.assertEqual(trace_id, header_to_long_id(self.w3cTraceId))
6666
self.assertEqual(parent_id, 67667974448284343)
6767
self.assertTrue(sampled_flag)
6868

@@ -80,20 +80,18 @@ def test_get_traceparent_fields_string_input_no_dash(self):
8080
self.assertIsNone(parent_id)
8181
self.assertFalse(sampled_flag)
8282

83-
@pytest.mark.skip("Handled when type of trace and span ids are modified to str")
8483
def test_update_traceparent(self):
85-
traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"
84+
traceparent = f"00-{self.w3cTraceId}-00f067aa0ba902b7-01"
8685
in_trace_id = "1234d0e0e4736234"
8786
in_span_id = "1234567890abcdef"
8887
level = 1
8988
expected_traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-1234567890abcdef-01"
90-
self.assertEqual(expected_traceparent, self.tp.update_traceparent(traceparent, in_trace_id, in_span_id, level))
89+
self.assertEqual(expected_traceparent, self.tp.update_traceparent(traceparent, in_trace_id, header_to_id(in_span_id), level))
9190

92-
@pytest.mark.skip("Handled when type of trace and span ids are modified to str")
9391
def test_update_traceparent_None(self):
9492
traceparent = None
9593
in_trace_id = "1234d0e0e4736234"
9694
in_span_id = "7890abcdef"
9795
level = 0
9896
expected_traceparent = "00-00000000000000001234d0e0e4736234-0000007890abcdef-00"
99-
self.assertEqual(expected_traceparent, self.tp.update_traceparent(traceparent, in_trace_id, in_span_id, level))
97+
self.assertEqual(expected_traceparent, self.tp.update_traceparent(traceparent, in_trace_id, header_to_id(in_span_id), level))

0 commit comments

Comments
 (0)