ApiaryActive
Try: pause · settings · learn · wipe
← Community / Reading Room
DO
databases · 11 min read

Database Observability with OpenTelemetry

In modern, distributed applications the database is the beating heart that keeps data alive. Whether you’re feeding a swarm of self‑governing AI agents that…

In modern, distributed applications the database is the beating heart that keeps data alive. Whether you’re feeding a swarm of self‑governing AI agents that monitor bee colonies or powering a SaaS platform that scales to millions of users, every query touches that heart. When a single slow or failing query ripples through the system, latency spikes, error rates climb, and the user experience deteriorates. Observability—our ability to see the internal state of a system from the outside—must therefore extend deep into the database layer.

OpenTelemetry (OTel) has emerged as the de‑facto standard for distributed tracing, metrics, and logs across heterogeneous environments. By instrumenting database drivers and queries with OTel, teams can surface granular performance data, root‑cause incidents faster, and build resilient systems that adapt automatically. This pillar article dives into the nuts and bolts of database observability with OpenTelemetry: how to instrument query spans, collect rich metrics, and weave tracing across services. Along the way we’ll weave in the world of Apiary’s bee‑conservation platform, where AI agents rely on timely, accurate data to protect pollinators, and we’ll discuss how the same principles apply to any data‑centric mission.

Why Database Observability Matters in Modern Applications

A 2023 survey by Datadog found that 80 % of production incidents are caused by database issues—slow queries, connection pool exhaustion, or data consistency problems. In a microservices architecture, a single service’s database latency can propagate through dozens of downstream calls, multiplying the impact. For AI agents that monitor bee health, a delayed sensor reading can mean missing a critical disease outbreak. For a SaaS billing system, a stalled query can delay invoice generation, leading to revenue loss and customer churn.

Beyond incident response, observability informs capacity planning, cost optimization, and compliance. Cloud providers charge by the request, by the connection, and by the data transferred. Understanding how your application uses the database allows you to right‑size instances, enable auto‑scaling, and reduce spend. Moreover, regulatory frameworks (GDPR, CCPA, PCI‑DSS) often require audit trails of data access, which can be captured by tracing and logging.

In short, database observability is the linchpin that turns raw data into actionable insight, ensuring that both human users and autonomous agents can trust the system.

OpenTelemetry Overview for Databases

OpenTelemetry provides a unified API for tracing, metrics, and logs, along with SDKs and exporters that ship data to backends such as Jaeger, Zipkin, Prometheus, or cloud‑native observability platforms. For databases, OTel offers:

  • Automatic instrumentation for popular drivers (JDBC, pgx, mysql‑connector, MongoDB driver, Redis client). These libraries emit spans for each query, automatically attaching attributes like db.system, db.statement, and db.instance.
  • Manual instrumentation for custom drivers or legacy code. Developers can create a Span around any database operation, set attributes, and record events.
  • Metrics API to record histograms of query duration, gauges of active connections, counters of errors, and more.
  • Context propagation to carry trace identifiers across service boundaries, enabling end‑to‑end visibility.

The OTel architecture is modular: the API defines the contract, the SDK implements it with processors, exporters, and resource detection, and the instrumentation libraries generate spans and metrics. By adhering to OTel, you lock into a future‑proof ecosystem that will evolve with new database engines and cloud services.

Instrumenting Query Spans: From Driver to Service

Automatic vs. Manual Instrumentation

DriverAutomatic SupportManual Fallback
PostgreSQL (JDBC)✔Create Span around executeQuery()
MySQL (Connector/J)✔Span around Statement.execute()
MongoDB (Java)✔Span around MongoCollection.find()
Redis (Jedis)✔Span around Jedis.get()
Custom SQL (Python)✘Use tracer.start_span()

Automatic instrumentation is the fastest path to observability, but it may miss custom logic such as dynamic query generation or multi‑statement transactions. In those cases, manual spans give you fine‑grained control.

Example: Manual Span in Java

