Skip to content

Commit 3364ba1

Browse files
[exporter/debug] format spans as one-liners in normal verbosity (#10280)
#### Description This is an initial barebones implementation that only outputs the span's name, trace ID and span ID. Other useful fields like duration etc. can be added in follow-up enhancements. This pull request is part of #7806; it implements the change for traces. The changes for [logs](#10225) and metrics will be proposed in separate pull requests. This change applies to the Debug exporter only. The behavior of the Logging exporter remains unchanged. To use this behavior, switch from the deprecated Logging exporter to Debug exporter. #### Link to tracking issue - #7806 #### Testing Added unit tests for the formatter. #### Documentation Described the formatting in the Debug exporter's README. --------- Co-authored-by: Pablo Baeyens <[email protected]>
1 parent ba22562 commit 3364ba1

File tree

7 files changed

+157
-20
lines changed

7 files changed

+157
-20
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: exporter/debug
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: In `normal` verbosity, display one line of text for each span
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [7806]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: []

exporter/debugexporter/README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,22 @@ Here's an example output:
6363

6464
### Normal verbosity
6565

66-
With `verbosity: normal`, the exporter outputs about one line for each telemetry record, including its body and attributes.
66+
With `verbosity: normal`, the exporter outputs about one line for each telemetry record.
6767
The "one line per telemetry record" is not a strict rule.
6868
For example, logs with multiline body will be output as multiple lines.
6969

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

7575
Here's an example output:
7676

7777
```console
78-
2024-05-27T12:46:22.423+0200 info LogsExporter {"kind": "exporter", "data_type": "logs", "name": "debug", "resource logs": 1, "log records": 1}
79-
2024-05-27T12:46:22.423+0200 info the message app=server
80-
{"kind": "exporter", "data_type": "logs", "name": "debug"}
78+
2024-05-31T13:26:37.531+0200 info TracesExporter {"kind": "exporter", "data_type": "traces", "name": "debug", "resource spans": 1, "spans": 2}
79+
2024-05-31T13:26:37.531+0200 info okey-dokey-0 082bc2f70f519e32a39fd26ae69b43c0 51201084f4d65159
80+
lets-go 082bc2f70f519e32a39fd26ae69b43c0 cd321682f3514378
81+
{"kind": "exporter", "data_type": "traces", "name": "debug"}
8182
```
8283

8384
### Detailed verbosity

exporter/debugexporter/exporter.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,28 @@ type debugExporter struct {
3030

3131
func newDebugExporter(logger *zap.Logger, verbosity configtelemetry.Level) *debugExporter {
3232
var logsMarshaler plog.Marshaler
33+
var tracesMarshaler ptrace.Marshaler
3334
if verbosity == configtelemetry.LevelDetailed {
3435
logsMarshaler = otlptext.NewTextLogsMarshaler()
36+
tracesMarshaler = otlptext.NewTextTracesMarshaler()
3537
} else {
3638
logsMarshaler = normal.NewNormalLogsMarshaler()
39+
tracesMarshaler = normal.NewNormalTracesMarshaler()
3740
}
3841
return &debugExporter{
3942
verbosity: verbosity,
4043
logger: logger,
4144
logsMarshaler: logsMarshaler,
4245
metricsMarshaler: otlptext.NewTextMetricsMarshaler(),
43-
tracesMarshaler: otlptext.NewTextTracesMarshaler(),
46+
tracesMarshaler: tracesMarshaler,
4447
}
4548
}
4649

4750
func (s *debugExporter) pushTraces(_ context.Context, td ptrace.Traces) error {
4851
s.logger.Info("TracesExporter",
4952
zap.Int("resource spans", td.ResourceSpans().Len()),
5053
zap.Int("spans", td.SpanCount()))
51-
if s.verbosity != configtelemetry.LevelDetailed {
54+
if s.verbosity == configtelemetry.LevelBasic {
5255
return nil
5356
}
5457

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package normal // import "go.opentelemetry.io/collector/exporter/debugexporter/internal/normal"
5+
6+
import (
7+
"fmt"
8+
9+
"go.opentelemetry.io/collector/pdata/pcommon"
10+
)
11+
12+
// writeAttributes returns a slice of strings in the form "attrKey=attrValue"
13+
func writeAttributes(attributes pcommon.Map) (attributeStrings []string) {
14+
attributes.Range(func(k string, v pcommon.Value) bool {
15+
attribute := fmt.Sprintf("%s=%s", k, v.AsString())
16+
attributeStrings = append(attributeStrings, attribute)
17+
return true
18+
})
19+
return attributeStrings
20+
}

exporter/debugexporter/internal/normal/logs.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99
"strings"
1010

11-
"go.opentelemetry.io/collector/pdata/pcommon"
1211
"go.opentelemetry.io/collector/pdata/plog"
1312
)
1413

@@ -40,13 +39,3 @@ func (normalLogsMarshaler) MarshalLogs(ld plog.Logs) ([]byte, error) {
4039
}
4140
return buffer.Bytes(), nil
4241
}
43-
44-
// writeAttributes returns a slice of strings in the form "attrKey=attrValue"
45-
func writeAttributes(attributes pcommon.Map) (attributeStrings []string) {
46-
attributes.Range(func(k string, v pcommon.Value) bool {
47-
attribute := fmt.Sprintf("%s=%s", k, v.AsString())
48-
attributeStrings = append(attributeStrings, attribute)
49-
return true
50-
})
51-
return attributeStrings
52-
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package normal // import "go.opentelemetry.io/collector/exporter/debugexporter/internal/normal"
5+
6+
import (
7+
"bytes"
8+
"strings"
9+
10+
"go.opentelemetry.io/collector/pdata/ptrace"
11+
)
12+
13+
type normalTracesMarshaler struct{}
14+
15+
// Ensure normalTracesMarshaller implements interface ptrace.Marshaler
16+
var _ ptrace.Marshaler = normalTracesMarshaler{}
17+
18+
// NewNormalTracesMarshaler returns a ptrace.Marshaler for normal verbosity. It writes one line of text per log record
19+
func NewNormalTracesMarshaler() ptrace.Marshaler {
20+
return normalTracesMarshaler{}
21+
}
22+
23+
func (normalTracesMarshaler) MarshalTraces(md ptrace.Traces) ([]byte, error) {
24+
var buffer bytes.Buffer
25+
for i := 0; i < md.ResourceSpans().Len(); i++ {
26+
resourceTraces := md.ResourceSpans().At(i)
27+
for j := 0; j < resourceTraces.ScopeSpans().Len(); j++ {
28+
scopeTraces := resourceTraces.ScopeSpans().At(j)
29+
for k := 0; k < scopeTraces.Spans().Len(); k++ {
30+
span := scopeTraces.Spans().At(k)
31+
32+
buffer.WriteString(span.Name())
33+
34+
buffer.WriteString(" ")
35+
buffer.WriteString(span.TraceID().String())
36+
37+
buffer.WriteString(" ")
38+
buffer.WriteString(span.SpanID().String())
39+
40+
if span.Attributes().Len() > 0 {
41+
spanAttributes := writeAttributes(span.Attributes())
42+
buffer.WriteString(" ")
43+
buffer.WriteString(strings.Join(spanAttributes, " "))
44+
}
45+
46+
buffer.WriteString("\n")
47+
}
48+
}
49+
}
50+
return buffer.Bytes(), nil
51+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package normal
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
11+
"go.opentelemetry.io/collector/pdata/ptrace"
12+
)
13+
14+
func TestMarshalTraces(t *testing.T) {
15+
tests := []struct {
16+
name string
17+
input ptrace.Traces
18+
expected string
19+
}{
20+
{
21+
name: "empty traces",
22+
input: ptrace.NewTraces(),
23+
expected: "",
24+
},
25+
{
26+
name: "one span",
27+
input: func() ptrace.Traces {
28+
traces := ptrace.NewTraces()
29+
span := traces.ResourceSpans().AppendEmpty().ScopeSpans().AppendEmpty().Spans().AppendEmpty()
30+
span.SetName("span-name")
31+
span.SetTraceID([16]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10})
32+
span.SetSpanID([8]byte{0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18})
33+
span.Attributes().PutStr("key1", "value1")
34+
span.Attributes().PutStr("key2", "value2")
35+
return traces
36+
}(),
37+
expected: `span-name 0102030405060708090a0b0c0d0e0f10 1112131415161718 key1=value1 key2=value2
38+
`,
39+
},
40+
}
41+
for _, tt := range tests {
42+
t.Run(tt.name, func(t *testing.T) {
43+
output, err := NewNormalTracesMarshaler().MarshalTraces(tt.input)
44+
assert.NoError(t, err)
45+
assert.Equal(t, tt.expected, string(output))
46+
})
47+
}
48+
}

0 commit comments

Comments
 (0)