I'm trying to have some tracing enabled on my Fastify application within Cloud Run which is connected with Cloud SQL (Postgres) via a VPC connector. The instrumentation of this database doesn't seem to work. I did get this working on a local environment. Any ideas why this would be going wrong? HTTP instrumentation seems to be working, so it seems something specific with the Postgres database.
Any help is welcome.
import config from 'config';
import fastifyEtag from 'fastify-etag';
import app from './app';
import logger from './util/logger';
import opentelemetry from '@opentelemetry/api';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { TraceExporter } from '@google-cloud/opentelemetry-cloud-trace-exporter';
import { PgInstrumentation } from '@opentelemetry/instrumentation-pg';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { TypeormInstrumentation } from 'opentelemetry-instrumentation-typeorm';
// Enable OpenTelemetry exporters to export traces to Google Cloud Trace.
// Exporters use Application Default Credentials (ADCs) to authenticate.
// See https://developers.google.com/identity/protocols/application-default-credentials
// for more details.
const provider = new NodeTracerProvider();
// Initialize the exporter. When your application is running on Google Cloud,
// you don't need to provide auth credentials or a project id.
const exporter = new TraceExporter();
// Configure the span processor to send spans to the exporter
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));
provider.register();
opentelemetry.trace.setGlobalTracerProvider(provider);
registerInstrumentations({
instrumentations: [
new HttpInstrumentation(),
new PgInstrumentation(),
new TypeormInstrumentation({
// see under for available configuration
}),
],
});
import Fastify from 'fastify';
import typeormPlugin from 'fastify-typeorm-plugin';
const fastify = Fastify({
logger: logger,
});
fastify.register(typeormPlugin, config.get('typeorm'));
fastify.register(fastifyEtag);
fastify.register(app);
// Run the server!
fastify.listen(process.env.PORT || '3000', process.env.HOST ?? '127.0.0.1', (err, address) => {
if (err) {
fastify.log.error(err);
process.exit(1);
}
fastify.log.info(`server listening on ${address}`);
});