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

prometheusremotewrite: Don't generate target_info unless at least one identifying label is defined #32148

Merged
27 changes: 27 additions & 0 deletions .chloggen/bugfix_target-info-required-attrs.yaml
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: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: prometheusremotewrite

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Change prometheusremotewrite.FromMetrics so that the target_info metric is only generated if at least one identifying OTel resource attribute (service.name and/or service.instance.id) is defined.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [32148]

# (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: 'If the service.name resource attribute starts with "unknown_service", it is considered undefined'

# 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: [user]
4 changes: 2 additions & 2 deletions exporter/prometheusremotewriteexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,14 +490,14 @@ func Test_PushMetrics(t *testing.T) {
metrics: invalidTypeBatch,
httpResponseCode: http.StatusAccepted,
reqTestFunc: checkFunc,
expectedTimeSeries: 1, // the resource target metric.
expectedTimeSeries: 0,
expectedFailedTranslations: 1,
},
{
name: "intSum_case",
metrics: intSumBatch,
reqTestFunc: checkFunc,
expectedTimeSeries: 5,
expectedTimeSeries: 4,
httpResponseCode: http.StatusAccepted,
},
{
Expand Down
23 changes: 23 additions & 0 deletions pkg/translator/prometheusremotewrite/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"slices"
"sort"
"strconv"
"strings"
"time"
"unicode/utf8"

Expand Down Expand Up @@ -105,6 +106,10 @@ func createAttributes(resource pcommon.Resource, attributes pcommon.Map, externa
ignoreAttrs []string, logOnOverwrite bool, extras ...string) []prompb.Label {
resourceAttrs := resource.Attributes()
serviceName, haveServiceName := resourceAttrs.Get(conventions.AttributeServiceName)
// SDKs operate with a default service name prefixed with "unknown_service", we should treat such values as undefined.
if haveServiceName && strings.HasPrefix(serviceName.AsString(), "unknown_service") {
haveServiceName = false
}
aknuds1 marked this conversation as resolved.
Show resolved Hide resolved
instance, haveInstanceID := resourceAttrs.Get(conventions.AttributeServiceInstanceID)

// Calculate the maximum possible number of labels we could return so we can preallocate l
Expand Down Expand Up @@ -540,6 +545,24 @@ func addResourceTargetInfo(resource pcommon.Resource, settings Settings, timesta
}

labels := createAttributes(resource, attributes, settings.ExternalLabels, identifyingAttrs, false, model.MetricNameLabel, name)
haveJob := false
haveInstance := false
for _, l := range labels {
if l.Name == model.JobLabel {
haveJob = true
} else if l.Name == model.InstanceLabel {
haveInstance = true
}
if haveJob && haveInstance {
break
}
}

if !haveJob && !haveInstance {
// We need at least one identifying label to generate target_info.
return
}
aknuds1 marked this conversation as resolved.
Show resolved Hide resolved

sample := &prompb.Sample{
Value: float64(1),
// convert ns to ms
Expand Down
87 changes: 48 additions & 39 deletions pkg/translator/prometheusremotewrite/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package prometheusremotewrite

import (
"fmt"
"math"
"sort"
"testing"
Expand Down Expand Up @@ -532,10 +531,22 @@ func TestAddResourceTargetInfo(t *testing.T) {
conventions.AttributeServiceInstanceID: "service-instance-id",
}
resourceWithServiceAttrs := pcommon.NewResource()
assert.NoError(t, resourceWithServiceAttrs.Attributes().FromRaw(resourceAttrMap))
require.NoError(t, resourceWithServiceAttrs.Attributes().FromRaw(resourceAttrMap))
resourceWithServiceAttrs.Attributes().PutStr("resource_attr", "resource-attr-val-1")
resourceWithOnlyServiceAttrs := pcommon.NewResource()
assert.NoError(t, resourceWithOnlyServiceAttrs.Attributes().FromRaw(resourceAttrMap))
require.NoError(t, resourceWithOnlyServiceAttrs.Attributes().FromRaw(resourceAttrMap))
// service.name is an identifying resource attribute.
resourceWithOnlyServiceName := pcommon.NewResource()
resourceWithOnlyServiceName.Attributes().PutStr(conventions.AttributeServiceName, "service-name")
resourceWithOnlyServiceName.Attributes().PutStr("resource_attr", "resource-attr-val-1")
// service.instance.id is an identifying resource attribute.
resourceWithOnlyServiceID := pcommon.NewResource()
resourceWithOnlyServiceID.Attributes().PutStr(conventions.AttributeServiceInstanceID, "service-instance-id")
resourceWithOnlyServiceID.Attributes().PutStr("resource_attr", "resource-attr-val-1")
// SDKs operate with a default service name prefixed with "unknown_service", we should treat such values as undefined.
resourceWithOnlyUnknownServiceName := pcommon.NewResource()
resourceWithOnlyUnknownServiceName.Attributes().PutStr(conventions.AttributeServiceName, "unknown_service: java")
resourceWithOnlyUnknownServiceName.Attributes().PutStr("resource_attr", "resource-attr-val-1")
for _, tc := range []struct {
desc string
resource pcommon.Resource
Expand All @@ -549,61 +560,59 @@ func TestAddResourceTargetInfo(t *testing.T) {
},
{
desc: "disable target info metric",
resource: testdata.GenerateMetricsNoLibraries().ResourceMetrics().At(0).Resource(),
resource: resourceWithOnlyServiceName,
settings: Settings{DisableTargetInfo: true},
},
{
desc: "with resource",
desc: "with resource missing both service.name and service.instance.id resource attributes",
resource: testdata.GenerateMetricsNoLibraries().ResourceMetrics().At(0).Resource(),
timestamp: testdata.TestMetricStartTimestamp,
},
{
desc: "with resource including service.instance.id, and missing service.name resource attribute",
resource: resourceWithOnlyServiceID,
timestamp: testdata.TestMetricStartTimestamp,
wantLabels: []prompb.Label{
{
Name: model.MetricNameLabel,
Value: targetMetricName,
},
{
Name: "resource_attr",
Value: "resource-attr-val-1",
},
{Name: model.MetricNameLabel, Value: "target_info"},
{Name: model.InstanceLabel, Value: "service-instance-id"},
{Name: "resource_attr", Value: "resource-attr-val-1"},
},
},
{
desc: "with resource, with namespace",
resource: testdata.GenerateMetricsNoLibraries().ResourceMetrics().At(0).Resource(),
desc: "with resource including service.name, and missing service.instance.id resource attribute",
resource: resourceWithOnlyServiceName,
timestamp: testdata.TestMetricStartTimestamp,
wantLabels: []prompb.Label{
{Name: model.MetricNameLabel, Value: "target_info"},
{Name: model.JobLabel, Value: "service-name"},
{Name: "resource_attr", Value: "resource-attr-val-1"},
},
},
{
desc: "with resource including unknown_service: java for service.name, and missing service.instance.id resource attribute",
resource: resourceWithOnlyUnknownServiceName,
timestamp: testdata.TestMetricStartTimestamp,
},
{
desc: "with valid resource, with namespace",
resource: resourceWithOnlyServiceName,
timestamp: testdata.TestMetricStartTimestamp,
settings: Settings{Namespace: "foo"},
wantLabels: []prompb.Label{
{
Name: model.MetricNameLabel,
Value: fmt.Sprintf("foo_%s", targetMetricName),
},
{
Name: "resource_attr",
Value: "resource-attr-val-1",
},
{Name: model.MetricNameLabel, Value: "foo_target_info"},
{Name: model.JobLabel, Value: "service-name"},
{Name: "resource_attr", Value: "resource-attr-val-1"},
},
},
{
desc: "with resource, with service attributes",
resource: resourceWithServiceAttrs,
timestamp: testdata.TestMetricStartTimestamp,
wantLabels: []prompb.Label{
{
Name: model.MetricNameLabel,
Value: targetMetricName,
},
{
Name: "instance",
Value: "service-instance-id",
},
{
Name: "job",
Value: "service-namespace/service-name",
},
{
Name: "resource_attr",
Value: "resource-attr-val-1",
},
{Name: model.MetricNameLabel, Value: "target_info"},
{Name: model.InstanceLabel, Value: "service-instance-id"},
{Name: model.JobLabel, Value: "service-namespace/service-name"},
{Name: "resource_attr", Value: "resource-attr-val-1"},
},
},
{
Expand Down
Loading