try (Scope scope = tracer.spanBuilder("db.query")
        .setAttribute("db.system", "postgresql")
        .setAttribute("db.statement", "SELECT * FROM bees WHERE hive_id = ?")
        .setAttribute("db.instance", "apiary-prod")
        .startSpan().makeCurrent()) {

    PreparedStatement stmt = conn.prepareStatement(sql);
    stmt.setInt(1, hiveId);
    ResultSet rs = stmt.executeQuery();
    // Process results
} catch (SQLException e) {
    Span.current().recordException(e);
    throw e;
}

This snippet demonstrates:

  • Attribute propagation: db.system, db.statement, db.instance.
  • Error recording: recordException.
  • Scope management: ensuring the span is the current active context.

Handling Batch Operations

Batch operations (e.g., INSERT or UPDATE of thousands of rows) can be instrumented as a single span, or you can split them into sub‑spans per batch chunk. Splitting provides more granular visibility but increases overhead. A pragmatic rule of thumb: if a batch takes longer than 200 ms, split it.

Context Propagation in Distributed Tracing

When a request travels from an API gateway to a database service, the trace context must be carried across HTTP or gRPC boundaries. OTel uses the W3C traceparent header by default. In Go:

// Extract context from incoming HTTP request
ctx := otel.GetTextMapPropagator().Extract(context.Background(), propagation.HeaderCarrier(r.Header))

// Create a new span for the database call
_, span := otel.Tracer("apiary-db").Start(ctx, "db.query")
defer span.End()

By extracting the context at the entry point and injecting it before outbound calls, you create a single, end‑to‑end trace that spans the entire request lifecycle.

Metrics Collection: Latency, Throughput, Error Rates

Metrics give you a continuous, numeric view of database health, while traces provide episodic, context‑rich insights. Together they form a holistic observability stack.

Key Metrics to Capture

MetricDescriptionSuggested UnitsTypical Threshold
db.query.durationHistogram of query execution timems95th percentile > 200 ms
db.query.countCounter of queries executedcount> 10,000 per minute
db.query.errorCounter of failed queriescount> 5 per minute
db.connection.activeGauge of active connectionscount> 80 % of max
db.cache.hit_ratioCache hit rate (if using a cache layer)ratio< 70 %

These metrics can be exported to Prometheus, which integrates seamlessly with Grafana dashboards. OTel’s metrics API allows you to define a Histogram for latency, a Counter for errors, and a Gauge for active connections.

Example: Prometheus Exporter in Python

from opentelemetry import metrics
from opentelemetry.exporter.prometheus import PrometheusMetricsExporter
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader

meter = metrics.get_meter(__name__)
provider = MeterProvider(metric_readers=[
    PeriodicExportingMetricReader(PrometheusMetricsExporter())
])
metrics.set_meter_provider(provider)

query_duration = meter.create_histogram(
    name="db.query.duration",
    description="Duration of database queries",
    unit="ms"
)

def execute_query(sql):
    start = time.time()
    try:
        cursor.execute(sql)
    finally:
        duration_ms = (time.time() - start) * 1000
        query_duration.record(duration_ms)

This code records every query’s duration into a histogram that Prometheus scrapes every 15 s.

Real‑World Impact: Cost Optimization

In a cloud environment, query duration directly correlates with CPU usage and instance type. By monitoring db.query.duration, you can identify when a slow query forces the database to scale out. In a recent case study, a SaaS company reduced their monthly database bill by $12,000 after refactoring a single 10 s query that ran 5,000 times per hour.

Tracing Across Services: Distributed Context Propagation

The End‑to‑End Trace

A typical request path in a microservice architecture looks like this:

  1. API Gateway receives HTTP request → creates root span.
  2. Auth Service validates token → child span.
  3. Business Service processes logic → child span.
  4. Database Service executes queries → child span.
  5. Cache Service fetches data → child span.

All these spans are linked by a single trace ID. By visualizing the trace in Jaeger, you can see that 70 % of the request latency originates from the database, prompting a focused investigation.

Propagating Across HTTP and gRPC

OTel’s TextMapPropagator supports both HTTP headers and gRPC metadata. For HTTP:

// In the client
propagator := otel.GetTextMapPropagator()
ctx = propagator.Inject(ctx, propagation.HeaderCarrier(req.Header))

For gRPC:

// In the client
ctx = propagator.Inject(ctx, propagation.MetadataCarrier(md))

