Code Monkey home page Code Monkey logo

opentelemetry-metric-js's Introduction

Dynatrace

Dynatrace supports native OpenTelemetry protocol (OTLP) ingest for traces, metrics and logs. All signals can be sent directly to Dynatrace via OTLP protobuf over HTTP using the built-in OTLP/HTTP Exporter available in the OpenTelemetry JavaScript SDK. More information on configuring your JavaScript applications to use the OTLP exporter can be found in the Dynatrace documentation.

Dynatrace OpenTelemetry Metrics Exporter for JavaScript

Static Badge

Warning Dynatrace supports native OpenTelemetry protocol (OTLP) ingest for traces, metrics and logs. Therefore, the proprietary Dynatrace OpenTelemetry metrics exporter is deprecated in favor of exporting via OTLP/HTTP.

The exporter is still available but after the end of 2023, no support, updates, or compatibility with newer OTel versions will be provided.

Please refer to the migration guide for instructions on how to migrate to the OTLP HTTP exporter, as well as reasoning and benefits for this transition.

For an example on how to configure the OTLP exporter in a JavaScript application, check out the JavaScript integration walk-through page in the Dynatrace documentation.

This exporter allows exporting metrics created using the OpenTelemetry SDK for JavaScript directly to Dynatrace.

It was built against OpenTelemetry SDK version 1.9.1.

More information on exporting OpenTelemetry metrics to Dynatrace can be found in the Dynatrace documentation.

Getting started

The general setup of OpenTelemetry JS is explained in the official Getting Started Guide.

Using the Metrics API is explained in the Monitor Your NodeJS Application section.

Install Dependencies

The Dynatrace OpenTelemetry exporter requires the following prerequisites:

  • Node.js 14+
  • NPM (8+ recommended, included with Node.js)
# Optional - update NPM
npm install --global npm

# Install the Dynatrace OpenTelemetry Metrics Exporter using NPM
npm install @dynatrace/opentelemetry-exporter-metrics

If you are using a npm version < 7, please install the @opentelemetry/api peer dependency manually.

# Install peer dependency @opentelemetry/api
npm install @opentelemetry/api

Initialize components

The Dynatrace exporter is added and set-up like this:

'use strict';

const { diag, DiagConsoleLogger, DiagLogLevel } = require('@opentelemetry/api');
const { Resource } = require('@opentelemetry/resources');
const { MeterProvider } = require('@opentelemetry/sdk-metrics');
const { configureDynatraceMetricExport } = require('@dynatrace/opentelemetry-exporter-metrics');


// optional: set up logging for OpenTelemetry
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.ALL)

// Configure a MeterProvider
const provider = new MeterProvider({
  resource: new Resource({
    'service.name': 'opentelemetry-metrics-sample-dynatrace'
  })
});

const reader = configureDynatraceMetricExport(
  // exporter configuration
  {
    prefix: 'sample', // optional
    defaultDimensions: [ // optional
      { key: 'default-dim', value: 'default-dim-value' },
    ],
    // If no OneAgent is available locally, export directly to the Dynatrace server:
    // url: 'https://myenv123.live.dynatrace.com/api/v2/metrics/ingest',
    // apiToken: '<load API token from secure location such as env or config file>'
  },
  // metric reader configuration
  {
    exportIntervalMillis: 5000
  }
);

provider.addMetricReader(reader);

const meter = provider.getMeter('opentelemetry-metrics-sample-dynatrace');

// Your SDK should be set up correctly now. You can create instruments...
const requestCounter = meter.createCounter('requests', {
  description: 'Example of a Counter'
});

const upDownCounter = meter.createUpDownCounter('test_up_down_counter', {
  description: 'Example of a UpDownCounter'
});

// ...set up attributes...
const attributes = {
  pid: process.pid.toString(),
  environment: 'staging'
};

