Skip to content

Commit 46a89db

Browse files
mariashnotrepo05
andauthored
Support distributed tracing (#11)
* Pass request trace ID to rep client Signed-off-by: Maria Shaldybin <[email protected]> * Move loggerWithTrace to trace package * Pass trace Id to bbs client --------- Signed-off-by: Maria Shaldybin <[email protected]> Co-authored-by: Nick Rohn <[email protected]>
1 parent 20b5e4e commit 46a89db

File tree

7 files changed

+58
-47
lines changed

7 files changed

+58
-47
lines changed

auctionrunner/auction_runner.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"os"
55
"time"
66

7+
"code.cloudfoundry.org/bbs/trace"
78
"code.cloudfoundry.org/clock"
89
"code.cloudfoundry.org/lager/v3"
910

@@ -51,21 +52,21 @@ func New(
5152
func (a *auctionRunner) Run(signals <-chan os.Signal, ready chan<- struct{}) error {
5253
close(ready)
5354

54-
var hasWork chan struct{}
55+
var hasWork chan Work
5556
hasWork = a.batch.HasWork
5657

5758
for {
5859
select {
59-
case <-hasWork:
60-
logger := a.logger.Session("auction")
60+
case work := <-hasWork:
61+
logger := trace.LoggerWithTraceInfo(a.logger, work.TraceID).Session("auction")
6162

6263
logger.Info("fetching-cell-reps")
63-
clients, err := a.delegate.FetchCellReps()
64+
clients, err := a.delegate.FetchCellReps(logger, work.TraceID)
6465
if err != nil {
6566
logger.Error("failed-to-fetch-reps", err)
6667
time.Sleep(time.Second)
67-
hasWork = make(chan struct{}, 1)
68-
hasWork <- struct{}{}
68+
hasWork = make(chan Work, 1)
69+
hasWork <- work
6970
break
7071
}
7172
logger.Info("fetched-cell-reps", lager.Data{"cell-reps-count": len(clients)})
@@ -119,17 +120,17 @@ func (a *auctionRunner) Run(signals <-chan os.Signal, ready chan<- struct{}) err
119120
})
120121

121122
a.metricEmitter.AuctionCompleted(auctionResults)
122-
a.delegate.AuctionCompleted(auctionResults)
123+
a.delegate.AuctionCompleted(logger, work.TraceID, auctionResults)
123124
case <-signals:
124125
return nil
125126
}
126127
}
127128
}
128129

