Skip to content
This repository was archived by the owner on Aug 17, 2020. It is now read-only.
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
18 changes: 9 additions & 9 deletions agent/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,12 @@ func (r *SpanRecorder) getPayloadComponents(span tracer.RawSpan) (PayloadSpan, [
if span.ParentSpanID != 0 {
parentSpanID = fmt.Sprintf("%x", span.ParentSpanID)
}
traceId := tracer.UUIDToString(span.Context.TraceID)
spanId := fmt.Sprintf("%x", span.Context.SpanID)
payloadSpan := PayloadSpan{
"context": map[string]interface{}{
"trace_id": tracer.UUIDToString(span.Context.TraceID),
"span_id": fmt.Sprintf("%x", span.Context.SpanID),
"trace_id": traceId,
"span_id": spanId,
"baggage": span.Context.Baggage,
},
"parent_span_id": parentSpanID,
Expand All @@ -339,16 +341,14 @@ func (r *SpanRecorder) getPayloadComponents(span tracer.RawSpan) (PayloadSpan, [
for _, event := range span.Logs {
var fields = make(map[string]interface{})
for _, field := range event.Fields {
fields[field.Key()] = field.Value()
}
eventId, err := uuid.NewRandom()
if err != nil {
panic(err)
value := field.Value()
fields[field.Key()] = value
}
eventId := uuid.New()
events = append(events, PayloadEvent{
"context": map[string]interface{}{
"trace_id": tracer.UUIDToString(span.Context.TraceID),
"span_id": fmt.Sprintf("%x", span.Context.SpanID),
"trace_id": traceId,
"span_id": spanId,
"event_id": eventId.String(),
},
"timestamp": r.applyNTPOffset(event.Timestamp).Format(time.RFC3339Nano),
Expand Down
18 changes: 5 additions & 13 deletions errors/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ import (
"go.undefinedlabs.com/scopeagent/tracer"
)

const (
EventType = "event"
EventSource = "source"
EventMessage = "message"
EventStack = "stack"
EventException = "exception"
)

type StackFrames struct {
File string
LineNumber int
Expand Down Expand Up @@ -136,11 +128,11 @@ func getExceptionLogFields(eventType string, recoverData interface{}, skipFrames
}

fields := make([]log.Field, 5)
fields[0] = log.String(EventType, eventType)
fields[1] = log.String(EventSource, source)
fields[2] = log.String(EventMessage, errMessage)
fields[3] = log.String(EventStack, getStringStack(err, errStack))
fields[4] = log.Object(EventException, exceptionData)
fields[0] = log.String(tags.EventType, eventType)
fields[1] = log.String(tags.EventSource, source)
fields[2] = log.String(tags.EventMessage, errMessage)
fields[3] = log.String(tags.EventStack, getStringStack(err, errStack))
fields[4] = log.Object(tags.EventException, exceptionData)
return fields
}
return nil
Expand Down
7 changes: 6 additions & 1 deletion instrumentation/gocheck/instrumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"go.undefinedlabs.com/scopeagent/instrumentation/coverage"
"go.undefinedlabs.com/scopeagent/instrumentation/logging"
"go.undefinedlabs.com/scopeagent/tags"
scopetracer "go.undefinedlabs.com/scopeagent/tracer"

chk "gopkg.in/check.v1"
)
Expand Down Expand Up @@ -103,7 +104,11 @@ func (test *Test) end(c *chk.C) {

if testing.CoverMode() != "" {
if cov := coverage.EndCoverage(); cov != nil {
test.span.SetTag(tags.Coverage, *cov)
if span, ok := test.span.(scopetracer.Span); ok {
span.UnsafeSetTag(tags.Coverage, *cov)
} else {
test.span.SetTag(tags.Coverage, *cov)
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions instrumentation/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/log"
"go.undefinedlabs.com/scopeagent/instrumentation"
scopetracer "go.undefinedlabs.com/scopeagent/tracer"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
Expand Down Expand Up @@ -214,6 +215,8 @@ func (cs *openTracingClientStream) Header() (metadata.MD, error) {
md, err := cs.ClientStream.Header()
if err != nil {
cs.finishFunc(err)
} else if span, ok := cs.span.(scopetracer.Span); ok {
span.UnsafeSetTag(Headers, md)
} else {
cs.span.SetTag(Headers, md)
}
Expand Down
4 changes: 2 additions & 2 deletions instrumentation/grpc/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ func SetSpanTags(span opentracing.Span, err error, client bool) {
if value, ok := codeStrings[code]; ok {
span.SetTag(Status, value)
} else {
span.SetTag(Status, code)
span.SetTag(Status, uint32(code))
}
if value, ok := classStrings[c]; ok {
span.SetTag("response_class", value)
} else {
span.SetTag("response_class", c)
span.SetTag("response_class", string(c))
}

if err == nil {
Expand Down
7 changes: 6 additions & 1 deletion instrumentation/nethttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

scopeerrors "go.undefinedlabs.com/scopeagent/errors"
"go.undefinedlabs.com/scopeagent/instrumentation"
scopetracer "go.undefinedlabs.com/scopeagent/tracer"
)

type contextKey int
Expand Down Expand Up @@ -183,7 +184,11 @@ func (t *Transport) doRoundTrip(req *http.Request) (*http.Response, error) {
tracer.start(req)

if t.Stacktrace {
tracer.sp.SetTag("stacktrace", scopeerrors.GetCurrentStackTrace(2))
if span, ok := tracer.sp.(scopetracer.Span); ok {
span.UnsafeSetTag("stacktrace", scopeerrors.GetCurrentStackTrace(2))
} else {
tracer.sp.SetTag("stacktrace", scopeerrors.GetCurrentStackTrace(2))
}
}

ext.HTTPMethod.Set(tracer.sp, req.Method)
Expand Down
7 changes: 6 additions & 1 deletion instrumentation/process/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/opentracing/opentracing-go"

"go.undefinedlabs.com/scopeagent/instrumentation"
scopetracer "go.undefinedlabs.com/scopeagent/tracer"
)

// Injects the span context to the command environment variables
Expand All @@ -27,7 +28,11 @@ func InjectToCmd(ctx context.Context, command *exec.Cmd) *exec.Cmd {
func InjectToCmdWithSpan(ctx context.Context, command *exec.Cmd) (opentracing.Span, context.Context) {
innerSpan, innerCtx := opentracing.StartSpanFromContextWithTracer(ctx, instrumentation.Tracer(),
"Exec: "+getOperationNameFromArgs(command.Args))
innerSpan.SetTag("Args", command.Args)
if sp, ok := innerSpan.(scopetracer.Span); ok {
sp.UnsafeSetTag("Args", command.Args)
} else {
innerSpan.SetTag("Args", command.Args)
}
innerSpan.SetTag("Path", command.Path)
innerSpan.SetTag("Dir", command.Dir)
InjectToCmd(innerCtx, command)
Expand Down
2 changes: 1 addition & 1 deletion instrumentation/sql/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (t *driverConfiguration) newSpan(operationName string, query string, args [
}
dbParams[name] = map[string]interface{}{
"type": reflect.TypeOf(item.Value).String(),
"value": item.Value,
"value": fmt.Sprint(item.Value),
}
}
opts = append(opts, opentracing.Tags{
Expand Down
5 changes: 3 additions & 2 deletions instrumentation/testing/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testing

import (
"context"
"go.undefinedlabs.com/scopeagent/tags"
"math"
"regexp"
"runtime"
Expand Down Expand Up @@ -141,9 +142,9 @@ func startBenchmark(b *testing.B, pc uintptr, benchFunc func(b *testing.B)) {
span.SetTag("benchmark.memory.mean_allocations", results.AllocsPerOp())
span.SetTag("benchmark.memory.mean_bytes_allocations", results.AllocedBytesPerOp())
if result {
span.SetTag("test.status", "PASS")
span.SetTag("test.status", tags.TestStatus_PASS)
} else {
span.SetTag("test.status", "FAIL")
span.SetTag("test.status", tags.TestStatus_FAIL)
}
span.FinishWithOptions(opentracing.FinishOptions{
FinishTime: startTime.Add(results.T),
Expand Down
6 changes: 5 additions & 1 deletion instrumentation/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,11 @@ func (test *Test) end() {
instrumentation.Logger().Printf("CodePath in parallel test is not supported: %v\n", test.t.Name())
coverage.RestoreCoverageCounters()
} else if cov := coverage.EndCoverage(); cov != nil {
test.span.SetTag(tags.Coverage, *cov)
if sp, ok := test.span.(tracer.Span); ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it is checked if the span is our span, tag is gonna be set using unsafe, which means tag is not gonna be escaped?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right the idea is that we know the coverage object is "safe" so if we detect our span, we ensure that we write the object without any modification. But if we are using other tracer we call the normal opentracing Api, that should be followed by the tracer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any case where we should use the "escaped" mode in our spans? Or only it is needed when the user is adding custom spans?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the user is adding custom spans, but we fallback to the Unescape mode if we don't detect a complex object, so its safe call the "escaped" version. Also there are some "helper" methods inside the api, that call directly to the api version of the SetTag (in our case the "escaped" version) so we need to play nice with the api.

sp.UnsafeSetTag(tags.Coverage, *cov)
} else {
test.span.SetTag(tags.Coverage, *cov)
}
}
}

Expand Down
27 changes: 27 additions & 0 deletions tags/tags.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package tags

import (
"fmt"
"reflect"
)

const (
AgentType = "agent.type"
AgentID = "agent.id"
Expand Down Expand Up @@ -65,3 +70,25 @@ const (

Coverage = "test.coverage"
)

func GetValidValue(value interface{}) (interface{}, bool) {
if value == nil {
return nil, false
}
if vs, ok := value.(fmt.Stringer); ok {
return vs.String(), true
}
rValue := reflect.ValueOf(value)
for {
rKind := rValue.Kind()
if rKind == reflect.Ptr {
rValue = rValue.Elem()
continue
}
if (rKind < 1 || rKind > 16) && rKind != reflect.String {
return fmt.Sprint(value), true
}
break
}
return value, false
}
16 changes: 15 additions & 1 deletion tracer/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/log"

"go.undefinedlabs.com/scopeagent/instrumentation"
scopetags "go.undefinedlabs.com/scopeagent/tags"
)

// Span provides access to the essential details of the span, for use
Expand All @@ -28,6 +31,9 @@ type Span interface {

// Log fields with timestamp
LogFieldsWithTimestamp(t time.Time, fields ...log.Field)

// Set tag without validation
UnsafeSetTag(key string, value interface{}) opentracing.Span
}

// Implements the `Span` interface. Created via tracerImpl (see
Expand Down Expand Up @@ -85,7 +91,7 @@ func (s *spanImpl) SetStart(start time.Time) opentracing.Span {
return s
}

func (s *spanImpl) SetTag(key string, value interface{}) opentracing.Span {
func (s *spanImpl) UnsafeSetTag(key string, value interface{}) opentracing.Span {
defer s.onTag(key, value)
s.Lock()
defer s.Unlock()
Expand All @@ -106,6 +112,14 @@ func (s *spanImpl) SetTag(key string, value interface{}) opentracing.Span {
return s
}

func (s *spanImpl) SetTag(key string, value interface{}) opentracing.Span {
cValue, c := scopetags.GetValidValue(value)
if c {
instrumentation.Logger().Printf("SetTag-ConvertedValue: %v", cValue)
}
return s.UnsafeSetTag(key, cValue)
}

func (s *spanImpl) LogKV(keyValues ...interface{}) {
fields, err := log.InterleavedKVToFields(keyValues...)
if err != nil {
Expand Down