@prisma/instrumentation
TypeScript icon, indicating that this package has built-in type declarations

5.13.0 • Public • Published

@prisma/instrumentation

npm version PRs Welcome GitHub license Slack Discord

OTEL - OpenTelemetry compliant instrumentation for Prisma Client.

⚠️ Warning: This package is provided as part of the tracing Preview Feature Its release cycle does not follow SemVer, which means we might release breaking changes (change APIs, remove functionality) without any prior warning.

Installing

$ npm install @prisma/instrumentation

Usage

import { registerInstrumentations } from '@opentelemetry/instrumentation'
import { PrismaInstrumentation } from '@prisma/instrumentation'

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

Don't forget to set previewFeatures:

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

Jaeger

Exporting traces to Jaeger Tracing.

import { context } from '@opentelemetry/api'
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'
import { registerInstrumentations } from '@opentelemetry/instrumentation'
import { Resource } from '@opentelemetry/resources'
import { BasicTracerProvider, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base'
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'
import { PrismaInstrumentation } from '@prisma/instrumentation'

import { PrismaClient } from '.prisma/client'

const contextManager = new AsyncHooksContextManager().enable()

context.setGlobalContextManager(contextManager)

const otlpTraceExporter = new OTLPTraceExporter()

const provider = new BasicTracerProvider({
  resource: new Resource({
    [SemanticResourceAttributes.SERVICE_NAME]: 'test-tracing-service',
    [SemanticResourceAttributes.SERVICE_VERSION]: '1.0.0',
  }),
})

provider.addSpanProcessor(new SimpleSpanProcessor(otlpTraceExporter))
provider.register()

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

async function main() {
  const prisma = new PrismaClient()

  const email = `user.${Date.now()}@prisma.io`

  await prisma.user.create({
    data: {
      email: email,
    },
  })
}

main()

Package Sidebar

Install

npm i @prisma/instrumentation

Weekly Downloads

243,673

Version

5.13.0

License

Apache-2.0

Unpacked Size

35.7 kB

Total Files

17

Last publish

Collaborators

  • pirix-gh
  • sevinf
  • aqrln
  • jolg42
  • janpio
  • prismabot