Skip to content
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
33 changes: 33 additions & 0 deletions internal/server/mock/trace/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ package trace

import (
"sync"
"time"

"github.com/skpr/api/pb"
)

// DefaultThreshold is the tracing threshold an environment has until one is set
// for it, matching the default which Compass ships with.
const DefaultThreshold = time.Millisecond

// Server implements the GRPC "trace" definition.
type Server struct {
pb.UnimplementedTraceServer

lock sync.Mutex
suspended map[string]bool
threshold map[string]time.Duration
}

// SetSuspended marks tracing as suspended, or resumed, for an environment.
Expand All @@ -33,3 +39,30 @@ func (s *Server) IsSuspended(environment string) bool {

return s.suspended[environment]
}

// setThreshold records the minimum call duration which is traced for an environment.
func (s *Server) setThreshold(environment string, threshold time.Duration) {
s.lock.Lock()
defer s.lock.Unlock()

if s.threshold == nil {
s.threshold = make(map[string]time.Duration)
}

s.threshold[environment] = threshold
}

// GetThreshold returns the minimum call duration which is traced for an
// environment, falling back to the default for an environment which has not had
// one set.
func (s *Server) GetThreshold(environment string) time.Duration {
s.lock.Lock()
defer s.lock.Unlock()

threshold, ok := s.threshold[environment]
if !ok {
return DefaultThreshold
}

return threshold
}
118 changes: 118 additions & 0 deletions internal/server/mock/trace/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package trace

import (
"context"
"testing"
"time"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/durationpb"

"github.com/skpr/api/pb"
)

// --- SetThreshold ---

func TestSetThreshold(t *testing.T) {
cases := []struct {
name string
threshold *durationpb.Duration
wantCode codes.Code
want time.Duration
}{
{"sets the threshold", durationpb.New(5 * time.Millisecond), codes.OK, 5 * time.Millisecond},
{"zero traces every call", durationpb.New(0), codes.OK, 0},
{"omitted is rejected", nil, codes.InvalidArgument, DefaultThreshold},
{"negative is rejected", durationpb.New(-time.Millisecond), codes.InvalidArgument, DefaultThreshold},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
srv := &Server{}

_, err := srv.SetThreshold(context.TODO(), &pb.TraceSetThresholdRequest{
Environment: "dev",
Threshold: c.threshold,
})

if got := status.Code(err); got != c.wantCode {
t.Errorf("got code %s, want %s", got, c.wantCode)
}

if got := srv.GetThreshold("dev"); got != c.want {
t.Errorf("got threshold %s, want %s", got, c.want)
}
})
}
}

// An environment which has never had a threshold set still has to stream, so it
// falls back to the default rather than to zero, which would trace every call.
func TestGetThresholdDefaults(t *testing.T) {
srv := &Server{}

if got := srv.GetThreshold("dev"); got != DefaultThreshold {
t.Errorf("got threshold %s, want %s", got, DefaultThreshold)
}
}

// Thresholds are held per environment, so lowering one does not quietly start
// tracing every call on another.
func TestSetThresholdIsPerEnvironment(t *testing.T) {
srv := &Server{}

if _, err := srv.SetThreshold(context.TODO(), &pb.TraceSetThresholdRequest{
Environment: "dev",
Threshold: durationpb.New(0),
}); err != nil {
t.Fatalf("unexpected error: %v", err)
}

if got := srv.GetThreshold("prod"); got != DefaultThreshold {
t.Errorf("got threshold %s for prod, want %s", got, DefaultThreshold)
}
}

// --- aboveThreshold ---

func TestAboveThreshold(t *testing.T) {
spans := []*pb.TraceSpan{
{Name: "slow", Elapsed: durationpb.New(6 * time.Millisecond), Calls: 3},
{Name: "exactly", Elapsed: durationpb.New(time.Millisecond), Calls: 2},
{Name: "fast", Elapsed: durationpb.New(250 * time.Microsecond), Calls: 5},
}

cases := []struct {
name string
threshold time.Duration
want []string
}{
{"everything is traced at zero", 0, []string{"slow", "exactly", "fast"}},
{"the threshold itself is traced", time.Millisecond, []string{"slow", "exactly"}},
{"a coarse threshold keeps the slowest", 5 * time.Millisecond, []string{"slow"}},
{"nothing reaches an impossible threshold", time.Hour, nil},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
retained, calls := aboveThreshold(spans, c.threshold)

if len(retained) != len(c.want) {
t.Fatalf("got %d spans, want %d", len(retained), len(c.want))
}

for i, name := range c.want {
if retained[i].Name != name {
t.Errorf("got span %q at %d, want %q", retained[i].Name, i, name)
}
}

// The request made these calls whether or not a span survived the
// threshold, so the count is the same at every threshold.
if calls != 10 {
t.Errorf("got %d calls, want 10", calls)
}
})
}
}
32 changes: 32 additions & 0 deletions internal/server/mock/trace/set_threshold.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package trace

import (
"context"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/skpr/api/pb"
)

