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

[splunkhecexporter]Add metric_type as key which maps to the type of the metric #3696

Merged
merged 4 commits into from
Jun 3, 2021
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
6 changes: 3 additions & 3 deletions exporter/splunkhecexporter/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,11 +444,11 @@ func TestReceiveLogs(t *testing.T) {
func TestReceiveMetrics(t *testing.T) {
actual, err := runMetricsExport(true, 3, t)
assert.NoError(t, err)
expected := `{"host":"unknown","event":"metric","fields":{"k/n0":"vn0","k/n1":"vn1","k/r0":"vr0","k/r1":"vr1","k0":"v0","k1":"v1","metric_name:gauge_double_with_dims":1234.5678}}`
expected := `{"host":"unknown","event":"metric","fields":{"k/n0":"vn0","k/n1":"vn1","k/r0":"vr0","k/r1":"vr1","k0":"v0","k1":"v1","metric_name:gauge_double_with_dims":1234.5678,"metric_type":"DoubleGauge"}}`
expected += "\n\r\n\r\n"
expected += `{"time":1.001,"host":"unknown","event":"metric","fields":{"k/n0":"vn0","k/n1":"vn1","k/r0":"vr0","k/r1":"vr1","k0":"v0","k1":"v1","metric_name:gauge_double_with_dims":1234.5678}}`
expected += `{"time":1.001,"host":"unknown","event":"metric","fields":{"k/n0":"vn0","k/n1":"vn1","k/r0":"vr0","k/r1":"vr1","k0":"v0","k1":"v1","metric_name:gauge_double_with_dims":1234.5678,"metric_type":"DoubleGauge"}}`
expected += "\n\r\n\r\n"
expected += `{"time":2.002,"host":"unknown","event":"metric","fields":{"k/n0":"vn0","k/n1":"vn1","k/r0":"vr0","k/r1":"vr1","k0":"v0","k1":"v1","metric_name:gauge_double_with_dims":1234.5678}}`
expected += `{"time":2.002,"host":"unknown","event":"metric","fields":{"k/n0":"vn0","k/n1":"vn1","k/r0":"vr0","k/r1":"vr1","k0":"v0","k1":"v1","metric_name:gauge_double_with_dims":1234.5678,"metric_type":"DoubleGauge"}}`
expected += "\n\r\n\r\n"
assert.Equal(t, expected, actual)
}
Expand Down
18 changes: 17 additions & 1 deletion exporter/splunkhecexporter/metricdata_to_splunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
const (
// unknownHostName is the default host name when no hostname label is passed.
unknownHostName = "unknown"
// splunkMetricTypeKey is the key which maps to the type of the metric.
splunkMetricTypeKey = "metric_type"
// splunkMetricValue is the splunk metric value prefix.
splunkMetricValue = "metric_name"
// countSuffix is the count metric value suffix.
Expand Down Expand Up @@ -89,7 +91,7 @@ func metricDataToSplunk(logger *zap.Logger, data pdata.Metrics, config *Config)
fields := cloneMap(commonFields)
populateLabels(fields, dataPt.LabelsMap())
fields[metricFieldName] = dataPt.Value()

fields[splunkMetricTypeKey] = pdata.MetricDataTypeIntGauge.String()
sm := createEvent(dataPt.Timestamp(), host, source, sourceType, index, fields)
splunkMetrics = append(splunkMetrics, sm)
}
Expand All @@ -100,6 +102,7 @@ func metricDataToSplunk(logger *zap.Logger, data pdata.Metrics, config *Config)
fields := cloneMap(commonFields)
populateLabels(fields, dataPt.LabelsMap())
fields[metricFieldName] = dataPt.Value()
fields[splunkMetricTypeKey] = pdata.MetricDataTypeDoubleGauge.String()
sm := createEvent(dataPt.Timestamp(), host, source, sourceType, index, fields)
splunkMetrics = append(splunkMetrics, sm)
}
Expand All @@ -114,13 +117,15 @@ func metricDataToSplunk(logger *zap.Logger, data pdata.Metrics, config *Config)
fields := cloneMap(commonFields)
populateLabels(fields, dataPt.LabelsMap())
fields[metricFieldName+sumSuffix] = dataPt.Sum()
fields[splunkMetricTypeKey] = pdata.MetricDataTypeHistogram.String()
sm := createEvent(dataPt.Timestamp(), host, source, sourceType, index, fields)
splunkMetrics = append(splunkMetrics, sm)
}
{
fields := cloneMap(commonFields)
populateLabels(fields, dataPt.LabelsMap())
fields[metricFieldName+countSuffix] = dataPt.Count()
fields[splunkMetricTypeKey] = pdata.MetricDataTypeHistogram.String()
sm := createEvent(dataPt.Timestamp(), host, source, sourceType, index, fields)
splunkMetrics = append(splunkMetrics, sm)
}
Expand All @@ -137,6 +142,7 @@ func metricDataToSplunk(logger *zap.Logger, data pdata.Metrics, config *Config)
fields["le"] = float64ToDimValue(bounds[bi])
value += counts[bi]
fields[metricFieldName+bucketSuffix] = value
fields[splunkMetricTypeKey] = pdata.MetricDataTypeHistogram.String()
sm := createEvent(dataPt.Timestamp(), host, source, sourceType, index, fields)
splunkMetrics = append(splunkMetrics, sm)
}
Expand All @@ -146,6 +152,7 @@ func metricDataToSplunk(logger *zap.Logger, data pdata.Metrics, config *Config)
populateLabels(fields, dataPt.LabelsMap())
fields["le"] = float64ToDimValue(math.Inf(1))
fields[metricFieldName+bucketSuffix] = value + counts[len(counts)-1]
fields[splunkMetricTypeKey] = pdata.MetricDataTypeHistogram.String()
sm := createEvent(dataPt.Timestamp(), host, source, sourceType, index, fields)
splunkMetrics = append(splunkMetrics, sm)
}
Expand All @@ -161,13 +168,15 @@ func metricDataToSplunk(logger *zap.Logger, data pdata.Metrics, config *Config)
fields := cloneMap(commonFields)
populateLabels(fields, dataPt.LabelsMap())
fields[metricFieldName+sumSuffix] = dataPt.Sum()
fields[splunkMetricTypeKey] = pdata.MetricDataTypeIntHistogram.String()
sm := createEvent(dataPt.Timestamp(), host, source, sourceType, index, fields)
splunkMetrics = append(splunkMetrics, sm)
}
{
fields := cloneMap(commonFields)
populateLabels(fields, dataPt.LabelsMap())
fields[metricFieldName+countSuffix] = dataPt.Count()
fields[splunkMetricTypeKey] = pdata.MetricDataTypeIntHistogram.String()
sm := createEvent(dataPt.Timestamp(), host, source, sourceType, index, fields)
splunkMetrics = append(splunkMetrics, sm)
}
Expand All @@ -184,6 +193,7 @@ func metricDataToSplunk(logger *zap.Logger, data pdata.Metrics, config *Config)
fields["le"] = float64ToDimValue(bounds[bi])
value += counts[bi]
fields[metricFieldName+bucketSuffix] = value
fields[splunkMetricTypeKey] = pdata.MetricDataTypeIntHistogram.String()
sm := createEvent(dataPt.Timestamp(), host, source, sourceType, index, fields)
splunkMetrics = append(splunkMetrics, sm)
}
Expand All @@ -193,6 +203,7 @@ func metricDataToSplunk(logger *zap.Logger, data pdata.Metrics, config *Config)
populateLabels(fields, dataPt.LabelsMap())
fields["le"] = float64ToDimValue(math.Inf(1))
fields[metricFieldName+bucketSuffix] = value + counts[len(counts)-1]
fields[splunkMetricTypeKey] = pdata.MetricDataTypeIntHistogram.String()
sm := createEvent(dataPt.Timestamp(), host, source, sourceType, index, fields)
splunkMetrics = append(splunkMetrics, sm)
}
Expand All @@ -204,6 +215,7 @@ func metricDataToSplunk(logger *zap.Logger, data pdata.Metrics, config *Config)
fields := cloneMap(commonFields)
populateLabels(fields, dataPt.LabelsMap())
fields[metricFieldName] = dataPt.Value()
fields[splunkMetricTypeKey] = pdata.MetricDataTypeDoubleSum.String()

