@opencensus/exporter-prometheus
Advanced tools
Comparing version 0.0.8 to 0.0.9
@@ -16,3 +16,3 @@ /** | ||
*/ | ||
import { ExporterConfig, Measurement, StatsEventListener, View } from '@opencensus/core'; | ||
import { ExporterConfig, Measurement, StatsEventListener, TagKey, TagValue, View } from '@opencensus/core'; | ||
export interface PrometheusExporterOptions extends ExporterConfig { | ||
@@ -56,12 +56,19 @@ /** App prefix for metrics, if needed - default opencensus */ | ||
* Method called every new stats' record | ||
* @param views | ||
* @param measurement | ||
* @param views The views related to the measurement | ||
* @param measurement The recorded measurement | ||
* @param tags The tags to which the value is applied | ||
*/ | ||
onRecord(views: View[], measurement: Measurement): void; | ||
onRecord(views: View[], measurement: Measurement, tags: Map<TagKey, TagValue>): void; | ||
/** | ||
* Starts the Prometheus exporter that polls Metric from Metrics library and | ||
* send batched data to backend. | ||
*/ | ||
start(): void; | ||
private getLabelValues; | ||
/** | ||
* Register or get a metric in Prometheus | ||
* @param view View will be used to register the metric | ||
* @param tags Optional, used only for histogram metric | ||
* @param labels Object with label keys and values | ||
*/ | ||
private registerMetric(view, tags?); | ||
private registerMetric; | ||
/** | ||
@@ -72,3 +79,3 @@ * Update the metric from a measurement value | ||
*/ | ||
private updateMetric(view, measurement); | ||
private updateMetric; | ||
/** | ||
@@ -78,3 +85,3 @@ * Build a metric name from view name | ||
*/ | ||
private getPrometheusMetricName(view); | ||
private getPrometheusMetricName; | ||
/** | ||
@@ -84,7 +91,7 @@ * Sanitize metric name | ||
*/ | ||
private sanitizePrometheusMetricName(name); | ||
private sanitizePrometheusMetricName; | ||
/** | ||
* Throws an error labels contain "le" label name in histogram label names. | ||
*/ | ||
private validateDisallowedLeLabelForHistogram(labels); | ||
private validateDisallowedLeLabelForHistogram; | ||
/** | ||
@@ -95,3 +102,3 @@ * Get the boundaries from buckets | ||
*/ | ||
private getBoundaries(view, tags); | ||
private getBoundaries; | ||
/** | ||
@@ -98,0 +105,0 @@ * Start the Prometheus exporter server |
@@ -44,14 +44,32 @@ "use strict"; | ||
* Method called every new stats' record | ||
* @param views | ||
* @param measurement | ||
* @param views The views related to the measurement | ||
* @param measurement The recorded measurement | ||
* @param tags The tags to which the value is applied | ||
*/ | ||
onRecord(views, measurement) { | ||
onRecord(views, measurement, tags) { | ||
for (const view of views) { | ||
this.updateMetric(view, measurement); | ||
this.updateMetric(view, measurement, tags); | ||
} | ||
} | ||
/** | ||
* Starts the Prometheus exporter that polls Metric from Metrics library and | ||
* send batched data to backend. | ||
*/ | ||
start() { | ||
// TODO(mayurkale): add setInterval here to poll Metric, transform and send | ||
// // it to backend (dependency with PR#253). | ||
} | ||
getLabelValues(columns, tags) { | ||
const labels = {}; | ||
columns.forEach((tagKey) => { | ||
if (tags.has(tagKey)) { | ||
labels[tagKey.name] = tags.get(tagKey).value; | ||
} | ||
}); | ||
return labels; | ||
} | ||
/** | ||
* Register or get a metric in Prometheus | ||
* @param view View will be used to register the metric | ||
* @param tags Optional, used only for histogram metric | ||
* @param labels Object with label keys and values | ||
*/ | ||
@@ -66,9 +84,5 @@ registerMetric(view, tags) { | ||
} | ||
const labels = view.getColumns(); | ||
const labelNames = view.getColumns().map((tagKey) => tagKey.name); | ||
// Create a new metric if there is no one | ||
const metricObj = { | ||
name: metricName, | ||
help: view.description, | ||
labelNames: labels | ||
}; | ||
const metricObj = { name: metricName, help: view.description, labelNames }; | ||
// Creating the metric based on aggregation type | ||
@@ -84,7 +98,7 @@ switch (view.aggregation) { | ||
case core_1.AggregationType.DISTRIBUTION: | ||
this.validateDisallowedLeLabelForHistogram(labels); | ||
this.validateDisallowedLeLabelForHistogram(labelNames); | ||
const distribution = { | ||
name: metricName, | ||
help: view.description, | ||
labelNames: labels, | ||
labelNames, | ||
buckets: this.getBoundaries(view, tags) | ||
@@ -106,17 +120,18 @@ }; | ||
*/ | ||
updateMetric(view, measurement) { | ||
const metric = this.registerMetric(view, measurement.tags); | ||
updateMetric(view, measurement, tags) { | ||
const metric = this.registerMetric(view, tags); | ||
// Updating the metric based on metric instance type and aggregation type | ||
const labelValues = this.getLabelValues(view.getColumns(), tags); | ||
if (metric instanceof prom_client_1.Counter) { | ||
metric.inc(measurement.tags); | ||
metric.inc(labelValues); | ||
} | ||
else if (view.aggregation === core_1.AggregationType.SUM && metric instanceof prom_client_1.Gauge) { | ||
metric.inc(measurement.tags, measurement.value); | ||
metric.inc(labelValues, measurement.value); | ||
} | ||
else if (view.aggregation === core_1.AggregationType.LAST_VALUE && | ||
metric instanceof prom_client_1.Gauge) { | ||
metric.set(measurement.tags, measurement.value); | ||
metric.set(labelValues, measurement.value); | ||
} | ||
else if (metric instanceof prom_client_1.Histogram) { | ||
metric.observe(measurement.tags, measurement.value); | ||
metric.observe(labelValues, measurement.value); | ||
} | ||
@@ -166,3 +181,4 @@ else { | ||
getBoundaries(view, tags) { | ||
const data = view.getSnapshot(tags); | ||
const tagValues = view.getColumns().map((tagKey) => (tags.get(tagKey) || null)); | ||
const data = view.getSnapshot(tagValues); | ||
return data.buckets; | ||
@@ -169,0 +185,0 @@ } |
{ | ||
"name": "@opencensus/exporter-prometheus", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "OpenCensus Exporter Prometheus allows user to send collected stats to Prometheus", | ||
@@ -9,3 +9,4 @@ "main": "build/src/index.js", | ||
"scripts": { | ||
"test": "nyc -x '**/test/**' --reporter=html --reporter=text mocha 'build/test/**/*.js'", | ||
"test": "nyc mocha build/test/**/*.js", | ||
"codecov": "nyc report --reporter=json && codecov -f coverage/*.json", | ||
"clean": "rimraf build/*", | ||
@@ -48,12 +49,13 @@ "check": "gts check", | ||
"axios": "^0.18.0", | ||
"codecov": "^3.1.0", | ||
"gts": "^0.9.0", | ||
"mocha": "^5.0.4", | ||
"ncp": "^2.0.0", | ||
"nock": "^9.2.6", | ||
"nyc": "^11.8.0", | ||
"nock": "^10.0.0", | ||
"nyc": "^13.0.0", | ||
"ts-node": "^7.0.1", | ||
"typescript": "~2.7.2" | ||
"typescript": "~3.2.0" | ||
}, | ||
"dependencies": { | ||
"@opencensus/core": "^0.0.8", | ||
"@opencensus/core": "^0.0.9", | ||
"express": "^4.16.3", | ||
@@ -60,0 +62,0 @@ "prom-client": "^11.1.1" |
# OpenCensus Prometheus Exporter for Node.js | ||
[![Gitter chat][gitter-image]][gitter-url] ![Node Version][node-img] [![NPM Published Version][npm-img]][npm-url] ![dependencies Status][dependencies-status] ![devDependencies Status][devdependencies-status] ![Apache License][license-image] | ||
The OpenCensus Prometheus Exporter allows the user to send collected stats with [OpenCensus Node.js](https://github.com/census-instrumentation/opencensus-node) to Prometheus. | ||
The OpenCensus Prometheus Exporter allows the user to send collected stats with [OpenCensus Core](https://github.com/census-instrumentation/opencensus-core) to Prometheus. | ||
@@ -18,7 +18,7 @@ This package is still at an early stage of development, and is subject to change. | ||
Instance the exporter on your application. | ||
Create & register the exporter on your application. | ||
For javascript: | ||
For Javascript: | ||
```javascript | ||
const { Stats } = require('@opencensus/core'); | ||
const { globalStats } = require('@opencensus/core'); | ||
const { PrometheusStatsExporter } = require('@opencensus/exporter-prometheus'); | ||
@@ -29,3 +29,3 @@ | ||
port: 9464, | ||
startServer: false | ||
startServer: true | ||
}); | ||
@@ -37,28 +37,17 @@ ``` | ||
```javascript | ||
// Our Stats manager | ||
const stats = new Stats(); | ||
// Pass the created exporter to Stats | ||
stats.registerExporter(exporter); | ||
// Run the server | ||
exporter.startServer(function callback() { | ||
// Callback | ||
}); | ||
globalStats.registerExporter(exporter); | ||
``` | ||
Similarly for Typescript (Since the source is written in TypeScript): | ||
Similarly for TypeScript (Since the source is written in TypeScript): | ||
```typescript | ||
import { PrometheusStatsExporter } from '@opencensus/exporter-prometheus'; | ||
import { Stats } from '@opencensus/core'; | ||
import { globalStats } from '@opencensus/core'; | ||
// Add your port and startServer to the Prometheus options | ||
const options = {port: 9464, startServer: false}; | ||
const options = {port: 9464, startServer: true}; | ||
const exporter = new PrometheusStatsExporter(options); | ||
// Our Stats manager | ||
const stats = new Stats(); | ||
// Pass the created exporter to Stats | ||
stats.registerExporter(exporter); | ||
globalStats.registerExporter(exporter); | ||
``` | ||
@@ -86,4 +75,4 @@ | ||
## LICENSE | ||
## LICENSE | ||
Apache License 2.0 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
29145
361
0
13
75
+ Added@opencensus/core@0.0.9(transitive)
- Removed@opencensus/core@0.0.8(transitive)
Updated@opencensus/core@^0.0.9