Skip to content

Commit

Permalink
Merge branch 'main' into tsconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas authored Feb 23, 2022
2 parents b6ecbd6 + 144e11a commit df319bc
Show file tree
Hide file tree
Showing 41 changed files with 852 additions and 155 deletions.
3 changes: 3 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ npm run clean

These commands can also be run for specific packages instead of the whole project, which can speed up compilations while developing.

**NOTE**: To run commands in specific packages (compile, lint, etc), please ensure you are using at least `7.x`
version of `npm`.

```sh
# Build a single module and all of its dependencies
cd packages/opentelemetry-module-name
Expand Down
17 changes: 7 additions & 10 deletions examples/prometheus/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
const { MeterProvider } = require('@opentelemetry/sdk-metrics-base');
const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus');

const exporter = new PrometheusExporter(
{
startServer: true,
},
() => {
console.log(
`prometheus scrape endpoint: http://localhost:${PrometheusExporter.DEFAULT_OPTIONS.port}${PrometheusExporter.DEFAULT_OPTIONS.endpoint}`,
);
},
);
const { endpoint, port } = PrometheusExporter.DEFAULT_OPTIONS;

const exporter = new PrometheusExporter({}, () => {
console.log(
`prometheus scrape endpoint: http://localhost:${port}${endpoint}`,
);
});

