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 spans as one-liners in normal verbosity #10280

Merged
Show file tree
Hide file tree
Changes from 3 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
25 changes: 25 additions & 0 deletions .chloggen/debug-exporter-normal-verbosity-traces.yaml
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 span

# 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: []
19 changes: 16 additions & 3 deletions exporter/debugexporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,21 @@ Here's an example output:

### Normal verbosity

With `verbosity: normal`, the exporter's behavior is currently the same as with `verbosity: basic`.
See above for more details.
With `verbosity: normal`, the exporter outputs one line for each telemetry record.

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

Here's an example output:

```console
2024-05-31T13:26:37.531+0200 info TracesExporter {"kind": "exporter", "data_type": "traces", "name": "debug", "resource spans": 1, "spans": 2}
2024-05-31T13:26:37.531+0200 info okey-dokey-0 082bc2f70f519e32a39fd26ae69b43c0 51201084f4d65159
lets-go 082bc2f70f519e32a39fd26ae69b43c0 cd321682f3514378
{"kind": "exporter", "data_type": "traces", "name": "debug"}
```

### Detailed verbosity

Expand Down Expand Up @@ -109,4 +122,4 @@ Attributes:

## Warnings

- Unstable Output Format: The output formats for all verbosity levels is not guaranteed and may be changed at any time without a breaking change.
- Unstable Output Format: The output formats for all verbosity levels is not guaranteed and may be changed at any time without a breaking change.
11 changes: 9 additions & 2 deletions exporter/debugexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"go.uber.org/zap"

"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/exporter/debugexporter/internal/normal"
"go.opentelemetry.io/collector/exporter/internal/otlptext"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
Expand All @@ -28,20 +29,26 @@ type debugExporter struct {
}

func newDebugExporter(logger *zap.Logger, verbosity configtelemetry.Level) *debugExporter {
var tracesMarshaler ptrace.Marshaler
if verbosity == configtelemetry.LevelDetailed {
tracesMarshaler = otlptext.NewTextTracesMarshaler()
} else {
tracesMarshaler = normal.NewNormalTracesMarshaler()
}
return &debugExporter{
verbosity: verbosity,
logger: logger,
logsMarshaler: otlptext.NewTextLogsMarshaler(),
metricsMarshaler: otlptext.NewTextMetricsMarshaler(),
tracesMarshaler: otlptext.NewTextTracesMarshaler(),
tracesMarshaler: tracesMarshaler,
}
}

func (s *debugExporter) pushTraces(_ context.Context, td ptrace.Traces) error {
s.logger.Info("TracesExporter",
zap.Int("resource spans", td.ResourceSpans().Len()),
zap.Int("spans", td.SpanCount()))
if s.verbosity != configtelemetry.LevelDetailed {
if s.verbosity == configtelemetry.LevelBasic {
return nil
}

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

package normal

import (
"bytes"

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

type normalTracesMarshaler struct{}

// Ensure normalTracesMarshaller implements interface ptrace.Marshaler
var _ ptrace.Marshaler = normalTracesMarshaler{}

// NewNormalTracesMarshaler returns a ptrace.Marshaler for normal verbosity. It writes one line of text per log record
func NewNormalTracesMarshaler() ptrace.Marshaler {
return normalTracesMarshaler{}
}

func (normalTracesMarshaler) MarshalTraces(md ptrace.Traces) ([]byte, error) {
var buffer bytes.Buffer
for i := 0; i < md.ResourceSpans().Len(); i++ {
resourceTraces := md.ResourceSpans().At(i)
for j := 0; j < resourceTraces.ScopeSpans().Len(); j++ {
scopeTraces := resourceTraces.ScopeSpans().At(j)
for k := 0; k < scopeTraces.Spans().Len(); k++ {
span := scopeTraces.Spans().At(k)
buffer.WriteString(span.Name())
buffer.WriteString(" ")
buffer.WriteString(span.TraceID().String())
buffer.WriteString(" ")
buffer.WriteString(span.SpanID().String())
buffer.WriteString("\n")
}
}
}
return buffer.Bytes(), nil
}
46 changes: 46 additions & 0 deletions exporter/debugexporter/internal/normal/traces_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package normal

import (
"testing"

"github.com/stretchr/testify/assert"

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

func TestMarshalTraces(t *testing.T) {
tests := []struct {
name string
input ptrace.Traces
expected string
}{
{
name: "empty traces",
input: ptrace.NewTraces(),
expected: "",
},
{
name: "one span",
input: func() ptrace.Traces {
traces := ptrace.NewTraces()
span := traces.ResourceSpans().AppendEmpty().ScopeSpans().AppendEmpty().Spans().AppendEmpty()
span.SetName("span-name")
span.SetTraceID([16]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10})
span.SetSpanID([8]byte{0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18})
return traces
}(),
expected: `span-name 0102030405060708090a0b0c0d0e0f10 1112131415161718
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
output, err := NewNormalTracesMarshaler().MarshalTraces(tt.input)
assert.NoError(t, err)
assert.Equal(t, tt.expected, string(output))
})
}
}
Loading