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

@dynatrace/metric-utils

v0.2.0

Published

Utility library for interacting with the Dynatrace metrics API v2

Downloads

4,851

Readme

dynatrace-metric-utils-js

JavaScript utility for preparing communication with the Dynatrace Metrics API v2.

Installation

The library may be built from source or installed from NPM by running:

npm install @dynatrace/metric-utils

Usage

An example for how to use this library can be found in example/index.ts. It shows how to create metrics lines that can be sent to a Dynatrace metrics ingest endpoint using an HTTP client library.

MetricFactory

For most basic use-cases, you will first create a MetricFactory. Using this MetricFactory, you will create individual Metrics which will then be serialized into strings.

import { MetricFactory } from "@dynatrace/metric-utils";

const factory = new MetricFactory(options);
const gauge = factory.createGauge("my_gauge", dimensions, value, date);

See a list of constructor options below.

Metric Creation Methods

There are three methods to create metrics. Each takes a name, dimension list, value, and an optional date. They return a Metric or undefined. When a metric is created, its name and dimensions are normalized for ingestion by the Dynatrace Metrics API v2. If a metric cannot be normalized, it will be undefined.

  • createGauge - A single value serialized as gauge,<value>
  • createCounterDelta - A single value serialized as count,delta=<value>
  • createSummary - A summary of multiple values serialized as gauge,min=<min>,max=<max>,sum=<sum>,count=<count>

Every metric is serializable using its metric.serialize() method, which returns a string. This string can be ingested by the Dynatrace Metrics API v2.

Dynatrace Metadata Enrichment

When run on a host which has an active OneAgent, the exported function getDynatraceMetadata will return a list of dimensions provided by Dynatrace. If no metadata is found, it will return an empty list. More information on the underlying feature that is used by the library can be found in the Dynatrace documentation provided by Dynatrace.

Common constants

The library also provides constants that might be helpful in the projects consuming this library.

To access the constants, call the respective methods from the apiconstants package:

const defaultOneAgentEndpoint = getDefaultOneAgentEndpoint()

Currently available constants are:

  • the default local OneAgent metric API endpoint (getDefaultOneAgentEndpoint())
  • the limit for how many metric lines can be ingested in one request (getPayloadLinesLimit())

Constructor Options

prefix: string

A prefix will be used to prefix the name of any Metric created by the MetricFactory. For example, if the prefix 'my_prefix' is used, a metric created with the name 'my_metric' will be serialized with the name 'my_prefix.my_metric'.

const factory = new MetricFactory({
    prefix: "my_prefix",
});

const metric = factory.createGauge("my_gauge", [], 10);

console.log(metric.serialize());
// my_prefix.my_gauge 10

defaultDimensions: Dimension[]

defaultDimensions is a list of dimensions which will be exported with every metric unless one or more of them is overridden by a metric dimension or one of the static dimensions.

const factory = new MetricFactory({
    defaultDimensions: [{ key: 'my_dimension', value: 'value 1'}],
});

const gauge1 = factory.createGauge("my_gauge", [], 10);
const gauge2 = factory.createGauge("my_gauge", [{ key: 'my_dimension', value: 'value 2' }], 10);

console.log(gauge1.serialize());
// my_gauge,my_dimension=value\ 1 10
console.log(gauge2.serialize());
// my_gauge,my_dimension=value\ 2 10

staticDimensions: Dimension[]

staticDimensions is a list of dimensions which will be exported with every metric and cannot be overridden by metric dimensions or by default dimensions.

const factory = new MetricFactory({
    staticDimensions: [{ key: 'dim', value: 'static'}],
});

const gauge1 = factory.createGauge("my_gauge", [], 10);
const gauge2 = factory.createGauge("my_gauge", [{ key: 'dim', value: 'metric'}], 10);

console.log(gauge1.serialize());
// my_gauge,dim=static 10
console.log(gauge2.serialize());
// my_gauge,dim=static 10