// SetThreshold sets the minimum call duration which is traced for a specific environment.
func (s *Server) SetThreshold(_ context.Context, req *pb.TraceSetThresholdRequest) (*pb.TraceSetThresholdResponse, error) {
// An omitted threshold is not the same as a zero one: zero traces every call,
// which is expensive enough that a caller has to ask for it rather than get
// it by leaving the field out.
if req.Threshold == nil {
return nil, status.Error(codes.InvalidArgument, "threshold is required")
}

threshold := req.Threshold.AsDuration()

// A negative threshold would trace nothing at all, which is not what a caller
// asking for less tracing means. Suspend is how tracing gets turned off.
if threshold < 0 {
return nil, status.Errorf(codes.InvalidArgument, "threshold cannot be negative: %s", threshold)
}

s.setThreshold(req.Environment, threshold)

return &pb.TraceSetThresholdResponse{}, nil
}
55 changes: 49 additions & 6 deletions internal/server/mock/trace/stream_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,50 @@ func (s *Server) StreamTraces(req *pb.StreamTracesRequest, server pb.Trace_Strea
return status.Errorf(codes.FailedPrecondition, "tracing is suspended for environment: %s", req.Environment)
}

threshold := s.GetThreshold(req.Environment)

now := time.Now()

// ~6ms realistic function execution time
latency := 6 * time.Millisecond

// Function calls generator, placed relative to the start of the request.
makeFunctionCalls := func() []*pb.TraceFunctionCall {
return []*pb.TraceFunctionCall{
// Spans generator, placed relative to the start of the request. Each span is
// the calls of one function during one slice of the request, so it carries
// how many there were, the longest of them, and what they cost altogether.
makeSpans := func() []*pb.TraceSpan {
return []*pb.TraceSpan{
{
Name: "PDOStatement::execute",
Offset: durationpb.New(0),
Elapsed: durationpb.New(latency),
Total: durationpb.New(3 * latency),
Calls: 3,
Memory: 1048576,
},
{
Name: "Drupal\\Core\\Database\\StatementPrefetchIterator::execute",
Offset: durationpb.New(500 * time.Microsecond),
Elapsed: durationpb.New(latency),
Total: durationpb.New(2 * latency),
Calls: 2,
Memory: 2097152,
},
{
Name: "Drupal\\sqlite\\Driver\\Database\\sqlite\\Statement::execute",
Offset: durationpb.New(1 * time.Millisecond),
Elapsed: durationpb.New(latency),
Total: durationpb.New(latency),
Calls: 1,
Memory: 524288,
},
{
// Below the default threshold, so it only appears for an
// environment which has asked for a finer one.
Name: "Drupal\\Core\\Database\\Query\\Upsert::execute",
Offset: durationpb.New(1500 * time.Microsecond),
Elapsed: durationpb.New(latency),
Elapsed: durationpb.New(250 * time.Microsecond),
Total: durationpb.New(500 * time.Microsecond),
Calls: 2,
Memory: 786432,
},
}
Expand Down Expand Up @@ -93,6 +107,8 @@ func (s *Server) StreamTraces(req *pb.StreamTracesRequest, server pb.Trace_Strea
start := now.Add(time.Duration(i*250) * time.Millisecond)
end := start.Add(50 * time.Millisecond)

spans, calls := aboveThreshold(makeSpans(), threshold)

traces = append(traces, &pb.Trace{
Metadata: &pb.TraceMetadata{
RequestId: gofakeit.UUID(),
Expand All @@ -105,8 +121,11 @@ func (s *Server) StreamTraces(req *pb.StreamTracesRequest, server pb.Trace_Strea
Uri: "/sites/default/files/styles/scale_crop_7_3_wide/public/veggie-pasta-bake-hero-umami.jpg.webp?itok=CYsHBUlX",
},
},
FunctionCalls: makeFunctionCalls(),
FunctionCallsDropped: 0,
Spans: spans,
// The call count is exact whether or not a span represents the calls,
// so it stays the same no matter where the threshold sits.
Calls: calls,
CallsDropped: 0,
ResourceUtilisation: &pb.TraceResourceUtilisation{
MaxMemory: 33554432,
},
Expand All @@ -123,3 +142,27 @@ func (s *Server) StreamTraces(req *pb.StreamTracesRequest, server pb.Trace_Strea
time.Sleep(time.Second)
}
}

// aboveThreshold keeps the spans whose longest call ran for at least the
// environment's threshold, which is what the extension's probe does, and returns
// them along with the total number of calls the request made. The call count
// covers every span, including those the threshold filtered out, because the
// request made those calls either way.
func aboveThreshold(spans []*pb.TraceSpan, threshold time.Duration) ([]*pb.TraceSpan, int64) {
var (
retained []*pb.TraceSpan
calls int64
)

for _, span := range spans {
calls += span.Calls

if span.Elapsed.AsDuration() < threshold {
continue
}

retained = append(retained, span)
}

return retained, calls
}
Loading