-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sumologicexporter: add carbon formatter (#2562)
Signed-off-by: Dominik Rosiek <drosiek@sumologic.com>
- Loading branch information
1 parent
90b3fb1
commit 5efd35d
Showing
6 changed files
with
251 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright 2021, 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 sumologicexporter | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"go.opentelemetry.io/collector/consumer/pdata" | ||
tracetranslator "go.opentelemetry.io/collector/translator/trace" | ||
) | ||
|
||
// carbon2TagString returns all attributes as space spearated key=value pairs. | ||
// In addition, metric name and unit are also included. | ||
// In case `metric` or `unit` attributes has been set too, they are prefixed | ||
// with underscore `_` to avoid overwriting the metric name and unit. | ||
func carbon2TagString(record metricPair) string { | ||
length := record.attributes.Len() | ||
|
||
if _, ok := record.attributes.Get("metric"); ok { | ||
length++ | ||
} | ||
|
||
if _, ok := record.attributes.Get("unit"); ok && len(record.metric.Unit()) > 0 { | ||
length++ | ||
} | ||
|
||
returnValue := make([]string, 0, length) | ||
record.attributes.ForEach(func(k string, v pdata.AttributeValue) { | ||
if k == "name" || k == "unit" { | ||
k = fmt.Sprintf("_%s", k) | ||
} | ||
returnValue = append(returnValue, fmt.Sprintf("%s=%s", k, tracetranslator.AttributeValueToString(v, false))) | ||
}) | ||
|
||
returnValue = append(returnValue, fmt.Sprintf("metric=%s", record.metric.Name())) | ||
|
||
if len(record.metric.Unit()) > 0 { | ||
returnValue = append(returnValue, fmt.Sprintf("unit=%s", record.metric.Unit())) | ||
} | ||
|
||
return strings.Join(returnValue, " ") | ||
} | ||
|
||
// carbon2IntRecord converts IntDataPoint to carbon2 metric string | ||
// with additional information from metricPair. | ||
func carbon2IntRecord(record metricPair, dataPoint pdata.IntDataPoint) string { | ||
return fmt.Sprintf("%s %d %d", | ||
carbon2TagString(record), | ||
dataPoint.Value(), | ||
dataPoint.Timestamp()/1e9, | ||
) | ||
} | ||
|
||
// carbon2DoubleRecord converts DoubleDataPoint to carbon2 metric string | ||
// with additional information from metricPair. | ||
func carbon2DoubleRecord(record metricPair, dataPoint pdata.DoubleDataPoint) string { | ||
return fmt.Sprintf("%s %g %d", | ||
carbon2TagString(record), | ||
dataPoint.Value(), | ||
dataPoint.Timestamp()/1e9, | ||
) | ||
} | ||
|
||
// carbon2metric2String converts metric to Carbon2 formatted string. | ||
func carbon2Metric2String(record metricPair) string { | ||
var nextLines []string | ||
|
||
switch record.metric.DataType() { | ||
case pdata.MetricDataTypeIntGauge: | ||
dps := record.metric.IntGauge().DataPoints() | ||
nextLines = make([]string, 0, dps.Len()) | ||
for i := 0; i < dps.Len(); i++ { | ||
nextLines = append(nextLines, carbon2IntRecord(record, dps.At(i))) | ||
} | ||
case pdata.MetricDataTypeIntSum: | ||
dps := record.metric.IntSum().DataPoints() | ||
nextLines = make([]string, 0, dps.Len()) | ||
for i := 0; i < dps.Len(); i++ { | ||
nextLines = append(nextLines, carbon2IntRecord(record, dps.At(i))) | ||
} | ||
case pdata.MetricDataTypeDoubleGauge: | ||
dps := record.metric.DoubleGauge().DataPoints() | ||
nextLines = make([]string, 0, dps.Len()) | ||
for i := 0; i < dps.Len(); i++ { | ||
nextLines = append(nextLines, carbon2DoubleRecord(record, dps.At(i))) | ||
} | ||
case pdata.MetricDataTypeDoubleSum: | ||
dps := record.metric.DoubleSum().DataPoints() | ||
nextLines = make([]string, 0, dps.Len()) | ||
for i := 0; i < dps.Len(); i++ { | ||
nextLines = append(nextLines, carbon2DoubleRecord(record, dps.At(i))) | ||
} | ||
// Skip complex metrics | ||
case pdata.MetricDataTypeDoubleHistogram: | ||
case pdata.MetricDataTypeIntHistogram: | ||
case pdata.MetricDataTypeDoubleSummary: | ||
} | ||
|
||
return strings.Join(nextLines, "\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2021, 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 sumologicexporter | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCarbon2TagString(t *testing.T) { | ||
metric := exampleIntMetric() | ||
data := carbon2TagString(metric) | ||
assert.Equal(t, "test=test_value test2=second_value metric=test.metric.data unit=bytes", data) | ||
|
||
metric = exampleIntGaugeMetric() | ||
data = carbon2TagString(metric) | ||
assert.Equal(t, "foo=bar metric=gauge_metric_name", data) | ||
|
||
metric = exampleDoubleSumMetric() | ||
data = carbon2TagString(metric) | ||
assert.Equal(t, "foo=bar metric=sum_metric_double_test", data) | ||
|
||
metric = exampleDoubleGaugeMetric() | ||
data = carbon2TagString(metric) | ||
assert.Equal(t, "foo=bar metric=gauge_metric_name_double_test", data) | ||
} | ||
|
||
func TestCarbonMetricDataTypeIntGauge(t *testing.T) { | ||
metric := exampleIntGaugeMetric() | ||
|
||
result := carbon2Metric2String(metric) | ||
expected := `foo=bar metric=gauge_metric_name 124 1608124661 | ||
foo=bar metric=gauge_metric_name 245 1608124662` | ||
assert.Equal(t, expected, result) | ||
} | ||
|
||
func TestCarbonMetricDataTypeDoubleGauge(t *testing.T) { | ||
metric := exampleDoubleGaugeMetric() | ||
|
||
result := carbon2Metric2String(metric) | ||
expected := `foo=bar metric=gauge_metric_name_double_test 33.4 1608124661 | ||
foo=bar metric=gauge_metric_name_double_test 56.8 1608124662` | ||
assert.Equal(t, expected, result) | ||
} | ||
|
||
func TestCarbonMetricDataTypeIntSum(t *testing.T) { | ||
metric := exampleIntSumMetric() | ||
|
||
result := carbon2Metric2String(metric) | ||
expected := `foo=bar metric=sum_metric_int_test 45 1608124444 | ||
foo=bar metric=sum_metric_int_test 1238 1608124699` | ||
assert.Equal(t, expected, result) | ||
} | ||
|
||
func TestCarbonMetricDataTypeDoubleSum(t *testing.T) { | ||
metric := exampleDoubleSumMetric() | ||
|
||
result := carbon2Metric2String(metric) | ||
expected := `foo=bar metric=sum_metric_double_test 45.6 1618124444 | ||
foo=bar metric=sum_metric_double_test 1238.1 1608424699` | ||
assert.Equal(t, expected, result) | ||
} | ||
|
||
func TestCarbonMetricDataTypeDoubleSummary(t *testing.T) { | ||
metric := exampleDoubleSummaryMetric() | ||
|
||
result := carbon2Metric2String(metric) | ||
expected := `` | ||
assert.Equal(t, expected, result) | ||
} | ||
|
||
func TestCarbonMetricDataTypeIntHistogram(t *testing.T) { | ||
metric := exampleIntHistogramMetric() | ||
|
||
result := carbon2Metric2String(metric) | ||
expected := `` | ||
assert.Equal(t, expected, result) | ||
} | ||
|
||
func TestCarbonMetricDataTypeDoubleHistogram(t *testing.T) { | ||
metric := exampleDoubleHistogramMetric() | ||
|
||
result := carbon2Metric2String(metric) | ||
expected := `` | ||
assert.Equal(t, expected, result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters