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

[chore] [translator/prometheusremotewrite] Clean up and simplify code #30749

Merged
merged 3 commits into from
Feb 15, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 24 additions & 30 deletions pkg/translator/prometheusremotewrite/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
)

const (
nameStr = "__name__"
bryan-aguilar marked this conversation as resolved.
Show resolved Hide resolved
sumStr = "_sum"
countStr = "_count"
bucketStr = "_bucket"
Expand Down Expand Up @@ -70,15 +69,14 @@ func (a ByLabelName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
// tsMap will be unmodified if either labels or sample is nil, but can still be modified if the exemplar is nil.
func addSample(tsMap map[string]*prompb.TimeSeries, sample *prompb.Sample, labels []prompb.Label,
datatype string) string {

if sample == nil || labels == nil || tsMap == nil {
// This shouldn't happen
return ""
}

sig := timeSeriesSignature(datatype, &labels)
ts, ok := tsMap[sig]

if ok {
sig := timeSeriesSignature(datatype, labels)
ts := tsMap[sig]
if ts != nil {
bryan-aguilar marked this conversation as resolved.
Show resolved Hide resolved
ts.Samples = append(ts.Samples, *sample)
} else {
newTs := &prompb.TimeSeries{
Expand All @@ -95,7 +93,7 @@ func addSample(tsMap map[string]*prompb.TimeSeries, sample *prompb.Sample, label
// we only add exemplars if samples are presents
// tsMap is unmodified if either of its parameters is nil and samples are nil.
func addExemplars(tsMap map[string]*prompb.TimeSeries, exemplars []prompb.Exemplar, bucketBoundsData []bucketBoundsData) {
if tsMap == nil || bucketBoundsData == nil || exemplars == nil {
if len(tsMap) == 0 || len(bucketBoundsData) == 0 || len(exemplars) == 0 {
return
}

Expand All @@ -111,14 +109,10 @@ func addExemplar(tsMap map[string]*prompb.TimeSeries, bucketBounds []bucketBound
sig := bucketBound.sig
bound := bucketBound.bound

_, ok := tsMap[sig]
if ok {
if tsMap[sig].Samples != nil {
if exemplar.Value <= bound {
tsMap[sig].Exemplars = append(tsMap[sig].Exemplars, exemplar)
return
}
}
ts := tsMap[sig]
if ts != nil && len(ts.Samples) > 0 && exemplar.Value <= bound {
ts.Exemplars = append(ts.Exemplars, exemplar)
return
}
}
}
Expand All @@ -129,20 +123,20 @@ func addExemplar(tsMap map[string]*prompb.TimeSeries, bucketBounds []bucketBound
//
// the label slice should not contain duplicate label names; this method sorts the slice by label name before creating
// the signature.
func timeSeriesSignature(datatype string, labels *[]prompb.Label) string {
func timeSeriesSignature(datatype string, labels []prompb.Label) string {
length := len(datatype)

for _, lb := range *labels {
for _, lb := range labels {
length += 2 + len(lb.GetName()) + len(lb.GetValue())
}

b := strings.Builder{}
b.Grow(length)
b.WriteString(datatype)

sort.Sort(ByLabelName(*labels))
sort.Sort(ByLabelName(labels))

for _, lb := range *labels {
for _, lb := range labels {
b.WriteString("-")
b.WriteString(lb.GetName())
b.WriteString("-")
Expand All @@ -152,9 +146,9 @@ func timeSeriesSignature(datatype string, labels *[]prompb.Label) string {
return b.String()
}

// createAttributes creates a slice of Cortex Label with OTLP attributes and pairs of string values.
// Unpaired string value is ignored. String pairs overwrites OTLP labels if collision happens, and the overwrite is
// logged. Resultant label names are sanitized.
// createAttributes creates a slice of Prometheus Labels with OTLP attributes and pairs of string values.
// Unpaired string values are ignored. String pairs overwrite OTLP labels if collisions happen, and overwrites are
// logged. Resulting label names are sanitized.
func createAttributes(resource pcommon.Resource, attributes pcommon.Map, externalLabels map[string]string, extras ...string) []prompb.Label {
serviceName, haveServiceName := resource.Attributes().Get(conventions.AttributeServiceName)
instance, haveInstanceID := resource.Attributes().Get(conventions.AttributeServiceInstanceID)
Expand Down Expand Up @@ -184,8 +178,8 @@ func createAttributes(resource pcommon.Resource, attributes pcommon.Map, externa

for _, label := range labels {
var finalKey = prometheustranslator.NormalizeLabel(label.Name)
if existingLabel, alreadyExists := l[finalKey]; alreadyExists {
l[finalKey] = existingLabel + ";" + label.Value
if existingValue, alreadyExists := l[finalKey]; alreadyExists {
l[finalKey] = existingValue + ";" + label.Value
} else {
l[finalKey] = label.Value
}
Expand Down Expand Up @@ -269,7 +263,7 @@ func addSingleHistogramDataPoint(pt pmetric.HistogramDataPoint, resource pcommon
}

// sum, count, and buckets of the histogram should append suffix to baseName
labels = append(labels, prompb.Label{Name: nameStr, Value: baseName + nameSuffix})
labels = append(labels, prompb.Label{Name: model.MetricNameLabel, Value: baseName + nameSuffix})

return labels
}
Expand Down Expand Up @@ -361,7 +355,7 @@ func getPromExemplars[T exemplarType](pt T) []prompb.Exemplar {
exemplar := pt.Exemplars().At(i)
exemplarRunes := 0

promExemplar := &prompb.Exemplar{
promExemplar := prompb.Exemplar{
Value: exemplar.DoubleValue(),
Timestamp: timestamp.FromTime(exemplar.Timestamp().AsTime()),
}
Expand Down Expand Up @@ -404,7 +398,7 @@ func getPromExemplars[T exemplarType](pt T) []prompb.Exemplar {
promExemplar.Labels = append(promExemplar.Labels, labelsFromAttributes...)
}

promExemplars = append(promExemplars, *promExemplar)
promExemplars = append(promExemplars, promExemplar)
}

return promExemplars
Expand Down Expand Up @@ -467,7 +461,7 @@ func addSingleSummaryDataPoint(pt pmetric.SummaryDataPoint, resource pcommon.Res
labels = append(labels, prompb.Label{Name: extras[extrasIdx], Value: extras[extrasIdx+1]})
}

labels = append(labels, prompb.Label{Name: nameStr, Value: name})
labels = append(labels, prompb.Label{Name: model.MetricNameLabel, Value: name})
bryan-aguilar marked this conversation as resolved.
Show resolved Hide resolved

return labels
}
Expand Down Expand Up @@ -527,7 +521,7 @@ func addCreatedTimeSeriesIfNeeded(
timestamp pcommon.Timestamp,
metricType string,
) {
sig := timeSeriesSignature(metricType, &labels)
sig := timeSeriesSignature(metricType, labels)
if _, ok := series[sig]; !ok {
series[sig] = &prompb.TimeSeries{
Labels: labels,
Expand Down Expand Up @@ -568,7 +562,7 @@ func addResourceTargetInfo(resource pcommon.Resource, settings Settings, timesta
if len(settings.Namespace) > 0 {
name = settings.Namespace + "_" + name
}
labels := createAttributes(resource, attributes, settings.ExternalLabels, nameStr, name)
labels := createAttributes(resource, attributes, settings.ExternalLabels, model.MetricNameLabel, name)
sample := &prompb.Sample{
Value: float64(1),
// convert ns to ms
Expand Down
23 changes: 11 additions & 12 deletions pkg/translator/prometheusremotewrite/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ func Test_timeSeriesSignature(t *testing.T) {
// run tests
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
lbs := tt.lbs
assert.EqualValues(t, tt.want, timeSeriesSignature(tt.metric.Type().String(), &lbs))
assert.EqualValues(t, tt.want, timeSeriesSignature(tt.metric.Type().String(), tt.lbs))
})
}
}
Expand Down Expand Up @@ -691,19 +690,19 @@ func TestAddSingleSummaryDataPoint(t *testing.T) {
{Name: model.MetricNameLabel, Value: "test_summary" + sumStr},
}
return map[string]*prompb.TimeSeries{
timeSeriesSignature(pmetric.MetricTypeSummary.String(), &labels): {
timeSeriesSignature(pmetric.MetricTypeSummary.String(), labels): {
Labels: labels,
Samples: []prompb.Sample{
{Value: 0, Timestamp: convertTimeStamp(ts)},
},
},
timeSeriesSignature(pmetric.MetricTypeSummary.String(), &sumLabels): {
timeSeriesSignature(pmetric.MetricTypeSummary.String(), sumLabels): {
Labels: sumLabels,
Samples: []prompb.Sample{
{Value: 0, Timestamp: convertTimeStamp(ts)},
},
},
timeSeriesSignature(pmetric.MetricTypeSummary.String(), &createdLabels): {
timeSeriesSignature(pmetric.MetricTypeSummary.String(), createdLabels): {
Labels: createdLabels,
Samples: []prompb.Sample{
{Value: float64(convertTimeStamp(ts)), Timestamp: convertTimeStamp(ts)},
Expand Down Expand Up @@ -732,13 +731,13 @@ func TestAddSingleSummaryDataPoint(t *testing.T) {
{Name: model.MetricNameLabel, Value: "test_summary" + sumStr},
}
return map[string]*prompb.TimeSeries{
timeSeriesSignature(pmetric.MetricTypeSummary.String(), &labels): {
timeSeriesSignature(pmetric.MetricTypeSummary.String(), labels): {
Labels: labels,
Samples: []prompb.Sample{
{Value: 0, Timestamp: convertTimeStamp(ts)},
},
},
timeSeriesSignature(pmetric.MetricTypeSummary.String(), &sumLabels): {
timeSeriesSignature(pmetric.MetricTypeSummary.String(), sumLabels): {
Labels: sumLabels,
Samples: []prompb.Sample{
{Value: 0, Timestamp: convertTimeStamp(ts)},
Expand Down Expand Up @@ -802,19 +801,19 @@ func TestAddSingleHistogramDataPoint(t *testing.T) {
{Name: model.BucketLabel, Value: "+Inf"},
}
return map[string]*prompb.TimeSeries{
timeSeriesSignature(pmetric.MetricTypeHistogram.String(), &infLabels): {
timeSeriesSignature(pmetric.MetricTypeHistogram.String(), infLabels): {
Labels: infLabels,
Samples: []prompb.Sample{
{Value: 0, Timestamp: convertTimeStamp(ts)},
},
},
timeSeriesSignature(pmetric.MetricTypeHistogram.String(), &labels): {
timeSeriesSignature(pmetric.MetricTypeHistogram.String(), labels): {
Labels: labels,
Samples: []prompb.Sample{
{Value: 0, Timestamp: convertTimeStamp(ts)},
},
},
timeSeriesSignature(pmetric.MetricTypeHistogram.String(), &createdLabels): {
timeSeriesSignature(pmetric.MetricTypeHistogram.String(), createdLabels): {
Labels: createdLabels,
Samples: []prompb.Sample{
{Value: float64(convertTimeStamp(ts)), Timestamp: convertTimeStamp(ts)},
Expand Down Expand Up @@ -844,13 +843,13 @@ func TestAddSingleHistogramDataPoint(t *testing.T) {
{Name: model.BucketLabel, Value: "+Inf"},
}
return map[string]*prompb.TimeSeries{
timeSeriesSignature(pmetric.MetricTypeHistogram.String(), &infLabels): {
timeSeriesSignature(pmetric.MetricTypeHistogram.String(), infLabels): {
Labels: infLabels,
Samples: []prompb.Sample{
{Value: 0, Timestamp: convertTimeStamp(ts)},
},
},
timeSeriesSignature(pmetric.MetricTypeHistogram.String(), &labels): {
timeSeriesSignature(pmetric.MetricTypeHistogram.String(), labels): {
Labels: labels,
Samples: []prompb.Sample{
{Value: 0, Timestamp: convertTimeStamp(ts)},
Expand Down
5 changes: 3 additions & 2 deletions pkg/translator/prometheusremotewrite/histograms.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ func addSingleExponentialHistogramDataPoint(
resource,
pt.Attributes(),
settings.ExternalLabels,
model.MetricNameLabel, metric,
model.MetricNameLabel,
metric,
)

sig := timeSeriesSignature(
pmetric.MetricTypeExponentialHistogram.String(),
&labels,
labels,
)
ts, ok := series[sig]
if !ok {
Expand Down
6 changes: 3 additions & 3 deletions pkg/translator/prometheusremotewrite/histograms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ func TestAddSingleExponentialHistogramDataPoint(t *testing.T) {
{Name: "attr", Value: "test_attr"},
}
return map[string]*prompb.TimeSeries{
timeSeriesSignature(pmetric.MetricTypeExponentialHistogram.String(), &labels): {
timeSeriesSignature(pmetric.MetricTypeExponentialHistogram.String(), labels): {
Labels: labels,
Histograms: []prompb.Histogram{
{
Expand Down Expand Up @@ -698,7 +698,7 @@ func TestAddSingleExponentialHistogramDataPoint(t *testing.T) {
}

return map[string]*prompb.TimeSeries{
timeSeriesSignature(pmetric.MetricTypeExponentialHistogram.String(), &labels): {
timeSeriesSignature(pmetric.MetricTypeExponentialHistogram.String(), labels): {
Labels: labels,
Histograms: []prompb.Histogram{
{
Expand All @@ -714,7 +714,7 @@ func TestAddSingleExponentialHistogramDataPoint(t *testing.T) {
{Value: 1},
},
},
timeSeriesSignature(pmetric.MetricTypeExponentialHistogram.String(), &labelsAnother): {
timeSeriesSignature(pmetric.MetricTypeExponentialHistogram.String(), labelsAnother): {
Labels: labelsAnother,
Histograms: []prompb.Histogram{
{
Expand Down
2 changes: 1 addition & 1 deletion pkg/translator/prometheusremotewrite/metrics_to_prw.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Settings struct {
SendMetadata bool
}

// FromMetrics converts pmetric.Metrics to prometheus remote write format.
// FromMetrics converts pmetric.Metrics to Prometheus remote write format.
func FromMetrics(md pmetric.Metrics, settings Settings) (tsMap map[string]*prompb.TimeSeries, errs error) {
tsMap = make(map[string]*prompb.TimeSeries)

Expand Down
27 changes: 15 additions & 12 deletions pkg/translator/prometheusremotewrite/number_data_points.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"go.opentelemetry.io/collector/pdata/pmetric"
)

// addSingleSumNumberDataPoint converts the Gauge metric data point to a
// addSingleGaugeNumberDataPoint converts the Gauge metric data point to a
// Prometheus time series with samples and labels. The result is stored in the
// series map.
func addSingleGaugeNumberDataPoint(
Expand All @@ -28,7 +28,8 @@ func addSingleGaugeNumberDataPoint(
resource,
pt.Attributes(),
settings.ExternalLabels,
model.MetricNameLabel, name,
model.MetricNameLabel,
name,
)
sample := &prompb.Sample{
// convert ns to ms
Expand Down Expand Up @@ -78,24 +79,26 @@ func addSingleSumNumberDataPoint(
}
sig := addSample(series, sample, labels, metric.Type().String())

if ts, ok := series[sig]; sig != "" && ok {
if ts := series[sig]; sig != "" && ts != nil {
bryan-aguilar marked this conversation as resolved.
Show resolved Hide resolved
exemplars := getPromExemplars[pmetric.NumberDataPoint](pt)
ts.Exemplars = append(ts.Exemplars, exemplars...)
}

// add _created time series if needed
if settings.ExportCreatedMetric && metric.Sum().IsMonotonic() {
startTimestamp := pt.StartTimestamp()
if startTimestamp != 0 {
createdLabels := make([]prompb.Label, len(labels))
copy(createdLabels, labels)
for i, l := range createdLabels {
if l.Name == model.MetricNameLabel {
createdLabels[i].Value = name + createdSuffix
break
}
if startTimestamp == 0 {
return
}

createdLabels := make([]prompb.Label, len(labels))
copy(createdLabels, labels)
for i, l := range createdLabels {
if l.Name == model.MetricNameLabel {
createdLabels[i].Value = name + createdSuffix
break
}
addCreatedTimeSeriesIfNeeded(series, createdLabels, startTimestamp, pt.Timestamp(), metric.Type().String())
}
addCreatedTimeSeriesIfNeeded(series, createdLabels, startTimestamp, pt.Timestamp(), metric.Type().String())
}
}
Loading
Loading