Skip to content

Commit 6655ba6

Browse files
Castellanos, Eduard Andres (Colombia)Castellanos, Eduard Andres (Colombia)
authored andcommitted
feat(prisma): Examples are added and the readme is updated
1 parent eff1af9 commit 6655ba6

8 files changed

Lines changed: 167 additions & 48 deletions

File tree

sdk_contrib/prisma/README.md

Lines changed: 75 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,103 @@
1-
## Requirements
1+
# Prisma-xray
22

3-
AWS X-Ray SDK Core (aws-xray-sdk-core)
4-
Koa 2.x or greater
3+
A prisma plugin to log requests and subsegments through AWSXray.
54

6-
## AWS X-Ray and Koa
5+
![Screenshot of the AWS X-Ray console](/sdk_contrib/prisma/images/servicemap.png?raw=true)
76

8-
The AWS X-Ray Koa package automatically records information for incoming and outgoing
9-
requests and responses, via the middleware functions in this package. To configure sampling,
10-
dynamic naming, and more see the [set up section](https://github.com/aws/aws-xray-sdk-node/tree/master/packages/core#setup).
7+
## Timeline
118

12-
The AWS X-Ray SDK Core has two modes - `manual` and `automatic`.
13-
Automatic mode uses the `cls-hooked` package and automatically
14-
tracks the current segment and subsegment. This is the default mode.
15-
Manual mode requires that you pass around the segment reference.
9+
![Screenshot of the AWS X-Ray console](/sdk_contrib/prisma/images/timeline.png?raw=true)
1610

17-
In automatic mode, you can get the current segment/subsegment at any time:
18-
var segment = AWSXRay.getSegment();
11+
## Requirements
1912

20-
In manual mode, you can get the base segment off of the context object:
21-
var segment = ctx.segment;
13+
- AWS X-Ray SDK Core
14+
- Prisma 4 or greater
2215

23-
## Middleware Usage
16+
## AWS X-Ray and Prisma
2417

25-
The Koa X-Ray SDK provides one middlewares: `xrayKoa.openSegment(<name>)`.
26-
This middleware will wrap all of the defined routes that you'd like to trace.
27-
In automatic mode, the `openSegment` middleware _must_ be the last middleware added
28-
before defining routes, otherwise issues with the `cls-hooked`
29-
context may occur.
18+
The AWS X-Ray Prisma package automatically records query information and request
19+
and response data. Simply patch the Prisma package via `capturePrisma` as shown below.
20+
21+
The AWS X-Ray SDK Core has two modes - `manual` and `automatic`.
22+
Automatic mode uses the `cls-hooked` package and automatically
23+
tracks the current segment and subsegment. This is the default mode.
24+
Manual mode requires that you pass around the segment reference. See the examples below.
3025

3126
## Automatic mode examples
3227

28+
![Screenshot of the AWS X-Ray console](/sdk_contrib/prisma/images/auto.png?raw=true)
29+
3330
```js
3431
import { PrismaClient } from '@prisma/client';
3532
import AWSXRay from 'aws-xray-sdk';
33+
import { capturePrisma } from 'aws-xray-sdk-prisma';
3634

37-
Xray.enableAutomaticMode();
35+
const ns = AWSXRay.getNamespace();
3836

3937
const prisma = new PrismaClient();
4038

41-
const client = capturePrisma(prisma, {
42-
namespace: 'remote',
43-
});
44-
45-
async function main() {
46-
const contacts = await client.contact.findMany();
47-
console.log(allUsers);
48-
}
49-
50-
main()
51-
.catch(e => {
52-
throw e;
53-
})
54-
.finally(async () => {
55-
await prisma.$disconnect();
39+
const client = capturePrisma(prisma, { namespace: 'remote' });
40+
41+
const segment = new AWSXRay.Segment('prisma-xray-sample-auto');
42+
43+
ns.run(async () => {
44+
const subSegment = segment.addNewSubsegment('Prisma');
45+
AWSXRay.setSegment(subSegment);
46+
await client.company.upsert({
47+
create: {
48+
name: 'Prisma',
49+
createdAt: new Date(),
50+
},
51+
update: {},
52+
where: {
53+
id: 1,
54+
},
5655
});
56+
const companies = await client.company.findMany();
57+
console.log(companies);
58+
subSegment.close();
59+
// ...
60+
});
5761
```
5862

5963
## Manual mode examples
6064

61-
```js
62-
var AWSXRay = require('aws-xray-sdk-core');
63-
var xrayKoa = require('aws-xray-sdk-koa2');
64-
var app = new Koa();
65+
![Screenshot of the AWS X-Ray console](/sdk_contrib/prisma/images/manual.png?raw=true)
6566

66-
//...
67+
```js
68+
import { PrismaClient } from '@prisma/client';
69+
import AWSXRay from 'aws-xray-sdk-core';
70+
import { capturePrisma } from 'aws-xray-sdk-prisma';
6771

68-
var AWSXRay = require('aws-xray-sdk');
72+
AWSXRay.enableManualMode();
6973

70-
app.use(xrayKoa.openSegment('defaultName')); //Required at the start of your routes
74+
const prisma = new PrismaClient();
7175

72-
router.get('/myRoute', ctx => {
73-
const segment = ctx.segment;
74-
//Do whatever
75-
});
76+
const client = capturePrisma(prisma, { namespace: 'remote' });
77+
78+
const segment = new AWSXRay.Segment('prisma-xray-sample');
79+
80+
const run = async () => {
81+
const subSegment = segment.addNewSubsegment('Prisma');
82+
83+
await client.company.upsert(
84+
{
85+
create: {
86+
name: 'Prisma',
87+
createdAt: new Date(),
88+
},
89+
update: {},
90+
where: {
91+
id: 1,
92+
},
93+
},
94+
subSegment
95+
);
96+
const companies = await client.company.findMany(subSegment);
97+
console.log(companies);
98+
subSegment.close();
99+
// ...
100+
};
101+
102+
run();
76103
```

sdk_contrib/prisma/images/auto.png

73.4 KB
Loading
73.1 KB
Loading
121 KB
Loading
374 KB
Loading
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const { PrismaClient } = require('@prisma/client');
2+
const awsXray = require('aws-xray-sdk-core');
3+
const { capturePrisma } = require('../../dist');
4+
5+
awsXray.enableAutomaticMode();
6+
7+
const prisma = new PrismaClient();
8+
9+
const client = capturePrisma(prisma, { namespace: 'remote' });
10+
const ns = awsXray.getNamespace();
11+
12+
const segment = new awsXray.Segment('prisma-xray-sample-auto');
13+
14+
ns.run(async () => {
15+
const subSegment = segment.addNewSubsegment('Prisma');
16+
awsXray.setSegment(subSegment);
17+
await client.company.upsert({
18+
create: {
19+
name: 'Prisma',
20+
createdAt: new Date(),
21+
},
22+
update: {},
23+
where: {
24+
id: 1,
25+
},
26+
});
27+
const companies = await client.company.findMany();
28+
console.log(companies);
29+
subSegment.close();
30+
const timeout = segment.addNewSubsegment('timeout');
31+
setTimeout(() => {
32+
timeout.close();
33+
segment.close();
34+
}, 1000);
35+
prisma.$disconnect();
36+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const { PrismaClient } = require('@prisma/client');
2+
const awsXray = require('aws-xray-sdk-core');
3+
const { capturePrisma } = require('../../dist');
4+
5+
awsXray.enableManualMode();
6+
7+
const prisma = new PrismaClient();
8+
9+
const client = capturePrisma(prisma, { namespace: 'remote' });
10+
11+
const segment = new awsXray.Segment('prisma-xray-sample');
12+
13+
const run = async () => {
14+
const subSegment = segment.addNewSubsegment('Prisma');
15+
16+
await client.company.upsert(
17+
{
18+
create: {
19+
name: 'Prisma',
20+
createdAt: new Date(),
21+
},
22+
update: {},
23+
where: {
24+
id: 1,
25+
},
26+
},
27+
subSegment
28+
);
29+
const companies = await client.company.findMany(subSegment);
30+
console.log(companies);
31+
subSegment.close();
32+
const timeout = segment.addNewSubsegment('timeout');
33+
setTimeout(() => {
34+
timeout.close();
35+
segment.close();
36+
}, 1000);
37+
prisma.$disconnect();
38+
};
39+
40+
run();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
generator client {
2+
provider = "prisma-client-js"
3+
engineType = "binary"
4+
}
5+
6+
datasource db {
7+
provider = "sqlite"
8+
url = "file:dev.db"
9+
}
10+
11+
model Company {
12+
id Int @id @default(autoincrement())
13+
name String
14+
createdAt DateTime @default(now())
15+
updatedAt DateTime @updatedAt
16+
}

0 commit comments

Comments
 (0)