Files
Andrzej StencelandGitHub 7e1ed34bbf [exporter/debug] display resource and scope in normal verbosity (#13011)
#### Description

Adds displaying of resource and scope details in `normal` verbosity.

Each resource is represented with one line showing the resource's
details:
- schema URL
- resource attributes

which looks like `ResourceTraces #0 [resource-schema-url]
resource_attr1=value1 attr2=value2`.

Each instrumentation scope is represented by one line showing the
scope's details:
- scope name
- scope version
- scope schema URL
- scope attributes

which looks like `ScopeTraces #0 scope-name@scope-version
[scope-schema-url] scope_attr1=value1 attr2=value2`.

Before:

```console
2025-05-09T19:57:16.332+0200    info    Traces  {"resource": {}, "otelcol.component.id": "debug/normal", "otelcol.component.kind": "exporter", "otelcol.signal": "traces", "resource spans": 1, "spans": 2}
2025-05-09T19:57:16.332+0200    info    okey-dokey-0 ab1030bd4ee554af936542b01d7b4807 1d8c93663d043aa8 net.sock.peer.addr=1.2.3.4 peer.service=telemetrygen-client
lets-go ab1030bd4ee554af936542b01d7b4807 0d238e8a2f97733f net.sock.peer.addr=1.2.3.4 peer.service=telemetrygen-server
        {"resource": {}, "otelcol.component.id": "debug/normal", "otelcol.component.kind": "exporter", "otelcol.signal": "traces"}
```

After:

```console
2025-05-09T19:57:16.332+0200    info    Traces  {"resource": {}, "otelcol.component.id": "debug/normal", "otelcol.component.kind": "exporter", "otelcol.signal": "traces", "resource spans": 1, "spans": 2}
2025-05-09T19:57:16.332+0200    info    ResourceTraces #0 [https://opentelemetry.io/schemas/1.25.0] service.name=telemetrygen
ScopeTraces #0 telemetrygen
okey-dokey-0 ab1030bd4ee554af936542b01d7b4807 1d8c93663d043aa8 net.sock.peer.addr=1.2.3.4 peer.service=telemetrygen-client
lets-go ab1030bd4ee554af936542b01d7b4807 0d238e8a2f97733f net.sock.peer.addr=1.2.3.4 peer.service=telemetrygen-server
        {"resource": {}, "otelcol.component.id": "debug/normal", "otelcol.component.kind": "exporter", "otelcol.signal": "traces"}
```

#### Link to tracking issue

Fixes
https://github.com/open-telemetry/opentelemetry-collector/issues/10515

#### Testing

Updated unit tests.

#### Documentation

Updated example in documentation.
2025-05-21 13:44:35 +00:00

79 lines
2.8 KiB
Go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package normal
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"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 with resource and scope attributes",
input: func() ptrace.Traces {
traces := ptrace.NewTraces()
resourceSpans := traces.ResourceSpans().AppendEmpty()
resourceSpans.SetSchemaUrl("https://opentelemetry.io/resource-schema-url")
resourceSpans.Resource().Attributes().PutStr("resourceKey1", "resourceValue1")
resourceSpans.Resource().Attributes().PutBool("resourceKey2", false)
scopeSpans := resourceSpans.ScopeSpans().AppendEmpty()
scopeSpans.SetSchemaUrl("http://opentelemetry.io/scope-schema-url")
scopeSpans.Scope().SetName("scope-name")
scopeSpans.Scope().SetVersion("1.2.3")
scopeSpans.Scope().Attributes().PutStr("scopeKey1", "scopeValue1")
scopeSpans.Scope().Attributes().PutBool("scopeKey2", true)
span := scopeSpans.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: `ResourceTraces #0 [https://opentelemetry.io/resource-schema-url] resourceKey1=resourceValue1 resourceKey2=false
ScopeTraces #0 [email protected] [http://opentelemetry.io/scope-schema-url] scopeKey1=scopeValue1 scopeKey2=true
span-name 0102030405060708090a0b0c0d0e0f10 1112131415161718 key1=value1 key2=value2
`,
},
{
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: `ResourceTraces #0
ScopeTraces #0
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)
require.NoError(t, err)
assert.Equal(t, tt.expected, string(output))
})
}
}