diff --git a/sentry_sdk/tracing_utils.py b/sentry_sdk/tracing_utils.py index b81d647c6d..90bf1a59a8 100644 --- a/sentry_sdk/tracing_utils.py +++ b/sentry_sdk/tracing_utils.py @@ -527,7 +527,9 @@ def _fill_sample_rand(self): ) return - self.dynamic_sampling_context["sample_rand"] = f"{sample_rand:.6f}" # noqa: E231 + self.dynamic_sampling_context["sample_rand"] = ( + f"{sample_rand:.6f}" # noqa: E231 + ) def _sample_rand(self): # type: () -> Optional[str] @@ -753,16 +755,22 @@ def should_propagate_trace(client, url): def normalize_incoming_data(incoming_data): - # type: (Dict[str, Any]) -> Dict[str, Any] """ Normalizes incoming data so the keys are all lowercase with dashes instead of underscores and stripped from known prefixes. """ + HTTP_PREFIX = "HTTP_" + HTTP_PREFIX_LEN = len(HTTP_PREFIX) + # Local var lookups are faster in inner loops + replace = str.replace + lower = str.lower + data = {} + # Use items() as in the original, but variables optimized above for key, value in incoming_data.items(): - if key.startswith("HTTP_"): - key = key[5:] - - key = key.replace("_", "-").lower() + if key.startswith(HTTP_PREFIX): + key = key[HTTP_PREFIX_LEN:] + # using local replace and lower bindings for performance + key = lower(replace(key, "_", "-")) data[key] = value return data