-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[exporter/debug] format spans as one-liners in
normal
verbosity (#1…
…0280) #### 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]>
- Loading branch information
1 parent
ba22562
commit 3364ba1
Showing
7 changed files
with
157 additions
and
20 deletions.
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
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: [] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package normal // import "go.opentelemetry.io/collector/exporter/debugexporter/internal/normal" | ||
|
||
import ( | ||
"fmt" | ||
|
||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
) | ||
|
||
// writeAttributes returns a slice of strings in the form "attrKey=attrValue" | ||
func writeAttributes(attributes pcommon.Map) (attributeStrings []string) { | ||
attributes.Range(func(k string, v pcommon.Value) bool { | ||
attribute := fmt.Sprintf("%s=%s", k, v.AsString()) | ||
attributeStrings = append(attributeStrings, attribute) | ||
return true | ||
}) | ||
return attributeStrings | ||
} |
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,51 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package normal // import "go.opentelemetry.io/collector/exporter/debugexporter/internal/normal" | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
|
||
"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()) | ||
|
||
if span.Attributes().Len() > 0 { | ||
spanAttributes := writeAttributes(span.Attributes()) | ||
buffer.WriteString(" ") | ||
buffer.WriteString(strings.Join(spanAttributes, " ")) | ||
} | ||
|
||
buffer.WriteString("\n") | ||
} | ||
} | ||
} | ||
return buffer.Bytes(), nil | ||
} |
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,48 @@ | ||
// 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}) | ||
span.Attributes().PutStr("key1", "value1") | ||
span.Attributes().PutStr("key2", "value2") | ||
return traces | ||
}(), | ||
expected: `span-name 0102030405060708090a0b0c0d0e0f10 1112131415161718 key1=value1 key2=value2 | ||
`, | ||
}, | ||
} | ||
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)) | ||
}) | ||
} | ||
} |