Skip to content

Commit d8f31db

Browse files
authored
added a inner mehtod to help clean up repeated code for dicts
1 parent cb50873 commit d8f31db

File tree

1 file changed

+14
-12
lines changed
  • instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi

1 file changed

+14
-12
lines changed

instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def client_response_hook(span: Span, message: dict):
193193
import urllib
194194
from functools import wraps
195195
from timeit import default_timer
196-
from typing import Tuple
196+
from typing import Tuple, List, Dict
197197

198198
from asgiref.compatibility import guarantee_single_callable
199199

@@ -277,19 +277,23 @@ def append_keys(key):
277277
if isinstance(key, bytes):
278278
returnable.append(key.decode("utf8"))
279279
elif isinstance(key, str):
280-
returnable.append(key)
280+
returnable.append(key)
281281

282-
#carrier is a dict, so iterate over .items()
283-
for _key, _val in carrier.items():
284-
285-
#if we have another dict, lets make a recursive call
286-
if isinstance(_val, Dict):
282+
def append_dict_keys(key, val):
287283
#append our current key
288284
append_keys(_key)
289285

290286
#append all keys within the dict
291287
for x in self.keys(_val):
292-
append_keys(x)
288+
append_keys(x)
289+
290+
#carrier is a dict, so iterate over .items()
291+
for _key, _val in carrier.items():
292+
293+
#if we have another dict, lets make a recursive call
294+
if isinstance(_val, Dict):
295+
296+
append_dict_keys(_key, _val)
293297

294298
# if we have a list, lets iter over that. List can contain tuples(headers) dicts and string so lets approach them all as well
295299
elif isinstance(_val, List):
@@ -301,11 +305,9 @@ def append_keys(key):
301305

302306
#check for the dict
303307
elif isinstance(list_key, Dict):
304-
append_keys(_key)
305308

306-
#append all keys within the dict
307-
for x in self.keys(_val):
308-
append_keys(x)
309+
append_dict_keys(_key, _val)
310+
309311
else:
310312
append_keys(list_key)
311313

0 commit comments

Comments
 (0)