|
1 | | -## Requirements |
| 1 | +# Prisma-xray |
2 | 2 |
|
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. |
5 | 4 |
|
6 | | -## AWS X-Ray and Koa |
| 5 | + |
7 | 6 |
|
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 |
11 | 8 |
|
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 | + |
16 | 10 |
|
17 | | -In automatic mode, you can get the current segment/subsegment at any time: |
18 | | -var segment = AWSXRay.getSegment(); |
| 11 | +## Requirements |
19 | 12 |
|
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 |
22 | 15 |
|
23 | | -## Middleware Usage |
| 16 | +## AWS X-Ray and Prisma |
24 | 17 |
|
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. |
30 | 25 |
|
31 | 26 | ## Automatic mode examples |
32 | 27 |
|
| 28 | + |
| 29 | + |
33 | 30 | ```js |
34 | 31 | import { PrismaClient } from '@prisma/client'; |
35 | 32 | import AWSXRay from 'aws-xray-sdk'; |
| 33 | +import { capturePrisma } from 'aws-xray-sdk-prisma'; |
36 | 34 |
|
37 | | -Xray.enableAutomaticMode(); |
| 35 | +const ns = AWSXRay.getNamespace(); |
38 | 36 |
|
39 | 37 | const prisma = new PrismaClient(); |
40 | 38 |
|
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 | + }, |
56 | 55 | }); |
| 56 | + const companies = await client.company.findMany(); |
| 57 | + console.log(companies); |
| 58 | + subSegment.close(); |
| 59 | + // ... |
| 60 | +}); |
57 | 61 | ``` |
58 | 62 |
|
59 | 63 | ## Manual mode examples |
60 | 64 |
|
61 | | -```js |
62 | | -var AWSXRay = require('aws-xray-sdk-core'); |
63 | | -var xrayKoa = require('aws-xray-sdk-koa2'); |
64 | | -var app = new Koa(); |
| 65 | + |
65 | 66 |
|
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'; |
67 | 71 |
|
68 | | -var AWSXRay = require('aws-xray-sdk'); |
| 72 | +AWSXRay.enableManualMode(); |
69 | 73 |
|
70 | | -app.use(xrayKoa.openSegment('defaultName')); //Required at the start of your routes |
| 74 | +const prisma = new PrismaClient(); |
71 | 75 |
|
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(); |
76 | 103 | ``` |
0 commit comments