// ... and start recording metrics:
setInterval(() => {
  requestCounter.add(Math.round(Math.random() * 1000), attributes);
  upDownCounter.add(Math.random() > 0.5 ? 1 : -1, attributes);
}, 1000);

Metrics are exported periodically, depending on the value of exportIntervalMillis set above.

A full setup is provided in our example project.

Configuration

The exporter allows for configuring the following settings by setting them in the ExporterConfig in configureDynatraceMetricExport:

Name Type Description
prefix string See Metric key prefix.
defaultDimensions Array<Dimension> See Default attributes
url string See Endpoint.
apiToken string See API token.
dynatraceMetadataEnrichment boolean See Dynatrace Metadata enrichment.
maxRetries number See Retries on Connection Failure.
retryDelay number See Retries on Connection Failure.

In addition, there are some settings that will be passed to the MetricReader. These can be set in the ReaderConfig

Name Type Description Default
exportIntervalMillis number The interval in which metrics are exported. 60000 (60 seconds)
exportTimeoutMillis number The maximum timeout to wait for an export to finish. 30000 (30 seconds)
Dynatrace API Endpoint

API Endpoint and Token are optional. By default, metrics will be exported to the local OneAgent endpoint described below, if it is available.

The endpoint to which the metrics are sent is specified using the url parameter.

Given an environment ID myenv123 on Dynatrace SaaS, the metrics ingest endpoint would be https://myenv123.live.dynatrace.com/api/v2/metrics/ingest.

If a OneAgent is installed on the host, it can provide a local endpoint for providing metrics directly without the need for an API token. Depending on your environment, this feature might have to be enabled as described in the OneAgent metric API documentation first. Using the local API endpoint, the host ID and host name context are automatically added to each metric as dimensions. The default metric API endpoint exposed by the OneAgent is http://localhost:14499/metrics/ingest. If no Dynatrace API endpoint is set, the exporter will default to the local OneAgent endpoint.

Dynatrace API Token

Required only if an API endpoint is also provided.

The Dynatrace API token to be used by the exporter is specified using the apiToken parameter and could, for example, be read from an environment variable.

Creating an API token for your Dynatrace environment is described in the Dynatrace API documentation. The permission required for sending metrics is Ingest metrics (metrics.ingest) and it is recommended to limit scope to only this permission.

Metric Key Prefix

The prefix parameter specifies an optional prefix, which is prepended to each metric key, separated by a dot (<prefix>.<namespace>.<name>).

Default Attributes/Dimensions

The defaultDimensions parameter can be used to optionally specify a list of key/value pairs, which will be added as additional attributes/dimensions to all data points.

Retries on Connection Failure

The maxRetries parameter can be used to set the amount of times the exporter should retry on connection failures. By default, the exporter will retry 3 times before marking the batch as failed. This number must be greater than or equal to 0.

The retryDelay parameter can be used to set the time in milliseconds to wait until re-trying an export after a connection failure, the default is 1000ms. This number must be greater than or equal to 0.

Dynatrace Metadata Enrichment

If running on a host with a running OneAgent, the exporter will export metadata collected by the OneAgent to the Dynatrace endpoint. This typically consists of the Dynatrace host ID and process group ID. More information on the underlying feature used by the exporter can be found in the Dynatrace documentation. By default, this option is turned on.

Limitations

Histogram

OpenTelemetry Histograms are exported to Dynatrace as statistical summaries consisting of a minimum and maximum value, the total sum of all values, and the count of the values summarized. If the min and max values are not directly available on the metric data point, estimations based on the boundaries of the first and last buckets containing values are used.

Attribute type limitations

Currently, only string type attribute values are supported. Attributes with values of any other type will be dropped and not exported. If you need those values to be exported, please convert them to string type before export.

opentelemetry-metric-js's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

opentelemetry-metric-js's Issues

No unit or description seen when exporting metrics

Hello,

