npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@prairielearn/opentelemetry

v2.1.5

Published

Opinionated wrapper around various `@opentelemetry/*` packages.

Downloads

153

Readme

@prairielearn/opentelemetry

Opinionated wrapper around various @opentelemetry/* packages.

Usage

You should require this package as early as possible during application initialization and call init() once the application configuration is available.

import { init } from '@prairielearn/opentelemetry';

// ...

await init({
  openTelemetryEnabled: true,
  openTelemetryExporter: 'honeycomb',
  openTelemetryMetricExporter: 'honeycomb',
  openTelemetryMetricExportIntervalMillis: 30_000,
  openTelemetrySamplerType: 'always-on',
  openTelemetrySampleRate: 0.1,
  honeycombApiKey: 'KEY',
  honeycombDataset: 'DATASET',
});

This will automatically instrument a variety of commonly-used Node packages.

When using code from the OpenTelemetry libraries, make sure you import it via @prairielearn/opentelemetry instead of installing it separately to ensure that there is only one version of each OpenTelemetry package in use at once. If the desired functionality is not yet exported, please add it!

Traces

To easily instrument individual pieces of functionality, you can use the instrumented() helper function:

import { instrumented } from '@prairielearn/opentelemetry';

async function doThing() {
  return instrumented('span.name', async (span) => {
    span.setAttribute('attribute.name', 'value');
    await doThing();
  });
}

This will automatically set the span status and record any exceptions that occur.

If you have a more complex use case, you can manually instrument code with the trace export:

import { trace, SpanStatusCode } from '@prairielearn/opentelemetry';

const tracer = trace.getTracer('default');
await tracer.startActiveSpan('span.name', async (span) => {
  try {
    await doWork();
    span.setStatus({ status: SpanStatusCode.OK });
  } catch (err) {
    span.recordException(err);
    span.setStatus({
      status: SpanStatusCode.ERROR,
      message: err.message,
    });
    throw err;
  }
});

Metrics

You can manually create counters and other metrics with the following functions

  • getHistogram
  • getCounter
  • getUpDownCounter
  • getObservableCounter
  • getObservableUpDownCounter
  • getObservableGauge
import { metrics, getCounter, ValueType } from '@prairielearn/opentelemetry';

function handleRequest(req, res) {
  const meter = metrics.getMeter('meter-name');
  const requestCounter = getCounter(meter, 'request.count', {
    valueType: ValueType.INT,
  });
  requestCounter.add(1);
}

You can also use the instrumentedWithMetrics helper to automatically capture a duration histogram and error count:

import { metrics, instrumentedWithMetrics } from '@prairielearn/opentelemetry';

const meter = metrics.getMeter('meter-name');
await instrumentedWithMetrics(meter, 'operation.name', async () => {
  const random = Math.random() * 1000;
  await new Promise((resolve) => setTimeout(resolve, random));
  if (random > 900) {
    throw new Error('Failed!');
  }
});

To capture statistics about a constantly changing value (for instance, the size of a database connection pool), you can use createObservableValueGauges. This will "observe" your chosen value on a regular interval and collect the min/max/average of that value for each metrics collection interval.

import { metrics, createObservableValueGauges } from '@prairielearn/opentelemetry';

const meter = metrics.getMeter('meter-name');
createObservableValueGauges(
  meter,
  'db.pool.size',
  {
    // The interval that your value will be observed, in milliseconds.
    interval: 1000,
  },
  () => pool.size,
);