|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// Use postgres from an ES module: |
| 18 | +// node --experimental-loader=@opentelemetry/instrumentation/hook.mjs use-pg.mjs |
| 19 | + |
| 20 | +import { trace } from '@opentelemetry/api'; |
| 21 | +import { createTestNodeSdk } from '@opentelemetry/contrib-test-utils'; |
| 22 | +import assert from 'assert'; |
| 23 | + |
| 24 | +import { PgInstrumentation } from '../../build/src/index.js'; |
| 25 | + |
| 26 | +const CONFIG = { |
| 27 | + user: process.env.POSTGRES_USER || 'postgres', |
| 28 | + password: process.env.POSTGRES_PASSWORD || 'postgres', |
| 29 | + database: process.env.POSTGRES_DB || 'postgres', |
| 30 | + host: process.env.POSTGRES_HOST || 'localhost', |
| 31 | + port: process.env.POSTGRES_PORT |
| 32 | + ? parseInt(process.env.POSTGRES_PORT, 10) |
| 33 | + : 54320, |
| 34 | +}; |
| 35 | + |
| 36 | +const sdk = createTestNodeSdk({ |
| 37 | + serviceName: 'use-pg-pool', |
| 38 | + instrumentations: [new PgInstrumentation()], |
| 39 | +}); |
| 40 | +sdk.start(); |
| 41 | + |
| 42 | +import { Pool as PGPool } from 'pg'; |
| 43 | +const pgPool = new PGPool(CONFIG); |
| 44 | +const tracer = trace.getTracer(); |
| 45 | + |
| 46 | +await tracer.startActiveSpan('test-span', async span => { |
| 47 | + pgPool.connect((connectErr, _, release) => { |
| 48 | + assert.ifError(connectErr) |
| 49 | + pgPool.query('SELECT NOW()', (err, res) => { |
| 50 | + assert.ok(res); |
| 51 | + assert.ifError(err) |
| 52 | + }); |
| 53 | + release(); |
| 54 | + span.end(); |
| 55 | + sdk.shutdown(); |
| 56 | + }); |
| 57 | +}); |
0 commit comments