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

axios-prometheus-adapter

v0.0.5

Published

Provides prometheus metrics for outgoing axios requests

Downloads

43

Readme

Axios Prometheus Adapter

Install

It requires prom-client library to be installed.

npm i axios-prometheus-adapter

yarn add axios-prometheus-adapter

Usage

Set up your code

You can use it with default metrics, or with custom metrics as well.

Here is an example, how to use it with default metrics, and with the global axios instance:

import axios from 'axios';
import { createAxiosPrometheusMiddleware } from 'axios-prometheus-adapter';
import { collectDefaultMetrics, Registry } from 'prom-client';

const registry = new Registry();
collectDefaultMetrics({ register: registry });
createAxiosPrometheusMiddleware(axios, registry);

This library does not depend on any framework like express, of fastify.

Here is an example, how to use it with express:

const app = express();
const port = 3000;

app.get('/', async (req, res) => {
  await axios('https://jsonplaceholder.typicode.com/posts/1');
  await axios('https://jsonplaceholder.typicode.com/posts/2');
  res.sendStatus(200);
});

app.get('/metrics', async (req, res) => {
  const metrics = await registry.metrics();
  res.send(metrics);
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

Output

Calling the / endpoint first will trigger two API calls, and return with a status code 200. To collect metrics of api calls require to have api calls first.

curl http://0.0.0.0/

After this first call we can call the /metrics endpoint to get some metics data:

curl http://0.0.0.0/metrics

Output:

[[[DEFAULT NODE METRICS]]]
...
# HELP http_client_requests_seconds_count http_client_requests_seconds_count
# TYPE http_client_requests_seconds_count histogram
http_client_requests_seconds_bucket{le="0.005",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 0
http_client_requests_seconds_bucket{le="0.01",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 0
http_client_requests_seconds_bucket{le="0.025",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 0
http_client_requests_seconds_bucket{le="0.05",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 0
http_client_requests_seconds_bucket{le="0.1",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 0
http_client_requests_seconds_bucket{le="0.25",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 0
http_client_requests_seconds_bucket{le="0.5",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 1
http_client_requests_seconds_bucket{le="1",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 1
http_client_requests_seconds_bucket{le="2.5",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 1
http_client_requests_seconds_bucket{le="5",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 1
http_client_requests_seconds_bucket{le="10",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 1
http_client_requests_seconds_bucket{le="+Inf",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 1
http_client_requests_seconds_sum{status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 0.257366432
http_client_requests_seconds_count{status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/1"} 1
http_client_requests_seconds_bucket{le="0.005",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 0
http_client_requests_seconds_bucket{le="0.01",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 0
http_client_requests_seconds_bucket{le="0.025",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 0
http_client_requests_seconds_bucket{le="0.05",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 0
http_client_requests_seconds_bucket{le="0.1",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 0
http_client_requests_seconds_bucket{le="0.25",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 1
http_client_requests_seconds_bucket{le="0.5",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 1
http_client_requests_seconds_bucket{le="1",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 1
http_client_requests_seconds_bucket{le="2.5",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 1
http_client_requests_seconds_bucket{le="5",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 1
http_client_requests_seconds_bucket{le="10",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 1
http_client_requests_seconds_bucket{le="+Inf",status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 1
http_client_requests_seconds_sum{status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 0.107517055
http_client_requests_seconds_count{status_code="200",method="GET",protocol="https:",host="jsonplaceholder.typicode.com",path="/posts/2"} 1

Custom config

You can overwrite the name, the help and the labels property of this metrics by providing an optional third argument of the createAxiosPrometheusMiddleware function.

createAxiosPrometheusMiddleware(axios, registry, {
  name: 'your_custom_metric_name',
  help: 'your costum help description',
  labelNames: ['status_code', 'method']
});

Here is the type of the config object:

export type AxiosPrometheusAdapterConfig = {
  name: string,
  help: string,
  labelNames: string[]
};

labelNames is an array with predefined values. By default we support the following labels: ['status_code', 'method', 'protocol', 'host', 'path']