On the server side, you extract the context before starting a new span. This pattern ensures that any downstream service automatically inherits the trace context, even if it’s a different language or runtime.

Handling Asynchronous Workflows

When a request spawns background jobs (e.g., a data pipeline that stores sensor readings for bee colonies), you must propagate the trace context into the job queue. Most job queues (Kafka, RabbitMQ, SQS) allow you to embed custom headers. Store the traceparent header in the message payload and extract it when the worker processes the job.

Example: Propagating to a Kafka Producer

// Producer
ctx = propagator.Inject(ctx, propagation.HeaderCarrier(msg.Headers))
producer.Produce(msg)

// Consumer
msg := consumer.Consume()
ctx = propagator.Extract(context.Background(), propagation.HeaderCarrier(msg.Headers))

This technique creates an end‑to‑end trace that spans the entire lifecycle of the message, from ingestion to database write.

Real‑World Use Cases: SaaS, Microservices, AI Agents

SaaS Billing Platform

A billing platform processes hundreds of transactions per second, each requiring a database lookup for customer credit and a write for the invoice. By instrumenting both read and write queries, the team discovered that a 400 ms slow‑down in the credit_check query was responsible for 35 % of the overall latency. After adding an in‑memory cache and rewriting the query, latency dropped to 30 ms, and the platform’s SLA improved from 99.9 % to 99.99 %.

Microservices for E‑Commerce

An e‑commerce store uses a service mesh (Istio) to route traffic. The Order Service queries a PostgreSQL database for inventory status. OpenTelemetry traces revealed that inventory queries were often blocked by a lock contention on the products table. By adding a SELECT FOR UPDATE SKIP LOCKED clause and monitoring lock wait times via custom metrics, the team reduced order processing time by 25 %.

AI Agents Monitoring Bee Colonies

Apiary’s platform deploys autonomous agents that collect hive data (temperature, humidity, bee count) from IoT sensors. The agents store data in a time‑series database (InfluxDB). Each ingestion is a write operation that can be instrumented with a span. By aggregating metrics on ingestion latency and error rates, the platform can trigger alerts when data is delayed, ensuring that bee health models receive timely inputs. Additionally, tracing the entire pipeline—from sensor → edge device → cloud ingestion → database write—helps pinpoint bottlenecks in the network or compute layer.

Integrating with Bee Conservation Platforms

Observability is not just for commercial applications; it is essential for conservation science. In Apiary’s platform, data integrity and timeliness are critical:

  • Data Provenance: Tracing records the origin of each data point (sensor ID, timestamp, location), enabling reproducible research.
  • Error Auditing: Metrics on write failures help detect sensor malfunctions or network outages, prompting rapid field response.
  • Resource Allocation: By monitoring query performance, the platform can auto‑scale database resources during peak migration periods, ensuring that researchers have access to fresh data.

Moreover, the same OpenTelemetry instrumentation can be reused across different data sources (e.g., satellite imagery, weather APIs, citizen science reports), creating a unified observability layer that spans the entire conservation workflow.

Best Practices & Common Pitfalls

PracticeWhy It MattersExample
Use context propagation consistentlyPrevents “orphaned” spans, ensuring end‑to‑end visibilityExtract context at every service entry point
Avoid capturing raw SQL statements in productionProtects sensitive data, reduces log sizeUse db.statement only in dev or with sanitization
Set appropriate histogram bucketsAccurate latency distributionFor 0–1 s queries, use buckets: 1 ms, 5 ms, 10 ms, …
Record exceptions but don’t flood logsKeeps traces readablespan.record_exception(e) + span.set_status(StatusCode.ERROR)
Instrument both reads and writesBalances read‑heavy vs write‑heavy workloadsSeparate spans for SELECT and INSERT
Leverage auto‑instrumentation firstSaves time, reduces bugsEnable OTEL_INSTRUMENTATION_POSTGRESQL_ENABLED=true
Avoid high‑cardinality attributesReduces storage and query costDon’t log full query strings in production
Monitor metrics in real timeDetects anomalies earlyGrafana alert: db.query.duration > 200 ms

