prisma / optimize-feedback

Community feedback on Prisma Optimize
0 stars 0 forks source link

Error: "prisma:client span is expected to be entered in the client extension" when using Prisma Optimize with tracing enabled #11

Closed fahimahammed closed 1 month ago

fahimahammed commented 2 months ago

Describe the bug When using Prisma Optimize with Prisma ORM and following the setup instructions, the following error occurs:

Error: prisma:client:operation span is expected to be entered in the client extension when tracing is enabled

To Reproduce Steps to reproduce the behavior:

  1. Install @prisma/extension-optimize and set up Prisma according to the documentation.
  2. Enable the tracing preview feature in the schema.prisma file.
  3. Extend Prisma Client with Prisma Optimize.
  4. Run a Prisma query in your application.

Expected behavior The Prisma queries should execute without errors, and performance profiling and recommendations should be available on the Prisma Optimize dashboard.

Development environment:

Prisma schema:

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

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model User {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  email     String   @unique
  name      String
  age       Int
  country   String
  role      Role     @default(USER)
  posts     Post[]
  profile   Profile?
}

model Profile {
  id     Int    @id @default(autoincrement())
  bio    String
  user   User   @relation(fields: [userId], references: [id], onDelete: Cascade)
  userId Int    @unique
}

model Post {
  id         Int        @id @default(autoincrement())
  createdAt  DateTime   @default(now())
  title      String
  published  Boolean    @default(false)
  author     User       @relation(fields: [authorId], references: [id], onDelete: Cascade)
  authorId   Int
  categories Category[]
}

model Category {
  id    Int    @id @default(autoincrement())
  name  String
  posts Post[]
}

enum Role {
  USER
  ADMIN
}

index.ts

import { PrismaClient, Role } from "@prisma/client";
import { withOptimize } from "@prisma/extension-optimize";

const prisma = new PrismaClient().$extends(withOptimize());

const main = async () => {
    // find all
    const findAllData = await prisma.user.findMany();

    // Find first
    const findFirstData = await prisma.user.findFirst({
        where: {
            id: 3,
            role: Role.ADMIN
        }
    });

    // find first or throw
    const findFirstOrThrow = await prisma.user.findFirstOrThrow({
        where: {
            id: 3,
            //email: "user@gmail.com"
        }
    });

    // selet fields
    const selectFields = await prisma.user.findFirst({
        where: {
            id: 3
        },
        select: {
            name: true,
            age: true,
            email: true
        }
    })

    console.log(selectFields)
}

main()
    .catch(err => console.error(err))
jonluca commented 1 month ago

Fixed it by enabling the instrumentationHook: true, in nextjs experimental and then enabling the open telemetry prisma-instrumentation.

fahimahammed commented 1 month ago

Thank you

millsp commented 1 month ago

We will be providing you with a proper fix in upcoming releases.