Skip to content

Commit c0186cc

Browse files
committed
refactor(otlp-transformer): improve code coverage for metrics
1 parent c38fea5 commit c0186cc

File tree

1 file changed

+46
-45
lines changed
  • experimental/packages/otlp-transformer/src/metrics

1 file changed

+46
-45
lines changed

experimental/packages/otlp-transformer/src/metrics/internal.ts

+46-45
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,21 @@ export function toResourceMetrics(
4444
attributes: toAttributes(resourceMetrics.resource.attributes),
4545
droppedAttributesCount: 0,
4646
},
47-
schemaUrl: undefined, // TODO: Schema Url does not exist yet in the SDK.
47+
schemaUrl: undefined,
4848
scopeMetrics: toScopeMetrics(resourceMetrics.scopeMetrics),
4949
};
5050
}
5151

5252
export function toScopeMetrics(scopeMetrics: ScopeMetrics[]): IScopeMetrics[] {
5353
return Array.from(
54-
scopeMetrics.map(metrics => {
55-
const scopeMetrics: IScopeMetrics = {
56-
scope: {
57-
name: metrics.scope.name,
58-
version: metrics.scope.version,
59-
},
60-
metrics: metrics.metrics.map(metricData => toMetric(metricData)),
61-
schemaUrl: metrics.scope.schemaUrl,
62-
};
63-
return scopeMetrics;
64-
})
54+
scopeMetrics.map(metrics => ({
55+
scope: {
56+
name: metrics.scope.name,
57+
version: metrics.scope.version,
58+
},
59+
metrics: metrics.metrics.map(metricData => toMetric(metricData)),
60+
schemaUrl: metrics.scope.schemaUrl,
61+
}))
6562
);
6663
}
6764

@@ -76,27 +73,31 @@ export function toMetric(metricData: MetricData): IMetric {
7673
metricData.aggregationTemporality
7774
);
7875

79-
if (metricData.dataPointType === DataPointType.SUM) {
80-
out.sum = {
81-
aggregationTemporality,
82-
isMonotonic: metricData.isMonotonic,
83-
dataPoints: toSingularDataPoints(metricData),
84-
};
85-
} else if (metricData.dataPointType === DataPointType.GAUGE) {
86-
// Instrument is a gauge.
87-
out.gauge = {
88-
dataPoints: toSingularDataPoints(metricData),
89-
};
90-
} else if (metricData.dataPointType === DataPointType.HISTOGRAM) {
91-
out.histogram = {
92-
aggregationTemporality,
93-
dataPoints: toHistogramDataPoints(metricData),
94-
};
95-
} else if (metricData.dataPointType === DataPointType.EXPONENTIAL_HISTOGRAM) {
96-
out.exponentialHistogram = {
97-
aggregationTemporality,
98-
dataPoints: toExponentialHistogramDataPoints(metricData),
99-
};
76+
switch (metricData.dataPointType) {
77+
case DataPointType.SUM:
78+
out.sum = {
79+
aggregationTemporality,
80+
isMonotonic: metricData.isMonotonic,
81+
dataPoints: toSingularDataPoints(metricData),
82+
};
83+
break;
84+
case DataPointType.GAUGE:
85+
out.gauge = {
86+
dataPoints: toSingularDataPoints(metricData),
87+
};
88+
break;
89+
case DataPointType.HISTOGRAM:
90+
out.histogram = {
91+
aggregationTemporality,
92+
dataPoints: toHistogramDataPoints(metricData),
93+
};
94+
break;
95+
case DataPointType.EXPONENTIAL_HISTOGRAM:
96+
out.exponentialHistogram = {
97+
aggregationTemporality,
98+
dataPoints: toExponentialHistogramDataPoints(metricData),
99+
};
100+
break;
100101
}
101102

102103
return out;
@@ -115,10 +116,13 @@ function toSingularDataPoint(
115116
timeUnixNano: hrTimeToNanoseconds(dataPoint.endTime),
116117
};
117118

118-
if (valueType === ValueType.INT) {
119-
out.asInt = dataPoint.value as number;
120-
} else if (valueType === ValueType.DOUBLE) {
121-
out.asDouble = dataPoint.value as number;
119+
switch (valueType) {
120+
case ValueType.INT:
121+
out.asInt = dataPoint.value as number;
122+
break;
123+
case ValueType.DOUBLE:
124+
out.asDouble = dataPoint.value as number;
125+
break;
122126
}
123127

124128
return out;
@@ -177,13 +181,10 @@ function toExponentialHistogramDataPoints(
177181
function toAggregationTemporality(
178182
temporality: AggregationTemporality
179183
): EAggregationTemporality {
180-
if (temporality === AggregationTemporality.DELTA) {
181-
return EAggregationTemporality.AGGREGATION_TEMPORALITY_DELTA;
184+
switch (temporality) {
185+
case AggregationTemporality.DELTA:
186+
return EAggregationTemporality.AGGREGATION_TEMPORALITY_DELTA;
187+
case AggregationTemporality.CUMULATIVE:
188+
return EAggregationTemporality.AGGREGATION_TEMPORALITY_CUMULATIVE;
182189
}
183-
184-
if (temporality === AggregationTemporality.CUMULATIVE) {
185-
return EAggregationTemporality.AGGREGATION_TEMPORALITY_CUMULATIVE;
186-
}
187-
188-
return EAggregationTemporality.AGGREGATION_TEMPORALITY_UNSPECIFIED;
189190
}

0 commit comments

Comments
 (0)