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

[snmp] add scale factor in snmp metrics #11438

Merged
merged 14 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from 10 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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type SymbolConfig struct {
MatchPattern string `yaml:"match_pattern"`
MatchValue string `yaml:"match_value"`
MatchPatternCompiled *regexp.Regexp

ScaleFactor float64 `yaml:"scale_factor"`
AlexandreYang marked this conversation as resolved.
Show resolved Hide resolved
}

// MetricTagConfig holds metric tag info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ metrics:
- symbol:
OID: 1.3.6.1.4.1.318.1.1.1.11.1.1.0
name: upsBasicStateOutputState
scale_factor: 10
forced_type: flag_stream
options:
placement: 5
Expand All @@ -58,6 +59,7 @@ metrics:
name: ifInErrors
- OID: 1.3.6.1.2.1.2.2.1.20
name: ifOutErrors
scale_factor: 0.5
metric_tags:
- tag: if_index
index: 1
Expand Down Expand Up @@ -137,11 +139,11 @@ bulk_max_repetitions: 20
{symbolTag: "mytag1"},
{symbolTag: "mytag2"},
}},
{Symbol: SymbolConfig{OID: "1.3.6.1.4.1.318.1.1.1.11.1.1.0", Name: "upsBasicStateOutputState"}, ForcedType: "flag_stream", Options: MetricsConfigOption{Placement: 5, MetricSuffix: "ReplaceBattery"}},
{Symbol: SymbolConfig{OID: "1.3.6.1.4.1.318.1.1.1.11.1.1.0", Name: "upsBasicStateOutputState", ScaleFactor: 10}, ForcedType: "flag_stream", Options: MetricsConfigOption{Placement: 5, MetricSuffix: "ReplaceBattery"}},
{
Symbols: []SymbolConfig{
{OID: "1.3.6.1.2.1.2.2.1.14", Name: "ifInErrors"},
{OID: "1.3.6.1.2.1.2.2.1.20", Name: "ifOutErrors"},
{OID: "1.3.6.1.2.1.2.2.1.20", Name: "ifOutErrors", ScaleFactor: 0.5},
},
MetricTags: []MetricTagConfig{
{Tag: "if_index", Index: 1},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func Test_validateEnrichMetrics(t *testing.T) {
},
},
expectedErrors: []string{
"column symbols [{1.2 abc <nil> <nil>}] doesn't have a 'metric_tags' section",
"column symbols [{1.2 abc <nil> <nil> 0}] doesn't have a 'metric_tags' section",
},
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ func (ms *MetricSender) sendBandwidthUsageMetric(symbol checkconfig.SymbolConfig
}
usageValue := ((octetsFloatValue * 8) / (ifHighSpeedFloatValue * (1e6))) * 100.0

ms.sendMetric(usageName+".rate", valuestore.ResultValue{SubmissionType: "counter", Value: usageValue}, tags, "counter", checkconfig.MetricsConfigOption{})
ms.sendMetric(checkconfig.SymbolConfig{Name: usageName + ".rate"}, valuestore.ResultValue{SubmissionType: "counter", Value: usageValue}, tags, checkconfig.MetricsConfig{ForcedType: "counter"})
return nil
}
16 changes: 12 additions & 4 deletions pkg/collector/corechecks/snmp/internal/report/report_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package report

import (
"fmt"

"github.com/DataDog/datadog-agent/pkg/aggregator"
"github.com/DataDog/datadog-agent/pkg/metrics"
"github.com/DataDog/datadog-agent/pkg/util/log"
Expand Down Expand Up @@ -68,7 +69,7 @@ func (ms *MetricSender) reportScalarMetrics(metric checkconfig.MetricsConfig, va

scalarTags := common.CopyStrings(tags)
scalarTags = append(scalarTags, metric.GetSymbolTags()...)
ms.sendMetric(metric.Symbol.Name, value, scalarTags, metric.ForcedType, metric.Options)
ms.sendMetric(metric.Symbol, value, scalarTags, metric)
}

func (ms *MetricSender) reportColumnMetrics(metricConfig checkconfig.MetricsConfig, values *valuestore.ResultValueStore, tags []string) {
Expand All @@ -84,14 +85,15 @@ func (ms *MetricSender) reportColumnMetrics(metricConfig checkconfig.MetricsConf
rowTagsCache[fullIndex] = append(common.CopyStrings(tags), metricConfig.MetricTags.GetTags(fullIndex, values)...)
}
rowTags := rowTagsCache[fullIndex]
ms.sendMetric(symbol.Name, value, rowTags, metricConfig.ForcedType, metricConfig.Options)
ms.sendMetric(symbol, value, rowTags, metricConfig)
ms.trySendBandwidthUsageMetric(symbol, fullIndex, values, rowTags)
}
}
}

func (ms *MetricSender) sendMetric(metricName string, value valuestore.ResultValue, tags []string, forcedType string, options checkconfig.MetricsConfigOption) {
metricFullName := "snmp." + metricName
func (ms *MetricSender) sendMetric(symbol checkconfig.SymbolConfig, value valuestore.ResultValue, tags []string, metricConfig checkconfig.MetricsConfig) {
metricFullName := "snmp." + symbol.Name
forcedType := metricConfig.ForcedType
if forcedType == "" {
if value.SubmissionType != "" {
forcedType = value.SubmissionType
Expand All @@ -104,6 +106,7 @@ func (ms *MetricSender) sendMetric(metricName string, value valuestore.ResultVal
log.Debugf("error converting value (%#v) to string : %v", value, err)
return
}
options := metricConfig.Options
floatValue, err := getFlagStreamValue(options.Placement, strValue)
if err != nil {
log.Debugf("metric `%s`: failed to get flag stream value: %s", metricFullName, err)
Expand All @@ -120,6 +123,11 @@ func (ms *MetricSender) sendMetric(metricName string, value valuestore.ResultVal
return
}

scaleFactor := symbol.ScaleFactor
if scaleFactor != 0 {
floatValue *= scaleFactor
}

switch forcedType {
case "gauge":
ms.Gauge(metricFullName, floatValue, tags)
Expand Down
Loading