Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Report `ot-baggage-*` extraction errors from `go.opentelemetry.io/contrib/propagators/ot` to `otel.Handle` instead of silently discarding them, while still attaching the successfully parsed baggage members to the context. (#9395)
- Set `error.type` on the `rpc.client.call.duration` and `rpc.server.call.duration` metrics in `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc` when the RPC fails with a non-OK status, per the RPC semantic conventions. (#9429)
- Reject OTLP exporter headers with an empty `name` in `go.opentelemetry.io/contrib/otelconf`, `go.opentelemetry.io/contrib/otelconf/x`, and `go.opentelemetry.io/contrib/otelconf/v0.3.0`, instead of forwarding invalid header names to OTLP exporters. (#9102)
- Format span attributes in `go.opentelemetry.io/contrib/zpages` using `attribute.Value.String` instead of the deprecated `attribute.Value.Emit`, following the OpenTelemetry AnyValue representation for non-OTLP protocols. (#9453)

<!-- Released section -->
<!-- Don't change this section unless doing release -->
Expand Down
2 changes: 1 addition & 1 deletion zpages/tracez.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func spanRows(s sdktrace.ReadOnlySpan) []spanRow {
sort.Sort(a)
var s []string
for i := range a {
s = append(s, fmt.Sprintf("%s=%v", a[i].Key, a[i].Value.Emit())) //nolint:staticcheck // Use deprecated method for formatting backward compatibility.
s = append(s, fmt.Sprintf("%s=%v", a[i].Key, a[i].Value.String()))
}
return "Attributes:{" + strings.Join(s, ", ") + "}"
}
Expand Down
97 changes: 97 additions & 0 deletions zpages/tracez_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ package zpages

import (
"context"
"math"
"net/http"
"net/http/httptest"
"sync"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)

Expand Down Expand Up @@ -174,6 +177,100 @@ func TestTracezHandler_ServeHTTP(t *testing.T) {
}
}

func TestTracezHandler_FormatsAttributesUsingSpecString(t *testing.T) {
Comment thread
om7057 marked this conversation as resolved.
// attribute.Value.Emit is deprecated in favor of attribute.Value.String,
// which follows the OpenTelemetry AnyValue representation for non-OTLP
// protocols. The two representations disagree for:
// - FLOAT64SLICE containing NaN or Inf: Emit produces invalid output
// (e.g. "invalid: [NaN 1.5]") while String produces a valid JSON
// array (e.g. ["NaN",1.5]).
// - FLOAT64 containing +/-Inf: Emit renders "+Inf"/"-Inf" while String
// renders the spec's "Infinity"/"-Infinity".
// - BOOLSLICE: Emit space-separates elements (Go's %v on a slice)
// while String comma-separates them as a JSON array.
testCases := []struct {
name string
key string
value attribute.Value
want string
}{
{
name: "float64 slice with NaN",
key: "nums",
value: attribute.Float64SliceValue([]float64{math.NaN(), 1.5}),
want: `nums=[&#34;NaN&#34;,1.5]`,
},
{
name: "float64 slice with +Inf",
key: "nums",
value: attribute.Float64SliceValue([]float64{1.5, math.Inf(1)}),
want: `nums=[1.5,&#34;Infinity&#34;]`,
},
{
name: "float64 +Inf",
key: "num",
value: attribute.Float64Value(math.Inf(1)),
want: `num=Infinity`,
},
{
name: "float64 -Inf",
key: "num",
value: attribute.Float64Value(math.Inf(-1)),
want: `num=-Infinity`,
},
{
name: "bool slice",
key: "flags",
value: attribute.BoolSliceValue([]bool{true, false}),
want: `flags=[true,false]`,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
sp := NewSpanProcessor()
defer func() {
require.NoError(t, sp.Shutdown(t.Context()))
}()

tp := sdktrace.NewTracerProvider(
sdktrace.WithSpanProcessor(sp),
)
defer func() {
require.NoError(t, tp.Shutdown(t.Context()))
}()

tracer := tp.Tracer("test-tracer")
ctx := t.Context()

// Use an error span rather than a latency-bucketed one: which
// latency bucket a near-instant span lands in is
// timing-dependent and flaky across environments, whereas
// error spans aren't bucketed.
_, span := tracer.Start(ctx, "attribute-span")
span.SetAttributes(attribute.KeyValue{Key: attribute.Key(tc.key), Value: tc.value})
span.SetStatus(codes.Error, "boom")
span.End()

handler := NewTracezHandler(sp)

req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/tracez?zspanname=attribute-span&ztype=2", http.NoBody)
w := httptest.NewRecorder()

handler.ServeHTTP(w, req)

resp := w.Result()
defer resp.Body.Close()

require.Equal(t, http.StatusOK, resp.StatusCode)

body := w.Body.String()
assert.Contains(t, body, tc.want)
assert.NotContains(t, body, "invalid:")
})
}
}

func TestTracezHandler_ConcurrentSafe(t *testing.T) {
sp := NewSpanProcessor()
defer func() {
Expand Down
Loading