P
Prisma2w ago
Nurul

How to use tracing preview feature of Prisma?

Give step by step instructions
1 Reply
Prisma AI Help
Skipping coffee for instant knowledge! Let's brew that AI answer now. To use the tracing preview feature in Prisma, follow these step-by-step instructions: 1. Install the required dependencies:
npm install prisma@latest @prisma/client@latest @prisma/instrumentation@latest @opentelemetry/api@latest
npm install @opentelemetry/semantic-conventions @opentelemetry/exporter-trace-otlp-http @opentelemetry/sdk-trace-base @opentelemetry/sdk-trace-node @opentelemetry/resources

npm install prisma@latest @prisma/client@latest @prisma/instrumentation@latest @opentelemetry/api@latest
npm install @opentelemetry/semantic-conventions @opentelemetry/exporter-trace-otlp-http @opentelemetry/sdk-trace-base @opentelemetry/sdk-trace-node @opentelemetry/resources

2. Enable the tracing feature in your schema.prisma file:
generator client {
provider = "prisma-client-js"
previewFeatures = ["tracing"]
}

generator client {
provider = "prisma-client-js"
previewFeatures = ["tracing"]
}

3. Regenerate Prisma Client:
npx prisma generate

npx prisma generate

4. Set up tracing in your application. Here's an example configuration using @opentelemetry/sdk-trace-node:
import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from '@opentelemetry/semantic-conventions'
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'
import { SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base'
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'
import { PrismaInstrumentation, registerInstrumentations } from '@prisma/instrumentation'
import { Resource } from '@opentelemetry/resources'

const provider = new NodeTracerProvider({
resource: new Resource({
[SEMRESATTRS_SERVICE_NAME]: 'your-service-name',
[SEMRESATTRS_SERVICE_VERSION]: 'your-service-version',
}),
})

provider.addSpanProcessor(new SimpleSpanProcessor(new OTLPTraceExporter()))

registerInstrumentations({
tracerProvider: provider,
instrumentations: [new PrismaInstrumentation()],
})

provider.register()

import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from '@opentelemetry/semantic-conventions'
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'
import { SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base'
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'
import { PrismaInstrumentation, registerInstrumentations } from '@prisma/instrumentation'
import { Resource } from '@opentelemetry/resources'

const provider = new NodeTracerProvider({
resource: new Resource({
[SEMRESATTRS_SERVICE_NAME]: 'your-service-name',
[SEMRESATTRS_SERVICE_VERSION]: 'your-service-version',
}),
})

provider.addSpanProcessor(new SimpleSpanProcessor(new OTLPTraceExporter()))

registerInstrumentations({
tracerProvider: provider,
instrumentations: [new PrismaInstrumentation()],
})

provider.register()

5. Make sure to initialize tracing before importing PrismaClient in your application. 6. Use Prisma Client as usual in your application. Tracing will now be automatically enabled for your Prisma queries. 7. To visualize your traces, you can use a tool like Jaeger. Run Jaeger locally us ing Docker:
docker run --rm --name jaeger -d -e COLLECTOR_OTLP_ENABLED=true -p 16686:16686 -p 4318:4318 jaegertracing/all-in-one:latest

docker run --rm --name jaeger -d -e COLLECTOR_OTLP_ENABLED=true -p 16686:16686 -p 4318:4318 jaegertracing/all-in-one:latest

8. Access the Jaeger dashboard at <http://localhost:16686/> to view your traces. Remember that tracing can have a performance impact on your application, especially when sending a large number of spans. Consider using techniques like batching or sampling to reduce this impact if necessary. For more detailed information and advanced configurations, refer to the OpenTelemetry tracing documentation. Prisma Support for Tracing and Metrics Is Now in Preview Monitor Your Server with Tracing Using OpenTelemetry & Prisma ✅ Great, thanks for letting us know that this solved your question!

Did you find this page helpful?