Skip to content

Commit

Permalink
Trace type is now being recorded (#1188)
Browse files Browse the repository at this point in the history
  • Loading branch information
crufter committed Feb 12, 2020
1 parent 79ad1e6 commit d76baf5
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 51 deletions.
24 changes: 16 additions & 8 deletions debug/service/handler/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,23 @@ func (d *Debug) Trace(ctx context.Context, req *proto.TraceRequest, rsp *proto.T
return err
}

for _, trace := range traces {
for _, t := range traces {
var typ proto.SpanType
switch t.Type {
case trace.SpanTypeRequestInbound:
typ = proto.SpanType_INBOUND
case trace.SpanTypeRequestOutbound:
typ = proto.SpanType_OUTBOUND
}
rsp.Spans = append(rsp.Spans, &proto.Span{
Trace: trace.Trace,
Id: trace.Id,
Parent: trace.Parent,
Name: trace.Name,
Started: uint64(trace.Started.UnixNano()),
Duration: uint64(trace.Duration.Nanoseconds()),
Metadata: trace.Metadata,
Trace: t.Trace,
Id: t.Id,
Parent: t.Parent,
Name: t.Name,
Started: uint64(t.Started.UnixNano()),
Duration: uint64(t.Duration.Nanoseconds()),
Type: typ,
Metadata: t.Metadata,
})
}

Expand Down
109 changes: 73 additions & 36 deletions debug/service/proto/debug.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 12 additions & 6 deletions debug/service/proto/debug.proto
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ message LogRequest {

// Record is service log record
message Record {
// timestamp of log record
int64 timestamp = 1;
// record metadata
map<string,string> metadata = 2;
// message
string message = 3;
// timestamp of log record
int64 timestamp = 1;
// record metadata
map<string,string> metadata = 2;
// message
string message = 3;
}

message TraceRequest {
Expand All @@ -75,6 +75,11 @@ message TraceResponse {
}


enum SpanType {
INBOUND = 0;
OUTBOUND = 1;
}

message Span {
// the trace id
string trace = 1;
Expand All @@ -90,4 +95,5 @@ message Span {
uint64 duration = 6;
// associated metadata
map<string,string> metadata = 7;
SpanType type = 8;
}
15 changes: 14 additions & 1 deletion debug/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package trace

import (
"context"
"github.com/micro/go-micro/v2/metadata"
"time"

"github.com/micro/go-micro/v2/metadata"
)

// Tracer is an interface for distributed tracing
Expand All @@ -17,6 +18,16 @@ type Tracer interface {
Read(...ReadOption) ([]*Span, error)
}

// SpanType describe the nature of the trace span
type SpanType int

const (
// SpanTypeRequestInbound is a span created when serving a request
SpanTypeRequestInbound SpanType = iota
// SpanTypeRequestOutbound is a span created when making a service call
SpanTypeRequestOutbound
)

// Span is used to record an entry
type Span struct {
// Id of the trace
Expand All @@ -33,6 +44,8 @@ type Span struct {
Duration time.Duration
// associated data
Metadata map[string]string
// Type
Type SpanType
}

const (
Expand Down
2 changes: 2 additions & 0 deletions util/wrapper/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (c *clientWrapper) Publish(ctx context.Context, p client.Message, opts ...c
func (c *traceWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
newCtx, s := c.trace.Start(ctx, req.Service()+"."+req.Endpoint())

s.Type = trace.SpanTypeRequestOutbound
err := c.Client.Call(newCtx, req, rsp, opts...)
if err != nil {
s.Metadata["error"] = err.Error()
Expand Down Expand Up @@ -122,6 +123,7 @@ func TraceHandler(t trace.Tracer) server.HandlerWrapper {

// get the span
newCtx, s := t.Start(ctx, req.Service()+"."+req.Endpoint())
s.Type = trace.SpanTypeRequestInbound

err := h(newCtx, req, rsp)
if err != nil {
Expand Down

0 comments on commit d76baf5

Please sign in to comment.