Hi ! I’m just starting to integrate metrics (prett...
# prisma-client
f
Hi ! I’m just starting to integrate metrics (pretty much following the example with hotshots in the docs) but my team and I have a quite strong dislike of using any, and I was trying to use the metric types defined in the client but it seems they are not exported. Would this be something that could be changed ? 🙂
1
Also in the example provided here: https://www.prisma.io/docs/concepts/components/prisma-client/metrics#use-prisma-metrics-with-statsd I have a little trouble understand what the diffing on histograms is for, I wouldn’t mind a clarification on what is done in this example 🤔
a
Hey Florian, exciting to hear you are trying out metrics 😁! I’ll check with the team if we plan to expose the metrics types, but in the meantime, you might be able to extract them using TypeScript’s utility types:
Copy code
type PrismaJsonMetrics = Awaited<ReturnType<typeof prisma.$metrics.json>>
I have a little trouble understand what the diffing on histograms is for, I wouldn’t mind a clarification on what is done in this example.
In the docs page you linked, we make this note:
Note: You must provide counter metrics to StatsD as a series of values that are incremented or decremented from a previous retrieval of the metrics. However, Prisma counter metrics return absolute values. Therefore, you must convert your counter metrics to a series of incremented and decremented values and send them to StatsD as gauge data. In the code example below, we convert counter metrics into incremented and decremented gauge data in
diffHistograms
.
So, we do the diffing in order to convert the metrics JSON into the correct format for StatsD. Let me know if I can clarify anything further!
f
you might be able to extract them using TypeScript’s utility types:
I did at first but I had to copy the types eventually to manage to remove all the `any`s in your example since I retrieving the type of arrays elements can be tricky and was taking me way too long. In my case getting Datadog to scrape the metrics would be the better solution but I have issues that are non-prisma related which make this approach a lot easier to implement to test those metrics.
> we convert counter metrics into incremented and decremented gauge data in
diffHistograms
.
I understood, though it’s a bit hard to understand exactly how it’s done at first 😅
But I’m glad you are exposing those, we’ve had some connections pool issues again recently, and my biggest pain point with prisma so far was running it in production and having no way to observe what’s happening on the prisma side.
a
I just checked with the team and it sounds like we already have a PR open to export the metrics types 🙌. They should be shipping in a new Prisma release in the near future.
f
👍
a
Is there a way of exporting Prisma metrics with a proper open-telemetry setup? I’m using otel packages for the app (nestjs) metrics
Copy code
const otelSDK = new NodeSDK({
  metricExporter: new PrometheusExporter({ port: 8081, endpoint: '/metrics' }),
  metricInterval: 5000,
  // spanProcessor: new BatchSpanProcessor(new JaegerExporter()),
  contextManager: new AsyncLocalStorageContextManager(),
  textMapPropagator: new CompositePropagator({
    propagators: [
      new JaegerPropagator(),
      new W3CTraceContextPropagator(),
      new W3CBaggagePropagator(),
      new B3Propagator(),
      new B3Propagator({ injectEncoding: B3InjectEncoding.MULTI_HEADER })
    ]
  }),
  instrumentations: [
    new AmqplibInstrumentation(),
    new DnsInstrumentation(),
    new ExpressInstrumentation(),
    new GraphQLInstrumentation(),
    new HttpInstrumentation(),
    new IORedisInstrumentation(),
    new NestInstrumentation(),
    new NetInstrumentation(),
    new PgInstrumentation(),
    new PinoInstrumentation()
  ]
});

export default otelSDK;
what’s the correct way of re-exporting Prisma metrics in this setup?
m
Hey @Alexandr Bordun, do you have some way to send custom metrics from your app? We don't have any automated thing, but I think you could manually set this up. Something like:
Copy code
// Send the current state of metrics every minute
setInterval(() => {
  prisma.$metrics().then(metrics => {
    otel.sendMetrics(metrics) // not sure how sending metrics works in otel
  })
}, 60000)
f
The metrics are pre-aggregated so if you have an otel collector setup I might recommend adding an endpoint that expose the prisma metrics in prometheus format, and add config in the collector to scrape that endpoint.
@Alexandr Bordun
a
@Matt Mueller (Prisma Client PM) otel sdk itself only represents a wrapper of instrumentations, propagators, exporters etc. So I was hoping Prisma metrics would be able to integrate into the managed otel instance somehow (via its own instrumentation or something similar). the problem is that prisma.metrics returns a set of pregenerated counters/gauges/etc (or even the generated output), so it works rather as a standalone otel instance (which in most of the cases - won’t be true, there will be some other app metrics) the documented examples don’t really help much, as they assume you don’t have any other metrics (so prisma metrics only), or “magically” get the generated otel output and concatenate it with prisma’s
otel.sendMetrics()
that’s one thing I have problems with, the rest is not a problem. @Florian Thelliez I do have a collector, and metrics are exposed for Prometheus already (by exposing other app metrics) I just need to find a way to stitch these metrics with prisma’s
m
the problem is that prisma.metrics returns a set of pregenerated counters/gauges/etc (or even the generated output), so it works rather as a standalone otel instance (which in most of the cases - won’t be true, there will be some other app metrics)
I would have thought the opposite. That prisma metrics aren't a standalone otel instance. However you send your app metrics is how you send your prisma metrics.