Configuring EDB OTEL

The following configuration options are available with edb_otel.

Permissions

No permissions changes are required for edb_otel.

Prerequisites

Before using edb_otel to export metrics, you must set up an OpenTelemetry collector endpoint that edb_otel can communicate with. For detailed information on how to set up a quick test of the OpenTelemetry endpoint using Docker, see OTLP Exporter Example.

GUCs

The following GUC variables are available in edb_otel.

VariableDescriptionUnitDefaultMinimum value
edb_otel.edb_otel_endpointThe OTEL endpoint URL.
edb_otel.intervalInterval between two consecutive exports. It translates to the OpenTelemetry metric reader parameter exportIntervalMillis.Milliseconds2 seconds1 second
edb_otel.timeoutHow long the export can run before it's canceled. It translates to the OpenTelemetry metric reader parameter exportTimeoutMillis.Milliseconds500 milliseconds100 milliseconds
edb_otel.worker_nap_timeDuration of background worker sleep time before it checks for metrics in the queue and processes them.Milliseconds2 seconds1 second
edb_otel.metrics_queue_sizeInterval between two consecutive exports. This is the maximum size of the metrics queue. If the queue reaches this size, any subsequent metrics are dropped until the queue is cleared while processing the metrics inside it.MB100100

Configuring to use from an SQL interface

To use the edb_otel extension from a SQL interface, in postgresql.conf:

  1. Set the parameter edb_otel.edb_otel_endpoint to point to the collector endpoint. For example:
edb_otel.edb_otel_endpoint='localhost:4317'
  1. Add edb_otel to the shared_preload_libraries. For example:
shared_preload_libraries = '$libdir/dbms_pipe,$libdir/edb_gen,$libdir/dbms_aq,$libdir/edb_otel'

Configuring to use in C code

To use the edb_otel extension from your C code:

  1. Ensure the edb_otel_ext.h file is in reach of your extension at build time. The file is placed in $prefix/include/server/extension/edb_otel/ when the edb_otel dev/devel package is installed. Your makefile must add pg_config --includedir-server to its include search paths so your extension can use it as:
#include "extension/edb_otel/edb_otel_ext.h"

This header contains the signature of the function that's used to send metrics to the OTEL endpoint.

  1. When the edb_otel extension is loaded, it places a function pointer in the rendezvous variable identified by edb_otel_metric_func. So the next step is to get that function pointer:
edb_otel_metric_func_type * p_edb_otel_metric =
    (edb_otel_metric_func_type *) find_rendezvous_variable("edb_otel_metric_func");

You can store the function pointer in a session-long variable, but take care in case edb_otel gets loaded after the calling code.

Since the instrumented code must continue to work even when edb_otel isn't in use, if the function pointer returns NULL, no error should be raised, and the function must not be called.

For example, instrumenting pg_stat_statements to send the number of plans can look similar to this:

if (*p_edb_otel_metric) {
    char val[100] = {0};
    sprintf(val, "%ld", e->counters.calls[PGSS_PLAN]);
    (*p_edb_otel_metric)("pg_stat_statements",
                         "plans",
                         EDB_OTEL_GAUGE,
                         val,
                         labels);
}

The edb_otel_metric function has the following parameters.

Parameter(s)Input or outputDescription
meter_nameInputA name to identify a group of metrics
metric_nameInputA name to identify an actual measurement of the metric
metric_typeInputIdentified by one of the enums in edb_otel_ext.h
measurementInputA string value
labelsInputA JSONB string representing additional metadata to be associated with the metric, for example, attaching the query text to the measurement of pg_stat_statements
Note

Take care when calling the function that sends metrics. It's important to avoid using it in hot paths and while holding locks, since it currently enters third-party code. In the future, it may still perform non-trivial work, if only pushing data through a shm_mq to a background worker.

For a detailed example of this C API usage, where pg_stat_statements was instrumented to send all metrics through OTEL, see this example.


Could this page be better? Report a problem or suggest an addition!