Skip to content

Commit

Permalink
[pkg/translator/prometheusremotewrite] Support non-string resource at…
Browse files Browse the repository at this point in the history
…tributes (#8116)
  • Loading branch information
dashpole authored Mar 1, 2022
1 parent eb87bea commit e6fe8dd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

- `prometheusreceiver`: Fix segfault that can occur after receiving stale metrics (#8056)
- `filelogreceiver`: Fix issue where logs could occasionally be duplicated (#8123)
- `prometheusremotewriteexporter`: Fix empty non-string resource attributes (#8116)

### 🚀 New components 🚀

Expand Down
2 changes: 1 addition & 1 deletion pkg/translator/prometheusremotewrite/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func createAttributes(resource pdata.Resource, attributes pdata.AttributeMap, ex
if isUsefulResourceAttribute(key) {
l[key] = prompb.Label{
Name: sanitize(key),
Value: value.StringVal(), // TODO(jbd): Decide what to do with non-string attributes.
Value: value.AsString(),
}
}

Expand Down
32 changes: 23 additions & 9 deletions pkg/translator/prometheusremotewrite/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,71 +189,85 @@ func Test_createLabelSet(t *testing.T) {
}{
{
"labels_clean",
getResource(),
getResource(map[string]pdata.AttributeValue{}),
lbs1,
map[string]string{},
[]string{label31, value31, label32, value32},
getPromLabels(label11, value11, label12, value12, label31, value31, label32, value32),
},
{
"labels_with_resource",
getResource("job", "prometheus", "instance", "127.0.0.1:8080"),
getResource(map[string]pdata.AttributeValue{
"job": pdata.NewAttributeValueString("prometheus"),
"instance": pdata.NewAttributeValueString("127.0.0.1:8080"),
}),
lbs1,
map[string]string{},
[]string{label31, value31, label32, value32},
getPromLabels(label11, value11, label12, value12, label31, value31, label32, value32, "job", "prometheus", "instance", "127.0.0.1:8080"),
},
{
"labels_with_nonstring_resource",
getResource(map[string]pdata.AttributeValue{
"job": pdata.NewAttributeValueInt(12345),
"instance": pdata.NewAttributeValueBool(true),
}),
lbs1,
map[string]string{},
[]string{label31, value31, label32, value32},
getPromLabels(label11, value11, label12, value12, label31, value31, label32, value32, "job", "12345", "instance", "true"),
},
{
"labels_duplicate_in_extras",
getResource(),
getResource(map[string]pdata.AttributeValue{}),
lbs1,
map[string]string{},
[]string{label11, value31},
getPromLabels(label11, value31, label12, value12),
},
{
"labels_dirty",
getResource(),
getResource(map[string]pdata.AttributeValue{}),
lbs1Dirty,
map[string]string{},
[]string{label31 + dirty1, value31, label32, value32},
getPromLabels(label11+"_", value11, "key_"+label12, value12, label31+"_", value31, label32, value32),
},
{
"no_original_case",
getResource(),
getResource(map[string]pdata.AttributeValue{}),
pdata.NewAttributeMap(),
nil,
[]string{label31, value31, label32, value32},
getPromLabels(label31, value31, label32, value32),
},
{
"empty_extra_case",
getResource(),
getResource(map[string]pdata.AttributeValue{}),
lbs1,
map[string]string{},
[]string{"", ""},
getPromLabels(label11, value11, label12, value12, "", ""),
},
{
"single_left_over_case",
getResource(),
getResource(map[string]pdata.AttributeValue{}),
lbs1,
map[string]string{},
[]string{label31, value31, label32},
getPromLabels(label11, value11, label12, value12, label31, value31),
},
{
"valid_external_labels",
getResource(),
getResource(map[string]pdata.AttributeValue{}),
lbs1,
exlbs1,
[]string{label31, value31, label32, value32},
getPromLabels(label11, value11, label12, value12, label41, value41, label31, value31, label32, value32),
},
{
"overwritten_external_labels",
getResource(),
getResource(map[string]pdata.AttributeValue{}),
lbs1,
exlbs2,
[]string{label31, value31, label32, value32},
Expand Down
6 changes: 3 additions & 3 deletions pkg/translator/prometheusremotewrite/testutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,11 @@ func getSummaryMetric(name string, attributes pdata.AttributeMap, ts uint64, sum
return metric
}

func getResource(resources ...string) pdata.Resource {
func getResource(resources map[string]pdata.AttributeValue) pdata.Resource {
resource := pdata.NewResource()

for i := 0; i < len(resources); i += 2 {
resource.Attributes().Upsert(resources[i], pdata.NewAttributeValueString(resources[i+1]))
for k, v := range resources {
resource.Attributes().Upsert(k, v)
}

return resource
Expand Down

0 comments on commit e6fe8dd

Please sign in to comment.