---
title: "Tracing Gorm queries with OpenCensus & Google Cloud Tracing"
description: "Gorm's Callbacks API can wrap every Postgres query in an OpenCensus span that feeds Google Cloud Tracing: a before-callback starts the span only when a parent span exists, and an after-callback ends it."
url: "https://swe.quest/articles/tracing-gorm-queries-with-opencensus-google-cloud-tracing-0546b4ac4bd6b7cf"
canonical: "https://incident.io/blog/gorm-tracing"
source: "incident.io"
published: "2022-07-07"
difficulty: "medium"
kind: "How-to"
reading_minutes: 6
tags: ["observability","go","postgres"]
---

# Tracing Gorm queries with OpenCensus & Google Cloud Tracing

A post on [incident.io](https://swe.quest/sources/incident-io.md), published 2022-07-07, 6 min read, How-to. Original: [https://incident.io/blog/gorm-tracing](https://incident.io/blog/gorm-tracing)

## Summary

Gorm's Callbacks API can wrap every Postgres query in an OpenCensus span that feeds Google Cloud Tracing: a before-callback starts the span only when a parent span exists, and an after-callback ends it. The after-callback sets the span status, mapping ErrRecordNotFound to NotFound. Spans are enriched with the table name, a pg\_query\_go query fingerprint in the span name, the calling source line found by walking runtime.Caller past gorm frames, rows affected, the SQL string and bound vars. Query vars can contain sensitive data, so record them only for SELECT queries or sanitise them first.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Observability](https://swe.quest/tags/observability.md) (Reliability & operations)
- [Go](https://swe.quest/tags/go.md) (Languages)
- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)

## Prerequisites

- Go context propagation ([easy posts on Go](https://swe.quest/tags/go.md?difficulty=easy))
- distributed tracing spans ([easy posts on Observability](https://swe.quest/tags/observability.md?difficulty=easy))
- ORM basics

## Keywords

Gorm, OpenCensus, Google Cloud Tracing, pg\_query\_go, PostgreSQL

## Related posts

- [Observability as a superpower](https://swe.quest/articles/observability-as-a-superpower-5609b41c95a2309f.md): incident.io · 2024-11-04 · Medium · 8 min read · Observability, Go, Debugging, Incidents & postmortems
  Distributed tracing with OpenTelemetry lets incident.io engineers spot bugs before customers report them, by showing each request's route as a tree of timed spans that exposes slow steps, N+1 queries and duplicate calls.
- [Zero-config Debugging with Deno and OpenTelemetry](https://swe.quest/articles/zero-config-debugging-with-deno-and-opentelemetry-84e6e1786536c301.md): Deno · 2025-04-09 · Medium · 9 min read · Observability, Debugging, Runtimes & VMs, TypeScript
  Deno's built-in OpenTelemetry support (enabled with --unstable-otel) automatically correlates console.log output with trace IDs, creates spans for incoming and outgoing HTTP requests, propagates W3C tracecontext headers across services, and records request-count and latency metrics by status code, all without code changes.
- [How we used ClickHouse to store OpenTelemetry Traces and up our Observability Game](https://swe.quest/articles/how-we-used-clickhouse-to-store-opentelemetry-traces-and-up-our-observability-b78dc8bc485a8f13.md): ClickHouse · 2023-04-14 · Medium · 13 min read · Observability, Analytics & OLAP, SQL, Cost optimization
  Resmo stores over 300 million unsampled OpenTelemetry spans per day in a self-hosted ClickHouse node, after dropping S3/Parquet/Athena because of its 2-3 second query startup and delayed data.
- [Building an Observability Solution with ClickHouse - Part 2 - Traces](https://swe.quest/articles/building-an-observability-solution-with-clickhouse-part-2-traces-1a2db2608573ffa7.md): ClickHouse · 2023-03-29 · Medium · 29 min read · Observability, Analytics & OLAP, Microservices
  Trace data from OpenTelemetry-instrumented services can be stored and queried in ClickHouse, treating spans as rows in a single analytics table and visualizing them in Grafana.
- [Logs and tracing: not just for production, local development too](https://swe.quest/articles/logs-and-tracing-not-just-for-production-local-development-too-cd937ebe394aa48b.md): incident.io · 2021-11-11 · Medium · 5 min read · Observability, Go, Developer productivity, Cloud platforms
  A small team wired its Go app to Google Cloud Trace via the OpenCensus Stackdriver exporter, then teed zap logs to both STDERR and Cloud Logging with trace and span IDs attached, so logs appear inline with traces.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