I'm using the library to export custom metrics to dynatrace.
The metrics are exported successfully, although when I'm creating the metric I specify a description and a unit to it, and I can't see it in Dynatrace.
In fact, I see that the unit is 'Unspecified' and that there's no description to it.

Here's a code sample:

import { Meter, MeterProvider } from "@opentelemetry/metrics";
import { DynatraceMetricExporter } from '@dynatrace/opentelemetry-exporter-metrics';
const exporter = new DynatraceMetricExporter({
		url: apiurl + "/v2/metrics/ingest",
		APIToken,
		logger,
		tags: getCustomTags()
});

const meter = new MeterProvider({ exporter, interval: 60000 }).getMeter('my-custom-meter');
const counter = meter.createCounter('active.session.max.size', {
   description: 'Active Session Max Size'
   unit: 'Count'
});

counter.add(1, getLabels());

In dynatrace, I can see my metric, but it lacks the description and the unit:
image

Can you kindly suggest what I'm doing wrong?

Thanks!

Export DynatraceMetricExporter class to be used outside library

Is your feature request related to a problem? Please describe.
Currently I have a scenario which I've built my own Reader on the top of OTLP MetricReader class. And in a best scenario solution I would like to use the DynatraceMetricExporter class which is part of this library but it unfortunately is not exported to external use.

My request id kind of simple, would be possible to make the DynatraceMetricExporter accessible by the library consumers?

Describe the solution you'd like
I want to be able to import the DynatraceMetricExporter class from this library directly in my project so I could use it directly in my project without calling the configureDynatraceMetricExport which returns an OTLP Reader.

import { DynatraceMetricExporter } from "@dynatrace/opentelemetry-exporter-metrics"

const exporter = new DynatraceMetricExporter(options);

....

Additional context
An workaround would be to access the library files and import the file directly from DynatraceMetricExporter.js

import { DynatraceMetricExporter } from "@dynatrace/opentelemetry-exporter-metrics/build/src/DynatraceMetricExporter"

....

This works but is not clean solution, would be better if the library exported it on the index.ts file.

Wrong processing of metrics when it's a string representation of a number

Hello,

We're using the latest version of your exported and we've encountered a weird behavior when we observe values with an ObservableGauge.
Basically, we sent by mistake a string representation of an integer to the observe method and found out that we can't see it in Dynatrace.
There was no indication that the value is in a wrong format and did not see any error, that's why it took us time to figure this out.
After debugging the code, it seems that the export method logic doesn't process strings correctly, and it just parsed the value to '0 0', which caused dynatrace to probably ignore this value.

Example of the code we used:

const meter = MetricsAPI.getInstance().getMeter(name);
meter.createObservableGauge(name, options, (observerResult) => {
   const count = getCount() // returns "3" and not 3
   observerResult.observe(count, {/*label*/});
})

This observation was converted to this body: <metric-name>,<dimensions> gauge,0 0 instead of <metric-name>,<dimensions> gauge,3

I understand that the observe method should accept only integers, but it could be helpful if you could possibly:

  1. Show an error/warning if the value is in a wrong format
  2. Be able to parse string representations of integers correctly

Thanks a lot!

deprecated @opentelemetry/[email protected]

Hi,

We're getting these warnings from the library:

npm WARN deprecated @opentelemetry/[email protected]: Package renamed to @opentelemetry/sdk-metrics-base
npm WARN @dynatrace/[email protected] requires a peer of @opentelemetry/[email protected] but none is installed. You must install peer dependencies yourself.

The opentelemetry/metrics is no longer exists and they changed the name
Further, can you change the peer dependencies to a higher version of opentelemetry/api ?

OneAgent Metrics API

Hi,
will this instrumentation work without a token & localhost url if the OneAgent runs in pass mode? A OneAgent runs in pass mode for example on cf environments.

Are there plans to auto inject if a metric is exposed using the opentelemetry sdk and a OneAgent is in place?

Best regards
Patrik

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.