Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(otlp-transformer): refine metrics transformer #3770

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to experimental packages in this project will be documented
* feat(otlp-transformer): support log records. [#3712](https://github.com/open-telemetry/opentelemetry-js/pull/3712/) @llc1123
* feat(otlp-grpc-exporter-base): support log records. [#3712](https://github.com/open-telemetry/opentelemetry-js/pull/3712/) @llc1123
* feat(exporter-logs-otlp-grpc): otlp-grpc exporter for logs. [#3712](https://github.com/open-telemetry/opentelemetry-js/pull/3712/) @llc1123
* refactor(otlp-transformer): refine metric transformers. [#3770](https://github.com/open-telemetry/opentelemetry-js/pull/3770/) @llc1123

### :bug: (Bug Fix)

Expand Down
91 changes: 46 additions & 45 deletions experimental/packages/otlp-transformer/src/metrics/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,21 @@ export function toResourceMetrics(
attributes: toAttributes(resourceMetrics.resource.attributes),
droppedAttributesCount: 0,
},
schemaUrl: undefined, // TODO: Schema Url does not exist yet in the SDK.
schemaUrl: undefined,
scopeMetrics: toScopeMetrics(resourceMetrics.scopeMetrics),
};
}

export function toScopeMetrics(scopeMetrics: ScopeMetrics[]): IScopeMetrics[] {
return Array.from(
scopeMetrics.map(metrics => {
const scopeMetrics: IScopeMetrics = {
scope: {
name: metrics.scope.name,
version: metrics.scope.version,
},
metrics: metrics.metrics.map(metricData => toMetric(metricData)),
schemaUrl: metrics.scope.schemaUrl,
};
return scopeMetrics;
})
scopeMetrics.map(metrics => ({
scope: {
name: metrics.scope.name,
version: metrics.scope.version,
},
metrics: metrics.metrics.map(metricData => toMetric(metricData)),
schemaUrl: metrics.scope.schemaUrl,
}))
);
}

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

if (metricData.dataPointType === DataPointType.SUM) {
out.sum = {
aggregationTemporality,
isMonotonic: metricData.isMonotonic,
dataPoints: toSingularDataPoints(metricData),
};
} else if (metricData.dataPointType === DataPointType.GAUGE) {
// Instrument is a gauge.
out.gauge = {
dataPoints: toSingularDataPoints(metricData),
};
} else if (metricData.dataPointType === DataPointType.HISTOGRAM) {
out.histogram = {
aggregationTemporality,
dataPoints: toHistogramDataPoints(metricData),
};
} else if (metricData.dataPointType === DataPointType.EXPONENTIAL_HISTOGRAM) {
out.exponentialHistogram = {
aggregationTemporality,
dataPoints: toExponentialHistogramDataPoints(metricData),
};
switch (metricData.dataPointType) {
case DataPointType.SUM:
out.sum = {
aggregationTemporality,
isMonotonic: metricData.isMonotonic,
dataPoints: toSingularDataPoints(metricData),
};
break;
case DataPointType.GAUGE:
out.gauge = {
dataPoints: toSingularDataPoints(metricData),
};
break;
case DataPointType.HISTOGRAM:
out.histogram = {
aggregationTemporality,
dataPoints: toHistogramDataPoints(metricData),
};
break;
case DataPointType.EXPONENTIAL_HISTOGRAM:
out.exponentialHistogram = {
aggregationTemporality,
dataPoints: toExponentialHistogramDataPoints(metricData),
};
break;
}

return out;
Expand All @@ -115,10 +116,13 @@ function toSingularDataPoint(
timeUnixNano: hrTimeToNanoseconds(dataPoint.endTime),
};

if (valueType === ValueType.INT) {
out.asInt = dataPoint.value as number;
} else if (valueType === ValueType.DOUBLE) {
out.asDouble = dataPoint.value as number;
switch (valueType) {
case ValueType.INT:
out.asInt = dataPoint.value as number;
break;
case ValueType.DOUBLE:
out.asDouble = dataPoint.value as number;
break;
}

return out;
Expand Down Expand Up @@ -177,13 +181,10 @@ function toExponentialHistogramDataPoints(
function toAggregationTemporality(
temporality: AggregationTemporality
): EAggregationTemporality {
if (temporality === AggregationTemporality.DELTA) {
return EAggregationTemporality.AGGREGATION_TEMPORALITY_DELTA;
switch (temporality) {
case AggregationTemporality.DELTA:
return EAggregationTemporality.AGGREGATION_TEMPORALITY_DELTA;
case AggregationTemporality.CUMULATIVE:
return EAggregationTemporality.AGGREGATION_TEMPORALITY_CUMULATIVE;
}

if (temporality === AggregationTemporality.CUMULATIVE) {
return EAggregationTemporality.AGGREGATION_TEMPORALITY_CUMULATIVE;
}

return EAggregationTemporality.AGGREGATION_TEMPORALITY_UNSPECIFIED;
}