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

[metrics_generation_processor] Add metrics generation logic #3433

Merged
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
4 changes: 4 additions & 0 deletions processor/metricsgenerationprocessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ processors:
# Name of the new metric. This is a required field.
- name: <new_metric_name>

# Unit for the new metric being generated.
unit: <new_metric_unit>

# type describes how the new metric will be generated. It can be one of `calculate` or `scale`. calculate generates a metric applying the given operation on two operand metrics. scale operates only on operand1 metric to generate the new metric.
type: {calculate, scale}

Expand Down Expand Up @@ -57,6 +60,7 @@ rules:
# create pod.memory.usage.bytes from pod.memory.usage.megabytes
rules:
- name: pod.memory.usage.bytes
unit: Bytes
type: scale
metric1: pod.memory.usage.megabytes
operation: multiply
Expand Down
3 changes: 3 additions & 0 deletions processor/metricsgenerationprocessor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ type Rule struct {
// Name of the new metric being generated. This is a required field.
Name string `mapstructure:"name"`

// Unit for the new metric being generated.
Unit string `mapstructure:"unit"`

// The rule type following which the new metric will be generated. This is a required field.
Type GenerationType `mapstructure:"type"`

Expand Down
2 changes: 2 additions & 0 deletions processor/metricsgenerationprocessor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ func TestLoadingFullConfig(t *testing.T) {
Rules: []Rule{
{
Name: "new_metric",
Unit: "percent",
Type: "calculate",
Metric1: "metric1",
Metric2: "metric2",
Operation: "percent",
},
{
Name: "new_metric",
Unit: "unit",
Type: "scale",
Metric1: "metric1",
ScaleBy: 1000,
Expand Down
1 change: 1 addition & 0 deletions processor/metricsgenerationprocessor/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func buildInternalConfig(config *Config) []internalRule {
for i, rule := range config.Rules {
customRule := internalRule{
name: rule.Name,
unit: rule.Unit,
ruleType: string(rule.Type),
metric1: rule.Metric1,
metric2: rule.Metric2,
Expand Down
32 changes: 32 additions & 0 deletions processor/metricsgenerationprocessor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var _ processorhelper.MProcessor = (*metricsGenerationProcessor)(nil)

type internalRule struct {
name string
unit string
ruleType string
metric1 string
metric2 string
Expand All @@ -53,6 +54,37 @@ func (mgp *metricsGenerationProcessor) Start(context.Context, component.Host) er

// ProcessMetrics implements the MProcessor interface.
func (mgp *metricsGenerationProcessor) ProcessMetrics(_ context.Context, md pdata.Metrics) (pdata.Metrics, error) {
resourceMetricsSlice := md.ResourceMetrics()

for i := 0; i < resourceMetricsSlice.Len(); i++ {
rm := resourceMetricsSlice.At(i)
nameToMetricMap := getNameToMetricMap(rm)

for _, rule := range mgp.rules {
operand2 := float64(0)
_, ok := nameToMetricMap[rule.metric1]
if !ok {
mgp.logger.Debug("Missing first metric", zap.String("metric_name", rule.metric1))
continue
}

if rule.ruleType == string(calculate) {
metric2, ok := nameToMetricMap[rule.metric2]
if !ok {
mgp.logger.Debug("Missing second metric", zap.String("metric_name", rule.metric2))
continue
}
operand2 = getMetricValue(metric2)
if operand2 <= 0 {
continue
}

} else if rule.ruleType == string(scale) {
operand2 = rule.scaleBy
}
generateMetrics(rm, operand2, rule, mgp.logger)
}
}
return md, nil
}

Expand Down
Loading