-
Notifications
You must be signed in to change notification settings - Fork 3.1k
feat: added prometheus rw2 translation for gauges #35734
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
Merged
andrzej-stencel
merged 8 commits into
open-telemetry:main
from
jmichalek132:jm-prom-translation-rw2-gauges-support-fixed
Oct 16, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5d0e663
feat: added translation rw2 translation for gauges
jmichalek132 74d386f
Update pkg/translator/prometheusremotewrite/number_data_points_v2_tes…
jmichalek132 eaf5add
Merge branch 'main' into jm-prom-translation-rw2-gauges-support-fixed
jmichalek132 7164a62
chore: remove unnecessary t.Run
jmichalek132 8b41734
Merge branch 'main' into jm-prom-translation-rw2-gauges-support-fixed
jmichalek132 3147bba
chore: update existing function instead of creating a new one
jmichalek132 bac5e92
Merge branch 'main' into jm-prom-translation-rw2-gauges-support-fixed
dashpole 58c8c02
Merge branch 'main' into jm-prom-translation-rw2-gauges-support-fixed
jmichalek132 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: pkg/translator/prometheusremotewrite | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: add FromMetricsV2 | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [33661] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: The public function is partially implemented and not ready for use | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [api] |
This file contains hidden or 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 hidden or 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
122 changes: 122 additions & 0 deletions
122
pkg/translator/prometheusremotewrite/metrics_to_prw_v2.go
This file contains hidden or 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,122 @@ | ||
// 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" | ||
"strconv" | ||
|
||
"github.com/prometheus/prometheus/model/labels" | ||
writev2 "github.com/prometheus/prometheus/prompb/io/prometheus/write/v2" | ||
"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" | ||
) | ||
|
||
// FromMetricsV2 converts pmetric.Metrics to Prometheus remote write format 2.0. | ||
func FromMetricsV2(md pmetric.Metrics, settings Settings) (map[string]*writev2.TimeSeries, writev2.SymbolsTable, error) { | ||
c := newPrometheusConverterV2() | ||
errs := c.fromMetrics(md, settings) | ||
tss := c.timeSeries() | ||
out := make(map[string]*writev2.TimeSeries, len(tss)) | ||
for i := range tss { | ||
out[strconv.Itoa(i)] = &tss[i] | ||
} | ||
|
||
return out, c.symbolTable, errs | ||
} | ||
|
||
// prometheusConverterV2 converts from OTLP to Prometheus write 2.0 format. | ||
type prometheusConverterV2 struct { | ||
// TODO handle conflicts | ||
unique map[uint64]*writev2.TimeSeries | ||
symbolTable writev2.SymbolsTable | ||
} | ||
|
||
func newPrometheusConverterV2() *prometheusConverterV2 { | ||
return &prometheusConverterV2{ | ||
unique: map[uint64]*writev2.TimeSeries{}, | ||
symbolTable: writev2.NewSymbolTable(), | ||
} | ||
} | ||
|
||
// fromMetrics converts pmetric.Metrics to Prometheus remote write format. | ||
func (c *prometheusConverterV2) fromMetrics(md pmetric.Metrics, settings Settings) (errs error) { | ||
resourceMetricsSlice := md.ResourceMetrics() | ||
for i := 0; i < resourceMetricsSlice.Len(); i++ { | ||
resourceMetrics := resourceMetricsSlice.At(i) | ||
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 | ||
} | ||
c.addGaugeNumberDataPoints(dataPoints, promName) | ||
case pmetric.MetricTypeSum: | ||
// TODO implement | ||
case pmetric.MetricTypeHistogram: | ||
// TODO implement | ||
case pmetric.MetricTypeExponentialHistogram: | ||
// TODO implement | ||
case pmetric.MetricTypeSummary: | ||
// TODO implement | ||
default: | ||
errs = multierr.Append(errs, errors.New("unsupported metric type")) | ||
} | ||
} | ||
} | ||
// TODO implement | ||
// addResourceTargetInfov2(resource, settings, mostRecentTimestamp, c) | ||
} | ||
|
||
return | ||
} | ||
|
||
// timeSeries returns a slice of the writev2.TimeSeries that were converted from OTel format. | ||
func (c *prometheusConverterV2) timeSeries() []writev2.TimeSeries { | ||
allTS := make([]writev2.TimeSeries, 0, len(c.unique)) | ||
for _, ts := range c.unique { | ||
allTS = append(allTS, *ts) | ||
} | ||
return allTS | ||
} | ||
|
||
func (c *prometheusConverterV2) addSample(sample *writev2.Sample, lbls labels.Labels) *writev2.TimeSeries { | ||
if sample == nil || len(lbls) == 0 { | ||
// This shouldn't happen | ||
return nil | ||
} | ||
ts := &writev2.TimeSeries{} | ||
ts.LabelsRefs = c.symbolTable.SymbolizeLabels(lbls, ts.LabelsRefs) | ||
ts.Samples = append(ts.Samples, *sample) | ||
|
||
c.unique[lbls.Hash()] = ts | ||
|
||
return ts | ||
} |
44 changes: 44 additions & 0 deletions
44
pkg/translator/prometheusremotewrite/metrics_to_prw_v2_test.go
This file contains hidden or 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,44 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package prometheusremotewrite | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
writev2 "github.com/prometheus/prometheus/prompb/io/prometheus/write/v2" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
) | ||
|
||
func TestFromMetricsV2(t *testing.T) { | ||
settings := Settings{ | ||
Namespace: "", | ||
ExternalLabels: nil, | ||
DisableTargetInfo: false, | ||
ExportCreatedMetric: false, | ||
AddMetricSuffixes: false, | ||
SendMetadata: false, | ||
} | ||
|
||
ts := uint64(time.Now().UnixNano()) | ||
payload := createExportRequest(5, 0, 1, 3, 0, pcommon.Timestamp(ts)) | ||
want := func() map[string]*writev2.TimeSeries { | ||
return map[string]*writev2.TimeSeries{ | ||
"0": { | ||
LabelsRefs: []uint32{1, 2}, | ||
Samples: []writev2.Sample{ | ||
{Timestamp: convertTimeStamp(pcommon.Timestamp(ts)), Value: 1.23}, | ||
}, | ||
}, | ||
} | ||
} | ||
tsMap, symbolsTable, err := FromMetricsV2(payload.Metrics(), settings) | ||
wanted := want() | ||
require.NoError(t, err) | ||
require.NotNil(t, tsMap) | ||
require.Equal(t, wanted, tsMap) | ||
require.NotNil(t, symbolsTable) | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
pkg/translator/prometheusremotewrite/number_data_points_v2.go
This file contains hidden or 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,43 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package prometheusremotewrite // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/prometheusremotewrite" | ||
|
||
import ( | ||
"math" | ||
|
||
"github.com/prometheus/common/model" | ||
"github.com/prometheus/prometheus/model/labels" | ||
"github.com/prometheus/prometheus/model/value" | ||
writev2 "github.com/prometheus/prometheus/prompb/io/prometheus/write/v2" | ||
"go.opentelemetry.io/collector/pdata/pmetric" | ||
) | ||
|
||
func (c *prometheusConverterV2) addGaugeNumberDataPoints(dataPoints pmetric.NumberDataPointSlice, name string) { | ||
for x := 0; x < dataPoints.Len(); x++ { | ||
pt := dataPoints.At(x) | ||
// TODO implement support for labels | ||
|
||
labels := labels.Labels{ | ||
labels.Label{ | ||
Name: model.MetricNameLabel, | ||
Value: name, | ||
}, | ||
} | ||
|
||
sample := &writev2.Sample{ | ||
// convert ns to ms | ||
Timestamp: convertTimeStamp(pt.Timestamp()), | ||
} | ||
switch pt.ValueType() { | ||
case pmetric.NumberDataPointValueTypeInt: | ||
sample.Value = float64(pt.IntValue()) | ||
case pmetric.NumberDataPointValueTypeDouble: | ||
sample.Value = pt.DoubleValue() | ||
} | ||
if pt.Flags().NoRecordedValue() { | ||
sample.Value = math.Float64frombits(value.StaleNaN) | ||
} | ||
c.addSample(sample, labels) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.