Skip to content

Commit

Permalink
fix(tests): failing tests after changes
Browse files Browse the repository at this point in the history
Signed-off-by: Varsha GS <varsha.gs@ibm.com>
  • Loading branch information
GSVarsha authored and pvital committed Oct 17, 2024
1 parent 7bf8247 commit 9e0300a
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 37 deletions.
5 changes: 2 additions & 3 deletions src/instana/collector/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
from opentelemetry.trace.span import format_span_id
from opentelemetry.trace import SpanKind

from instana.util.ids import hex_id, internal_id

from instana.util.ids import hex_id
if TYPE_CHECKING:
from instana.span.base_span import BaseSpan

Expand All @@ -24,7 +23,7 @@ def format_span(
span.t = format_span_id(span.t)
span.s = format_span_id(span.s)
span.p = format_span_id(span.p) if span.p else None
span.lt = hex_id(internal_id(span.lt)) if hasattr(span, "lt") else None
span.lt = hex_id(span.lt) if hasattr(span, "lt") else None
if isinstance(span.k, SpanKind):
span.k = span.k.value if not span.k is SpanKind.INTERNAL else 3
spans.append(span)
Expand Down
3 changes: 1 addition & 2 deletions src/instana/instrumentation/aws/lambda_inst.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import wrapt
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.trace.span import format_span_id

from instana import get_aws_lambda_handler
from instana.instrumentation.aws.triggers import enrich_lambda_span, get_context
Expand Down Expand Up @@ -45,7 +44,7 @@ def lambda_handler_with_instana(
result = wrapped(*args, **kwargs)

if isinstance(result, dict):
server_timing_value = define_server_timing(format_span_id(span.context.trace_id))
server_timing_value = define_server_timing(span.context.trace_id)
if "headers" in result:
result["headers"]["Server-Timing"] = server_timing_value
elif "multiValueHeaders" in result:
Expand Down
22 changes: 12 additions & 10 deletions src/instana/util/ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,17 @@ def hex_id(id: Union[int, str]) -> str:
Returns the hexadecimal representation of the given ID.
Left pad with zeros when the length is not equal to 16
"""

hex_id = hex(int(id))[2:]
length = len(hex_id)
# Left pad ID with zeros
if length < 16:
hex_id = hex_id.zfill(16)
elif length > 16 and length < 32:
hex_id = hex_id.zfill(32)
return hex_id
try:
hex_id = hex(int(id))[2:]
length = len(hex_id)
# Left pad ID with zeros
if length < 16:
hex_id = hex_id.zfill(16)
elif length > 16 and length < 32:
hex_id = hex_id.zfill(32)
return hex_id
except ValueError: # Handles ValueError: invalid literal for int() with base 10:
return id

def hex_id_limited(id: Union[int, str]) -> str:
"""
Expand All @@ -119,7 +121,7 @@ def hex_id_limited(id: Union[int, str]) -> str:
# Phase 0: Discard everything but the last 16byte
hex_id = hex_id[-16:]
return hex_id
except ValueError: # ValueError: invalid literal for int() with base 10:
except ValueError: # Handles ValueError: invalid literal for int() with base 10:
return id

def define_server_timing(trace_id: Union[int, str]) -> str:
Expand Down
2 changes: 1 addition & 1 deletion tests/frameworks/test_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def test_get_request_with_suppression(self) -> None:
assert spans == []

def test_get_request_with_suppression_and_w3c(self) -> None:
"""https://github.ibm.com/instana/technical-documentation/tree/master/tracing/specification#incoming-level-0-plus-w3c-trace-context-specification-headers"""
"""Incoming Level 0 Plus W3C Trace Context Specification Headers"""
headers = {
'X-INSTANA-L':'0',
'traceparent': '00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01',
Expand Down
2 changes: 2 additions & 0 deletions tests/propagators/test_http_propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def test_extract_carrier_list(
_traceparent: str,
_tracestate: str,
) -> None:
_trace_id = str(_trace_id)
carrier = [
("user-agent", "python-requests/2.23.0"),
("accept-encoding", "gzip, deflate"),
Expand Down Expand Up @@ -213,6 +214,7 @@ def test_extract_carrier_dict_level_header_not_splitable(
_traceparent: str,
_tracestate: str,
) -> None:
_trace_id = str(_trace_id)
carrier = {
"traceparent": _traceparent,
"tracestate": _tracestate,
Expand Down
6 changes: 4 additions & 2 deletions tests/span/test_base_span.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ def test_basespan_with_synthetic_source_and_kwargs(

assert trace_id == base_span.t
assert span_id == base_span.s
assert base_span.sy
# synthetic should be true only for entry spans
assert not base_span.sy
assert source == base_span.f
assert _kwarg1 == base_span.arg1

Expand Down Expand Up @@ -101,7 +102,8 @@ def test_populate_extra_span_attributes_with_values(

assert trace_id == base_span.t
assert span_id == base_span.s
assert base_span.sy
# synthetic should be true only for entry spans
assert not base_span.sy
assert base_span.tp
assert "IDK" == base_span.ia
assert long_id == base_span.lt
Expand Down
36 changes: 17 additions & 19 deletions tests/w3c_trace_context/test_traceparent.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
# (c) Copyright IBM Corp. 2021
# (c) Copyright Instana Inc. 2021

import pytest
from instana.w3c_trace_context.traceparent import Traceparent
import unittest

from instana.util.ids import header_to_long_id, header_to_id

class TestTraceparent(unittest.TestCase):
def setUp(self):
self.tp = Traceparent()
self.w3cTraceId = "4bf92f3577b34da6a3ce929d0e0e4736"

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

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

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

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

def test_validate_invalid_traceparent(self):
Expand All @@ -37,32 +37,32 @@ def test_validate_traceparent_None(self):
self.assertIsNone(self.tp.validate(traceparent))

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

def test_get_traceparent_fields_unsampled(self):
traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00"
traceparent = f"00-{self.w3cTraceId}-00f067aa0ba902b7-00"
version, trace_id, parent_id, sampled_flag = self.tp.get_traceparent_fields(traceparent)
self.assertEqual(trace_id, 11803532876627986230)
self.assertEqual(trace_id, header_to_long_id(self.w3cTraceId))
self.assertEqual(parent_id, 67667974448284343)
self.assertFalse(sampled_flag)

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

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

Expand All @@ -80,20 +80,18 @@ def test_get_traceparent_fields_string_input_no_dash(self):
self.assertIsNone(parent_id)
self.assertFalse(sampled_flag)

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

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

0 comments on commit 9e0300a

Please sign in to comment.