Node.js - Prisma
Quickstart Guide
You will need your project token for initializing your library. You can get your project token from Projects Page
Run the following command to install the SDK:
npm install --save @metis-data/prisma-interceptor
If it is the first time you set up opentelemetry, some more opentelemetry packages are required:
npm install --save @opentelemetry/api \
@opentelemetry/context-async-hooks \
@opentelemetry/instrumentation \
@opentelemetry/instrumentation-http \
@opentelemetry/resources \
@opentelemetry/sdk-trace-base \
@opentelemetry/semantic-conventions
Open your
prisma.schema
and add to your generator client
struct the following line:generator client {
...
previewFeatures = ["tracing"] // Add this line.
}
Run the following command:
prisma generate
Create
tracer
file and add the following code:1
import opentelemetry from '@opentelemetry/api';
2
import { registerInstrumentations } from '@opentelemetry/instrumentation';
3
import {
4
BasicTracerProvider,
5
BatchSpanProcessor,
6
ConsoleSpanExporter,
7
SimpleSpanProcessor,
8
} from '@opentelemetry/sdk-trace-base';
9
import {
10
getPrismaInstrumentation,
11
getMetisExporter,
12
MetisHttpInstrumentation,
13
getResource,
14
} from '@metis-data/prisma-interceptor';
15
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
16
17
export const startMetisInstrumentation = () => {
18
const tracerProvider = new BasicTracerProvider({
19
resource: getResource(process.env.METIS_SERVICE_NAME, 'service-version'),
20
});
21
22
const metisExporter = getMetisExporter(process.env.METIS_API_KEY);
23
24
tracerProvider.addSpanProcessor(new BatchSpanProcessor(metisExporter));
25
26
if (process.env.OTEL_DEBUG) {
27
tracerProvider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
28
}
29
30
const contextManager = new AsyncHooksContextManager();
31
32
contextManager.enable();
33
opentelemetry.context.setGlobalContextManager(contextManager);
34
35
tracerProvider.register();
36
37
// Urls regex to exclude from instrumentation
38
const excludeUrls = [/favicon.ico/];
39
registerInstrumentations({
40
instrumentations: [
41
new MetisHttpInstrumentation(excludeUrls),
42
getPrismaInstrumentation()
43
],
44
});
45
};
After creating the tracer call it from application root and add the following code:
// main.ts
import { startMetisInstrumentation } from './tracer';
startMetisInstrumentation();
import { PrismaClient } from "@prisma/client";
import { setInstrumentedPrismaClient } from "@metis-data/prisma-interceptor";
// more imports...
const prisma = new PrismaClient({
log: [
{
emit: 'event',
level: 'query'
}
]
});
setInstrumentedPrismaClient(prisma);
// bootstrap()
Variable Name | Description |
---|---|
METIS_API_KEY | <String> API Key to use |
METIS_ENVIRONMENT | <String> Text used to identify the source that sends the instrumentation data. |
METIS_DISABLED | <Boolean> If True Metis Instrumentation is fully disabled. We strongly advise to disable the instrumentation when in production to prevent sensitive data from leaving your organization's database. |
METIS_SERVICE_NAME | <String> Gives ability to distinguish between services. Useful when working with Micro Services. |
OTEL_DEBUG | <Boolean> If True, console exporter is added to print incoming spans to console. |
Now you can run your application as you normally would, and interact with it in a manner that causes the server to interact with your database.
Last modified 3d ago