const meter = new MeterProvider({
exporter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"@types/webpack-env": "1.16.3",
"codecov": "3.8.3",
"istanbul-instrumenter-loader": "3.0.1",
"karma": "6.3.8",
"karma": "6.3.14",
"karma-chrome-launcher": "3.1.0",
"karma-coverage-istanbul-reporter": "3.0.3",
"karma-mocha": "2.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"codecov": "3.8.3",
"cpx": "1.5.0",
"istanbul-instrumenter-loader": "3.0.1",
"karma": "6.3.8",
"karma": "6.3.14",
"karma-chrome-launcher": "3.1.0",
"karma-coverage-istanbul-reporter": "3.0.3",
"karma-mocha": "2.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"babel-loader": "8.2.3",
"codecov": "3.8.3",
"istanbul-instrumenter-loader": "3.0.1",
"karma": "6.3.8",
"karma": "6.3.14",
"karma-chrome-launcher": "3.1.0",
"karma-coverage-istanbul-reporter": "3.0.3",
"karma-mocha": "2.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"babel-loader": "8.2.3",
"codecov": "3.8.3",
"istanbul-instrumenter-loader": "3.0.1",
"karma": "6.3.8",
"karma": "6.3.14",
"karma-chrome-launcher": "3.1.0",
"karma-coverage-istanbul-reporter": "3.0.3",
"karma-mocha": "2.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"codecov": "3.8.3",
"cpx": "1.5.0",
"istanbul-instrumenter-loader": "3.0.1",
"karma": "6.3.8",
"karma": "6.3.14",
"karma-chrome-launcher": "3.1.0",
"karma-coverage-istanbul-reporter": "3.0.3",
"karma-mocha": "2.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus');
// Optional and only needed to see the internal diagnostic logging (during development)
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);

const exporter = new PrometheusExporter(
{
startServer: true,
},
() => {
console.log(
`prometheus scrape endpoint: http://localhost:${PrometheusExporter.DEFAULT_OPTIONS.port}${PrometheusExporter.DEFAULT_OPTIONS.endpoint}`,
);
},
);
const { endpoint, port } = PrometheusExporter.DEFAULT_OPTIONS;

const exporter = new PrometheusExporter({}, () => {
console.log(
`prometheus scrape endpoint: http://localhost:${port}${endpoint}`,
);
});

const meter = new MeterProvider({
exporter,
Expand All @@ -34,12 +31,12 @@ meter.createObservableGauge('cpu_core_usage', {

function getAsyncValue() {
return new Promise((resolve) => {
setTimeout(()=> {
setTimeout(() => {
resolve(Math.random());
}, 100);
});
}

setInterval(function(){
setInterval(function () {
console.log("simulating an app being kept open")
}, 5000);
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"@types/node": "14.17.33",
"@types/sinon": "10.0.6",
"codecov": "3.8.3",
"karma": "6.3.8",
"karma": "6.3.14",
"karma-chrome-launcher": "3.1.0",
"karma-coverage-istanbul-reporter": "3.0.3",
"karma-mocha": "2.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ import { InstrumentDescriptor } from './InstrumentDescriptor';
import { WritableMetricStorage } from './state/WritableMetricStorage';

export class SyncInstrument {
constructor(private _writableMetricStorage: WritableMetricStorage, private _descriptor: InstrumentDescriptor) { }
constructor(private _writableMetricStorage: WritableMetricStorage, private _descriptor: InstrumentDescriptor) {}

getName(): string {
return this._descriptor.name;
}

aggregate(value: number, attributes: metrics.Attributes = {}, context: api.Context = api.context.active()) {
protected _record(value: number, attributes: metrics.Attributes = {}, context: api.Context = api.context.active()) {
if (this._descriptor.valueType === metrics.ValueType.INT && !Number.isInteger(value)) {
api.diag.warn(
`INT value type cannot accept a floating-point value for ${this._descriptor.name}, ignoring the fractional digits.`
);
value = Math.trunc(value);
}
this._writableMetricStorage.record(value, attributes, context);
}
}
Expand All @@ -39,7 +45,7 @@ export class UpDownCounter extends SyncInstrument implements metrics.UpDownCount
* Increment value of counter by the input. Inputs may be negative.
*/
add(value: number, attributes?: metrics.Attributes, ctx?: api.Context): void {
this.aggregate(value, attributes, ctx);
this._record(value, attributes, ctx);
}
}

Expand All @@ -56,7 +62,7 @@ export class Counter extends SyncInstrument implements metrics.Counter {
return;
}

this.aggregate(value, attributes, ctx);
this._record(value, attributes, ctx);
}
}

Expand All @@ -68,6 +74,6 @@ export class Histogram extends SyncInstrument implements metrics.Histogram {
* Records a measurement. Value of the measurement must not be negative.
*/
record(value: number, attributes?: metrics.Attributes, ctx?: api.Context): void {
this.aggregate(value, attributes, ctx);
this._record(value, attributes, ctx);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,34 @@

import { AggregationTemporality } from './AggregationTemporality';
import { MetricData } from './MetricData';
import {
ExportResult,
ExportResultCode,
} from '@opentelemetry/core';


// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#metricexporter

// TODO should this just be an interface and exporters can implement their own shutdown?
export abstract class MetricExporter {
protected _shutdown = false;
export interface PushMetricExporter {

abstract export(batch: MetricData[]): Promise<void>;
export(batch: MetricData[], resultCallback: (result: ExportResult) => void): void;

abstract forceFlush(): Promise<void>;
forceFlush(): Promise<void>;

abstract getPreferredAggregationTemporality(): AggregationTemporality;
getPreferredAggregationTemporality(): AggregationTemporality;

async shutdown(): Promise<void> {
if (this._shutdown) {
return;
}
shutdown(): Promise<void>;

// Setting _shutdown before flushing might prevent some exporters from flushing
// Waiting until flushing is complete might allow another flush to occur during shutdown
const flushPromise = this.forceFlush();
this._shutdown = true;
await flushPromise;
}

isShutdown() {
return this._shutdown;
}
}

export class ConsoleMetricExporter extends MetricExporter {
async export(_batch: MetricData[]) {
throw new Error('Method not implemented');
export class ConsoleMetricExporter implements PushMetricExporter {
protected _shutdown = true;

export(_batch: MetricData[], resultCallback: (result: ExportResult) => void) {
return resultCallback({
code: ExportResultCode.FAILED,
error: new Error('Method not implemented')
});
}

getPreferredAggregationTemporality() {
Expand All @@ -58,4 +52,8 @@ export class ConsoleMetricExporter extends MetricExporter {

// nothing to do
async forceFlush() {}

async shutdown() {
this._shutdown = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
*/

import * as api from '@opentelemetry/api';
import { ExportResultCode, globalErrorHandler } from '@opentelemetry/core';
import { MetricReader } from './MetricReader';
import { MetricExporter } from './MetricExporter';
import { PushMetricExporter } from './MetricExporter';
import { callWithTimeout, TimeoutError } from '../utils';

export type PeriodicExportingMetricReaderOptions = {
exporter: MetricExporter
exporter: PushMetricExporter
exportIntervalMillis?: number,
exportTimeoutMillis?: number
};
Expand All @@ -32,7 +33,7 @@ export type PeriodicExportingMetricReaderOptions = {
export class PeriodicExportingMetricReader extends MetricReader {
private _interval?: ReturnType<typeof setInterval>;

private _exporter: MetricExporter;
private _exporter: PushMetricExporter;

private readonly _exportInterval: number;

Expand Down Expand Up @@ -62,7 +63,20 @@ export class PeriodicExportingMetricReader extends MetricReader {

private async _runOnce(): Promise<void> {
const metrics = await this.collect({});
await this._exporter.export(metrics);
return new Promise((resolve, reject) => {
this._exporter.export(metrics, result => {
if (result.code !== ExportResultCode.SUCCESS) {
reject(
result.error ??
new Error(
`PeriodicExportingMetricReader: metrics export failed (error ${result.error})`
)
);
} else {
resolve();
}
});
});
}

protected override onInitialized(): void {
Expand All @@ -76,7 +90,7 @@ export class PeriodicExportingMetricReader extends MetricReader {
return;
}

api.diag.error('Unexpected error during export: %s', err);
globalErrorHandler(err);
}
}, this._exportInterval);
}
Expand Down
Loading

0 comments on commit df319bc

Please sign in to comment.