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

[exporter/debug] format metric data points as one-liners in normal verbosity #10462

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 25 additions & 0 deletions .chloggen/debug-exporter-normal-verbosity-metrics.yaml
mx-psi marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: exporter/debug

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: In `normal` verbosity, display one line of text for each metric data point

# One or more tracking issues or pull requests related to the change
issues: [7806]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
5 changes: 0 additions & 5 deletions exporter/debugexporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ With `verbosity: normal`, the exporter outputs about one line for each telemetry
The "one line per telemetry record" is not a strict rule.
For example, logs with multiline body will be output as multiple lines.

> [!IMPORTANT]
> Currently the `normal` verbosity is only implemented for logs and traces.
> Metrics are going to be implemented in the future.
> The current behavior for metrics is the same as in `basic` verbosity.

Here's an example output:

```console
Expand Down
7 changes: 5 additions & 2 deletions exporter/debugexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,22 @@ type debugExporter struct {

func newDebugExporter(logger *zap.Logger, verbosity configtelemetry.Level) *debugExporter {
var logsMarshaler plog.Marshaler
var metricsMarshaler pmetric.Marshaler
var tracesMarshaler ptrace.Marshaler
if verbosity == configtelemetry.LevelDetailed {
logsMarshaler = otlptext.NewTextLogsMarshaler()
metricsMarshaler = otlptext.NewTextMetricsMarshaler()
tracesMarshaler = otlptext.NewTextTracesMarshaler()
} else {
logsMarshaler = normal.NewNormalLogsMarshaler()
metricsMarshaler = normal.NewNormalMetricsMarshaler()
tracesMarshaler = normal.NewNormalTracesMarshaler()
}
return &debugExporter{
verbosity: verbosity,
logger: logger,
logsMarshaler: logsMarshaler,
metricsMarshaler: otlptext.NewTextMetricsMarshaler(),
metricsMarshaler: metricsMarshaler,
tracesMarshaler: tracesMarshaler,
}
}
Expand All @@ -68,7 +71,7 @@ func (s *debugExporter) pushMetrics(_ context.Context, md pmetric.Metrics) error
zap.Int("resource metrics", md.ResourceMetrics().Len()),
zap.Int("metrics", md.MetricCount()),
zap.Int("data points", md.DataPointCount()))
if s.verbosity != configtelemetry.LevelDetailed {
if s.verbosity == configtelemetry.LevelBasic {
return nil
}

Expand Down
149 changes: 149 additions & 0 deletions exporter/debugexporter/internal/normal/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package normal

import (
"bytes"
"fmt"
"strings"

"go.opentelemetry.io/collector/pdata/pmetric"
)

type normalMetricsMarshaler struct{}

// Ensure normalMetricsMarshaller implements interface pmetric.Marshaler
var _ pmetric.Marshaler = normalMetricsMarshaler{}

// NewNormalMetricsMarshaler returns a pmetric.Marshaler for normal verbosity. It writes one line of text per log record
func NewNormalMetricsMarshaler() pmetric.Marshaler {
return normalMetricsMarshaler{}
}

func (normalMetricsMarshaler) MarshalMetrics(md pmetric.Metrics) ([]byte, error) {
var buffer bytes.Buffer
for i := 0; i < md.ResourceMetrics().Len(); i++ {
resourceMetrics := md.ResourceMetrics().At(i)
for j := 0; j < resourceMetrics.ScopeMetrics().Len(); j++ {
scopeMetrics := resourceMetrics.ScopeMetrics().At(j)
for k := 0; k < scopeMetrics.Metrics().Len(); k++ {
metric := scopeMetrics.Metrics().At(k)

var dataPointLines []string
switch metric.Type() {
case pmetric.MetricTypeGauge:
dataPointLines = writeNumberDataPoints(metric, metric.Gauge().DataPoints())
case pmetric.MetricTypeSum:
dataPointLines = writeNumberDataPoints(metric, metric.Sum().DataPoints())
case pmetric.MetricTypeHistogram:
dataPointLines = writeHistogramDataPoints(metric)
case pmetric.MetricTypeExponentialHistogram:
dataPointLines = writeExponentialHistogramDataPoints(metric)
case pmetric.MetricTypeSummary:
dataPointLines = writeSummaryDataPoints(metric)
}
for _, line := range dataPointLines {
buffer.WriteString(line)
}
}
}
}
return buffer.Bytes(), nil
}

func writeNumberDataPoints(metric pmetric.Metric, dataPoints pmetric.NumberDataPointSlice) (lines []string) {
for i := 0; i < dataPoints.Len(); i++ {
dataPoint := dataPoints.At(i)
dataPointAttributes := writeAttributes(dataPoint.Attributes())

var value string
switch dataPoint.ValueType() {
case pmetric.NumberDataPointValueTypeInt:
value = fmt.Sprintf("%v", dataPoint.IntValue())

Check warning on line 63 in exporter/debugexporter/internal/normal/metrics.go

View check run for this annotation

Codecov / codecov/patch

exporter/debugexporter/internal/normal/metrics.go#L62-L63

Added lines #L62 - L63 were not covered by tests
case pmetric.NumberDataPointValueTypeDouble:
value = fmt.Sprintf("%v", dataPoint.DoubleValue())
}

dataPointLine := fmt.Sprintf("%s{%s} %s\n", metric.Name(), strings.Join(dataPointAttributes, ","), value)
lines = append(lines, dataPointLine)
}
return lines
}

func writeHistogramDataPoints(metric pmetric.Metric) (lines []string) {
for i := 0; i < metric.Histogram().DataPoints().Len(); i++ {
dataPoint := metric.Histogram().DataPoints().At(i)
dataPointAttributes := writeAttributes(dataPoint.Attributes())

var value string
value = fmt.Sprintf("count=%d", dataPoint.Count())
if dataPoint.HasSum() {
value += fmt.Sprintf(" sum=%v", dataPoint.Sum())
}
if dataPoint.HasMin() {
value += fmt.Sprintf(" min=%v", dataPoint.Min())
}
if dataPoint.HasMax() {
value += fmt.Sprintf(" max=%v", dataPoint.Max())
}

for bucketIndex := 0; bucketIndex < dataPoint.BucketCounts().Len(); bucketIndex++ {
bucketBound := ""
if bucketIndex < dataPoint.ExplicitBounds().Len() {
bucketBound = fmt.Sprintf("le%v=", dataPoint.ExplicitBounds().At(bucketIndex))
}
bucketCount := dataPoint.BucketCounts().At(bucketIndex)
value += fmt.Sprintf(" %s%d", bucketBound, bucketCount)
}

dataPointLine := fmt.Sprintf("%s{%s} %s\n", metric.Name(), strings.Join(dataPointAttributes, ","), value)
lines = append(lines, dataPointLine)
}
return lines
}

func writeExponentialHistogramDataPoints(metric pmetric.Metric) (lines []string) {
for i := 0; i < metric.ExponentialHistogram().DataPoints().Len(); i++ {
dataPoint := metric.ExponentialHistogram().DataPoints().At(i)
dataPointAttributes := writeAttributes(dataPoint.Attributes())

var value string
value = fmt.Sprintf("count=%d", dataPoint.Count())
if dataPoint.HasSum() {
value += fmt.Sprintf(" sum=%v", dataPoint.Sum())
}
if dataPoint.HasMin() {
value += fmt.Sprintf(" min=%v", dataPoint.Min())
}
if dataPoint.HasMax() {
value += fmt.Sprintf(" max=%v", dataPoint.Max())
}

// TODO display buckets
mx-psi marked this conversation as resolved.
Show resolved Hide resolved

dataPointLine := fmt.Sprintf("%s{%s} %s\n", metric.Name(), strings.Join(dataPointAttributes, ","), value)
lines = append(lines, dataPointLine)
}
return lines
}

func writeSummaryDataPoints(metric pmetric.Metric) (lines []string) {
for i := 0; i < metric.Summary().DataPoints().Len(); i++ {
dataPoint := metric.Summary().DataPoints().At(i)
dataPointAttributes := writeAttributes(dataPoint.Attributes())

var value string
value = fmt.Sprintf("count=%d", dataPoint.Count())
value += fmt.Sprintf(" sum=%f", dataPoint.Sum())

for quantileIndex := 0; quantileIndex < dataPoint.QuantileValues().Len(); quantileIndex++ {
quantile := dataPoint.QuantileValues().At(quantileIndex)
value += fmt.Sprintf(" q%v=%v", quantile.Quantile(), quantile.Value())
}

dataPointLine := fmt.Sprintf("%s{%s} %s\n", metric.Name(), strings.Join(dataPointAttributes, ","), value)
lines = append(lines, dataPointLine)
}
return lines
}
120 changes: 120 additions & 0 deletions exporter/debugexporter/internal/normal/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package normal

import (
"testing"

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/collector/pdata/pmetric"
)

func TestMarshalMetrics(t *testing.T) {
tests := []struct {
name string
input pmetric.Metrics
expected string
}{
{
name: "empty metrics",
input: pmetric.NewMetrics(),
expected: "",
},
{
name: "sum data point",
input: func() pmetric.Metrics {
metrics := pmetric.NewMetrics()
metric := metrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty()
metric.SetName("system.cpu.time")
dataPoint := metric.SetEmptySum().DataPoints().AppendEmpty()
dataPoint.SetDoubleValue(123.456)
dataPoint.Attributes().PutStr("state", "user")
dataPoint.Attributes().PutStr("cpu", "0")
return metrics
}(),
expected: `system.cpu.time{state=user,cpu=0} 123.456
`,
},
{
name: "gauge data point",
input: func() pmetric.Metrics {
metrics := pmetric.NewMetrics()
metric := metrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty()
metric.SetName("system.cpu.utilization")
dataPoint := metric.SetEmptyGauge().DataPoints().AppendEmpty()
dataPoint.SetDoubleValue(78.901234567)
dataPoint.Attributes().PutStr("state", "free")
dataPoint.Attributes().PutStr("cpu", "8")
return metrics
}(),
expected: `system.cpu.utilization{state=free,cpu=8} 78.901234567
`,
},
{
name: "histogram",
input: func() pmetric.Metrics {
metrics := pmetric.NewMetrics()
metric := metrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty()
metric.SetName("http.server.request.duration")
dataPoint := metric.SetEmptyHistogram().DataPoints().AppendEmpty()
dataPoint.Attributes().PutInt("http.response.status_code", 200)
dataPoint.Attributes().PutStr("http.request.method", "GET")
dataPoint.ExplicitBounds().FromRaw([]float64{0.125, 0.5, 1, 3})
dataPoint.BucketCounts().FromRaw([]uint64{1324, 13, 0, 2, 1})
dataPoint.SetCount(1340)
dataPoint.SetSum(99.573)
dataPoint.SetMin(0.017)
dataPoint.SetMax(8.13)
return metrics
}(),
expected: `http.server.request.duration{http.response.status_code=200,http.request.method=GET} count=1340 sum=99.573 min=0.017 max=8.13 le0.125=1324 le0.5=13 le1=0 le3=2 1
`,
},
{
name: "exponential histogram",
input: func() pmetric.Metrics {
metrics := pmetric.NewMetrics()
metric := metrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty()
metric.SetName("http.server.request.duration")
dataPoint := metric.SetEmptyExponentialHistogram().DataPoints().AppendEmpty()
dataPoint.Attributes().PutInt("http.response.status_code", 200)
dataPoint.Attributes().PutStr("http.request.method", "GET")
dataPoint.SetCount(1340)
dataPoint.SetSum(99.573)
dataPoint.SetMin(0.017)
dataPoint.SetMax(8.13)
return metrics
}(),
expected: `http.server.request.duration{http.response.status_code=200,http.request.method=GET} count=1340 sum=99.573 min=0.017 max=8.13
`,
},
{
name: "summary",
input: func() pmetric.Metrics {
metrics := pmetric.NewMetrics()
metric := metrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty()
metric.SetName("summary")
dataPoint := metric.SetEmptySummary().DataPoints().AppendEmpty()
dataPoint.Attributes().PutInt("http.response.status_code", 200)
dataPoint.Attributes().PutStr("http.request.method", "GET")
dataPoint.SetCount(1340)
dataPoint.SetSum(99.573)
quantile := dataPoint.QuantileValues().AppendEmpty()
quantile.SetQuantile(0.01)
quantile.SetValue(15)
return metrics
}(),
expected: `summary{http.response.status_code=200,http.request.method=GET} count=1340 sum=99.573000 q0.01=15
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
output, err := NewNormalMetricsMarshaler().MarshalMetrics(tt.input)
assert.NoError(t, err)
assert.Equal(t, tt.expected, string(output))
})
}
}
Loading