sm := createEvent(dataPt.Timestamp(), host, source, sourceType, index, fields)
splunkMetrics = append(splunkMetrics, sm)
Expand All @@ -215,6 +227,7 @@ func metricDataToSplunk(logger *zap.Logger, data pdata.Metrics, config *Config)
fields := cloneMap(commonFields)
populateLabels(fields, dataPt.LabelsMap())
fields[metricFieldName] = dataPt.Value()
fields[splunkMetricTypeKey] = pdata.MetricDataTypeIntSum.String()

sm := createEvent(dataPt.Timestamp(), host, source, sourceType, index, fields)
splunkMetrics = append(splunkMetrics, sm)
Expand All @@ -228,13 +241,15 @@ func metricDataToSplunk(logger *zap.Logger, data pdata.Metrics, config *Config)
fields := cloneMap(commonFields)
populateLabels(fields, dataPt.LabelsMap())
fields[metricFieldName+sumSuffix] = dataPt.Sum()
fields[splunkMetricTypeKey] = pdata.MetricDataTypeSummary.String()
sm := createEvent(dataPt.Timestamp(), host, source, sourceType, index, fields)
splunkMetrics = append(splunkMetrics, sm)
}
{
fields := cloneMap(commonFields)
populateLabels(fields, dataPt.LabelsMap())
fields[metricFieldName+countSuffix] = dataPt.Count()
fields[splunkMetricTypeKey] = pdata.MetricDataTypeSummary.String()
sm := createEvent(dataPt.Timestamp(), host, source, sourceType, index, fields)
splunkMetrics = append(splunkMetrics, sm)
}
Expand All @@ -246,6 +261,7 @@ func metricDataToSplunk(logger *zap.Logger, data pdata.Metrics, config *Config)
dp := dataPt.QuantileValues().At(bi)
fields["qt"] = float64ToDimValue(dp.Quantile())
fields[metricFieldName+"_"+strconv.FormatFloat(dp.Quantile(), 'f', -1, 64)] = dp.Value()
fields[splunkMetricTypeKey] = pdata.MetricDataTypeSummary.String()
sm := createEvent(dataPt.Timestamp(), host, source, sourceType, index, fields)
splunkMetrics = append(splunkMetrics, sm)
}
Expand Down
22 changes: 20 additions & 2 deletions exporter/splunkhecexporter/metricdata_to_splunk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ func Test_metricDataToSplunk(t *testing.T) {
return metrics
},
wantSplunkMetrics: []*splunk.Event{
commonSplunkMetric("gauge_double_with_dims", tsMSecs, []string{"com.splunk.index", "com.splunk.sourcetype", "host.name", "service.name", "k0", "k1"}, []interface{}{"myindex", "mysourcetype", "myhost", "mysource", "v0", "v1"}, doubleVal, "mysource", "mysourcetype", "myindex", "myhost"),
commonSplunkMetric("gauge_int_with_dims", tsMSecs, []string{"com.splunk.index", "com.splunk.sourcetype", "host.name", "service.name", "k0", "k1"}, []interface{}{"myindex", "mysourcetype", "myhost", "mysource", "v0", "v1"}, int64Val, "mysource", "mysourcetype", "myindex", "myhost"),
commonSplunkMetric("gauge_double_with_dims", tsMSecs, []string{"com.splunk.index", "com.splunk.sourcetype", "host.name", "service.name", "k0", "k1", "metric_type"}, []interface{}{"myindex", "mysourcetype", "myhost", "mysource", "v0", "v1", "DoubleGauge"}, doubleVal, "mysource", "mysourcetype", "myindex", "myhost"),
commonSplunkMetric("gauge_int_with_dims", tsMSecs, []string{"com.splunk.index", "com.splunk.sourcetype", "host.name", "service.name", "k0", "k1", "metric_type"}, []interface{}{"myindex", "mysourcetype", "myhost", "mysource", "v0", "v1", "IntGauge"}, int64Val, "mysource", "mysourcetype", "myindex", "myhost"),
},
},

Expand Down Expand Up @@ -290,6 +290,7 @@ func Test_metricDataToSplunk(t *testing.T) {
"k0": "v0",
"k1": "v1",
"metric_name:double_histogram_with_dims_sum": float64(23),
"metric_type": "Histogram",
},
},
{
Expand All @@ -302,6 +303,7 @@ func Test_metricDataToSplunk(t *testing.T) {
"k0": "v0",
"k1": "v1",
"metric_name:double_histogram_with_dims_count": uint64(7),
"metric_type": "Histogram",
},
},
{
Expand All @@ -315,6 +317,7 @@ func Test_metricDataToSplunk(t *testing.T) {
"k1": "v1",
"le": "1",
"metric_name:double_histogram_with_dims_bucket": uint64(4),
"metric_type": "Histogram",
},
},
{
Expand All @@ -328,6 +331,7 @@ func Test_metricDataToSplunk(t *testing.T) {
"k1": "v1",
"le": "2",
"metric_name:double_histogram_with_dims_bucket": uint64(6),
"metric_type": "Histogram",
},
},
{
Expand All @@ -341,6 +345,7 @@ func Test_metricDataToSplunk(t *testing.T) {
"k1": "v1",
"le": "4",
"metric_name:double_histogram_with_dims_bucket": uint64(9),
"metric_type": "Histogram",
},
},
{
Expand All @@ -354,6 +359,7 @@ func Test_metricDataToSplunk(t *testing.T) {
"k1": "v1",
"le": "+Inf",
"metric_name:double_histogram_with_dims_bucket": uint64(14),
"metric_type": "Histogram",
},
},
},
Expand Down Expand Up @@ -403,6 +409,7 @@ func Test_metricDataToSplunk(t *testing.T) {
"k0": "v0",
"k1": "v1",
"metric_name:int_histogram_with_dims_sum": int64(23),
"metric_type": "IntHistogram",
},
},
{
Expand All @@ -415,6 +422,7 @@ func Test_metricDataToSplunk(t *testing.T) {
"k0": "v0",
"k1": "v1",
"metric_name:int_histogram_with_dims_count": uint64(7),
"metric_type": "IntHistogram",
},
},
{
Expand All @@ -428,6 +436,7 @@ func Test_metricDataToSplunk(t *testing.T) {
"k1": "v1",
"le": "1",
"metric_name:int_histogram_with_dims_bucket": uint64(4),
"metric_type": "IntHistogram",
},
},
{
Expand All @@ -441,6 +450,7 @@ func Test_metricDataToSplunk(t *testing.T) {
"k1": "v1",
"le": "2",
"metric_name:int_histogram_with_dims_bucket": uint64(6),
"metric_type": "IntHistogram",
},
},
{
Expand All @@ -454,6 +464,7 @@ func Test_metricDataToSplunk(t *testing.T) {
"k1": "v1",
"le": "4",
"metric_name:int_histogram_with_dims_bucket": uint64(9),
"metric_type": "IntHistogram",
},
},
{
Expand All @@ -467,6 +478,7 @@ func Test_metricDataToSplunk(t *testing.T) {
"k1": "v1",
"le": "+Inf",
"metric_name:int_histogram_with_dims_bucket": uint64(14),
"metric_type": "IntHistogram",
},
},
},
Expand Down Expand Up @@ -495,6 +507,7 @@ func Test_metricDataToSplunk(t *testing.T) {
"k0": "v0",
"k1": "v1",
"metric_name:int_sum_with_dims": int64(62),
"metric_type": "IntSum",
},
},
},
Expand Down Expand Up @@ -523,6 +536,7 @@ func Test_metricDataToSplunk(t *testing.T) {
"k0": "v0",
"k1": "v1",
"metric_name:double_sum_with_dims": float64(62),
"metric_type": "DoubleSum",
},
},
},
Expand Down Expand Up @@ -559,6 +573,7 @@ func Test_metricDataToSplunk(t *testing.T) {
"k0": "v0",
"k1": "v1",
"metric_name:summary_sum": float64(42),
"metric_type": "Summary",
},
},
{
Expand All @@ -571,6 +586,7 @@ func Test_metricDataToSplunk(t *testing.T) {
"k0": "v0",
"k1": "v1",
"metric_name:summary_count": uint64(2),
"metric_type": "Summary",
},
},
{
Expand All @@ -584,6 +600,7 @@ func Test_metricDataToSplunk(t *testing.T) {
"k1": "v1",
"qt": "0.5",
"metric_name:summary_0.5": float64(34),
"metric_type": "Summary",
},
},
{
Expand All @@ -597,6 +614,7 @@ func Test_metricDataToSplunk(t *testing.T) {
"k1": "v1",
"qt": "0.6",
"metric_name:summary_0.6": float64(45),
"metric_type": "Summary",
},
},
},
Expand Down