-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
[metricstransformprocessor] add new operation- convert_resource_attributes_to_metric_labels #1337
Changes from 3 commits
e806ba0
bca2712
7232818
4dbdafa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,10 +27,11 @@ import ( | |
) | ||
|
||
type internalTransform struct { | ||
MetricName string | ||
Action ConfigAction | ||
NewName string | ||
Operations []internalOperation | ||
MetricName string | ||
Action ConfigAction | ||
NewName string | ||
Operations []internalOperation | ||
resourceAttributeMap map[string]string | ||
} | ||
|
||
type internalOperation struct { | ||
|
@@ -65,7 +66,24 @@ func (mtp *metricsTransformProcessor) ProcessMetrics(_ context.Context, md pdata | |
nameToMetricMapping[metric.MetricDescriptor.Name] = metric | ||
} | ||
|
||
resourceAttributes := make(map[string]string) | ||
if data.Resource != nil { | ||
resourceAttributes = data.Resource.Labels | ||
} | ||
for _, transform := range mtp.transforms { | ||
transform.resourceAttributeMap = resourceAttributes | ||
|
||
// When user sets "metric_name=all_metrics", these operations are going to be applied | ||
// to all metric data points. Right now, this batch operation only works and is tested for | ||
// `convert_resource_attributes_to_labels` operation, and does not break existing experience | ||
// for other operations. After updating the metrics, it will continue for the next iteration. | ||
if transform.MetricName == "all_metrics" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than use "all_metrics" as a special value, I think it would make more sense to do what @tigrannajaryan suggested and make There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pushed an update. Nice suggestion. Thanks @james-bebbington. |
||
for _, metric := range data.Metrics { | ||
mtp.update(metric, transform) | ||
} | ||
continue | ||
} | ||
|
||
metric, ok := nameToMetricMapping[transform.MetricName] | ||
if !ok { | ||
continue | ||
|
@@ -110,6 +128,8 @@ func (mtp *metricsTransformProcessor) update(metric *metricspb.Metric, transform | |
mtp.addLabelOp(metric, op) | ||
case DeleteLabelValue: | ||
mtp.deleteLabelValueOp(metric, op) | ||
case ConvertResourceAttributesToLabels: | ||
mtp.convertResourceAttributesToLabels(metric, transform.resourceAttributeMap, op) | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -570,6 +570,129 @@ var ( | |
build(), | ||
}, | ||
}, | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a test case for the nil case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a new test case for nil or empty resource attributes. |
||
name: "all_metrics_batch_operation_test", | ||
transforms: []internalTransform{ | ||
{ | ||
MetricName: "all_metrics", | ||
Action: Update, | ||
Operations: []internalOperation{ | ||
{ | ||
configOperation: Operation{ | ||
Action: UpdateLabel, | ||
Label: "label1", | ||
NewLabel: "new/label1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
in: []*metricspb.Metric{ | ||
metricBuilder().setName("metric1"). | ||
setDataType(metricspb.MetricDescriptor_CUMULATIVE_INT64). | ||
setLabels([]string{"label1"}). | ||
addTimeseries(1, []string{"value1"}). | ||
addInt64Point(0, 3, 2).build(), | ||
}, | ||
out: []*metricspb.Metric{ | ||
metricBuilder().setName("metric1"). | ||
setDataType(metricspb.MetricDescriptor_CUMULATIVE_INT64). | ||
setLabels([]string{"new/label1"}). | ||
addTimeseries(1, []string{"value1"}). | ||
addInt64Point(0, 3, 2).build(), | ||
}, | ||
}, | ||
{ | ||
name: "convert_resource_attributes_to_labels_with_nil_resource", | ||
transforms: []internalTransform{ | ||
{ | ||
MetricName: "all_metrics", | ||
Action: Update, | ||
Operations: []internalOperation{ | ||
{ | ||
configOperation: Operation{ | ||
Action: ConvertResourceAttributesToLabels, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
in: []*metricspb.Metric{ | ||
metricBuilder().setName("metric1"). | ||
setDataType(metricspb.MetricDescriptor_CUMULATIVE_INT64). | ||
setLabels([]string{"label1"}). | ||
addTimeseries(1, []string{"value1"}). | ||
addInt64Point(0, 3, 2).build(), | ||
}, | ||
out: []*metricspb.Metric{ | ||
metricBuilder().setName("metric1"). | ||
setDataType(metricspb.MetricDescriptor_CUMULATIVE_INT64). | ||
setLabels([]string{"label1"}). | ||
addTimeseries(1, []string{"value1"}). | ||
addInt64Point(0, 3, 2).build(), | ||
}, | ||
}, | ||
{ | ||
name: "convert_resource_attributes_to_labels_empty_list_config", | ||
transforms: []internalTransform{ | ||
{ | ||
MetricName: "all_metrics", | ||
Action: Update, | ||
Operations: []internalOperation{ | ||
{ | ||
configOperation: Operation{ | ||
Action: ConvertResourceAttributesToLabels, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
in: []*metricspb.Metric{ | ||
metricBuilderWithResource().setName("metric1"). | ||
setDataType(metricspb.MetricDescriptor_CUMULATIVE_INT64). | ||
setResourceAttributes(map[string]string{"label1": "value1"}). | ||
addTimeseries(1, []string{}). | ||
addInt64Point(0, 3, 2).build(), | ||
}, | ||
out: []*metricspb.Metric{ | ||
metricBuilder().setName("metric1"). | ||
setDataType(metricspb.MetricDescriptor_CUMULATIVE_INT64). | ||
setLabels([]string{"label1"}). | ||
addTimeseries(1, []string{"value1"}). | ||
addInt64Point(0, 3, 2).build(), | ||
}, | ||
}, | ||
{ | ||
name: "convert_resource_attributes_to_labels_with_config_value", | ||
transforms: []internalTransform{ | ||
{ | ||
MetricName: "all_metrics", | ||
Action: Update, | ||
Operations: []internalOperation{ | ||
{ | ||
configOperation: Operation{ | ||
Action: ConvertResourceAttributesToLabels, | ||
ResourceAttributes: []string{"label1"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
in: []*metricspb.Metric{ | ||
metricBuilderWithResource().setName("metric1"). | ||
setDataType(metricspb.MetricDescriptor_CUMULATIVE_INT64). | ||
setResourceAttributes(map[string]string{"label1": "value1"}). | ||
addTimeseries(1, []string{}). | ||
addInt64Point(0, 3, 2).build(), | ||
}, | ||
out: []*metricspb.Metric{ | ||
metricBuilder().setName("metric1"). | ||
setDataType(metricspb.MetricDescriptor_CUMULATIVE_INT64). | ||
setLabels([]string{"label1"}). | ||
addTimeseries(1, []string{"value1"}). | ||
addInt64Point(0, 3, 2).build(), | ||
}, | ||
}, | ||
// INSERT | ||
{ | ||
name: "metric_name_insert", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2020 OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package metricstransformprocessor | ||
|
||
import ( | ||
metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1" | ||
) | ||
|
||
func (mtp *metricsTransformProcessor) convertResourceAttributesToLabels(metric *metricspb.Metric, resourceAttributes map[string]string, op internalOperation) { | ||
expectedLabelMap := make(map[string]string) | ||
|
||
if len(op.configOperation.ResourceAttributes) > 0 { | ||
for _, attribute := range op.configOperation.ResourceAttributes { | ||
attributeValue, ok := resourceAttributes[attribute] | ||
if ok { | ||
expectedLabelMap[attribute] = attributeValue | ||
} | ||
} | ||
} else { | ||
expectedLabelMap = resourceAttributes | ||
} | ||
|
||
for key, value := range expectedLabelMap { | ||
lablelKey := &metricspb.LabelKey{ | ||
Key: key, | ||
} | ||
labelValue := &metricspb.LabelValue{ | ||
Value: value, | ||
HasValue: true, | ||
} | ||
|
||
metric.MetricDescriptor.LabelKeys = append(metric.MetricDescriptor.LabelKeys, lablelKey) | ||
for _, ts := range metric.Timeseries { | ||
ts.LabelValues = append(ts.LabelValues, labelValue) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Can you just implement it for all operations in this PR? Or create a separate PR for just the all_metrics change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For most of the update operations, it should work fine. I already did some manual testing. Need more unit tests to verify it works fine for the operations we are claiming. Separate PR would make the process faster. Here is the issue I created for tracking- #1342 . Also, for the aggregation operations, I don't know how it will work. That's why planned for a separate PR.