scttnlsn / sql-critic

Capture and analyze SQL queries made during a run of your app's test suite
0 stars 0 forks source link

Identify where query originated #3

Open scttnlsn opened 1 year ago

scttnlsn commented 1 year ago

Have the Collector save stack trace info. Can we use the stack trace to find (or at least approximate) where the query originated? Right now we just show the test that triggered the execution but would be nice to point to app code if possible.

class SpanProcessor(SimpleSpanProcessor):
    def on_start(self, span: Span, **kwargs):
        stack = traceback.extract_stack()

        span.set_attribute(
            "stack",
            json.dumps(
                [
                    {
                        "filename": frame.filename,
                        "lineno": frame.lineno,
                        "name": frame.name,
                    }
                    for frame in stack
                ]
            ),
        )
scttnlsn commented 1 year ago

Did some experimentation with this and the source based approach seems to not work reliably since the location that triggers the query might not necessarily be where the query is constructed (i.e. at least in the case of lazy Django querysets). A better approach here might be seeing if there's any identifiable info in the ancestor spans.

Try adding Otel Django instrumentation and see if we can at least get info about the endpoint in which the query was executed.