Skip to content

Commit

Permalink
Merge pull request #15 from axiomhq/arne/with-axiom-fn-wrapper
Browse files Browse the repository at this point in the history
Make withAxiom a function wrapper
  • Loading branch information
bahlo authored Sep 29, 2022
2 parents 2e12e9d + 17ac73a commit d2827ef
Show file tree
Hide file tree
Showing 19 changed files with 544 additions and 3,788 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ Axiom observability middleware for Prisma.
npm install --save prisma-axiom
```

2. Add the following where you initialize your Prisma client:
2. Wrap your main functions in `withAxiom` to automatically set up telemetry
and flush traces before exit.

```ts
import withAxiom from 'prisma-axiom';
const prisma = withAxiom(new PrismaClient());
const prisma = new PrismaClient();

async function main() {
// do something with prisma
}

withAxiom(main)() // wrap function
```

> **Note**: This will configure Axiom from the `AXIOM_TOKEN` and other
Expand Down Expand Up @@ -42,7 +49,7 @@ parameter to `withAxiom`.
This snippet shows all available options:

```ts
const prisma = withAxiom(new PrismaClient(), {
const myFn = withAxiom(myFn, {
axiomToken: "xaat-xxxxx",
axiomUrl: "https://my-axiom.example.org",
additionalInstrumentations: [new HttpInstrumentation()] // add more instrumentations to the tracing setup
Expand Down Expand Up @@ -106,10 +113,6 @@ generator client {

Also note that this preview feature is only available starting with Prisma v4.2.0.

#### Disconnect prisma client

Make sure to call `prisma.$disconnect()` after all your other code. If not doing so, Axiom will not receive any traces.

## License

© Axiom, Inc., 2022
Expand Down
4 changes: 4 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Running an example

1. Run `docker-compose up` to start a local Postgres server
2. Go into the folder and run `npm run start` to start the example
11 changes: 11 additions & 0 deletions examples/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: "3"

services:
postgres:
image: postgres:alpine
environment:
POSTGRES_USER: axiom-prisma
POSTGRES_PASSWORD: axiom-prisma
POSTGRES_DB: axiom-prisma
ports:
- 5432:5432
19 changes: 5 additions & 14 deletions examples/express/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import withAxiom from '../../src/axiom';
import withAxiom from 'prisma-axiom';
import { PrismaClient } from '@prisma/client';
const express = require('express')
const app = express()

const client = new PrismaClient();
const prisma = withAxiom(client);
const prisma = client;

app.get('/', async (_: any, res: any) => {
app.get('/', withAxiom(async (_: any, res: any) => {
await prisma.user.create({
data: {
name: 'Alice',
Expand All @@ -20,15 +20,6 @@ app.get('/', async (_: any, res: any) => {
await prisma.user.deleteMany();

res.send('hello world')
});
}));

const server = app.listen(3000)

process.on('SIGINT', () => {
console.log('SIGINT signal received: closing HTTP server')
// call prisma disconnect t ensure logs and traces are flushed
prisma.$disconnect().finally(() => {
server.close();
process.exit(0)
});
})
app.listen(3000)
Loading

0 comments on commit d2827ef

Please sign in to comment.