Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions nodejs/sqlcommenter-nodejs/samples/express-opentelemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ OpenTelemetry trace and span ID to the queries for correlation.

## Running

This example reads some environment variables to connect to a Cloud SQL database.
The examples read some environment variables to connect to a Cloud SQL
database.

- `DBHOST` - The hostname, ip, or path to unix socket directory for Cloud SQL
database. For example, if you are using Cloud SQL proxy with a unix socket,
Expand All @@ -24,13 +25,22 @@ $ export DBPASSWORD="<database password>"
$ export DBDIALECT="<postgres | mysql>"
$ npm install

# on first run, create some data
# before the first run, create some data
$ npm run createTodos
```

# run the sequelize server
To run the sequelize server example:
```bash
$ npm run sequelizeServer
```

# in a separate terminal
Or run the knex server:
```bash
$ npm run knexServer
```

In a separate terminal, hit the server with `curl` and check the output:
```bash
$ curl http://localhost:8000
{"todos":[{"id":11,"title":"Do dishes","description":null,"done":false,"createdAt":"2020-11-06T01:59:50.111Z","updatedAt":"2020-11-06T01:59:50.111Z"},{"id":12,"title":"Buy groceries","description":null,"done":false,"createdAt":"2020-11-06T01:59:50.111Z","updatedAt":"2020-11-06T01:59:50.111Z"},{"id":13,"title":"Do laundry","description":"Finish before Thursday!","done":false,"createdAt":"2020-11-06T01:59:50.111Z","updatedAt":"2020-11-06T01:59:50.111Z"},{"id":14,"title":"Clean room","description":null,"done":false,"createdAt":"2020-11-06T01:59:50.111Z","updatedAt":"2020-11-06T01:59:50.111Z"},{"id":15,"title":"Wash car","description":null,"done":false,"createdAt":"2020-11-06T01:59:50.112Z","updatedAt":"2020-11-06T01:59:50.112Z"}]}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { wrapMainKnexAsMiddleware } = require("@google-cloud/sqlcommenter-knex");
const Knex = require("knex");

const sqlcommenterMiddleware = wrapMainKnexAsMiddleware(
Knex,
{
traceparent: true,
tracestate: true,

// These are optional and will cause a high cardinality burst traced queries
db_driver: false,
route: false,
},
{ TraceProvider: "OpenTelemetry" }
);

const knex = Knex({
client: "pg",
connection: {
host: process.env.DBHOST,
user: process.env.DBUSERNAME,
password: process.env.DBPASSWORD,
database: "postgres",
},
});

module.exports = {
knex,
sqlcommenterMiddleware,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const { LogLevel } = require("@opentelemetry/core");
const { NodeTracerProvider } = require("@opentelemetry/node");
const { BatchSpanProcessor } = require("@opentelemetry/tracing");
const {
TraceExporter,
} = require("@google-cloud/opentelemetry-cloud-trace-exporter");
const { logger, sleep } = require("./util");

const tracerProvider = new NodeTracerProvider({
logLevel: LogLevel.DEBUG,
logger,
});
tracerProvider.addSpanProcessor(
new BatchSpanProcessor(new TraceExporter({ logger }), {
bufferSize: 500,
bufferTimeout: 5 * 1000,
})
);
tracerProvider.register();

// OpenTelemetry initialization should happen before importing any libraries
// that it instruments
const express = require("express");
const app = express();
const { sqlcommenterMiddleware, knex } = require("./knexConnection");

const tracer = tracerProvider.getTracer(__filename);

async function main() {
if (!(await knex.schema.hasTable("Todos"))) {
console.error("Todos table does not exist. Run `npm run createTodos`");
process.exit(1);
}

const PORT = 8000;

// SQLCommenter express middleware injects the route into the traces
app.use(sqlcommenterMiddleware);

app.get("/", async (req, res) => {
const span = tracer.startSpan("sleep for no reason (parent)");
await sleep(250);
span.end();

const getRecordsSpan = tracer.startSpan("query records with knex");
await tracer.withSpan(getRecordsSpan, async () => {
tracer.startSpan("testing 123").end();
const todos = await knex.select().table("Todos").limit(20);
const countByDone = await knex
.select("done", knex.raw("COUNT(id) as count"))
.table("Todos")
.groupBy("done");
res.json({ countByDone, todos });
});
getRecordsSpan.end();
});
app.listen(PORT, async () => {
console.log(`⚡️[server]: Server is running at https://localhost:${PORT}`);
});
}

main().catch(console.error);
Loading