diff --git a/examples/postgres/README.md b/examples/postgres/README.md deleted file mode 100644 index 4b29d482c8..0000000000 --- a/examples/postgres/README.md +++ /dev/null @@ -1,109 +0,0 @@ -# Overview - -OpenTelemetry PostgreSQL Instrumentation allows the user to automatically collect trace data and export them to the backend of choice (we can use Zipkin or Jaeger for this example), to give observability to distributed systems. - -This is a simple example that demonstrates tracing HTTP request from client to server. The example -shows key aspects of tracing such as - -- Root Span (on Client) -- Child Span (on Client) -- Child Span from a Remote Parent (on Server) -- SpanContext Propagation (from Client to Server) -- Span Events -- Span Attributes - -## Installation - -```sh -# from this directory -npm install -``` - -Setup [Zipkin Tracing](https://zipkin.io/pages/quickstart.html) -or -Setup [Jaeger Tracing](https://www.jaegertracing.io/docs/latest/getting-started/#all-in-one) - -## Run the Application - -### Zipkin - -- Start postgres via docker - - ```sh - # from this directory - npm run docker:start - ``` - -- Run the server - - ```sh - # from this directory - npm run zipkin:server - ``` - -- Run the client - - ```sh - # from this directory - npm run zipkin:client - ``` - -- Cleanup docker - - ```sh - # from this directory - npm run docker:stop - ``` - -#### Zipkin UI - -`zipkin:server` script should output the `traceid` in the terminal (e.g `traceid: 4815c3d576d930189725f1f1d1bdfcc6`). -Go to Zipkin with your browser (e.g ) - -

- -### Jaeger - -- Start postgres via docker - - ```sh - # from this directory - npm run docker:start - ``` - -- Run the server - - ```sh - # from this directory - npm run jaeger:server - ``` - -- Run the client - - ```sh - # from this directory - npm run jaeger:client - ``` - -- Cleanup docker - - ```sh - # from this directory - npm run docker:stop - ``` - -#### Jaeger UI - -`jaeger:server` script should output the `traceid` in the terminal (e.g `traceid: 4815c3d576d930189725f1f1d1bdfcc6`). -Go to Jaeger with your browser (e.g ) - -

