Skip to content

Commit

Permalink
Merge e293cdd into 1c3af6c
Browse files Browse the repository at this point in the history
  • Loading branch information
mwear committed Dec 22, 2022
2 parents 1c3af6c + e293cdd commit 46eecbb
Show file tree
Hide file tree
Showing 21 changed files with 3,065 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/

### :rocket: (Enhancement)

* feat(sdk-metrics): add exponential histogram implementation [#3498](https://github.com/open-telemetry/opentelemetry-js/pull/3498) @mwear
* feat(api): add `getActiveBaggage` API [#3385](https://github.com/open-telemetry/opentelemetry-js/pull/3385)
* feat(instrumentation-grpc): set net.peer.name and net.peer.port on client spans [#3430](https://github.com/open-telemetry/opentelemetry-js/pull/3430)

Expand Down
39 changes: 38 additions & 1 deletion experimental/packages/otlp-transformer/src/metrics/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
AggregationTemporality,
DataPoint,
DataPointType,
ExponentialHistogram,
Histogram,
MetricData,
ResourceMetrics,
Expand All @@ -27,6 +28,7 @@ import {
import { toAttributes } from '../common/internal';
import {
EAggregationTemporality,
IExponentialHistogramDataPoint,
IHistogramDataPoint,
IMetric,
INumberDataPoint,
Expand Down Expand Up @@ -90,13 +92,21 @@ export function toMetric(metricData: MetricData): IMetric {
aggregationTemporality,
dataPoints: toHistogramDataPoints(metricData),
};
} else if (metricData.dataPointType === DataPointType.EXPONENTIAL_HISTOGRAM) {
out.exponentialHistogram = {
aggregationTemporality,
dataPoints: toExponentialHistogramDataPoints(metricData),
};
}

return out;
}

function toSingularDataPoint(
dataPoint: DataPoint<number> | DataPoint<Histogram>,
dataPoint:
| DataPoint<number>
| DataPoint<Histogram>
| DataPoint<ExponentialHistogram>,
valueType: ValueType
) {
const out: INumberDataPoint = {
Expand Down Expand Up @@ -137,6 +147,33 @@ function toHistogramDataPoints(metricData: MetricData): IHistogramDataPoint[] {
});
}

function toExponentialHistogramDataPoints(
metricData: MetricData
): IExponentialHistogramDataPoint[] {
return metricData.dataPoints.map(dataPoint => {
const histogram = dataPoint.value as ExponentialHistogram;
return {
attributes: toAttributes(dataPoint.attributes),
count: histogram.count,
min: histogram.min,
max: histogram.max,
sum: histogram.sum,
positive: {
offset: histogram.positive.offset,
bucketCounts: histogram.positive.bucketCounts,
},
negative: {
offset: histogram.negative.offset,
bucketCounts: histogram.negative.bucketCounts,
},
scale: histogram.scale,
zeroCount: histogram.zeroCount,
startTimeUnixNano: hrTimeToNanoseconds(dataPoint.startTime),
timeUnixNano: hrTimeToNanoseconds(dataPoint.endTime),
};
});
}

function toAggregationTemporality(
temporality: AggregationTemporality
): EAggregationTemporality {
Expand Down
8 changes: 7 additions & 1 deletion experimental/packages/otlp-transformer/src/metrics/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export interface IExponentialHistogramDataPoint {
startTimeUnixNano?: number;

/** ExponentialHistogramDataPoint timeUnixNano */
timeUnixNano?: string;
timeUnixNano?: number;

/** ExponentialHistogramDataPoint count */
count?: number;
Expand All @@ -209,6 +209,12 @@ export interface IExponentialHistogramDataPoint {

/** ExponentialHistogramDataPoint exemplars */
exemplars?: IExemplar[];

/** ExponentialHistogramDataPoint min */
min?: number;

/** ExponentialHistogramDataPoint max */
max?: number;
}

/** Properties of a SummaryDataPoint. */
Expand Down
169 changes: 169 additions & 0 deletions experimental/packages/otlp-transformer/test/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,47 @@ describe('Metrics', () => {
};
}

function createExponentialHistogramMetrics(
count: number,
sum: number,
scale: number,
zeroCount: number,
positive: { offset: number; bucketCounts: number[] },
negative: { offset: number; bucketCounts: number[] },
aggregationTemporality: AggregationTemporality,
min?: number,
max?: number
): MetricData {
return {
descriptor: {
description: 'this is a description',
type: InstrumentType.HISTOGRAM,
name: 'xhist',
unit: '1',
valueType: ValueType.INT,
},
aggregationTemporality,
dataPointType: DataPointType.EXPONENTIAL_HISTOGRAM,
dataPoints: [
{
value: {
sum: sum,
count: count,
min: min,
max: max,
zeroCount: zeroCount,
scale: scale,
positive: positive,
negative: negative,
},
startTime: START_TIME,
endTime: END_TIME,
attributes: ATTRIBUTES,
},
],
};
}

function createResourceMetrics(metricData: MetricData[]): ResourceMetrics {
const resource = new Resource({
'resource-attribute': 'resource attribute value',
Expand Down Expand Up @@ -608,5 +649,133 @@ describe('Metrics', () => {
});
});
});

describe('serializes an exponential histogram metric record', () => {
it('with min/max', () => {
const exportRequest = createExportMetricsServiceRequest([
createResourceMetrics([
createExponentialHistogramMetrics(
3,
10,
1,
0,
{ offset: 0, bucketCounts: [1, 0, 0, 0, 1, 0, 1, 0] },
{ offset: 0, bucketCounts: [0] },
AggregationTemporality.CUMULATIVE,
1,
8
),
]),
]);

assert.ok(exportRequest);

assert.deepStrictEqual(exportRequest, {
resourceMetrics: [
{
resource: expectedResource,
schemaUrl: undefined,
scopeMetrics: [
{
scope: expectedScope,
schemaUrl: expectedSchemaUrl,
metrics: [
{
name: 'xhist',
description: 'this is a description',
unit: '1',
exponentialHistogram: {
aggregationTemporality:
EAggregationTemporality.AGGREGATION_TEMPORALITY_CUMULATIVE,
dataPoints: [
{
attributes: expectedAttributes,
count: 3,
sum: 10,
min: 1,
max: 8,
zeroCount: 0,
scale: 1,
positive: {
offset: 0,
bucketCounts: [1, 0, 0, 0, 1, 0, 1, 0],
},
negative: { offset: 0, bucketCounts: [0] },
startTimeUnixNano: hrTimeToNanoseconds(START_TIME),
timeUnixNano: hrTimeToNanoseconds(END_TIME),
},
],
},
},
],
},
],
},
],
});
});

it('without min/max', () => {
const exportRequest = createExportMetricsServiceRequest([
createResourceMetrics([
createExponentialHistogramMetrics(
3,
10,
1,
0,
{ offset: 0, bucketCounts: [1, 0, 0, 0, 1, 0, 1, 0] },
{ offset: 0, bucketCounts: [0] },
AggregationTemporality.CUMULATIVE
),
]),
]);

assert.ok(exportRequest);

assert.deepStrictEqual(exportRequest, {
resourceMetrics: [
{
resource: expectedResource,
schemaUrl: undefined,
scopeMetrics: [
{
scope: expectedScope,
schemaUrl: expectedSchemaUrl,
metrics: [
{
name: 'xhist',
description: 'this is a description',
unit: '1',
exponentialHistogram: {
aggregationTemporality:
EAggregationTemporality.AGGREGATION_TEMPORALITY_CUMULATIVE,
dataPoints: [
{
attributes: expectedAttributes,
count: 3,
sum: 10,
min: undefined,
max: undefined,
zeroCount: 0,
scale: 1,
positive: {
offset: 0,
bucketCounts: [1, 0, 0, 0, 1, 0, 1, 0],
},
negative: { offset: 0, bucketCounts: [0] },
startTimeUnixNano: hrTimeToNanoseconds(START_TIME),
timeUnixNano: hrTimeToNanoseconds(END_TIME),
},
],
},
},
],
},
],
},
],
});
});
});
});
});
Loading

0 comments on commit 46eecbb

Please sign in to comment.