-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prometheusremotewrite: Introduce pluggable conversion architecture
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
- Loading branch information
Showing
15 changed files
with
242 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package prometheusremotewrite // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/prometheusremotewrite" | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/collector/pdata/pmetric" | ||
"go.uber.org/multierr" | ||
|
||
prometheustranslator "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/prometheus" | ||
) | ||
|
||
// ConvertMetrics converts pmetric.Metrics to another metrics format via the converter. | ||
func ConvertMetrics(md pmetric.Metrics, settings Settings, converter MetricsConverter) (errs error) { | ||
resourceMetricsSlice := md.ResourceMetrics() | ||
for i := 0; i < resourceMetricsSlice.Len(); i++ { | ||
resourceMetrics := resourceMetricsSlice.At(i) | ||
resource := resourceMetrics.Resource() | ||
scopeMetricsSlice := resourceMetrics.ScopeMetrics() | ||
// keep track of the most recent timestamp in the ResourceMetrics for | ||
// use with the "target" info metric | ||
var mostRecentTimestamp pcommon.Timestamp | ||
for j := 0; j < scopeMetricsSlice.Len(); j++ { | ||
metricSlice := scopeMetricsSlice.At(j).Metrics() | ||
|
||
// TODO: decide if instrumentation library information should be exported as labels | ||
for k := 0; k < metricSlice.Len(); k++ { | ||
metric := metricSlice.At(k) | ||
mostRecentTimestamp = maxTimestamp(mostRecentTimestamp, mostRecentTimestampInMetric(metric)) | ||
|
||
if !isValidAggregationTemporality(metric) { | ||
errs = multierr.Append(errs, fmt.Errorf("invalid temporality and type combination for metric %q", metric.Name())) | ||
continue | ||
} | ||
|
||
promName := prometheustranslator.BuildCompliantName(metric, settings.Namespace, settings.AddMetricSuffixes) | ||
|
||
// handle individual metrics based on type | ||
//exhaustive:enforce | ||
switch metric.Type() { | ||
case pmetric.MetricTypeGauge: | ||
dataPoints := metric.Gauge().DataPoints() | ||
if dataPoints.Len() == 0 { | ||
errs = multierr.Append(errs, fmt.Errorf("empty data points. %s is dropped", metric.Name())) | ||
break | ||
} | ||
errs = multierr.Append(errs, converter.AddGaugeNumberDataPoints(dataPoints, resource, settings, promName)) | ||
case pmetric.MetricTypeSum: | ||
dataPoints := metric.Sum().DataPoints() | ||
if dataPoints.Len() == 0 { | ||
errs = multierr.Append(errs, fmt.Errorf("empty data points. %s is dropped", metric.Name())) | ||
break | ||
} | ||
errs = multierr.Append(errs, converter.AddSumNumberDataPoints(dataPoints, resource, metric, settings, promName)) | ||
case pmetric.MetricTypeHistogram: | ||
dataPoints := metric.Histogram().DataPoints() | ||
if dataPoints.Len() == 0 { | ||
errs = multierr.Append(errs, fmt.Errorf("empty data points. %s is dropped", metric.Name())) | ||
break | ||
} | ||
errs = multierr.Append(errs, converter.AddHistogramDataPoints(dataPoints, resource, settings, promName)) | ||
case pmetric.MetricTypeExponentialHistogram: | ||
dataPoints := metric.ExponentialHistogram().DataPoints() | ||
if dataPoints.Len() == 0 { | ||
errs = multierr.Append(errs, fmt.Errorf("empty data points. %s is dropped", metric.Name())) | ||
break | ||
} | ||
errs = multierr.Append(errs, converter.AddExponentialHistogramDataPoints( | ||
dataPoints, | ||
resource, | ||
settings, | ||
promName, | ||
)) | ||
case pmetric.MetricTypeSummary: | ||
dataPoints := metric.Summary().DataPoints() | ||
if dataPoints.Len() == 0 { | ||
errs = multierr.Append(errs, fmt.Errorf("empty data points. %s is dropped", metric.Name())) | ||
break | ||
} | ||
errs = multierr.Append(errs, converter.AddSummaryDataPoints(dataPoints, resource, settings, promName)) | ||
default: | ||
errs = multierr.Append(errs, errors.New("unsupported metric type")) | ||
} | ||
} | ||
} | ||
addResourceTargetInfo(resource, settings, mostRecentTimestamp, converter) | ||
} | ||
|
||
return | ||
} |
Oops, something went wrong.