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
14 changes: 14 additions & 0 deletions internal/server/mock/trace/get_suspended.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package trace

import (
"context"

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

// GetSuspended returns whether tracing is currently suspended for a specific environment.
func (s *Server) GetSuspended(_ context.Context, req *pb.TraceGetSuspendedRequest) (*pb.TraceGetSuspendedResponse, error) {
return &pb.TraceGetSuspendedResponse{
Suspended: s.IsSuspended(req.Environment),
}, nil
}
16 changes: 16 additions & 0 deletions internal/server/mock/trace/get_threshold.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package trace

import (
"context"

"google.golang.org/protobuf/types/known/durationpb"

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

// GetThreshold returns the minimum call duration which is traced for a specific environment.
func (s *Server) GetThreshold(_ context.Context, req *pb.TraceGetThresholdRequest) (*pb.TraceGetThresholdResponse, error) {
return &pb.TraceGetThresholdResponse{
Threshold: durationpb.New(s.getThreshold(req.Environment)),
}, nil
}
18 changes: 9 additions & 9 deletions internal/server/mock/trace/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const DefaultThreshold = time.Millisecond
type Server struct {
pb.UnimplementedTraceServer

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

// SetSuspended marks tracing as suspended, or resumed, for an environment.
Expand Down Expand Up @@ -45,21 +45,21 @@ 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)
if s.thresholds == nil {
s.thresholds = make(map[string]time.Duration)
}

s.threshold[environment] = threshold
s.thresholds[environment] = threshold
}

// GetThreshold returns the minimum call duration which is traced for an
// 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 {
func (s *Server) getThreshold(environment string) time.Duration {
s.lock.Lock()
defer s.lock.Unlock()

threshold, ok := s.threshold[environment]
threshold, ok := s.thresholds[environment]
if !ok {
return DefaultThreshold
}
Expand Down
63 changes: 60 additions & 3 deletions internal/server/mock/trace/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestSetThreshold(t *testing.T) {
t.Errorf("got code %s, want %s", got, c.wantCode)
}

if got := srv.GetThreshold("dev"); got != c.want {
if got := getThreshold(t, srv, "dev"); got != c.want {
t.Errorf("got threshold %s, want %s", got, c.want)
}
})
Expand All @@ -52,7 +52,7 @@ func TestSetThreshold(t *testing.T) {
func TestGetThresholdDefaults(t *testing.T) {
srv := &Server{}

if got := srv.GetThreshold("dev"); got != DefaultThreshold {
if got := getThreshold(t, srv, "dev"); got != DefaultThreshold {
t.Errorf("got threshold %s, want %s", got, DefaultThreshold)
}
}
Expand All @@ -69,11 +69,44 @@ func TestSetThresholdIsPerEnvironment(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}

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

// --- GetSuspended ---

// Suspending and resuming are only useful if a client can find out which of the
// two an environment is in, without opening a stream to see if it fails.
func TestGetSuspended(t *testing.T) {
srv := &Server{}

if getSuspended(t, srv, "dev") {
t.Error("got suspended for an environment which was never suspended")
}

if _, err := srv.Suspend(context.TODO(), &pb.TraceSuspendRequest{Environment: "dev"}); err != nil {
t.Fatalf("unexpected error: %v", err)
}

if !getSuspended(t, srv, "dev") {
t.Error("got resumed after Suspend")
}

// Suspending one environment does not suspend the rest.
if getSuspended(t, srv, "prod") {
t.Error("got suspended for prod after suspending dev")
}

if _, err := srv.Resume(context.TODO(), &pb.TraceResumeRequest{Environment: "dev"}); err != nil {
t.Fatalf("unexpected error: %v", err)
}

if getSuspended(t, srv, "dev") {
t.Error("got suspended after Resume")
}
}

// --- aboveThreshold ---

func TestAboveThreshold(t *testing.T) {
Expand Down Expand Up @@ -116,3 +149,27 @@ func TestAboveThreshold(t *testing.T) {
})
}
}

// --- helpers ---

func getThreshold(t *testing.T, srv *Server, environment string) time.Duration {
t.Helper()

resp, err := srv.GetThreshold(context.TODO(), &pb.TraceGetThresholdRequest{Environment: environment})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

return resp.Threshold.AsDuration()
}

func getSuspended(t *testing.T, srv *Server, environment string) bool {
t.Helper()

resp, err := srv.GetSuspended(context.TODO(), &pb.TraceGetSuspendedRequest{Environment: environment})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

return resp.Suspended
}
2 changes: 1 addition & 1 deletion internal/server/mock/trace/stream_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ 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)
threshold := s.getThreshold(req.Environment)

now := time.Now()

Expand Down
Loading