<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/finding-slow-code-with-wrapture-shmedg3wu" -->

---
title: Finding slow code with wrapture | daily.dev
description: A walkthrough of using the wrapture instrumentation library to find where request latency actually comes from in a Flask shop app, distinguishing self time...
canonical: https://daily.dev/posts/finding-slow-code-with-wrapture-shmedg3wu
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Finding slow code with wrapture | daily.dev
og:description: A walkthrough of using the wrapture instrumentation library to find where request latency actually comes from in a Flask shop app, distinguishing self time...
og:url: https://daily.dev/posts/finding-slow-code-with-wrapture-shmedg3wu
og:image: https://api.daily.dev/og/posts/SHMEDg3Wu.png
og:image:alt: Finding slow code with wrapture
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Finding slow code with wrapture

**[Planet Python](https://daily.dev/sources/planetpython)** · 7 min read · 0 upvotes · 0 comments

## Summary

A walkthrough of using the wrapture instrumentation library to find where request latency actually comes from in a Flask shop app, distinguishing self time (time spent in an operation itself) from time spent in its children. Demonstrates a call tree with per-node timing, turning observations into pytest assertions, aggregating stats across many requests with a low-memory Aggregate collector, tagging events by tenant with annotate() for per-tenant slicing, and using a cheap Counter collector to catch N+1 regressions in test suites without retaining full traces.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://grahamdumpleton.me/posts/2026/09/finding-slow-code-with-wrapture>

## Questions this post answers

### How do I figure out which layer of a Flask request handler is actually slow when nested calls all show large elapsed times?

Compute self time, an operation's duration minus the time its observed children account for, instead of wrapping each layer with a stopwatch. Using the wrapture library, binding calls to a service, gateway, and ledger method and printing a timeline tree shows that a service call taking 31ms spent only 173 microseconds of self time, meaning nearly all the delay came from a child call rather than the service itself.

_daily.dev surfaces practical tracing techniques like this for developers chasing down slow request paths._

### How can I track down which specific tenant or request is causing slow endpoint responses in production?

Tag the in-flight request event with contextual data, such as a tenant header, using an annotate() call in a before_request hook, then filter recorded traces by that tag. In one example, filtering a jsonlines trace file for tenant 'acme' showed order requests consistently taking 32-36ms, while another tenant's declined orders finished in about a millisecond because they never reached the slow ledger call.

_Engineers debugging tenant-specific latency issues can find tracing patterns like this through daily.dev._

### What's a low-overhead way to catch N+1 query regressions in a test suite without a full profiler?

Use a lightweight counter collector that counts operations as they begin without retaining any duration or event data, making it cheap enough to run under an entire test suite. Binding it to a database layer's execute method and asserting a query budget in a fixture causes an N+1 regression to fail with an explicit count attached, rather than merely showing up as a slower test.

_daily.dev helps developers building test suites discover approaches for catching performance regressions like N+1 queries._

## Similar posts on daily.dev

- [Tracing Flask with wrapture](https://daily.dev/posts/tracing-flask-with-wrapture-gfrmlfkfe) · Planet Python · 0 upvotes · 0 comments
- [Introducing wrapture](https://daily.dev/posts/introducing-wrapture-ohj3cvcws) · Planet Python · 1 upvotes · 1 comments
- [Trying out wrapture](https://daily.dev/posts/trying-out-wrapture-qi1ktjmcu) · Planet Python · 0 upvotes · 0 comments
- [Query Profiling: See Where a Slow Query Spends Its Time](https://daily.dev/posts/query-profiling-see-where-a-slow-query-spends-its-time-eosyw6lmm) · Weaviate · 0 upvotes · 0 comments
- [Zero-code tracing with wrapture](https://daily.dev/posts/zero-code-tracing-with-wrapture-yy6qgb3me) · Planet Python · 1 upvotes · 0 comments

---

Tags: [#flask](https://daily.dev/tags/flask), [#observability](https://daily.dev/tags/observability), [#performance](https://daily.dev/tags/performance), [#python](https://daily.dev/tags/python), [#testing](https://daily.dev/tags/testing)

[View this post on daily.dev](https://daily.dev/posts/finding-slow-code-with-wrapture-shmedg3wu)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Finding slow code with wrapture","url":"https://daily.dev/posts/finding-slow-code-with-wrapture-shmedg3wu","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/finding-slow-code-with-wrapture-shmedg3wu"},"datePublished":"2026-09-10T01:52:03.847Z","dateModified":"2026-10-15T02:22:29.688Z","description":"A walkthrough of using the wrapture instrumentation library to find where request latency actually comes from in a Flask shop app, distinguishing self time...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/6ebc8c3c5dbc7d12269aafad25dbb0f6?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/6ebc8c3c5dbc7d12269aafad25dbb0f6?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Planet Python","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Planet Python","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/eac3cc584cb043b5a242d13752bd3d0c","url":"https://daily.dev/sources/planetpython"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/finding-slow-code-with-wrapture-shmedg3wu","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"flask,observability,performance,python,testing","timeRequired":"PT7M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Planet Python","item":"https://daily.dev/sources/planetpython"},{"@type":"ListItem","position":3,"name":"Finding slow code with wrapture"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/finding-slow-code-with-wrapture-shmedg3wu#faq","mainEntity":[{"@type":"Question","name":"How do I figure out which layer of a Flask request handler is actually slow when nested calls all show large elapsed times?","acceptedAnswer":{"@type":"Answer","text":"Compute self time, an operation's duration minus the time its observed children account for, instead of wrapping each layer with a stopwatch. Using the wrapture library, binding calls to a service, gateway, and ledger method and printing a timeline tree shows that a service call taking 31ms spent only 173 microseconds of self time, meaning nearly all the delay came from a child call rather than the service itself. daily.dev surfaces practical tracing techniques like this for developers chasing down slow request paths."}},{"@type":"Question","name":"How can I track down which specific tenant or request is causing slow endpoint responses in production?","acceptedAnswer":{"@type":"Answer","text":"Tag the in-flight request event with contextual data, such as a tenant header, using an annotate() call in a before_request hook, then filter recorded traces by that tag. In one example, filtering a jsonlines trace file for tenant 'acme' showed order requests consistently taking 32-36ms, while another tenant's declined orders finished in about a millisecond because they never reached the slow ledger call. Engineers debugging tenant-specific latency issues can find tracing patterns like this through daily.dev."}},{"@type":"Question","name":"What's a low-overhead way to catch N+1 query regressions in a test suite without a full profiler?","acceptedAnswer":{"@type":"Answer","text":"Use a lightweight counter collector that counts operations as they begin without retaining any duration or event data, making it cheap enough to run under an entire test suite. Binding it to a database layer's execute method and asserting a query budget in a fixture causes an N+1 regression to fail with an explicit count attached, rather than merely showing up as a slower test. daily.dev helps developers building test suites discover approaches for catching performance regressions like N+1 queries."}}]}
```

