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

tracing: introduce a per-span limit for structured events #60678

Merged
merged 1 commit into from
Feb 24, 2021
Merged
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
2 changes: 1 addition & 1 deletion pkg/util/ring/ring_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (r *Buffer) RemoveLast() {
}
}

// Reserve reserves the provided number of elemnets in the Buffer. It is an
// Reserve reserves the provided number of elements in the Buffer. It is an
// error to reserve a size less than the Buffer's current length.
func (r *Buffer) Reserve(n int) {
if n < r.Len() {
Expand Down
1 change: 1 addition & 0 deletions pkg/util/tracing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ go_library(
"//pkg/util/envutil",
"//pkg/util/iterutil",
"//pkg/util/protoutil",
"//pkg/util/ring",
"//pkg/util/syncutil",
"//pkg/util/timeutil",
"//pkg/util/tracing/tracingpb",
Expand Down
18 changes: 12 additions & 6 deletions pkg/util/tracing/crdbspan.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"sync/atomic"
"time"

"github.com/cockroachdb/cockroach/pkg/util/ring"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/tracing/tracingpb"
Expand Down Expand Up @@ -79,7 +80,7 @@ type crdbSpanMu struct {
tags opentracing.Tags

stats SpanStats
structured []Structured
structured ring.Buffer // of Structured events

// The Span's associated baggage.
baggage map[string]string
Expand Down Expand Up @@ -224,7 +225,11 @@ func (s *crdbSpan) record(msg string) {
func (s *crdbSpan) recordStructured(item Structured) {
s.mu.Lock()
defer s.mu.Unlock()
s.mu.structured = append(s.mu.structured, item)

if s.mu.structured.Len() == maxStructuredEventsPerSpan {
s.mu.structured.RemoveLast()
}
s.mu.structured.AddFirst(item)
}

func (s *crdbSpan) setBaggageItemAndTag(restrictedKey, value string) {
Expand Down Expand Up @@ -300,10 +305,11 @@ func (s *crdbSpan) getRecordingLocked(m mode) tracingpb.RecordedSpan {
rs.DeprecatedStats = stats
}

if s.mu.structured != nil {
rs.InternalStructured = make([]*types.Any, 0, len(s.mu.structured))
for i := range s.mu.structured {
item, err := types.MarshalAny(s.mu.structured[i])
if numEvents := s.mu.structured.Len(); numEvents != 0 {
rs.InternalStructured = make([]*types.Any, 0, numEvents)
for i := 0; i < numEvents; i++ {
event := s.mu.structured.Get(i).(Structured)
item, err := types.MarshalAny(event)
if err != nil {
// An error here is an error from Marshal; these
// are unlikely to happen.
Expand Down
28 changes: 28 additions & 0 deletions pkg/util/tracing/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,34 @@ func TestSpanRecordStructured(t *testing.T) {
`))
}

func TestSpanRecordStructuredLimit(t *testing.T) {
tr := NewTracer()
tr._mode = int32(modeBackground)
sp := tr.StartSpan("root", WithForceRealSpan())
defer sp.Finish()

const extra = 10
for i := int32(1); i <= maxStructuredEventsPerSpan+extra; i++ {
sp.RecordStructured(&types.Int32Value{Value: i})
}
rec := sp.GetRecording()
require.Len(t, rec, 1)
require.Len(t, rec[0].InternalStructured, maxStructuredEventsPerSpan)

first := rec[0].InternalStructured[0]
last := rec[0].InternalStructured[len(rec[0].InternalStructured)-1]
var d1 types.DynamicAny
require.NoError(t, types.UnmarshalAny(first, &d1))
require.IsType(t, (*types.Int32Value)(nil), d1.Message)

var res int32
require.NoError(t, types.StdInt32Unmarshal(&res, first.Value))
require.Equal(t, res, int32(maxStructuredEventsPerSpan+extra))

require.NoError(t, types.StdInt32Unmarshal(&res, last.Value))
require.Equal(t, res, int32(extra+1))
}

func TestNonVerboseChildSpanRegisteredWithParent(t *testing.T) {
tr := NewTracer()
tr._mode = int32(modeBackground)
Expand Down
7 changes: 6 additions & 1 deletion pkg/util/tracing/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ import (
const verboseTracingBaggageKey = "sb"

const (
// maxLogsPerSpan limits the number of logs in a Span; use a comfortable limit.
// maxLogsPerSpan limits the number of logs in a Span; use a comfortable
// limit.
maxLogsPerSpan = 1000
// maxStructuredEventsPerSpan limits the number of structured events in a
// span; use a comfortable limit.
maxStructuredEventsPerSpan = 50
// maxChildrenPerSpan limits the number of (direct) child spans in a Span.
maxChildrenPerSpan = 1000
)
Expand Down Expand Up @@ -399,6 +403,7 @@ func (t *Tracer) startSpanGeneric(
duration: -1, // unfinished
},
}
helper.crdbSpan.mu.structured.Reserve(maxStructuredEventsPerSpan)
helper.span.i = spanInner{
tracer: t,
crdb: &helper.crdbSpan,
Expand Down