diff --git a/pkg/BUILD.bazel b/pkg/BUILD.bazel index 1b54f651aca2..1fcf06c74d42 100644 --- a/pkg/BUILD.bazel +++ b/pkg/BUILD.bazel @@ -358,6 +358,8 @@ ALL_TESTS = [ "//pkg/util/timetz:timetz_test", "//pkg/util/timeutil/pgdate:pgdate_test", "//pkg/util/timeutil:timeutil_test", + "//pkg/util/tracing/collector:collector_test", + "//pkg/util/tracing/service:service_test", "//pkg/util/tracing:tracing_test", "//pkg/util/treeprinter:treeprinter_test", "//pkg/util/uint128:uint128_test", diff --git a/pkg/kv/kvserver/client_raft_test.go b/pkg/kv/kvserver/client_raft_test.go index 4a4f75247d77..4fe08a3cc32c 100644 --- a/pkg/kv/kvserver/client_raft_test.go +++ b/pkg/kv/kvserver/client_raft_test.go @@ -4321,7 +4321,7 @@ func TestStoreRangeWaitForApplication(t *testing.T) { var targets []target for _, s := range tc.Servers { - conn, err := s.NodeDialer().Dial(ctx, s.NodeID(), rpc.DefaultClass) + conn, err := s.NodeDialer().(*nodedialer.Dialer).Dial(ctx, s.NodeID(), rpc.DefaultClass) if err != nil { t.Fatal(err) } @@ -4448,7 +4448,7 @@ func TestStoreWaitForReplicaInit(t *testing.T) { defer tc.Stopper().Stop(ctx) store := tc.GetFirstStoreFromServer(t, 0) - conn, err := tc.Servers[0].NodeDialer().Dial(ctx, store.Ident.NodeID, rpc.DefaultClass) + conn, err := tc.Servers[0].NodeDialer().(*nodedialer.Dialer).Dial(ctx, store.Ident.NodeID, rpc.DefaultClass) if err != nil { t.Fatal(err) } diff --git a/pkg/server/BUILD.bazel b/pkg/server/BUILD.bazel index fb3882bf4e40..6ec0ef2df695 100644 --- a/pkg/server/BUILD.bazel +++ b/pkg/server/BUILD.bazel @@ -173,7 +173,9 @@ go_library( "//pkg/util/syncutil", "//pkg/util/timeutil", "//pkg/util/tracing", + "//pkg/util/tracing/service", "//pkg/util/tracing/tracingpb", + "//pkg/util/tracing/tracingservicepb:tracingservicepb_go_proto", "//pkg/util/uuid", "@com_github_cenkalti_backoff//:backoff", "@com_github_cockroachdb_apd_v2//:apd", diff --git a/pkg/server/server_sql.go b/pkg/server/server_sql.go index b98791b4f2a2..2461040e9725 100644 --- a/pkg/server/server_sql.go +++ b/pkg/server/server_sql.go @@ -82,6 +82,8 @@ import ( "github.com/cockroachdb/cockroach/pkg/util/stop" "github.com/cockroachdb/cockroach/pkg/util/syncutil" "github.com/cockroachdb/cockroach/pkg/util/timeutil" + "github.com/cockroachdb/cockroach/pkg/util/tracing/service" + "github.com/cockroachdb/cockroach/pkg/util/tracing/tracingservicepb" "github.com/cockroachdb/errors" "github.com/marusama/semaphore" "google.golang.org/grpc" @@ -101,6 +103,7 @@ type SQLServer struct { internalExecutor *sql.InternalExecutor leaseMgr *lease.Manager blobService *blobs.Service + tracingService *service.Service tenantConnect kvtenant.Connector // sessionRegistry can be queried for info on running SQL sessions. It is // shared between the sql.Server and the statusServer. @@ -261,6 +264,10 @@ func newSQLServer(ctx context.Context, cfg sqlServerArgs) (*SQLServer, error) { } blobspb.RegisterBlobServer(cfg.grpcServer, blobService) + // Create trace service for inter-node sharing of inflight trace spans. + tracingService := service.New(cfg.Settings.Tracer) + tracingservicepb.RegisterTracingServer(cfg.grpcServer, tracingService) + jobRegistry := cfg.circularJobRegistry { regLiveness := cfg.nodeLiveness @@ -732,6 +739,7 @@ func newSQLServer(ctx context.Context, cfg sqlServerArgs) (*SQLServer, error) { internalExecutor: cfg.circularInternalExecutor, leaseMgr: leaseMgr, blobService: blobService, + tracingService: tracingService, tenantConnect: cfg.tenantConnect, sessionRegistry: cfg.sessionRegistry, jobRegistry: jobRegistry, diff --git a/pkg/server/testserver.go b/pkg/server/testserver.go index db5d4d54838b..882e65da1724 100644 --- a/pkg/server/testserver.go +++ b/pkg/server/testserver.go @@ -376,6 +376,14 @@ func (ts *TestServer) NodeLiveness() interface{} { return nil } +// NodeDialer returns the NodeDialer used by the TestServer. +func (ts *TestServer) NodeDialer() interface{} { + if ts != nil { + return ts.nodeDialer + } + return nil +} + // HeartbeatNodeLiveness heartbeats the server's NodeLiveness record. func (ts *TestServer) HeartbeatNodeLiveness() error { if ts == nil { @@ -437,14 +445,6 @@ func (ts *TestServer) RaftTransport() *kvserver.RaftTransport { return nil } -// NodeDialer returns the NodeDialer used by the TestServer. -func (ts *TestServer) NodeDialer() *nodedialer.Dialer { - if ts != nil { - return ts.nodeDialer - } - return nil -} - // Start starts the TestServer by bootstrapping an in-memory store // (defaults to maximum of 100M). The server is started, launching the // node RPC server and all HTTP endpoints. Use the value of diff --git a/pkg/testutils/serverutils/test_server_shim.go b/pkg/testutils/serverutils/test_server_shim.go index d377082336d2..565c07d6eb70 100644 --- a/pkg/testutils/serverutils/test_server_shim.go +++ b/pkg/testutils/serverutils/test_server_shim.go @@ -136,6 +136,10 @@ type TestServerInterface interface { // HeartbeatNodeLiveness heartbeats the server's NodeLiveness record. HeartbeatNodeLiveness() error + // NodeDialer exposes the NodeDialer instance used by the TestServer as an + // interface{}. + NodeDialer() interface{} + // SetDistSQLSpanResolver changes the SpanResolver used for DistSQL inside the // server's executor. The argument must be a physicalplan.SpanResolver // instance. diff --git a/pkg/testutils/testcluster/BUILD.bazel b/pkg/testutils/testcluster/BUILD.bazel index 7f5f0bbb74b3..528434ad5c29 100644 --- a/pkg/testutils/testcluster/BUILD.bazel +++ b/pkg/testutils/testcluster/BUILD.bazel @@ -14,6 +14,7 @@ go_library( "//pkg/kv/kvserver/liveness/livenesspb", "//pkg/roachpb", "//pkg/rpc", + "//pkg/rpc/nodedialer", "//pkg/server", "//pkg/server/serverpb", "//pkg/storage", diff --git a/pkg/testutils/testcluster/testcluster.go b/pkg/testutils/testcluster/testcluster.go index 933c02a93ffd..b5710bf77267 100644 --- a/pkg/testutils/testcluster/testcluster.go +++ b/pkg/testutils/testcluster/testcluster.go @@ -29,6 +29,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/kv/kvserver/liveness/livenesspb" "github.com/cockroachdb/cockroach/pkg/roachpb" "github.com/cockroachdb/cockroach/pkg/rpc" + "github.com/cockroachdb/cockroach/pkg/rpc/nodedialer" "github.com/cockroachdb/cockroach/pkg/server" "github.com/cockroachdb/cockroach/pkg/server/serverpb" "github.com/cockroachdb/cockroach/pkg/storage" @@ -1439,7 +1440,7 @@ func (tc *TestCluster) RestartServerWithInspect(idx int, inspect func(s *server. } for i := 0; i < rpc.NumConnectionClasses; i++ { class := rpc.ConnectionClass(i) - if _, err := s.NodeDialer().Dial(ctx, srv.NodeID(), class); err != nil { + if _, err := s.NodeDialer().(*nodedialer.Dialer).Dial(ctx, srv.NodeID(), class); err != nil { return errors.Wrapf(err, "connecting n%d->n%d (class %v)", s.NodeID(), srv.NodeID(), class) } } diff --git a/pkg/util/tracing/collector/BUILD.bazel b/pkg/util/tracing/collector/BUILD.bazel new file mode 100644 index 000000000000..398ca2ba1816 --- /dev/null +++ b/pkg/util/tracing/collector/BUILD.bazel @@ -0,0 +1,50 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "collector", + srcs = [ + "collector.go", + "nodes.go", + ], + importpath = "github.com/cockroachdb/cockroach/pkg/util/tracing/collector", + visibility = ["//visibility:public"], + deps = [ + "//pkg/kv/kvserver/liveness/livenesspb", + "//pkg/roachpb", + "//pkg/rpc", + "//pkg/rpc/nodedialer", + "//pkg/util/ctxgroup", + "//pkg/util/log", + "//pkg/util/quotapool", + "//pkg/util/syncutil", + "//pkg/util/tracing", + "//pkg/util/tracing/tracingpb", + "//pkg/util/tracing/tracingservicepb:tracingservicepb_go_proto", + "@com_github_cockroachdb_redact//:redact", + ], +) + +go_test( + name = "collector_test", + srcs = [ + "collector_test.go", + "main_test.go", + ], + deps = [ + ":collector", + "//pkg/base", + "//pkg/ccl/utilccl", + "//pkg/kv/kvserver/liveness", + "//pkg/rpc/nodedialer", + "//pkg/security", + "//pkg/security/securitytest", + "//pkg/server", + "//pkg/testutils/serverutils", + "//pkg/testutils/testcluster", + "//pkg/util/leaktest", + "//pkg/util/randutil", + "//pkg/util/tracing", + "@com_github_gogo_protobuf//types", + "@com_github_stretchr_testify//require", + ], +) diff --git a/pkg/util/tracing/collector/collector.go b/pkg/util/tracing/collector/collector.go new file mode 100644 index 000000000000..4419c1f21c37 --- /dev/null +++ b/pkg/util/tracing/collector/collector.go @@ -0,0 +1,111 @@ +// Copyright 2021 The Cockroach Authors. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +package collector + +import ( + "context" + "sort" + + "github.com/cockroachdb/cockroach/pkg/kv/kvserver/liveness/livenesspb" + "github.com/cockroachdb/cockroach/pkg/roachpb" + "github.com/cockroachdb/cockroach/pkg/rpc" + "github.com/cockroachdb/cockroach/pkg/rpc/nodedialer" + "github.com/cockroachdb/cockroach/pkg/util/ctxgroup" + "github.com/cockroachdb/cockroach/pkg/util/log" + "github.com/cockroachdb/cockroach/pkg/util/quotapool" + "github.com/cockroachdb/cockroach/pkg/util/syncutil" + "github.com/cockroachdb/cockroach/pkg/util/tracing" + "github.com/cockroachdb/cockroach/pkg/util/tracing/tracingpb" + "github.com/cockroachdb/cockroach/pkg/util/tracing/tracingservicepb" +) + +// NodeLiveness is the subset of the interface satisfied by CRDB's node liveness +// component that the tracing service relies upon. +type NodeLiveness interface { + GetLivenessesFromKV(context.Context) ([]livenesspb.Liveness, error) + IsLive(roachpb.NodeID) (bool, error) +} + +// TraceCollector can be used to extract recordings from inflight spans for a +// given traceID, from all nodes of the cluster. +type TraceCollector struct { + tracer *tracing.Tracer + dialer *nodedialer.Dialer + nodeliveness NodeLiveness +} + +// New returns a TraceCollector. +func New( + dialer *nodedialer.Dialer, nodeliveness NodeLiveness, tracer *tracing.Tracer, +) *TraceCollector { + return &TraceCollector{ + dialer: dialer, + nodeliveness: nodeliveness, + tracer: tracer, + } +} + +// GetSpanRecordingsFromCluster returns the inflight span recordings from all +// nodes in the cluster. +// This method does not distinguish between requests for local and remote +// inflight spans, and relies on gRPC short circuiting local requests. +func (t *TraceCollector) GetSpanRecordingsFromCluster( + ctx context.Context, traceID uint64, +) ([]tracingpb.RecordedSpan, error) { + var res []tracingpb.RecordedSpan + ns, err := nodesFromNodeLiveness(ctx, t.nodeliveness) + if err != nil { + return res, err + } + + // Collect spans from all clients. + // We'll want to rate limit outgoing RPCs (limit pulled out of thin air). + var mu syncutil.Mutex + qp := quotapool.NewIntPool("every-node", 25) + log.Infof(ctx, "executing GetSpanRecordings on nodes %s", ns) + grp := ctxgroup.WithContext(ctx) + + for _, node := range ns { + id := node.id // copy out of the loop variable + alloc, err := qp.Acquire(ctx, 1) + if err != nil { + return res, err + } + + grp.GoCtx(func(ctx context.Context) error { + defer alloc.Release() + + conn, err := t.dialer.Dial(ctx, id, rpc.DefaultClass) + if err != nil { + return err + } + traceClient := tracingservicepb.NewTracingClient(conn) + resp, err := traceClient.GetSpanRecordings(ctx, + &tracingservicepb.SpanRecordingRequest{TraceID: traceID}) + if err != nil { + return err + } + mu.Lock() + res = append(res, resp.SpanRecordings...) + mu.Unlock() + return nil + }) + } + if err := grp.Wait(); err != nil { + return res, err + } + + sort.SliceStable(res, func(i, j int) bool { + return res[i].StartTime.Before(res[j].StartTime) + }) + + return res, nil +} diff --git a/pkg/util/tracing/collector/collector_test.go b/pkg/util/tracing/collector/collector_test.go new file mode 100644 index 000000000000..a7f3a3b69ba9 --- /dev/null +++ b/pkg/util/tracing/collector/collector_test.go @@ -0,0 +1,167 @@ +// Copyright 2021 The Cockroach Authors. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +package collector_test + +import ( + "context" + "fmt" + "testing" + "time" + + "github.com/cockroachdb/cockroach/pkg/base" + "github.com/cockroachdb/cockroach/pkg/kv/kvserver/liveness" + "github.com/cockroachdb/cockroach/pkg/rpc/nodedialer" + "github.com/cockroachdb/cockroach/pkg/testutils/testcluster" + "github.com/cockroachdb/cockroach/pkg/util/leaktest" + "github.com/cockroachdb/cockroach/pkg/util/tracing" + "github.com/cockroachdb/cockroach/pkg/util/tracing/collector" + "github.com/gogo/protobuf/types" + "github.com/stretchr/testify/require" +) + +// testStructuredImpl is a testing implementation of Structured event. +type testStructuredImpl struct { + *types.StringValue +} + +var _ tracing.Structured = &testStructuredImpl{} + +func (t *testStructuredImpl) String() string { + return fmt.Sprintf("structured=%s", t.Value) +} + +func newTestStructured(i string) *testStructuredImpl { + return &testStructuredImpl{ + &types.StringValue{Value: i}, + } +} + +// setupTraces takes two tracers (potentially on different nodes), and creates +// two span hierarchies as depicted below. The method returns the traceIDs for +// both these span hierarchies, along with a cleanup method to Finish() all the +// opened spans. +// +// Trace for t1: +// ------------- +// root <-- traceID1 +// root.child <-- traceID1 +// root2.child.remotechild <-- traceID2 +// root2.child.remotechild2 <-- traceID2 +// +// Trace for t2: +// ------------- +// root.child.remotechild <-- traceID1 +// root.child.remotechilddone <-- traceID1 +// root2 <-- traceID2 +// root2.child <-- traceID2 +func setupTraces(t1, t2 *tracing.Tracer) (uint64, uint64, func()) { + // Start a root span on "node 1". + root := t1.StartSpan("root", tracing.WithForceRealSpan()) + root.SetVerbose(true) + root.RecordStructured(newTestStructured("root")) + + time.Sleep(10 * time.Millisecond) + + // Start a child span on "node 1". + child := t1.StartSpan("root.child", tracing.WithParentAndAutoCollection(root)) + + // Sleep a bit so that everything that comes afterwards has higher timestamps + // than the one we just assigned. Otherwise the sorting is not deterministic. + time.Sleep(10 * time.Millisecond) + + // Start a remote child span on "node 2". + childRemoteChild := t2.StartSpan("root.child.remotechild", tracing.WithParentAndManualCollection(child.Meta())) + childRemoteChild.RecordStructured(newTestStructured("root.child.remotechild")) + + time.Sleep(10 * time.Millisecond) + + // Start another remote child span on "node 2" that we finish. + childRemoteChildFinished := t2.StartSpan("root.child.remotechilddone", tracing.WithParentAndManualCollection(child.Meta())) + childRemoteChildFinished.Finish() + child.ImportRemoteSpans(childRemoteChildFinished.GetRecording()) + + // Start a root span on "node 2". + root2 := t2.StartSpan("root2", tracing.WithForceRealSpan()) + root2.SetVerbose(true) + root2.RecordStructured(newTestStructured("root2")) + + // Start a child span on "node 2". + child2 := t2.StartSpan("root2.child", tracing.WithParentAndAutoCollection(root2)) + // Start a remote child span on "node 1". + child2RemoteChild := t1.StartSpan("root2.child.remotechild", tracing.WithParentAndManualCollection(child2.Meta())) + + time.Sleep(10 * time.Millisecond) + + // Start another remote child span on "node 1". + anotherChild2RemoteChild := t1.StartSpan("root2.child.remotechild2", tracing.WithParentAndManualCollection(child2.Meta())) + return root.TraceID(), root2.TraceID(), func() { + for _, span := range []*tracing.Span{root, child, childRemoteChild, root2, child2, + child2RemoteChild, anotherChild2RemoteChild} { + span.Finish() + } + } +} + +func TestTracingCollectorGetSpanRecordings(t *testing.T) { + defer leaktest.AfterTest(t)() + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + args := base.TestClusterArgs{} + tc := testcluster.StartTestCluster(t, 2 /* nodes */, args) + defer tc.Stopper().Stop(ctx) + + localTracer := tc.Server(0).Tracer().(*tracing.Tracer) + remoteTracer := tc.Server(1).Tracer().(*tracing.Tracer) + + traceCollector := collector.New( + tc.Server(0).NodeDialer().(*nodedialer.Dialer), + tc.Server(0).NodeLiveness().(*liveness.NodeLiveness), localTracer) + localTraceID, remoteTraceID, cleanup := setupTraces(localTracer, remoteTracer) + defer cleanup() + + t.Run("fetch-local-recordings", func(t *testing.T) { + recordedSpan, err := traceCollector.GetSpanRecordingsFromCluster(ctx, localTraceID) + require.NoError(t, err) + + require.NoError(t, tracing.TestingCheckRecordedSpans(recordedSpan, ` + span: root + tags: _unfinished=1 _verbose=1 + event: structured=root + span: root.child + tags: _unfinished=1 _verbose=1 + span: root.child.remotechild + tags: _unfinished=1 _verbose=1 + event: structured=root.child.remotechild + span: root.child.remotechilddone + tags: _verbose=1 +`)) + }) + + // The traceCollector is running on node 1, so most of the recordings for this + // subtest will be passed back by node 2 over RPC. + t.Run("fetch-remote-recordings", func(t *testing.T) { + recordedSpan, err := traceCollector.GetSpanRecordingsFromCluster(ctx, remoteTraceID) + require.NoError(t, err) + + require.NoError(t, tracing.TestingCheckRecordedSpans(recordedSpan, ` + span: root2 + tags: _unfinished=1 _verbose=1 + event: structured=root2 + span: root2.child + tags: _unfinished=1 _verbose=1 + span: root2.child.remotechild + tags: _unfinished=1 _verbose=1 + span: root2.child.remotechild2 + tags: _unfinished=1 _verbose=1 +`)) + }) +} diff --git a/pkg/util/tracing/collector/main_test.go b/pkg/util/tracing/collector/main_test.go new file mode 100644 index 000000000000..ed79ee67f334 --- /dev/null +++ b/pkg/util/tracing/collector/main_test.go @@ -0,0 +1,33 @@ +// Copyright 2021 The Cockroach Authors. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +package collector_test + +import ( + "os" + "testing" + + "github.com/cockroachdb/cockroach/pkg/ccl/utilccl" + "github.com/cockroachdb/cockroach/pkg/security" + "github.com/cockroachdb/cockroach/pkg/security/securitytest" + "github.com/cockroachdb/cockroach/pkg/server" + "github.com/cockroachdb/cockroach/pkg/testutils/serverutils" + "github.com/cockroachdb/cockroach/pkg/util/randutil" +) + +func TestMain(m *testing.M) { + defer utilccl.TestingEnableEnterprise()() + security.SetAssetLoader(securitytest.EmbeddedAssets) + randutil.SeedForTests() + serverutils.InitTestServerFactory(server.TestServerFactory) + os.Exit(m.Run()) +} + +//go:generate ../../leaktest/add-leaktest.sh *_test.go diff --git a/pkg/util/tracing/collector/nodes.go b/pkg/util/tracing/collector/nodes.go new file mode 100644 index 000000000000..ace6b977e503 --- /dev/null +++ b/pkg/util/tracing/collector/nodes.go @@ -0,0 +1,67 @@ +// Copyright 2021 The Cockroach Authors. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +package collector + +import ( + "context" + + "github.com/cockroachdb/cockroach/pkg/roachpb" + "github.com/cockroachdb/redact" +) + +// node captures the relevant bits of each node as it pertains to the tracing +// service infrastructure. +type node struct { + id roachpb.NodeID +} + +// nodes is a collection of node objects. +type nodes []node + +// nodesFromNodeLiveness returns the IDs for all nodes that are currently part +// of the cluster (i.e. they haven't been decommissioned away). This list might +// also include nodes that are dead, in which case the RPC to collect traces +// from the dead node will timeout, and we will be able to better surface that +// error. +// +// It's important to note that this makes no guarantees about new nodes being +// added to the cluster. It's entirely possible for that to happen concurrently +// with the retrieval of the current set of nodes. +func nodesFromNodeLiveness(ctx context.Context, nl NodeLiveness) (nodes, error) { + var ns []node + ls, err := nl.GetLivenessesFromKV(ctx) + if err != nil { + return nil, err + } + for _, l := range ls { + if l.Membership.Decommissioned() { + continue + } + ns = append(ns, node{id: l.NodeID}) + } + return ns, nil +} + +func (ns nodes) String() string { + return redact.StringWithoutMarkers(ns) +} + +// SafeFormat implements redact.SafeFormatter. +func (ns nodes) SafeFormat(s redact.SafePrinter, _ rune) { + s.SafeString("n{") + if len(ns) > 0 { + s.Printf("%d", ns[0].id) + for _, node := range ns[1:] { + s.Printf(",%d", node.id) + } + } + s.SafeString("}") +} diff --git a/pkg/util/tracing/service/BUILD.bazel b/pkg/util/tracing/service/BUILD.bazel new file mode 100644 index 000000000000..5fc85ef6ec25 --- /dev/null +++ b/pkg/util/tracing/service/BUILD.bazel @@ -0,0 +1,24 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "service", + srcs = ["service.go"], + importpath = "github.com/cockroachdb/cockroach/pkg/util/tracing/service", + visibility = ["//visibility:public"], + deps = [ + "//pkg/util/tracing", + "//pkg/util/tracing/tracingservicepb:tracingservicepb_go_proto", + ], +) + +go_test( + name = "service_test", + srcs = ["service_test.go"], + embed = [":service"], + deps = [ + "//pkg/util/leaktest", + "//pkg/util/tracing", + "//pkg/util/tracing/tracingservicepb:tracingservicepb_go_proto", + "@com_github_stretchr_testify//require", + ], +) diff --git a/pkg/util/tracing/service/service.go b/pkg/util/tracing/service/service.go new file mode 100644 index 000000000000..1b1b2dfdc7dd --- /dev/null +++ b/pkg/util/tracing/service/service.go @@ -0,0 +1,58 @@ +// Copyright 2021 The Cockroach Authors. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +/* +Package service contains a gRPC service to be used for remote inflight +span access. + +It is used for pulling inflight spans from all CockroachDB nodes. Each node will +run a trace service, which serves the inflight spans from the local span +registry on that node. Each node will also have a trace collector, which uses +the nodedialer to connect to another node's trace service, and access its +inflight spans. +*/ +package service + +import ( + "context" + + "github.com/cockroachdb/cockroach/pkg/util/tracing" + "github.com/cockroachdb/cockroach/pkg/util/tracing/tracingservicepb" +) + +// Service implements the gRPC TraceServer that exchanges inflight span +// recordings between different nodes. +type Service struct { + tracer *tracing.Tracer +} + +var _ tracingservicepb.TracingServer = &Service{} + +// New instantiates a tracing service server. +func New(tracer *tracing.Tracer) *Service { + return &Service{tracer: tracer} +} + +// GetSpanRecordings implements the tracingpb.TraceServer interface. +func (s *Service) GetSpanRecordings( + _ context.Context, request *tracingservicepb.SpanRecordingRequest, +) (*tracingservicepb.SpanRecordingResponse, error) { + var resp tracingservicepb.SpanRecordingResponse + err := s.tracer.VisitSpans(func(span *tracing.Span) error { + if span.TraceID() != request.TraceID { + return nil + } + for _, rec := range span.GetRecording() { + resp.SpanRecordings = append(resp.SpanRecordings, rec) + } + return nil + }) + return &resp, err +} diff --git a/pkg/util/tracing/service/service_test.go b/pkg/util/tracing/service/service_test.go new file mode 100644 index 000000000000..6849e3255975 --- /dev/null +++ b/pkg/util/tracing/service/service_test.go @@ -0,0 +1,72 @@ +// Copyright 2021 The Cockroach Authors. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +package service + +import ( + "context" + "sort" + "testing" + "time" + + "github.com/cockroachdb/cockroach/pkg/util/leaktest" + "github.com/cockroachdb/cockroach/pkg/util/tracing" + "github.com/cockroachdb/cockroach/pkg/util/tracing/tracingservicepb" + "github.com/stretchr/testify/require" +) + +func TestTracingServiceGetSpanRecordings(t *testing.T) { + defer leaktest.AfterTest(t)() + + tracer1 := tracing.NewTracer() + setupTraces := func() (uint64, func()) { + // Start a root span. + root1 := tracer1.StartSpan("root1", tracing.WithForceRealSpan()) + root1.SetVerbose(true) + + child1 := tracer1.StartSpan("root1.child", tracing.WithParentAndAutoCollection(root1)) + + time.Sleep(10 * time.Millisecond) + + // Create a span that will be added to the tracers' active span map, but + // will share the same traceID as root. + fork1 := tracer1.StartSpan("fork1", tracing.WithParentAndManualCollection(root1.Meta())) + + // Start span with different trace ID. + root2 := tracer1.StartSpan("root2", tracing.WithForceRealSpan()) + root2.SetVerbose(true) + root2.Record("root2") + + return root1.TraceID(), func() { + for _, span := range []*tracing.Span{root1, child1, fork1, root2} { + span.Finish() + } + } + } + + traceID1, cleanup := setupTraces() + defer cleanup() + + ctx := context.Background() + s := New(tracer1) + resp, err := s.GetSpanRecordings(ctx, &tracingservicepb.SpanRecordingRequest{TraceID: traceID1}) + require.NoError(t, err) + sort.SliceStable(resp.SpanRecordings, func(i, j int) bool { + return resp.SpanRecordings[i].StartTime.Before(resp.SpanRecordings[j].StartTime) + }) + require.NoError(t, tracing.TestingCheckRecordedSpans(resp.SpanRecordings, ` + span: root1 + tags: _unfinished=1 _verbose=1 + span: root1.child + tags: _unfinished=1 _verbose=1 + span: fork1 + tags: _unfinished=1 _verbose=1 +`)) +} diff --git a/pkg/util/tracing/tracingservicepb/BUILD.bazel b/pkg/util/tracing/tracingservicepb/BUILD.bazel new file mode 100644 index 000000000000..0cd074e67050 --- /dev/null +++ b/pkg/util/tracing/tracingservicepb/BUILD.bazel @@ -0,0 +1,25 @@ +load("@rules_proto//proto:defs.bzl", "proto_library") +load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library") + +proto_library( + name = "tracingservicepb_proto", + srcs = ["tracing_service.proto"], + strip_import_prefix = "/pkg", + visibility = ["//visibility:public"], + deps = [ + "//pkg/util/tracing/tracingpb:tracingpb_proto", + "@com_github_gogo_protobuf//gogoproto:gogo_proto", + ], +) + +go_proto_library( + name = "tracingservicepb_go_proto", + compilers = ["//pkg/cmd/protoc-gen-gogoroach:protoc-gen-gogoroach_grpc_compiler"], + importpath = "github.com/cockroachdb/cockroach/pkg/util/tracing/tracingservicepb", + proto = ":tracingservicepb_proto", + visibility = ["//visibility:public"], + deps = [ + "//pkg/util/tracing/tracingpb", + "@com_github_gogo_protobuf//gogoproto", + ], +) diff --git a/pkg/util/tracing/tracingservicepb/tracing_service.pb.go b/pkg/util/tracing/tracingservicepb/tracing_service.pb.go new file mode 100644 index 000000000000..2dd3abb205a3 --- /dev/null +++ b/pkg/util/tracing/tracingservicepb/tracing_service.pb.go @@ -0,0 +1,554 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: util/tracing/tracingservicepb/tracing_service.proto + +package tracingservicepb + +import ( + context "context" + fmt "fmt" + tracingpb "github.com/cockroachdb/cockroach/pkg/util/tracing/tracingpb" + _ "github.com/gogo/protobuf/gogoproto" + proto "github.com/gogo/protobuf/proto" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" + io "io" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package + +type SpanRecordingRequest struct { + TraceID uint64 `protobuf:"varint,1,opt,name=trace_id,json=traceId,proto3" json:"trace_id,omitempty"` +} + +func (m *SpanRecordingRequest) Reset() { *m = SpanRecordingRequest{} } +func (m *SpanRecordingRequest) String() string { return proto.CompactTextString(m) } +func (*SpanRecordingRequest) ProtoMessage() {} +func (*SpanRecordingRequest) Descriptor() ([]byte, []int) { + return fileDescriptor_29b78bec82996ca3, []int{0} +} +func (m *SpanRecordingRequest) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *SpanRecordingRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil +} +func (m *SpanRecordingRequest) XXX_Merge(src proto.Message) { + xxx_messageInfo_SpanRecordingRequest.Merge(m, src) +} +func (m *SpanRecordingRequest) XXX_Size() int { + return m.Size() +} +func (m *SpanRecordingRequest) XXX_DiscardUnknown() { + xxx_messageInfo_SpanRecordingRequest.DiscardUnknown(m) +} + +var xxx_messageInfo_SpanRecordingRequest proto.InternalMessageInfo + +type SpanRecordingResponse struct { + SpanRecordings []tracingpb.RecordedSpan `protobuf:"bytes,1,rep,name=span_recordings,json=spanRecordings,proto3" json:"span_recordings"` +} + +func (m *SpanRecordingResponse) Reset() { *m = SpanRecordingResponse{} } +func (m *SpanRecordingResponse) String() string { return proto.CompactTextString(m) } +func (*SpanRecordingResponse) ProtoMessage() {} +func (*SpanRecordingResponse) Descriptor() ([]byte, []int) { + return fileDescriptor_29b78bec82996ca3, []int{1} +} +func (m *SpanRecordingResponse) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *SpanRecordingResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil +} +func (m *SpanRecordingResponse) XXX_Merge(src proto.Message) { + xxx_messageInfo_SpanRecordingResponse.Merge(m, src) +} +func (m *SpanRecordingResponse) XXX_Size() int { + return m.Size() +} +func (m *SpanRecordingResponse) XXX_DiscardUnknown() { + xxx_messageInfo_SpanRecordingResponse.DiscardUnknown(m) +} + +var xxx_messageInfo_SpanRecordingResponse proto.InternalMessageInfo + +func init() { + proto.RegisterType((*SpanRecordingRequest)(nil), "cockroach.util.tracing.SpanRecordingRequest") + proto.RegisterType((*SpanRecordingResponse)(nil), "cockroach.util.tracing.SpanRecordingResponse") +} + +func init() { + proto.RegisterFile("util/tracing/tracingservicepb/tracing_service.proto", fileDescriptor_29b78bec82996ca3) +} + +var fileDescriptor_29b78bec82996ca3 = []byte{ + // 301 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x51, 0x4d, 0x4b, 0xc3, 0x30, + 0x00, 0x4d, 0x70, 0x38, 0xc9, 0xc0, 0x8f, 0x32, 0x65, 0xec, 0x90, 0x8d, 0x1d, 0x64, 0x88, 0x66, + 0xd0, 0xdd, 0x3d, 0x14, 0x41, 0x76, 0xad, 0x9e, 0x04, 0x29, 0x6d, 0x1a, 0x6a, 0x50, 0x92, 0x98, + 0x74, 0xbb, 0xf9, 0x1f, 0xfc, 0x59, 0x3d, 0xee, 0xb8, 0xd3, 0xd0, 0xf6, 0x8f, 0x48, 0xfa, 0x21, + 0x3a, 0x2a, 0x78, 0x6a, 0x79, 0x1f, 0x79, 0xef, 0x25, 0x68, 0xbe, 0x4c, 0xf9, 0xcb, 0x2c, 0xd5, + 0x21, 0xe5, 0x22, 0x69, 0xbe, 0x86, 0xe9, 0x15, 0xa7, 0x4c, 0x45, 0x0d, 0x10, 0xd4, 0x08, 0x51, + 0x5a, 0xa6, 0xd2, 0x39, 0xa3, 0x92, 0x3e, 0x6b, 0x19, 0xd2, 0x27, 0x62, 0xed, 0xa4, 0x56, 0x0d, + 0xfb, 0x89, 0x4c, 0x64, 0x29, 0x99, 0xd9, 0xbf, 0x4a, 0x3d, 0xbc, 0x68, 0x8b, 0x50, 0xd1, 0x4c, + 0x33, 0x2a, 0x75, 0xcc, 0xe2, 0xc0, 0xa8, 0x50, 0x54, 0xda, 0xc9, 0x35, 0xea, 0xdf, 0xa9, 0x50, + 0xf8, 0x25, 0xc5, 0x45, 0xe2, 0xb3, 0xd7, 0x25, 0x33, 0xa9, 0x73, 0x8e, 0x0e, 0xac, 0x91, 0x05, + 0x3c, 0x1e, 0xc0, 0x31, 0x9c, 0x76, 0xbc, 0x5e, 0xbe, 0x1d, 0x75, 0xef, 0x2d, 0xb6, 0xb8, 0xf1, + 0xbb, 0x25, 0xb9, 0x88, 0x27, 0x2b, 0x74, 0xba, 0xe3, 0x37, 0x4a, 0x0a, 0xc3, 0x9c, 0x47, 0x74, + 0x64, 0x63, 0x02, 0xdd, 0x30, 0x66, 0x00, 0xc7, 0x7b, 0xd3, 0x9e, 0x4b, 0x48, 0xfb, 0x18, 0xf2, + 0x5d, 0x94, 0xf8, 0x75, 0x51, 0x7b, 0xb2, 0xd7, 0xc9, 0xb6, 0x23, 0xe0, 0x1f, 0x9a, 0x9f, 0x29, + 0xc6, 0x7d, 0x43, 0x65, 0x17, 0x2e, 0x12, 0x47, 0xa3, 0x93, 0x5b, 0x96, 0xfe, 0x6a, 0x61, 0x9c, + 0xcb, 0xbf, 0x52, 0xda, 0xd6, 0x0e, 0xaf, 0xfe, 0xa9, 0xae, 0xb6, 0x4d, 0x80, 0xe7, 0x66, 0x9f, + 0x18, 0x64, 0x39, 0x86, 0xeb, 0x1c, 0xc3, 0x4d, 0x8e, 0xe1, 0x47, 0x8e, 0xe1, 0x7b, 0x81, 0xc1, + 0xba, 0xc0, 0x60, 0x53, 0x60, 0xf0, 0x70, 0xbc, 0xfb, 0xb4, 0xd1, 0x7e, 0x79, 0xe3, 0xf3, 0xaf, + 0x00, 0x00, 0x00, 0xff, 0xff, 0xe4, 0xe8, 0x32, 0x41, 0x02, 0x02, 0x00, 0x00, +} + +// Reference imports to suppress errors if they are not otherwise used. +var _ context.Context +var _ grpc.ClientConn + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +const _ = grpc.SupportPackageIsVersion4 + +// TracingClient is the client API for Tracing service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. +type TracingClient interface { + GetSpanRecordings(ctx context.Context, in *SpanRecordingRequest, opts ...grpc.CallOption) (*SpanRecordingResponse, error) +} + +type tracingClient struct { + cc *grpc.ClientConn +} + +func NewTracingClient(cc *grpc.ClientConn) TracingClient { + return &tracingClient{cc} +} + +func (c *tracingClient) GetSpanRecordings(ctx context.Context, in *SpanRecordingRequest, opts ...grpc.CallOption) (*SpanRecordingResponse, error) { + out := new(SpanRecordingResponse) + err := c.cc.Invoke(ctx, "/cockroach.util.tracing.Tracing/GetSpanRecordings", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// TracingServer is the server API for Tracing service. +type TracingServer interface { + GetSpanRecordings(context.Context, *SpanRecordingRequest) (*SpanRecordingResponse, error) +} + +// UnimplementedTracingServer can be embedded to have forward compatible implementations. +type UnimplementedTracingServer struct { +} + +func (*UnimplementedTracingServer) GetSpanRecordings(ctx context.Context, req *SpanRecordingRequest) (*SpanRecordingResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method GetSpanRecordings not implemented") +} + +func RegisterTracingServer(s *grpc.Server, srv TracingServer) { + s.RegisterService(&_Tracing_serviceDesc, srv) +} + +func _Tracing_GetSpanRecordings_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(SpanRecordingRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(TracingServer).GetSpanRecordings(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/cockroach.util.tracing.Tracing/GetSpanRecordings", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(TracingServer).GetSpanRecordings(ctx, req.(*SpanRecordingRequest)) + } + return interceptor(ctx, in, info, handler) +} + +var _Tracing_serviceDesc = grpc.ServiceDesc{ + ServiceName: "cockroach.util.tracing.Tracing", + HandlerType: (*TracingServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "GetSpanRecordings", + Handler: _Tracing_GetSpanRecordings_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "util/tracing/tracingservicepb/tracing_service.proto", +} + +func (m *SpanRecordingRequest) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *SpanRecordingRequest) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *SpanRecordingRequest) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.TraceID != 0 { + i = encodeVarintTracingService(dAtA, i, uint64(m.TraceID)) + i-- + dAtA[i] = 0x8 + } + return len(dAtA) - i, nil +} + +func (m *SpanRecordingResponse) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *SpanRecordingResponse) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *SpanRecordingResponse) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.SpanRecordings) > 0 { + for iNdEx := len(m.SpanRecordings) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.SpanRecordings[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintTracingService(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0xa + } + } + return len(dAtA) - i, nil +} + +func encodeVarintTracingService(dAtA []byte, offset int, v uint64) int { + offset -= sovTracingService(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *SpanRecordingRequest) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.TraceID != 0 { + n += 1 + sovTracingService(uint64(m.TraceID)) + } + return n +} + +func (m *SpanRecordingResponse) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if len(m.SpanRecordings) > 0 { + for _, e := range m.SpanRecordings { + l = e.Size() + n += 1 + l + sovTracingService(uint64(l)) + } + } + return n +} + +func sovTracingService(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozTracingService(x uint64) (n int) { + return sovTracingService(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *SpanRecordingRequest) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTracingService + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: SpanRecordingRequest: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: SpanRecordingRequest: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field TraceID", wireType) + } + m.TraceID = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTracingService + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.TraceID |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + default: + iNdEx = preIndex + skippy, err := skipTracingService(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthTracingService + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *SpanRecordingResponse) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTracingService + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: SpanRecordingResponse: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: SpanRecordingResponse: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field SpanRecordings", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTracingService + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthTracingService + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthTracingService + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.SpanRecordings = append(m.SpanRecordings, tracingpb.RecordedSpan{}) + if err := m.SpanRecordings[len(m.SpanRecordings)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipTracingService(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthTracingService + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipTracingService(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + depth := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowTracingService + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowTracingService + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + case 1: + iNdEx += 8 + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowTracingService + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if length < 0 { + return 0, ErrInvalidLengthTracingService + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupTracingService + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthTracingService + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthTracingService = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowTracingService = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupTracingService = fmt.Errorf("proto: unexpected end of group") +) diff --git a/pkg/util/tracing/tracingservicepb/tracing_service.proto b/pkg/util/tracing/tracingservicepb/tracing_service.proto new file mode 100644 index 000000000000..3f421baeef35 --- /dev/null +++ b/pkg/util/tracing/tracingservicepb/tracing_service.proto @@ -0,0 +1,28 @@ +// Copyright 2021 The Cockroach Authors. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +syntax = "proto3"; +package cockroach.util.tracing; +option go_package = "tracingservicepb"; + +import "gogoproto/gogo.proto"; +import "util/tracing/tracingpb/recorded_span.proto"; + +message SpanRecordingRequest { + uint64 trace_id = 1 [(gogoproto.customname) = "TraceID"]; +} + +message SpanRecordingResponse { + repeated tracing.tracingpb.RecordedSpan span_recordings = 1 [(gogoproto.nullable) = false]; +} + +service Tracing { + rpc GetSpanRecordings(SpanRecordingRequest) returns (SpanRecordingResponse) {} +}