129-
func (a *auctionRunner) ScheduleLRPsForAuctions(lrpStarts []auctioneer.LRPStartRequest) {
130-
a.batch.AddLRPStarts(lrpStarts)
130+
func (a *auctionRunner) ScheduleLRPsForAuctions(lrpStarts []auctioneer.LRPStartRequest, traceID string) {
131+
a.batch.AddLRPStarts(lrpStarts, traceID)
131132
}
132133

133-
func (a *auctionRunner) ScheduleTasksForAuctions(tasks []auctioneer.TaskStartRequest) {
134-
a.batch.AddTasks(tasks)
134+
func (a *auctionRunner) ScheduleTasksForAuctions(tasks []auctioneer.TaskStartRequest, traceID string) {
135+
a.batch.AddTasks(tasks, traceID)
135136
}

auctionrunner/batch.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ import (
1111
"code.cloudfoundry.org/clock"
1212
)
1313

14+
type Work struct {
15+
TraceID string
16+
}
17+
1418
type Batch struct {
1519
lrpAuctions []auctiontypes.LRPAuction
1620
taskAuctions []auctiontypes.TaskAuction
1721
lock *sync.Mutex
18-
HasWork chan struct{}
22+
HasWork chan Work
1923
clock clock.Clock
2024
}
2125

@@ -24,11 +28,11 @@ func NewBatch(clock clock.Clock) *Batch {
2428
lrpAuctions: []auctiontypes.LRPAuction{},
2529
lock: &sync.Mutex{},
2630
clock: clock,
27-
HasWork: make(chan struct{}, 1),
31+
HasWork: make(chan Work, 1),
2832
}
2933
}
3034

31-
func (b *Batch) AddLRPStarts(starts []auctioneer.LRPStartRequest) {
35+
func (b *Batch) AddLRPStarts(starts []auctioneer.LRPStartRequest, traceID string) {
3236
auctions := make([]auctiontypes.LRPAuction, 0, len(starts))
3337
now := b.clock.Now()
3438
for i := range starts {
@@ -42,11 +46,11 @@ func (b *Batch) AddLRPStarts(starts []auctioneer.LRPStartRequest) {
4246

4347
b.lock.Lock()
4448
b.lrpAuctions = append(b.lrpAuctions, auctions...)
45-
b.claimToHaveWork()
49+
b.claimToHaveWork(traceID)
4650
b.lock.Unlock()
4751
}
4852

49-
func (b *Batch) AddTasks(tasks []auctioneer.TaskStartRequest) {
53+
func (b *Batch) AddTasks(tasks []auctioneer.TaskStartRequest, traceID string) {
5054
auctions := make([]auctiontypes.TaskAuction, 0, len(tasks))
5155
now := b.clock.Now()
5256
for i := range tasks {
@@ -55,7 +59,7 @@ func (b *Batch) AddTasks(tasks []auctioneer.TaskStartRequest) {
5559

5660
b.lock.Lock()
5761
b.taskAuctions = append(b.taskAuctions, auctions...)
58-
b.claimToHaveWork()
62+
b.claimToHaveWork(traceID)
5963
b.lock.Unlock()
6064
}
6165

@@ -96,9 +100,9 @@ func (b *Batch) DedupeAndDrain() ([]auctiontypes.LRPAuction, []auctiontypes.Task
96100
return dedupedLRPAuctions, dedupedTaskAuctions
97101
}
98102

99-
func (b *Batch) claimToHaveWork() {
103+
func (b *Batch) claimToHaveWork(traceID string) {
100104
select {
101-
case b.HasWork <- struct{}{}:
105+
case b.HasWork <- Work{TraceID: traceID}:
102106
default:
103107
}
104108
}

auctionrunner/batch_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var _ = Describe("Batch", func() {
3434
Context("when adding start auctions", func() {
3535
BeforeEach(func() {
3636
lrpStart = BuildLRPStartRequest("pg-1", "domain", []int{1}, "linux", 10, 10, 10, []string{}, []string{})
37-
batch.AddLRPStarts([]auctioneer.LRPStartRequest{lrpStart})
37+
batch.AddLRPStarts([]auctioneer.LRPStartRequest{lrpStart}, "some-trace-id")
3838
})
3939

4040
It("makes the start auction available when drained", func() {
@@ -50,7 +50,7 @@ var _ = Describe("Batch", func() {
5050
Context("when adding tasks", func() {
5151
BeforeEach(func() {
5252
task = BuildTaskStartRequest("tg-1", "domain", "linux", 10, 10, 10)
53-
batch.AddTasks([]auctioneer.TaskStartRequest{task})
53+
batch.AddTasks([]auctioneer.TaskStartRequest{task}, "some-trace-id")
5454
})
5555

5656
It("makes the stop auction available when drained", func() {
@@ -70,12 +70,12 @@ var _ = Describe("Batch", func() {
7070
BuildLRPStartRequest("pg-1", "domain", []int{1}, "linux", 10, 10, 10, []string{"driver-1"}, []string{"tag-1"}),
7171
BuildLRPStartRequest("pg-1", "domain", []int{1}, "linux", 10, 10, 10, []string{"driver-1"}, []string{"tag-1"}),
7272
BuildLRPStartRequest("pg-2", "domain", []int{2}, "linux", 10, 10, 10, []string{"driver-2"}, []string{"tag-2"}),
73-
})
73+
}, "some-trace-id")
7474

7575
batch.AddTasks([]auctioneer.TaskStartRequest{
7676
BuildTaskStartRequest("tg-1", "domain", "linux", 10, 10, 10),
7777
BuildTaskStartRequest("tg-1", "domain", "linux", 10, 10, 10),
78-
BuildTaskStartRequest("tg-2", "domain", "linux", 10, 10, 10)})
78+
BuildTaskStartRequest("tg-2", "domain", "linux", 10, 10, 10)}, "some-trace-id")
7979
})
8080

8181
It("should dedupe any duplicate start auctions and stop auctions", func() {

auctiontypes/fakes/fake_auction_runner.go

Lines changed: 20 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

auctiontypes/types.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77

88
"code.cloudfoundry.org/auctioneer"
9+
"code.cloudfoundry.org/lager/v3"
910
"code.cloudfoundry.org/rep"
1011
"github.com/tedsuo/ifrit"
1112
)
@@ -47,13 +48,13 @@ var ErrorExceededInflightCreation = errors.New("waiting to start instance: reach
4748
//go:generate counterfeiter -o fakes/fake_auction_runner.go . AuctionRunner
4849
type AuctionRunner interface {
4950
ifrit.Runner
50-
ScheduleLRPsForAuctions([]auctioneer.LRPStartRequest)
51-
ScheduleTasksForAuctions([]auctioneer.TaskStartRequest)
51+
ScheduleLRPsForAuctions([]auctioneer.LRPStartRequest, string)
52+
ScheduleTasksForAuctions([]auctioneer.TaskStartRequest, string)
5253
}
5354

5455
type AuctionRunnerDelegate interface {
55-
FetchCellReps() (map[string]rep.Client, error)
56-
AuctionCompleted(AuctionResults)
56+
FetchCellReps(lager.Logger, string) (map[string]rep.Client, error)
57+
AuctionCompleted(lager.Logger, string, AuctionResults)
5758
}
5859

5960
//go:generate counterfeiter -o fakes/fake_metric_emitter.go . AuctionMetricEmitterDelegate

simulation/auction_runner_delegate_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"sync"
55

66
"code.cloudfoundry.org/auction/auctiontypes"
7+
"code.cloudfoundry.org/lager/v3"
78
"code.cloudfoundry.org/rep"
89
)
910

@@ -30,15 +31,15 @@ func (a *auctionRunnerDelegate) SetCellLimit(limit int) {
3031
a.cellLimit = limit
3132
}
3233

33-
func (a *auctionRunnerDelegate) FetchCellReps() (map[string]rep.Client, error) {
34+
func (a *auctionRunnerDelegate) FetchCellReps(lager.Logger, string) (map[string]rep.Client, error) {
3435
subset := map[string]rep.Client{}
3536
for i := 0; i < a.cellLimit; i++ {
3637
subset[cellGuid(i)] = a.cells[cellGuid(i)]
3738
}
3839
return subset, nil
3940
}
4041

41-
func (a *auctionRunnerDelegate) AuctionCompleted(work auctiontypes.AuctionResults) {
42+
func (a *auctionRunnerDelegate) AuctionCompleted(logger lager.Logger, traceID string, work auctiontypes.AuctionResults) {
4243
a.lock.Lock()
4344
defer a.lock.Unlock()
4445
a.workResults.FailedLRPs = append(a.workResults.FailedLRPs, work.FailedLRPs...)

simulation/simulation_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ var _ = Describe("Auction", func() {
7171

7272
runStartAuction := func(lrpStartAuctions []auctioneer.LRPStartRequest, numCells int) {
7373
runnerDelegate.SetCellLimit(numCells)
74-
runner.ScheduleLRPsForAuctions(lrpStartAuctions)
74+
runner.ScheduleLRPsForAuctions(lrpStartAuctions, "some-trace-id")
7575

7676
Eventually(runnerDelegate.ResultSize, time.Minute, 100*time.Millisecond).Should(Equal(len(lrpStartAuctions)))
7777
}
@@ -83,7 +83,7 @@ var _ = Describe("Auction", func() {
8383
Eventually(runnerDelegate.ResultSize, time.Minute, 100*time.Millisecond).Should(Equal(len(lrpStartAuctions)))
8484
duration := time.Since(t)
8585

86-
cells, _ := runnerDelegate.FetchCellReps()
86+
cells, _ := runnerDelegate.FetchCellReps(logger, "some-trace-id")
8787
report := visualization.NewReport(len(lrpStartAuctions), cells, runnerDelegate.Results(), duration)
8888

8989
visualization.PrintReport(report)

0 commit comments

Comments
 (0)