Skip to content

Commit ef69fb6

Browse files
mariashnotrepo05
andauthored
Support distributed tracing (#7)
* Add trace to bbs and rep clients * Fix tests * Move trace ID generation into common method * Pass trace ID to cfdot logger --------- Co-authored-by: Nick Rohn <[email protected]>
1 parent 9c6e1b4 commit ef69fb6

40 files changed

+217
-91
lines changed

commands/actual_lrp_groups.go

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

77
"code.cloudfoundry.org/bbs"
88
"code.cloudfoundry.org/bbs/models"
9+
"code.cloudfoundry.org/bbs/trace"
910
"code.cloudfoundry.org/cfdot/commands/helpers"
1011
"github.com/spf13/cobra"
1112
)
@@ -65,7 +66,8 @@ func ValidateActualLRPGroupsArguments(args []string) error {
6566
}
6667

6768
func ActualLRPGroups(stdout, stderr io.Writer, bbsClient bbs.Client, domain, cellID string) error {
68-
logger := globalLogger.Session("actual-lrp-groups")
69+
traceID := trace.GenerateTraceID()
70+
logger := trace.LoggerWithTraceInfo(globalLogger.Session("actual-lrp-groups"), traceID)
6971

7072
encoder := json.NewEncoder(stdout)
7173

@@ -74,7 +76,7 @@ func ActualLRPGroups(stdout, stderr io.Writer, bbsClient bbs.Client, domain, cel
7476
Domain: domain,
7577
}
7678

77-
actualLRPGroups, err := bbsClient.ActualLRPGroups(logger, actualLRPFilter)
79+
actualLRPGroups, err := bbsClient.ActualLRPGroups(logger, traceID, actualLRPFilter)
7880
if err != nil {
7981
return err
8082
}

commands/actual_lrp_groups_for_guid.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strconv"
99

1010
"code.cloudfoundry.org/bbs"
11+
"code.cloudfoundry.org/bbs/trace"
1112

1213
"code.cloudfoundry.org/cfdot/commands/helpers"
1314
"github.com/spf13/cobra"
@@ -80,11 +81,12 @@ func ValidateActualLRPGroupsForGuidArgs(args []string, indexFlag string) (string
8081
}
8182

8283
func ActualLRPGroupsForGuid(stdout, stderr io.Writer, bbsClient bbs.Client, processGuid string, index int) error {
83-
logger := globalLogger.Session("actual-lrp-groups-for-guid")
84+
traceID := trace.GenerateTraceID()
85+
logger := trace.LoggerWithTraceInfo(globalLogger.Session("actual-lrp-groups-for-guid"), traceID)
8486

8587
encoder := json.NewEncoder(stdout)
8688
if index < 0 {
87-
actualLRPGroups, err := bbsClient.ActualLRPGroupsByProcessGuid(logger, processGuid)
89+
actualLRPGroups, err := bbsClient.ActualLRPGroupsByProcessGuid(logger, traceID, processGuid)
8890
if err != nil {
8991
return err
9092
}
@@ -98,7 +100,7 @@ func ActualLRPGroupsForGuid(stdout, stderr io.Writer, bbsClient bbs.Client, proc
98100

99101
return nil
100102
} else {
101-
actualLRPGroup, err := bbsClient.ActualLRPGroupByProcessGuidAndIndex(logger, processGuid, index)
103+
actualLRPGroup, err := bbsClient.ActualLRPGroupByProcessGuidAndIndex(logger, traceID, processGuid, index)
102104
if err != nil {
103105
return err
104106
}

commands/actual_lrp_groups_for_guid_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
. "github.com/onsi/ginkgo/v2"
1313
. "github.com/onsi/gomega"
1414
"github.com/onsi/gomega/gbytes"
15+
"github.com/openzipkin/zipkin-go/model"
1516
)
1617

1718
var _ = Describe("ActualLRPGroupsForGuid", func() {
@@ -106,8 +107,10 @@ var _ = Describe("ActualLRPGroupsForGuid", func() {
106107
Expect(err).NotTo(HaveOccurred())
107108

108109
Expect(fakeBBSClient.ActualLRPGroupsByProcessGuidCallCount()).To(Equal(1))
109-
_, guid := fakeBBSClient.ActualLRPGroupsByProcessGuidArgsForCall(0)
110+
_, traceID, guid := fakeBBSClient.ActualLRPGroupsByProcessGuidArgsForCall(0)
110111
Expect(guid).To(Equal("guid"))
112+
_, err = model.TraceIDFromHex(traceID)
113+
Expect(err).NotTo(HaveOccurred())
111114

112115
expectedOutput := ""
113116
for _, group := range actualLRPGroups {
@@ -149,9 +152,11 @@ var _ = Describe("ActualLRPGroupsForGuid", func() {
149152
Expect(err).NotTo(HaveOccurred())
150153

151154
Expect(fakeBBSClient.ActualLRPGroupByProcessGuidAndIndexCallCount()).To(Equal(1))
152-
_, guid, index := fakeBBSClient.ActualLRPGroupByProcessGuidAndIndexArgsForCall(0)
155+
_, traceID, guid, index := fakeBBSClient.ActualLRPGroupByProcessGuidAndIndexArgsForCall(0)
153156
Expect(guid).To(Equal("guid"))
154157
Expect(index).To(Equal(2))
158+
_, err = model.TraceIDFromHex(traceID)
159+
Expect(err).NotTo(HaveOccurred())
155160

156161
jsonData, err := json.Marshal(actualLRPGroup)
157162
Expect(err).NotTo(HaveOccurred())

commands/actual_lrp_groups_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
. "github.com/onsi/ginkgo/v2"
1111
. "github.com/onsi/gomega"
1212
"github.com/onsi/gomega/gbytes"
13+
"github.com/openzipkin/zipkin-go/model"
1314
)
1415

1516
var _ = Describe("ActualLRPGroups", func() {
@@ -49,8 +50,10 @@ var _ = Describe("ActualLRPGroups", func() {
4950

5051
Expect(fakeBBSClient.ActualLRPGroupsCallCount()).To(Equal(1))
5152

52-
_, filter := fakeBBSClient.ActualLRPGroupsArgsForCall(0)
53+
_, traceID, filter := fakeBBSClient.ActualLRPGroupsArgsForCall(0)
5354
Expect(filter).To(Equal(models.ActualLRPFilter{CellID: "cell-1", Domain: "domain-1"}))
55+
_, err = model.TraceIDFromHex(traceID)
56+
Expect(err).NotTo(HaveOccurred())
5457

5558
expectedOutput := ""
5659
for _, group := range actualLRPGroups {

commands/actual_lrps.go

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

77
"code.cloudfoundry.org/bbs"
88
"code.cloudfoundry.org/bbs/models"
9+
"code.cloudfoundry.org/bbs/trace"
910
"code.cloudfoundry.org/cfdot/commands/helpers"
1011
"github.com/spf13/cobra"
1112
)
@@ -74,7 +75,8 @@ func ValidateActualLRPsArguments(args []string) error {
7475
}
7576

7677
func ActualLRPs(stdout, stderr io.Writer, bbsClient bbs.Client, domain, cellID, processGuid string, index *int32) error {
77-
logger := globalLogger.Session("actual-lrps")
78+
traceID := trace.GenerateTraceID()
79+
logger := trace.LoggerWithTraceInfo(globalLogger.Session("actual-lrps"), traceID)
7880

7981
encoder := json.NewEncoder(stdout)
8082

@@ -85,7 +87,7 @@ func ActualLRPs(stdout, stderr io.Writer, bbsClient bbs.Client, domain, cellID,
8587
Index: index,
8688
}
8789

88-
actualLRPs, err := bbsClient.ActualLRPs(logger, actualLRPFilter)
90+
actualLRPs, err := bbsClient.ActualLRPs(logger, traceID, actualLRPFilter)
8991
if err != nil {
9092
return err
9193
}

commands/actual_lrps_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
. "github.com/onsi/ginkgo/v2"
1010
. "github.com/onsi/gomega"
1111
"github.com/onsi/gomega/gbytes"
12+
"github.com/openzipkin/zipkin-go/model"
1213
)
1314

1415
var _ = Describe("ActualLRPs", func() {
@@ -47,13 +48,15 @@ var _ = Describe("ActualLRPs", func() {
4748

4849
Expect(fakeBBSClient.ActualLRPsCallCount()).To(Equal(1))
4950

50-
_, filter := fakeBBSClient.ActualLRPsArgsForCall(0)
51+
_, traceID, filter := fakeBBSClient.ActualLRPsArgsForCall(0)
5152
Expect(filter).To(Equal(models.ActualLRPFilter{
5253
CellID: "cell-1",
5354
Domain: "domain-1",
5455
ProcessGuid: "pg-2",
5556
Index: &index,
5657
}))
58+
_, err = model.TraceIDFromHex(traceID)
59+
Expect(err).NotTo(HaveOccurred())
5760

5861
expectedOutput := ""
5962
for _, lrp := range actualLRPs {

commands/cancel_task.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/spf13/cobra"
77

88
"code.cloudfoundry.org/bbs"
9+
"code.cloudfoundry.org/bbs/trace"
910
"code.cloudfoundry.org/cfdot/commands/helpers"
1011
)
1112

@@ -40,9 +41,10 @@ func cancelTask(cmd *cobra.Command, args []string) error {
4041
}
4142

4243
func CancelTaskByGuid(stdout, _ io.Writer, bbsClient bbs.Client, taskGuid string) error {
43-
logger := globalLogger.Session("cancel-task-by-guid")
44+
traceID := trace.GenerateTraceID()
45+
logger := trace.LoggerWithTraceInfo(globalLogger.Session("cancel-task-by-guid"), traceID)
4446

45-
err := bbsClient.CancelTask(logger, taskGuid)
47+
err := bbsClient.CancelTask(logger, traceID, taskGuid)
4648
if err != nil {
4749
return err
4850
}

commands/cancel_task_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
. "github.com/onsi/ginkgo/v2"
99
. "github.com/onsi/gomega"
1010
"github.com/onsi/gomega/gbytes"
11+
"github.com/openzipkin/zipkin-go/model"
1112
)
1213

1314
var _ = Describe("CancelTask", func() {
@@ -31,8 +32,10 @@ var _ = Describe("CancelTask", func() {
3132

3233
Expect(fakeBBSClient.CancelTaskCallCount()).To(Equal(1))
3334

34-
_, guid := fakeBBSClient.CancelTaskArgsForCall(0)
35+
_, traceID, guid := fakeBBSClient.CancelTaskArgsForCall(0)
3536
Expect(guid).To(Equal(taskGuid))
37+
_, err = model.TraceIDFromHex(traceID)
38+
Expect(err).NotTo(HaveOccurred())
3639
})
3740

3841
Context("when the bbs client errors", func() {

commands/cell.go

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

88
"code.cloudfoundry.org/bbs"
9+
"code.cloudfoundry.org/bbs/trace"
910
"code.cloudfoundry.org/cfdot/commands/helpers"
1011

1112
"github.com/spf13/cobra"
@@ -59,11 +60,12 @@ func ValidateCellArguments(args []string) error {
5960
}
6061

6162
func Cell(stdout, stderr io.Writer, bbsClient bbs.Client, cellId string) error {
62-
logger := globalLogger.Session("cell-presence")
63+
traceID := trace.GenerateTraceID()
64+
logger := trace.LoggerWithTraceInfo(globalLogger.Session("cell-presence"), traceID)
6365

6466
encoder := json.NewEncoder(stdout)
6567

66-
cells, err := bbsClient.Cells(logger)
68+
cells, err := bbsClient.Cells(logger, traceID)
6769
if err != nil {
6870
return err
6971
}

commands/cell_state.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"code.cloudfoundry.org/bbs"
1111
"code.cloudfoundry.org/bbs/models"
12+
"code.cloudfoundry.org/bbs/trace"
1213
"code.cloudfoundry.org/cfdot/commands/helpers"
1314
cfhttp "code.cloudfoundry.org/cfhttp/v2"
1415
"code.cloudfoundry.org/rep"
@@ -37,7 +38,9 @@ func cellState(cmd *cobra.Command, args []string) error {
3738
if err != nil {
3839
return NewCFDotError(cmd, err)
3940
}
40-
cellRegistration, err := FetchCellRegistration(bbsClient, args[0])
41+
42+
traceID := trace.GenerateTraceID()
43+
cellRegistration, err := FetchCellRegistration(bbsClient, traceID, args[0])
4144
if err != nil {
4245
return NewCFDotError(cmd, err)
4346
}
@@ -62,6 +65,7 @@ func cellState(cmd *cobra.Command, args []string) error {
6265
cmd.OutOrStderr(),
6366
repClientFactory,
6467
cellRegistration,
68+
traceID,
6569
)
6670
if err != nil {
6771
return NewCFDotComponentError(cmd, fmt.Errorf("Rep error: Failed to get cell state for cell %s: %s", args[0], err.Error()))
@@ -81,10 +85,10 @@ func ValidateCellStateArguments(args []string) error {
8185
}
8286
}
8387

84-
func FetchCellRegistration(bbsClient bbs.Client, cellId string) (*models.CellPresence, error) {
85-
logger := globalLogger.Session("fetch-cell-presence")
88+
func FetchCellRegistration(bbsClient bbs.Client, traceID string, cellId string) (*models.CellPresence, error) {
89+
logger := trace.LoggerWithTraceInfo(globalLogger.Session("fetch-cell-presence"), traceID)
8690

87-
cells, err := bbsClient.Cells(logger)
91+
cells, err := bbsClient.Cells(logger, traceID)
8892
if err != nil {
8993
return nil, err
9094
}
@@ -98,13 +102,13 @@ func FetchCellRegistration(bbsClient bbs.Client, cellId string) (*models.CellPre
98102
return nil, errors.New("Cell not found")
99103
}
100104

101-
func FetchCellState(stdout, stderr io.Writer, clientFactory rep.ClientFactory, registration *models.CellPresence) error {
102-
repClient, err := clientFactory.CreateClient(registration.RepAddress, registration.RepUrl)
105+
func FetchCellState(stdout, stderr io.Writer, clientFactory rep.ClientFactory, registration *models.CellPresence, traceID string) error {
106+
repClient, err := clientFactory.CreateClient(registration.RepAddress, registration.RepUrl, traceID)
103107
if err != nil {
104108
return err
105109
}
106110

107-
logger := globalLogger.Session("cell-state")
111+
logger := trace.LoggerWithTraceInfo(globalLogger.Session("cell-state"), traceID)
108112
encoder := json.NewEncoder(stdout)
109113

110114
state, err := repClient.State(logger)

0 commit comments

Comments
 (0)