- -## Useful links - -- For more information on OpenTelemetry, visit: -- For more information on OpenTelemetry for Node.js, visit: - -## LICENSE - -Apache License 2.0 diff --git a/examples/postgres/client.js b/examples/postgres/client.js deleted file mode 100644 index 86e96ad263..0000000000 --- a/examples/postgres/client.js +++ /dev/null @@ -1,33 +0,0 @@ -'use strict'; - -const api = require('@opentelemetry/api'); -const tracer = require('./tracer')('postgres-client-service'); -// eslint-disable-next-line import/order -const http = require('http'); - -function makeRequest() { - const span = tracer.startSpan('makeRequest'); - const randomId = Math.floor(Math.random() * 10); - api.context.with(api.trace.setSpan(api.ROOT_CONTEXT, span), () => { - console.log('Client traceId ', span.spanContext().traceId); - http.get({ - host: 'localhost', - port: 3000, - path: `/insert?id=${randomId}&text=randomstring`, - }); - - http.get({ - host: 'localhost', - port: 3000, - path: `/get?id=${randomId}`, - }); - }); - - // The process must live for at least the interval past any traces that - // must be exported, or some risk being lost if they are recorded after the - // last export. - console.log('Sleeping 5 seconds before shutdown to ensure all records are flushed.'); - setTimeout(() => { console.log('Completed.'); }, 5000); -} - -makeRequest(); diff --git a/examples/postgres/images/jaeger.png b/examples/postgres/images/jaeger.png deleted file mode 100644 index aaecd50a56..0000000000 Binary files a/examples/postgres/images/jaeger.png and /dev/null differ diff --git a/examples/postgres/images/zipkin.png b/examples/postgres/images/zipkin.png deleted file mode 100644 index ea907ae047..0000000000 Binary files a/examples/postgres/images/zipkin.png and /dev/null differ diff --git a/examples/postgres/package.json b/examples/postgres/package.json deleted file mode 100644 index 0197d15772..0000000000 --- a/examples/postgres/package.json +++ /dev/null @@ -1,48 +0,0 @@ -{ - "name": "postgres-example", - "private": true, - "version": "0.23.0", - "description": "Example of Postgres integration with OpenTelemetry", - "main": "index.js", - "scripts": { - "zipkin:server": "cross-env EXPORTER=zipkin node ./server.js", - "zipkin:client": "cross-env EXPORTER=zipkin node ./client.js", - "jaeger:server": "cross-env EXPORTER=jaeger node ./server.js", - "jaeger:client": "cross-env EXPORTER=jaeger node ./client.js", - "docker:start": "docker run -d -p 54320:5432 -e POSTGRES_PASSWORD=postgres --name otpostgres postgres:alpine", - "docker:stop": "docker stop otpostgres & docker rm otpostgres" - }, - "repository": { - "type": "git", - "url": "git+ssh://git@github.com/open-telemetry/opentelemetry-js.git" - }, - "keywords": [ - "opentelemetry", - "postgres", - "tracing" - ], - "engines": { - "node": ">=8.5.0" - }, - "author": "OpenTelemetry Authors", - "license": "Apache-2.0", - "bugs": { - "url": "https://github.com/open-telemetry/opentelemetry-js/issues" - }, - "dependencies": { - "@opentelemetry/api": "^1.0.2", - "@opentelemetry/exporter-jaeger": "^0.25.0", - "@opentelemetry/exporter-zipkin": "^0.25.0", - "@opentelemetry/instrumentation": "^0.25.0", - "@opentelemetry/instrumentation-http": "^0.25.0", - "@opentelemetry/instrumentation-pg": "^0.23.0", - "@opentelemetry/sdk-trace-node": "^0.25.0", - "@opentelemetry/sdk-trace-base": "^0.25.0", - "express": "^4.17.1", - "pg": "^8.6.0" - }, - "homepage": "https://github.com/open-telemetry/opentelemetry-js#readme", - "devDependencies": { - "cross-env": "^6.0.0" - } -} diff --git a/examples/postgres/server.js b/examples/postgres/server.js deleted file mode 100644 index 1c2ac91c54..0000000000 --- a/examples/postgres/server.js +++ /dev/null @@ -1,54 +0,0 @@ -'use strict'; - -const api = require('@opentelemetry/api'); -// eslint-disable-next-line import/order -const tracer = require('./tracer')('postgres-server-service'); -const { SpanKind, SpanStatusCode } = require('@opentelemetry/api'); -const express = require('express'); -const setupPg = require('./setupPsql'); - -const pool = setupPg.startPsql(); - -const app = express(); - -app.get('/:cmd', (req, res) => { - const cmd = req.path.slice(1); - if (!req.query.id) { - res.status(400).send('No id provided'); - return; - } - let queryText = `SELECT id, text FROM test WHERE id = ${req.query.id}`; - if (cmd === 'insert') { - if (!req.query.text) { - res.status(400).send('No text provided'); - return; - } - queryText = { - text: 'INSERT INTO test (id, text) VALUES($1, $2) ON CONFLICT(id) DO UPDATE SET text=$2', - values: [req.query.id, req.query.text], - }; - } - const currentSpan = api.trace.getSpan(api.context.active()); - console.log(`traceid: ${currentSpan.spanContext().traceId}`); - const span = tracer.startSpan(cmd, { - kind: SpanKind.SERVER, - }); - api.context.with(api.trace.setSpan(api.ROOT_CONTEXT, span), () => { - try { - pool.query(queryText, (err, ret) => { - if (err) throw err; - res.send(ret.rows); - }); - } catch (e) { - res.status(400).send({ message: e.message }); - span.setStatus(SpanStatusCode.ERROR); - } - span.end(); - }); -}); - -// start server -const port = 3000; -app.listen(port, () => { - console.log(`Node HTTP listening on ${port}`); -}); diff --git a/examples/postgres/setupPsql.js b/examples/postgres/setupPsql.js deleted file mode 100644 index e07ae2604c..0000000000 --- a/examples/postgres/setupPsql.js +++ /dev/null @@ -1,32 +0,0 @@ -'use strict'; - -const { Pool } = require('pg'); - -// create new pool for psql -const CONFIG = { - password: process.env.POSTGRES_USER || 'postgres', - user: process.env.POSTGRES_USER || 'postgres', - database: process.env.POSTGRES_DB || 'postgres', - host: process.env.POSTGRES_HOST || 'localhost', - port: process.env.POSTGRES_PORT - ? parseInt(process.env.POSTGRES_PORT, 10) - : 54320, -}; - -function startPsql() { - const pool = new Pool(CONFIG); - - pool.connect((connectErr, client, release) => { - if (connectErr) throw connectErr; - release(); - const queryText = 'CREATE TABLE IF NOT EXISTS test(id SERIAL PRIMARY KEY, text VARCHAR(40) not null)'; - client.query(queryText, (err, res) => { - if (err) throw err; - console.log(res.rows[0]); - }); - }); - - return pool; -} - -exports.startPsql = startPsql; diff --git a/examples/postgres/tracer.js b/examples/postgres/tracer.js deleted file mode 100644 index 3f1c5f7e66..0000000000 --- a/examples/postgres/tracer.js +++ /dev/null @@ -1,43 +0,0 @@ -'use strict'; - -const opentelemetry = require('@opentelemetry/api'); -const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node'); -const { SimpleSpanProcessor } = require('@opentelemetry/sdk-trace-base'); -const { JaegerExporter } = require('@opentelemetry/exporter-jaeger'); -const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin'); -const { registerInstrumentations } = require('@opentelemetry/instrumentation'); -const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http'); -const { PgInstrumentation } = require('@opentelemetry/instrumentation-pg'); - -const EXPORTER = process.env.EXPORTER || ''; - -module.exports = (serviceName) => { - const provider = new NodeTracerProvider(); - - let exporter; - if (EXPORTER.toLowerCase().startsWith('z')) { - exporter = new ZipkinExporter({ - serviceName, - }); - } else { - exporter = new JaegerExporter({ - serviceName, - // The default flush interval is 5 seconds. - flushInterval: 2000, - }); - } - - provider.addSpanProcessor(new SimpleSpanProcessor(exporter)); - - // Initialize the OpenTelemetry APIs to use the BasicTracer bindings - provider.register(); - - registerInstrumentations({ - instrumentations: [ - new PgInstrumentation(), - new HttpInstrumentation(), - ], - }); - - return opentelemetry.trace.getTracer('example-postgres'); -};