Skip to content

Commit

Permalink
Histogram
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwitten committed Jul 1, 2020
1 parent fed2475 commit 3380b66
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 32 deletions.
95 changes: 70 additions & 25 deletions packages/opentelemetry-exporter-collector/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ import ValueType = opentelemetryProto.common.v1.ValueType;
import { ValueType as apiValueType } from '@opentelemetry/api';
import {
MetricRecord,
MetricDescriptor,
MetricKind,
LastValueAggregator,
HistogramAggregator,
MinMaxSumCountAggregator,
Histogram,
} from '@opentelemetry/metrics';

/**
Expand Down Expand Up @@ -330,25 +333,27 @@ function toCollectorInstrumentationLibrarySpans(
* @param descriptor
*/
export function toCollectorType(
descriptor: MetricDescriptor
metric: MetricRecord
): opentelemetryProto.metrics.v1.MetricDescriptorType {
const metricKind = metric.descriptor.metricKind;
const valueType = metric.descriptor.valueType;
if (
descriptor.metricKind === MetricKind.UP_DOWN_COUNTER ||
descriptor.metricKind === MetricKind.OBSERVER
metricKind === MetricKind.COUNTER ||
metricKind === MetricKind.SUM_OBSERVER
) {
if (descriptor.valueType === apiValueType.INT) {
return opentelemetryProto.metrics.v1.MetricDescriptorType.INT64;
}
return opentelemetryProto.metrics.v1.MetricDescriptorType.DOUBLE;
}
// Monotonic counter
else if (descriptor.metricKind === MetricKind.COUNTER) {
if (descriptor.valueType === apiValueType.INT) {
if (valueType === apiValueType.INT) {
return opentelemetryProto.metrics.v1.MetricDescriptorType.MONOTONIC_INT64;
}
return opentelemetryProto.metrics.v1.MetricDescriptorType.MONOTONIC_DOUBLE;
} else if (metric.aggregator instanceof HistogramAggregator) {
return opentelemetryProto.metrics.v1.MetricDescriptorType.HISTOGRAM;
} else if (metric.aggregator instanceof MinMaxSumCountAggregator) {
return opentelemetryProto.metrics.v1.MetricDescriptorType.SUMMARY;
} else if (valueType == apiValueType.INT) {
return opentelemetryProto.metrics.v1.MetricDescriptorType.INT64;
} else if (valueType === apiValueType.DOUBLE) {
return opentelemetryProto.metrics.v1.MetricDescriptorType.DOUBLE;
} else {
// @TODO: Implement other metric kinds
return opentelemetryProto.metrics.v1.MetricDescriptorType.INVALID_TYPE;
}
}
Expand All @@ -358,14 +363,29 @@ export function toCollectorType(
* @param descriptor
*/
export function toCollectorTemporality(
descriptor: MetricDescriptor
metric: MetricRecord
): opentelemetryProto.metrics.v1.MetricDescriptorTemporality {
if (descriptor.metricKind === MetricKind.COUNTER) {
const metricKind = metric.descriptor.metricKind;
const aggregator = metric.aggregator;
if (
metricKind === MetricKind.COUNTER ||
metricKind === MetricKind.SUM_OBSERVER
) {
return opentelemetryProto.metrics.v1.MetricDescriptorTemporality.CUMULATIVE;
} else if (descriptor.metricKind === MetricKind.OBSERVER) {
return opentelemetryProto.metrics.v1.MetricDescriptorTemporality
.INSTANTANEOUS;
} else if (descriptor.metricKind === MetricKind.VALUE_RECORDER) {
} else if (
metricKind === MetricKind.UP_DOWN_COUNTER ||
metricKind === MetricKind.UP_DOWN_SUM_OBSERVER
) {
return opentelemetryProto.metrics.v1.MetricDescriptorTemporality.DELTA;
} else if (
metricKind === MetricKind.VALUE_OBSERVER ||
metricKind === MetricKind.VALUE_RECORDER
) {
if (aggregator instanceof LastValueAggregator) {
return opentelemetryProto.metrics.v1.MetricDescriptorTemporality
.INSTANTANEOUS;
}
// Any other aggregator (MinMaxLastSumCount, Summary, Histogram) will be delta
return opentelemetryProto.metrics.v1.MetricDescriptorTemporality.DELTA;
}
return opentelemetryProto.metrics.v1.MetricDescriptorTemporality
Expand All @@ -384,8 +404,26 @@ export function toCollectorMetricDescriptor(
description: metric.descriptor.description,
unit: metric.descriptor.unit,
labels: toCollectorLabels(metric.labels),
type: toCollectorType(metric.descriptor),
temporality: toCollectorTemporality(metric.descriptor),
type: toCollectorType(metric),
temporality: toCollectorTemporality(metric),
};
}

export function toHistogramPoint(
metric: MetricRecord,
startTime: number
): opentelemetryProto.metrics.v1.HistogramDataPoint {
const histValue = metric.aggregator.toPoint().value as Histogram;
return {
labels: toCollectorLabels(metric.labels),
sum: histValue.sum,
count: histValue.count,
startTimeUnixNano: startTime,
timeUnixNano: 0,
buckets: histValue.buckets.counts.map(count => {
return { count };
}),
explicitBounds: histValue.buckets.boundaries,
};
}

Expand All @@ -400,6 +438,8 @@ export function toCollectorMetric(
): opentelemetryProto.metrics.v1.Metric {
let int64DataPoints: opentelemetryProto.metrics.v1.Int64DataPoint[] = [];
let doubleDataPoints: opentelemetryProto.metrics.v1.DoubleDataPoint[] = [];
let histogramDataPoints: opentelemetryProto.metrics.v1.HistogramDataPoint[] = [];
const descriptor = toCollectorMetricDescriptor(metric);

const points = {
labels: toCollectorLabels(metric.labels),
Expand All @@ -410,18 +450,23 @@ export function toCollectorMetric(
),
};

if (metric.descriptor.valueType == apiValueType.INT) {
if (
descriptor.type ===
opentelemetryProto.metrics.v1.MetricDescriptorType.HISTOGRAM
) {
histogramDataPoints = [toHistogramPoint(metric, startTime)];
} else if (metric.descriptor.valueType == apiValueType.INT) {
int64DataPoints = [points];
} else if (metric.descriptor.valueType === apiValueType.DOUBLE) {
doubleDataPoints = [points];
} // @TODO: Implement support for histogram/distribution points
} // TODO: Implement support for summary points

return {
metricDescriptor: toCollectorMetricDescriptor(metric),
doubleDataPoints,
int64DataPoints,
summaryDataPoints: [],
histogramDataPoints: [],
summaryDataPoints: [], // TODO: Implement support for summary points
histogramDataPoints,
};
}

Expand Down
1 change: 0 additions & 1 deletion packages/opentelemetry-exporter-collector/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ export namespace opentelemetryProto {
labels: opentelemetryProto.common.v1.StringKeyValue[];
startTimeUnixNano: number;
timeUnixNano: number;
value: number;
count: number;
sum: number;
buckets: opentelemetryProto.metrics.v1.HistogramDataPointBucket[];
Expand Down
10 changes: 5 additions & 5 deletions packages/opentelemetry-exporter-collector/test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import * as collectorTypes from '../src/types';
import {
MetricRecord,
MetricKind,
CounterSumAggregator,
ObserverAggregator,
SumAggregator,
LastValueAggregator,
} from '@opentelemetry/metrics';
import { InstrumentationLibrary } from '@opentelemetry/core';
import * as grpc from 'grpc';
Expand Down Expand Up @@ -87,7 +87,7 @@ export const mockCounter: MetricRecord = {
valueType: ValueType.INT,
},
labels: {},
aggregator: new CounterSumAggregator(),
aggregator: new SumAggregator(),
resource: new Resource({
service: 'ui',
version: 1,
Expand All @@ -101,11 +101,11 @@ export const mockObserver: MetricRecord = {
name: 'test-observer',
description: 'sample observer description',
unit: '2',
metricKind: MetricKind.OBSERVER,
metricKind: MetricKind.VALUE_OBSERVER,
valueType: ValueType.DOUBLE,
},
labels: {},
aggregator: new ObserverAggregator(),
aggregator: new LastValueAggregator(),
resource: new Resource({
service: 'ui',
version: 1,
Expand Down

0 comments on commit 3380b66

Please sign in to comment.