Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Host Metrics #227

Merged
merged 18 commits into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion examples/rca-metrics/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const { RCAMetrics } = require('@opentelemetry/rca-metrics');
const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus');
const { MeterProvider } = require('@opentelemetry/metrics');

const exporter = new PrometheusExporter(
{
Expand All @@ -12,8 +13,10 @@ const exporter = new PrometheusExporter(
},
);

const rcaMetrics = new RCAMetrics({
const meterProvider = new MeterProvider({
exporter,
interval: 2000,
});

const rcaMetrics = new RCAMetrics({ meterProvider, name: 'example-rca-metrics' });
rcaMetrics.start();
13 changes: 10 additions & 3 deletions packages/opentelemetry-rca-metrics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
[![devDependencies][devDependencies-image]][devDependencies-url]
[![Apache License][license-image]][license-url]

This module provides automatic collection of RCA Metrics
This module provides automatic collection of RCA Metrics which includes basic metrics for:
obecny marked this conversation as resolved.
Show resolved Hide resolved
* CPU
* Memory
* Network

## Installation

Expand All @@ -16,6 +19,7 @@ npm install --save @opentelemetry/rca-metrics
## Usage

```javascript
const { MeterProvider } = require('@opentelemetry/metrics');
const { RCAMetrics } = require('@opentelemetry/rca-metrics');
const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus');

Expand All @@ -25,11 +29,14 @@ const exporter = new PrometheusExporter(
}
);

const rcaMetrics = new RCAMetrics({
const meterProvider = new MeterProvider({
exporter,
interval: 5000, // default 60000 (60s)
interval: 2000,
});

const rcaMetrics = new RCAMetrics({ meterProvider, name: 'example-rca-metrics' });
rcaMetrics.start();

```

## Useful links
Expand Down
26 changes: 11 additions & 15 deletions packages/opentelemetry-rca-metrics/src/metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,27 @@ import { getCpuUsageData, getMemoryData } from './stats/common';
import { getNetworkData } from './stats/si';

import * as types from './types';
import { VERSION } from './version';

/**
* Metrics Collector Configuration
*/
export interface MetricsCollectorConfig {
logger?: api.Logger;
exporter: metrics.MetricExporter;
// maximum timeout to wait for stats collection default is 500ms
maxTimeoutUpdateMS?: number;
// Meter Provider
meterProvider: metrics.MeterProvider;
// Character to be used to join metrics - default is "."
metricNameSeparator?: string;
// Name of component
name: string;
// metric export endpoint
url: string;
// How often the metrics should be exported
interval?: number;
}

const DEFAULT_INTERVAL = 60 * 1000;
const DEFAULT_MAX_TIMEOUT_UPDATE_MS = 500;
const DEFAULT_NAME = 'opentelemetry-metrics-collector';
const DEFAULT_NAME = 'opentelemetry-rca-metrics';
const DEFAULT_METRIC_NAME_SEPARATOR = '.';

// default label name to be used to store metric name
Expand All @@ -55,8 +54,6 @@ const DEFAULT_KEY = 'name';
* the default label name for metric name is "name"
*/
export class RCAMetrics {
private _intervalExport: number | undefined;
private _exporter: metrics.MetricExporter;
protected _logger: api.Logger | undefined;
protected _maxTimeoutUpdateMS: number;
protected _meter: metrics.Meter;
Expand All @@ -67,19 +64,18 @@ export class RCAMetrics {
private _memValueObserver: types.ValueObserverWithObservations | undefined;

constructor(config: MetricsCollectorConfig) {
this._intervalExport =
typeof config.interval === 'number' ? config.interval : DEFAULT_INTERVAL;
this._exporter = config.exporter;
this._logger = config.logger;
this._logger = config.logger || new api.NoopLogger();
this._name = config.name || DEFAULT_NAME;
this._maxTimeoutUpdateMS =
config.maxTimeoutUpdateMS || DEFAULT_MAX_TIMEOUT_UPDATE_MS;
this._metricNameSeparator =
config.metricNameSeparator || DEFAULT_METRIC_NAME_SEPARATOR;
this._meter = new metrics.MeterProvider({
interval: this._intervalExport,
exporter: this._exporter,
}).getMeter(this._name);
const meterProvider =
config.meterProvider || api.metrics.getMeterProvider();
if (!config.meterProvider) {
this._logger.warn('No meter provider, using default');
}
this._meter = meterProvider.getMeter(this._name, VERSION);
}

/**
Expand Down
18 changes: 15 additions & 3 deletions packages/opentelemetry-rca-metrics/test/metric.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const SI = require('systeminformation');
import { ExportResult } from '@opentelemetry/core';
import {
Histogram,
MeterProvider,
MetricExporter,
MetricRecord,
} from '@opentelemetry/metrics';
Expand Down Expand Up @@ -97,15 +98,18 @@ describe('RCA Metrics', () => {
exporter = new NoopExporter();
exportSpy = sandbox.stub(exporter, 'export');

const meterProvider = new MeterProvider({
interval: INTERVAL,
exporter,
});

// it seems like this is the only way to be able to mock
// `node-gyp-build` before metrics are being loaded, if import them before
// the first pass on unit tests will not mock correctly
metrics = require('../src');
rcaMetrics = new metrics.RCAMetrics({
exporter,
interval: INTERVAL,
meterProvider,
name: 'opentelemetry-rca-metrics',
url: '',
});
rcaMetrics.start();

Expand Down Expand Up @@ -139,6 +143,14 @@ describe('RCA Metrics', () => {
assert.ok(rcaMetrics instanceof metrics.RCAMetrics);
});

it('should create a new instance with default meter provider', () => {
rcaMetrics = new metrics.RCAMetrics({
name: 'opentelemetry-rca-metrics',
});
rcaMetrics.start();
assert.ok(rcaMetrics instanceof metrics.RCAMetrics);
});

it('should export CPU metrics', () => {
const records = getRecords(exportSpy.args[0][0], 'cpu');
assert.strictEqual(records.length, 4);
Expand Down