Skip to content

Commit

Permalink
feat(otlp-exporter): cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
pichlermarc committed Mar 30, 2022
1 parent 9066ce1 commit 1f9a214
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,6 @@ const testOTLPMetricExporter = (params: TestParams) =>
metadata: params.metadata,
aggregationTemporality: AggregationTemporality.CUMULATIVE
});
// Overwrites the start time to make tests consistent
Object.defineProperty(collectorExporter, '_startTime', {
value: 1592602232694000000,
});

setUp();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,25 @@ export class TestMetricReader extends MetricReader {
}
}

const defaultResource = Resource.default().merge(new Resource({
const testResource = Resource.default().merge(new Resource({
service: 'ui',
version: 1,
cost: 112.12,
}));

let meterProvider = new MeterProvider({ resource: defaultResource });
let meterProvider = new MeterProvider({ resource: testResource });

let reader = new TestMetricReader();
meterProvider.addMetricReader(
reader
);
meterProvider.addMetricReader(reader);

let meter = meterProvider.getMeter('default', '0.0.1');

export async function collect(){
export async function collect() {
return (await reader.collect())!;
}

export function setUp() {
meterProvider = new MeterProvider({ resource: defaultResource });
meterProvider = new MeterProvider({ resource: testResource });
reader = new TestMetricReader();
meterProvider.addMetricReader(
reader
Expand All @@ -62,7 +62,6 @@ export async function shutdown() {
await meterProvider.shutdown();
}


export function mockCounter(): Counter {
const name = 'int-counter';
return meter.createCounter(name, {
Expand All @@ -87,7 +86,7 @@ export function mockObservableGauge(

export function mockHistogram(): Histogram {
const name = 'int-histogram';
meterProvider.addView({aggregation: new HistogramAggregation([0,100])});
meterProvider.addView({ aggregation: new HistogramAggregation([0, 100]) });

return meter.createHistogram(name, {
description: 'sample histogram description',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
import { Attributes, ValueType } from '@opentelemetry/api-metrics';

/**
* Converts labels
* Converts {@link Attributes} to a collector-compatible format.
* @param attributes
*/
export function toCollectorAttributes(
Expand All @@ -40,7 +40,7 @@ export function toCollectorAttributes(
}

/**
* Given a {@link AggregationTemporality}, return its temporality in a compatible format with the collector
* Convert {@link AggregationTemporality} to a collector-compatible format.
* @param aggregationTemporality
*/
export function toAggregationTemporality(
Expand All @@ -60,44 +60,44 @@ export function toAggregationTemporality(
}

/**
* Returns an array of DataPoints which can have integers or double values
* @param metric
* Convert {@link MetricData} of {@link DataPointType.SINGULAR} to a collector-compatible format.
* @param metricData
*/
export function toDataPoints(
metric: MetricData
export function toSingularDataPoints(
metricData: MetricData
): otlpTypes.opentelemetryProto.metrics.v1.DataPoint[] {
return Array.from(metric.dataPoints.map(pointData => {
return Array.from(metricData.dataPoints.map(dataPoint => {
return {
labels: toCollectorAttributes(pointData.attributes),
value: pointData.value as number,
labels: toCollectorAttributes(dataPoint.attributes),
value: dataPoint.value as number,
startTimeUnixNano: core.hrTimeToNanoseconds(
pointData.startTime
dataPoint.startTime
),
timeUnixNano: core.hrTimeToNanoseconds(
pointData.endTime
dataPoint.endTime
),
};
}));
}

/**
* Returns an array of HistogramPoints to the collector
* @param metric
* Convert {@link MetricData} of {@link DataPointType.HISTOGRAM} to a collector-compatible format.
* @param metricData
*/
export function toHistogramPoints(
metric: MetricData
export function toHistogramDataPoints(
metricData: MetricData
): otlpTypes.opentelemetryProto.metrics.v1.HistogramDataPoint[] {
return Array.from(metric.dataPoints.map(pointData => {
const histogram = pointData.value as Histogram;
return Array.from(metricData.dataPoints.map(dataPoints => {
const histogram = dataPoints.value as Histogram;
return {
labels: toCollectorAttributes(pointData.attributes),
labels: toCollectorAttributes(dataPoints.attributes),
sum: histogram.sum,
count: histogram.count,
startTimeUnixNano: core.hrTimeToNanoseconds(
pointData.startTime
dataPoints.startTime
),
timeUnixNano: core.hrTimeToNanoseconds(
pointData.endTime
dataPoints.endTime
),
bucketCounts: histogram.buckets.counts,
explicitBounds: histogram.buckets.boundaries,
Expand All @@ -106,54 +106,54 @@ export function toHistogramPoints(
}

/**
* Converts a metric to be compatible with the collector
* @param metric
* Converts {@link MetricData} to a collector-compatible format.
* @param metricData
* @param aggregationTemporality
*/
export function toCollectorMetric(
metric: MetricData,
metricData: MetricData,
aggregationTemporality: AggregationTemporality
): otlpTypes.opentelemetryProto.metrics.v1.Metric {
const metricCollector: otlpTypes.opentelemetryProto.metrics.v1.Metric = {
name: metric.descriptor.name,
description: metric.descriptor.description,
unit: metric.descriptor.unit,
name: metricData.descriptor.name,
description: metricData.descriptor.description,
unit: metricData.descriptor.unit,
};

if (metric.dataPointType === DataPointType.SINGULAR) {
if (metricData.dataPointType === DataPointType.SINGULAR) {
const result = {
dataPoints: toDataPoints(metric),
dataPoints: toSingularDataPoints(metricData),
isMonotonic:
metric.descriptor.type === InstrumentType.COUNTER ||
metric.descriptor.type === InstrumentType.OBSERVABLE_COUNTER,
metricData.descriptor.type === InstrumentType.COUNTER ||
metricData.descriptor.type === InstrumentType.OBSERVABLE_COUNTER,
aggregationTemporality: toAggregationTemporality(aggregationTemporality),
};

if (
metric.descriptor.type === InstrumentType.COUNTER ||
metric.descriptor.type === InstrumentType.OBSERVABLE_COUNTER ||
metric.descriptor.type === InstrumentType.UP_DOWN_COUNTER ||
metric.descriptor.type === InstrumentType.OBSERVABLE_UP_DOWN_COUNTER
metricData.descriptor.type === InstrumentType.COUNTER ||
metricData.descriptor.type === InstrumentType.OBSERVABLE_COUNTER ||
metricData.descriptor.type === InstrumentType.UP_DOWN_COUNTER ||
metricData.descriptor.type === InstrumentType.OBSERVABLE_UP_DOWN_COUNTER
) {
if (metric.descriptor.valueType === ValueType.INT) {
if (metricData.descriptor.valueType === ValueType.INT) {
metricCollector.intSum = result;
} else {
metricCollector.doubleSum = result;
}
} else{
// Instrument is a gauge.
if (metric.descriptor.valueType === ValueType.INT) {
if (metricData.descriptor.valueType === ValueType.INT) {
metricCollector.intGauge = result;
} else {
metricCollector.doubleGauge = result;
}
}
} else if (metric.dataPointType === DataPointType.HISTOGRAM) {
} else if (metricData.dataPointType === DataPointType.HISTOGRAM) {
const result = {
dataPoints: toHistogramPoints(metric),
dataPoints: toHistogramDataPoints(metricData),
aggregationTemporality: toAggregationTemporality(aggregationTemporality)
};
if (metric.descriptor.valueType === ValueType.INT) {
if (metricData.descriptor.valueType === ValueType.INT) {
metricCollector.intHistogram = result;
} else {
metricCollector.doubleHistogram = result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ import { Resource } from '@opentelemetry/resources';
import * as assert from 'assert';
import { otlpTypes } from '@opentelemetry/exporter-trace-otlp-http';

if (typeof Buffer === 'undefined') {
(window as any).Buffer = {
from: function (arr: []) {
return new Uint8Array(arr);
},
};
}

export class TestMetricReader extends MetricReader {
protected onForceFlush(): Promise<void> {
return Promise.resolve(undefined);
Expand All @@ -44,12 +52,8 @@ meterProvider.addMetricReader(
);
let meter = meterProvider.getMeter('default', '0.0.1');

if (typeof Buffer === 'undefined') {
(window as any).Buffer = {
from: function (arr: []) {
return new Uint8Array(arr);
},
};
export async function collect() {
return (await reader.collect())!;
}

export function setUp() {
Expand All @@ -65,10 +69,6 @@ export async function shutdown() {
await meterProvider.shutdown();
}

export async function collect() {
return (await reader.collect())!;
}

export function mockCounter(): Counter {
const name = 'int-counter';
return meter.createCounter(name, {
Expand All @@ -77,14 +77,6 @@ export function mockCounter(): Counter {
});
}

export function mockDoubleCounter(): Counter {
const name = 'double-counter';
return meter.createCounter(name, {
description: 'sample counter description',
valueType: ValueType.DOUBLE,
});
}

export function mockObservableGauge(
callback: (observableResult: ObservableResult) => void,
name = 'double-observable-gauge'
Expand All @@ -99,6 +91,16 @@ export function mockObservableGauge(
);
}

export function mockDoubleCounter(): Counter {
const name = 'double-counter';
return meter.createCounter(name, {
description: 'sample counter description',
valueType: ValueType.DOUBLE,
});
}



export function mockObservableCounter(
callback: (observableResult: ObservableResult) => void,
name = 'double-observable-counter'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,25 @@ export class TestMetricReader extends MetricReader {
}
}

const defaultResource = new Resource({
const testResource = Resource.default().merge(new Resource({
service: 'ui',
version: 1,
cost: 112.12,
});
}));

let meterProvider = new MeterProvider({ resource: testResource });

let meterProvider = new MeterProvider({ resource: defaultResource });
let reader = new TestMetricReader();
meterProvider.addMetricReader(
reader
);
meterProvider.addMetricReader(reader);

let meter = meterProvider.getMeter('default', '0.0.1');

export async function collect(){
export async function collect() {
return (await reader.collect())!;
}


export function setUp() {
meterProvider = new MeterProvider({ resource: defaultResource });
meterProvider = new MeterProvider({ resource: testResource });
reader = new TestMetricReader();
meterProvider.addMetricReader(
reader
Expand Down Expand Up @@ -91,7 +90,7 @@ export function mockObservableGauge(

export function mockHistogram(): Histogram {
const name = 'int-histogram';
meterProvider.addView({aggregation: new HistogramAggregation([0,100])});
meterProvider.addView({ aggregation: new HistogramAggregation([0, 100]) });

return meter.createHistogram(name, {
description: 'sample histogram description',
Expand Down

0 comments on commit 1f9a214

Please sign in to comment.