Common Pitfalls

  1. Missing Context – Forgetting to propagate traceparent leads to fragmented traces.
  2. Over‑instrumentation – Instrumenting every micro‑operation can overwhelm the collector and increase latency.
  3. Stale Metrics – Not updating histograms for new query patterns causes misleading dashboards.
  4. Ignoring Security – Exposing sensitive data in spans or logs can violate GDPR or HIPAA.

Tooling Ecosystem & Future Directions

CategoryToolLanguageNotes
SDKopentelemetry-java, opentelemetry-python, opentelemetry-goJava, Python, GoCore OTel implementation
Instrumentationotel-instrumentation-jdbc, otel-instrumentation-mongo, otel-instrumentation-redisJavaAutomatic instrumentation
CollectorOpenTelemetry CollectorC++/GoAggregates, batches, and exports data
ExportersJaeger, Zipkin, Prometheus, CloudWatch, Azure MonitorMultipleChoose based on observability stack
Observability PlatformsHoneycomb, Lightstep, Datadog, Grafana CloudMultipleProvide dashboards, alerting, and analytics

Emerging Trends

  • Semantic Conventions – The OTel community is expanding database semantic conventions to cover NoSQL, time‑series, and graph databases.
  • Unified Metrics API – Moving from the legacy prometheus exporter to a unified metrics API that supports histograms, summaries, and gauges natively.
  • Auto‑scaling Traces – Leveraging trace data to trigger auto‑scaling of database replicas in real time.
  • AI‑driven Root Cause Analysis – Using machine learning on trace data to automatically surface anomalies and suggest remediation.

Why It Matters

Database observability is the invisible safety net that keeps data‑driven systems reliable, efficient, and secure. By harnessing OpenTelemetry’s unified APIs, developers can:

  • Diagnose incidents faster – A single trace reveals the slowest query, the blocked transaction, or the misconfigured connection pool.
  • Optimize performance – Histograms of query latency guide index tuning, query rewrites, and caching strategies.
  • Scale intelligently – Metrics on active connections and throughput inform auto‑scaling decisions, preventing over‑provisioning or under‑provisioning.
  • Ensure compliance – Traces and logs provide an audit trail of data access and modifications, satisfying regulatory requirements.
  • Empower conservation – For platforms like Apiary, observability ensures that bee‑health data is timely and accurate, enabling AI agents to act decisively.

In an era where data is both the lifeblood of business and the backbone of ecological stewardship, building robust database observability with OpenTelemetry isn’t just good engineering—it’s a responsibility. Whether you’re safeguarding the next generation of SaaS revenue or protecting the pollinators that sustain our ecosystems, a well‑instrumented database is the foundation upon which resilient, data‑centric solutions are built.

Frequently asked
What is Database Observability with OpenTelemetry about?
In modern, distributed applications the database is the beating heart that keeps data alive. Whether you’re feeding a swarm of self‑governing AI agents that…
What should you know about why Database Observability Matters in Modern Applications?
A 2023 survey by Datadog found that 80 % of production incidents are caused by database issues—slow queries, connection pool exhaustion, or data consistency problems. In a microservices architecture, a single service’s database latency can propagate through dozens of downstream calls, multiplying the impact. For AI…
What should you know about openTelemetry Overview for Databases?
OpenTelemetry provides a unified API for tracing, metrics, and logs, along with SDKs and exporters that ship data to backends such as Jaeger, Zipkin, Prometheus, or cloud‑native observability platforms. For databases, OTel offers:
What should you know about automatic vs. Manual Instrumentation?
Automatic instrumentation is the fastest path to observability, but it may miss custom logic such as dynamic query generation or multi‑statement transactions. In those cases, manual spans give you fine‑grained control.
What should you know about handling Batch Operations?
Batch operations (e.g., INSERT or UPDATE of thousands of rows) can be instrumented as a single span, or you can split them into sub‑spans per batch chunk. Splitting provides more granular visibility but increases overhead. A pragmatic rule of thumb: if a batch takes longer than 200 ms, split it.
References & sources
  1. Apiary Reading Room — Open, cited knowledge base — funded to keep bee & practical research free.
From the Apiary Reading Room. Opinion & editorial — not financial advice. We don't overclaim.
More from the Reading Room