Monitoring PostgreSQL Performance using OpenTelemetry

This tutorial explains how you can use OpenTelemetry and Uptrace to monitor PostgreSQL performance. Besides using Otel Collector to monitor your PostgreSQL servers, you can also use OpenTelemetry to instrument your applications with traces, metrics, and logs.

What is OpenTelemetry?

OpenTelemetryopen in new window is an open-source observability framework hosted by Cloud Native Computing Foundation. It is a merger of OpenCensus and OpenTracing projects.

OpenTelemetry aims to provide a single standard across all types of observability signals such as OpenTemetry tracingopen in new window, and OpenTelemetry metricsopen in new window.

OpenTelemetry specifies how to collect and send telemetry data to backend platforms. With OpenTelemetry, you can instrument your application once and then add or change vendors without changing the instrumentation.

OpenTelemetry is available for most programming languages and provides interoperability across different languages and environments.

What is OpenTelemetry Collector?

OpenTelemetry Collectoropen in new window is an agent that pulls telemetry data from systems you want to monitor and sends it to tracing toolsopen in new window using the OpenTelemetry protocol (OTLP).

You can use OpenTelemetry Collector to monitor system metricsopen in new window, PostgreSQLopen in new window, MySQLopen in new window, Redisopen in new window, and more.

Uptrace

Uptrace is an open source APMopen in new window for OpenTelemetry that helps developers pinpoint failures and find performance bottlenecks. Uptrace can process billions of spans on a single server and allows to monitor your software at 10x lower cost.

You can get startedopen in new window with Uptrace by downloading a DEB/RPM package or a pre-compiled Go binary.

OpenTelemetry PostgreSQL receiver

To start monitoring PostgreSQL with Otel Collector, you need to configure PostgreSQL receiver in /etc/otel-contrib-collector/config.yaml using Uptrace DSNopen in new window:

receivers:
  otlp:
    protocols:
      grpc:
      http:
  postgresql:
    endpoint: localhost:5432
    transport: tcp
    username: otel
    password: $POSTGRESQL_PASSWORD
    databases:
      - otel
    collection_interval: 10s
    tls:
      insecure: true

exporters:
  otlp:
    endpoint: otlp.uptrace.dev:4317
    headers: { 'uptrace-dsn': '<FIXME>' }

processors:
  resourcedetection:
    detectors: [system]
  batch:
    timeout: 10s

service:
  pipelines:
    traces:
      receivers: [otlp]
      processors: [batch]
      exporters: [otlp]
    metrics:
      receivers: [otlp, postgresql]
      processors: [batch, resourcedetection]
      exporters: [otlp]

Don't forget to restart the service:

sudo systemctl restart otelcol-contrib

You can also check OpenTelemetry Collector logs for any errors:

sudo journalctl -u otelcol-contrib -f

Available metrics

Uptrace automatically creates the following dashboard when PostgreSQL metrics are available:

PostgreSQL metrics

Prometheus

You can also send OpenTelemetry metrics to Prometheus using OpenTelemetry Prometheus exporter.

What's next?

Next, you can learn more about configuring OpenTelemetry Collector and check available receivers.

To start using OpenTelemetry and Uptrace, see Getting started with Uptraceopen in new window.

Last Updated: