Integrating pganalyze with New Relic

You can set up the pganalyze collector OpenTelemetry exporter with New Relic to capture spans for query EXPLAIN plans inside your traces. The span contains the link to the EXPLAIN plan in the pganalyze app, so that you can easily visit the pganalyze page to find out why the query is slow.

New Relic provides OTLP endpoints that you can use to export tracing spans from the pganalyze collector. On the application side, you can either use the New Relic agent or OpenTelemetry SDKs.

Setup steps

  1. Start sending tracing data from your application to New Relic
  2. Add traceparent query tag to the query
  3. Create a New Relic API key
  4. Set up an API key and an endpoint in the pganalyze collector

Start sending tracing data from your application to New Relic

To start sending tracing data from your application to New Relic, you can either use the guided install and install the New Relic agent, or use OpenTelemetry SDKs. You can read how to set up the OpenTelemetry integration in New Relic documentation.

If you choose to use the New Relic agent, make sure to use the version that supports Distributed Tracing. The latest versions of agents in each language support it and it's enabled by default.

Add traceparent query tag to the query

When you use OpenTelemetry SDKs to send data to New Relic, you can follow the traceparent documentation to add a traceparent query tag to the query.

When you are using the New Relic agent, you'll need to modify how to generate a traceparent value. You can still follow the traceparent documentation for how to add it as a query tag, then you can generate a traceparent tag as follows.

Ruby

# make sure NewRelic::Agent::Transaction.tl_current is present
# before generating a traceparent
current_trace = NewRelic::Agent::Transaction.tl_current

format(
  '00-%<trace_id>s-%<parent_id>s-%<trace_flags>.2d',
  trace_id: current_trace.trace_id.rjust(32, '0').downcase,
  parent_id: current_trace.current_segment.guid,
  trace_flags: current_trace.sampled? ? '01' : '00'
)

Python

import newrelic.agent


dummy_headers = []
newrelic.agent.insert_distributed_trace_headers(dummy_headers)
traceparent = None

for key, value in dummy_headers:
    if key == 'traceparent':
        traceparent = value
        break

# only add traceparent as a query tag when it's not None

For Go, it is best to use the OpenTelemetry SDK as it will integrate well with sqlcommenter and the traceparent query tag will be added without any extra setup.

Create a New Relic API key

You can create a new API key with a "License" key type. You can optionally use the same key as the one used by your application.

Set up an API key and an endpoint in the pganalyze collector

Once you obtain an API key, update the following collector settings to start sending out tracing spans to New Relic:

  • otel_exporter_otlp_endpoint (OTEL_EXPORTER_OTLP_ENDPOINT):
    • https://otlp.nr-data.net
    • other endpoints and ports are available, see New Relic documentation for the detail to pick the best one for your application
  • otel_exporter_otlp_headers (OTEL_EXPORTER_OTLP_HEADERS):
    • api-key=<API key from the previous step>

Optionally, you can also set otel_service_name (OTEL_SERVICE_NAME) to update the service name reported to New Relic from the pganalyze collector. It defaults to Postgres (pganalyze).

Make sure to restart the collector after updating the settings.


Couldn't find what you were looking for or want to talk about something specific?
Start a conversation with us →