Skip to content

OpenTelemetry and general metrics support for typescript generated prisma client #28141

@vpmedia

Description

@vpmedia

Feature Summary

After upgrading to Prisma 6.16.2 and client generated using the prisma-client provider, I can no longer query metrics using the preview feature.
I'm using MariaDb database adapter and it does not expose the relevant API either.

Use Cases & Problem Description

My schema file for reference:

generator client {
  provider               = "prisma-client"
  output                 = "../compiled"
  engineType             = "client"
  generatedFileExtension = "ts"
  importFileExtension    = "ts"
  moduleFormat           = "esm"
  runtime                = "nodejs"
}

Calling await prisma.$metrics.prometheus() raises a Method not implemented error.

Code reference for the raised exception:

throw new Error('Method not implemented.')

--

Checking at
https://github.com/prisma/prisma/tree/main/packages/instrumentation/src
I do not see any metrics related implementation, only for tracing. Since I'm using Open-Telemetry in my application, auto instrumentation would be the easiest choice.

--

Because I'm using the MariaDb database driver, I've also tried to collect metrics using it's API but it does not expose the required pool instance in order to do this.

const adapter = new PrismaMariaDb(connectionString);
const driver = await adapter.connect(); // instantiates a new PrismaMariaDbAdapter
const pool = driver.underlyingDriver(); // returns the mariadb.Pool instance

const getAdapterMetrics = () => {
    return {
        totalConnections: pool.totalConnections(),
        idleConnections: pool.idleConnections(),
        waitingConnections: pool.taskQueueSize(),
        activeConnections: pool.totalConnections() - pool.idleConnections(),
    };
};
console.log(getAdapterMetrics());

My issue with the code above is that if I understand the logic correctly it does create a new connection pool, but I would like to be able to query the PrismaClient.adapter.driver.pool which the Prisma MariaDb database adapter does not expose.

export declare class PrismaMariaDb implements SqlDriverAdapterFactory {
    #private;
    readonly provider = "mysql";
    readonly adapterName: string;
    constructor(config: mariadb.PoolConfig | string, options?: PrismaMariadbOptions);
    connect(): Promise<PrismaMariaDbAdapter>; // CREATES A NEW DB ADAPTER INSTANCE
}
declare class PrismaMariaDbAdapter extends MariaDbQueryable<mariadb.Pool> implements SqlDriverAdapter {
    private readonly capabilities;
    private readonly options?;
    constructor(client: mariadb.Pool, capabilities: Capabilities, options?: PrismaMariadbOptions | undefined);
    executeScript(_script: string): Promise<void>;
    getConnectionInfo(): ConnectionInfo;
    startTransaction(isolationLevel?: IsolationLevel): Promise<Transaction>;
    dispose(): Promise<void>;
    underlyingDriver(): mariadb.Pool; // THE POOL INSTANCE WHICH NEEDED FOR CONN METRICS
}

The ability to pass a Pool instance to the factory would also help to do this, refs:
#27992
#26885

--

Additionally the documentation related to metrics collection, post v6.16.2 (and future v7) Prisma Client version is missing, I'm only finding obsolete information about the deprecated metrics API.

Proposed Solution

  • Expose the used adapter instance with pool in the generated PrismaClient instance
  • Implement auto metrics instrumentation in the instrumentation pkg
  • Update documentation how to collect metrics using the new client

Alternative Solutions

Potential Considerations

No response

Prisma Version

6.16.2

What part of Prisma does this affect?

Prisma Client

Additional Context

No response

Pre-Submission Checklist

  • I have searched existing issues to make sure this is not a duplicate
  • I have checked the Prisma roadmap to see if this is already planned
  • I have described why this belongs in Prisma Core rather than a solution in application code

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug/1-unconfirmedBug should have enough information for reproduction, but confirmation has not happened yet.kind/featureA request for a new feature.topic: driverAdapterstopic: metricstrackedThis issue is internally tracked by the Prisma Team on Linear.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions