@@ -21,51 +21,44 @@ def check_span_attributes(self, span, attributes):
21
21
for attribute_name in span .attributes .keys ():
22
22
# Check if the attribute name exists in the input attributes
23
23
if attribute_name not in attribute_dict :
24
- print ("Attribute name " + attribute_name + " not in attribute dictionary" )
25
- return False
24
+ raise AssertionError ("Attribute name " + attribute_name + " not in attribute dictionary" )
26
25
27
26
attribute_value = attribute_dict [attribute_name ]
28
27
if isinstance (attribute_value , list ):
29
28
# Check if the attribute value in the span matches the provided list
30
29
if span .attributes [attribute_name ] != attribute_value :
31
- print (
30
+ raise AssertionError (
32
31
"Attribute value list "
33
32
+ str (span .attributes [attribute_name ])
34
33
+ " does not match with "
35
34
+ str (attribute_value )
36
35
)
37
- return False
38
36
elif isinstance (attribute_value , tuple ):
39
37
# Check if the attribute value in the span matches the provided list
40
38
if span .attributes [attribute_name ] != attribute_value :
41
- print (
39
+ raise AssertionError (
42
40
"Attribute value tuple "
43
41
+ str (span .attributes [attribute_name ])
44
42
+ " does not match with "
45
43
+ str (attribute_value )
46
44
)
47
- return False
48
45
else :
49
46
# Check if the attribute value matches the provided value
50
47
if attribute_value == "+" :
51
48
if not isinstance (span .attributes [attribute_name ], numbers .Number ):
52
- print ("Attribute value " + str (span .attributes [attribute_name ]) + " is not a number" )
53
- return False
49
+ raise AssertionError ("Attribute value " + str (span .attributes [attribute_name ]) + " is not a number" )
54
50
if span .attributes [attribute_name ] < 0 :
55
- print ("Attribute value " + str (span .attributes [attribute_name ]) + " is negative" )
56
- return False
51
+ raise AssertionError ("Attribute value " + str (span .attributes [attribute_name ]) + " is negative" )
57
52
elif attribute_value != "" and span .attributes [attribute_name ] != attribute_value :
58
- print (
53
+ raise AssertionError (
59
54
"Attribute value "
60
55
+ str (span .attributes [attribute_name ])
61
56
+ " does not match with "
62
57
+ str (attribute_value )
63
58
)
64
- return False
65
59
# Check if the attribute value in the span is not empty when the provided value is ""
66
60
elif attribute_value == "" and not span .attributes [attribute_name ]:
67
- print ("Expected non-empty attribute value" )
68
- return False
61
+ raise AssertionError ("Expected non-empty attribute value" )
69
62
70
63
return True
71
64
@@ -76,63 +69,53 @@ def check_decorator_span_attributes(self, span: Span, attributes: List[tuple]) -
76
69
# Ensure all required attributes are present in the span
77
70
for attribute_name in attribute_dict .keys ():
78
71
if attribute_name not in span .attributes :
79
- print ("Required attribute name " + attribute_name + " not found in span attributes" )
80
- return False
72
+ raise AssertionError ("Required attribute name " + attribute_name + " not found in span attributes" )
81
73
82
74
for attribute_name in span .attributes .keys ():
83
75
# Check if the attribute name exists in the input attributes
84
76
if attribute_name not in attribute_dict :
85
- print ("Attribute name " + attribute_name + " not in attribute dictionary" )
86
- return False
77
+ raise AssertionError ("Attribute name " + attribute_name + " not in attribute dictionary" )
87
78
88
79
attribute_value = attribute_dict [attribute_name ]
89
80
span_value = span .attributes [attribute_name ]
90
81
91
82
if isinstance (attribute_value , (list , tuple )):
92
83
# Convert both to lists for comparison
93
84
if list (span_value ) != list (attribute_value ):
94
- print (
85
+ raise AssertionError (
95
86
"Attribute value list/tuple " + str (span_value ) + " does not match with " + str (attribute_value )
96
87
)
97
- return False
98
88
elif isinstance (attribute_value , dict ):
99
89
# Check if both are dictionaries and compare them
100
90
if not isinstance (span_value , dict ) or span_value != attribute_value :
101
- print ("Attribute value dict " + str (span_value ) + " does not match with " + str (attribute_value ))
102
- return False
91
+ raise AssertionError ("Attribute value dict " + str (span_value ) + " does not match with " + str (attribute_value ))
103
92
else :
104
93
# Check if the attribute value matches the provided value
105
94
if attribute_value == "+" :
106
95
if not isinstance (span_value , numbers .Number ):
107
- print ("Attribute value " + str (span_value ) + " is not a number" )
108
- return False
96
+ raise AssertionError ("Attribute value " + str (span_value ) + " is not a number" )
109
97
if span_value < 0 :
110
- print ("Attribute value " + str (span_value ) + " is negative" )
111
- return False
98
+ raise AssertionError ("Attribute value " + str (span_value ) + " is negative" )
112
99
elif attribute_value != "" and span_value != attribute_value :
113
- print ("Attribute value " + str (span_value ) + " does not match with " + str (attribute_value ))
114
- return False
100
+ raise AssertionError ("Attribute value " + str (span_value ) + " does not match with " + str (attribute_value ))
115
101
# Check if the attribute value in the span is not empty when the provided value is ""
116
102
elif attribute_value == "" and not span_value :
117
- print ("Expected non-empty attribute value" )
118
- return False
103
+ raise AssertionError ("Expected non-empty attribute value" )
119
104
120
105
return True
121
106
122
107
def is_valid_json (self , my_string ):
123
108
try :
124
- json_object = json .loads (my_string )
109
+ json .loads (my_string )
125
110
except ValueError as e1 :
126
111
return False
127
112
except TypeError as e2 :
128
113
return False
129
114
return True
130
115
131
116
def check_json_string (self , expected_json , actual_json ):
132
- if self .is_valid_json (expected_json ) and self .is_valid_json (actual_json ):
133
- return self .check_event_attributes (json .loads (expected_json ), json .loads (actual_json ))
134
- else :
135
- return False
117
+ assert self .is_valid_json (expected_json ) and self .is_valid_json (actual_json )
118
+ return self .check_event_attributes (json .loads (expected_json ), json .loads (actual_json ))
136
119
137
120
def check_event_attributes (self , expected_dict , actual_dict ):
138
121
if set (expected_dict .keys ()) != set (actual_dict .keys ()):
@@ -144,56 +127,39 @@ def check_event_attributes(self, expected_dict, actual_dict):
144
127
actual_val = json .dumps (actual_dict )
145
128
else :
146
129
actual_val = actual_dict
147
- print ("check_event_attributes: keys do not match: " + expected_val + "!=" + actual_val )
148
- return False
130
+ raise AssertionError ("check_event_attributes: keys do not match: " + expected_val + "!=" + actual_val )
149
131
for key , expected_val in expected_dict .items ():
150
132
if key not in actual_dict :
151
- print ("check_event_attributes: key not found" )
152
- return False
133
+ raise AssertionError (f"check_event_attributes: key { key } not found in actuals" )
153
134
actual_val = actual_dict [key ]
154
135
155
136
if self .is_valid_json (expected_val ):
156
137
if not self .is_valid_json (actual_val ):
157
- print ("check_event_attributes: actual_val is not valid json" )
158
- return False
159
- if not self .check_json_string (expected_val , actual_val ):
160
- print ("check_event_attributes: check_json_string failed" )
161
- return False
138
+ raise AssertionError (f"check_event_attributes: actual_val for { key } is not valid json" )
139
+ self .check_json_string (expected_val , actual_val )
162
140
elif isinstance (expected_val , dict ):
163
141
if not isinstance (actual_val , dict ):
164
- print ("check_event_attributes: actual_val is not dict" )
165
- return False
166
- if not self .check_event_attributes (expected_val , actual_val ):
167
- print ("check_event_attributes: check_event_attributes failed" )
168
- return False
142
+ raise AssertionError (f"check_event_attributes: actual_val for { key } is not dict" )
143
+ self .check_event_attributes (expected_val , actual_val )
169
144
elif isinstance (expected_val , list ):
170
145
if not isinstance (actual_val , list ):
171
- print ("check_event_attributes: actual_val is not list" )
172
- return False
146
+ raise AssertionError (f"check_event_attributes: actual_val for { key } is not list" )
173
147
if len (expected_val ) != len (actual_val ):
174
- print ("check_event_attributes: list lengths do not match" )
175
- return False
148
+ raise AssertionError (f"check_event_attributes: list lengths do not match for key { key } : expected { len (expected_val )} , actual { len (actual_val )} " )
176
149
for expected_list , actual_list in zip (expected_val , actual_val ):
177
- if not self .check_event_attributes (expected_list , actual_list ):
178
- print ("check_event_attributes: check_event_attributes for list failed" )
179
- return False
150
+ self .check_event_attributes (expected_list , actual_list )
180
151
elif isinstance (expected_val , str ) and expected_val == "*" :
181
152
if actual_val == "" :
182
- print ("check_event_attributes: actual_val is empty" )
183
- return False
153
+ raise AssertionError (f"check_event_attributes: actual_val for { key } is empty" )
184
154
elif isinstance (expected_val , str ) and expected_val == "+" :
185
- if not isinstance (actual_val , numbers .Number ):
186
- return False
187
- if actual_val < 0 :
188
- return False
155
+ assert isinstance (actual_val , numbers .Number ), f"The { key } is not a number."
156
+ assert actual_val > 0 , f"The { key } is <0 { actual_val } "
189
157
elif expected_val != actual_val :
190
158
if isinstance (expected_val , dict ):
191
159
expected_val = json .dumps (expected_val )
192
160
if isinstance (actual_val , dict ):
193
161
actual_val = json .dumps (actual_val )
194
- print ("check_event_attributes: values do not match: " + expected_val + "!=" + actual_val )
195
- return False
196
- return True
162
+ raise AssertionError (f"check_event_attributes: values do not match for key { key } : { expected_val } != { actual_val } " )
197
163
198
164
def check_span_events (self , span , expected_events ):
199
165
print ("Checking span: " + span .name )
@@ -202,17 +168,13 @@ def check_span_events(self, span, expected_events):
202
168
for expected_event in expected_events :
203
169
for actual_event in span_events :
204
170
if expected_event ["name" ] == actual_event .name :
205
- if not self .check_event_attributes (expected_event ["attributes" ], actual_event .attributes ._dict ):
206
- print ("Event attributes do not match" )
207
- return False
171
+ self .check_event_attributes (expected_event ["attributes" ], actual_event .attributes ._dict )
208
172
span_events .remove (actual_event ) # Remove the matched event from the span_events
209
173
break
210
174
else :
211
- print ("check_span_events: event not found" )
212
- return False # If no match found for an expected event
175
+ raise AssertionError ("check_span_events: event not found" )
213
176
214
177
if len (span_events ) > 0 : # If there are any additional events in the span_events
215
- print ("check_span_events: unexpected event found" )
216
- return False
178
+ raise AssertionError ("check_span_events: unexpected event found" )
217
179
218
180
return True
0 commit comments